MycilaJSY 13.0.0
Arduino / ESP32 library for the JSY1031, JSY-MK-163, JSY-MK-193, JSY-MK-194, JSY-MK-227, JSY-MK-229, JSY-MK-333 families single-phase and three-phase AC bidirectional meters from Shenzhen Jiansiyan Technologies Co, Ltd.
Loading...
Searching...
No Matches
MycilaDimmerPWM.h
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright (C) 2023-2025 Mathieu Carbou
4 */
5#pragma once
6
7#include "MycilaDimmer.h"
8
9#define MYCILA_DIMMER_PWM_RESOLUTION 12 // 12 bits resolution => 0-4095 watts
10#define MYCILA_DIMMER_PWM_FREQUENCY 1000 // 1 kHz
11
12namespace Mycila {
13 class PWMDimmer : public Dimmer {
14 public:
15 virtual ~PWMDimmer() { end(); }
16
20 void setPin(gpio_num_t pin) { _pin = pin; }
21
25 gpio_num_t getPin() const { return _pin; }
26
30 void setFrequency(uint32_t frequency) { this->_frequency = frequency; }
31
35 uint32_t getFrequency() const { return _frequency; }
36
40 void setResolution(uint8_t resolution) { this->_resolution = resolution; }
41
45 uint8_t getResolution() const { return _resolution; }
46
53 virtual void begin();
54
60 virtual void end();
61
62 virtual const char* type() const { return "pwm"; }
63
64#ifdef MYCILA_JSON_SUPPORT
70 void toJson(const JsonObject& root) const override {
71 Dimmer::toJson(root);
72 root["pwm_pin"] = static_cast<int>(_pin);
73 root["pwm_frequency"] = _frequency;
74 root["pwm_resolution"] = _resolution;
75 }
76#endif
77
78 protected:
79 virtual bool _apply() {
80 if (!_online) {
81 return ledcWrite(_pin, 0);
82 }
83 uint32_t duty = _dutyCycleFire * ((1 << _resolution) - 1);
84 return ledcWrite(_pin, duty);
85 }
86
87 private:
88 gpio_num_t _pin = GPIO_NUM_NC;
89 uint32_t _frequency = MYCILA_DIMMER_PWM_FREQUENCY;
90 uint8_t _resolution = MYCILA_DIMMER_PWM_RESOLUTION;
91 };
92} // namespace Mycila
void setPin(gpio_num_t pin)
Set the GPIO pin to use for the dimmer.
void setResolution(uint8_t resolution)
Set the PWM resolution in bits.
void setFrequency(uint32_t frequency)
Set the PWM frequency in Hz.
virtual void begin()
Enable a dimmer on a specific GPIO pin.
uint8_t getResolution() const
Get the PWM resolution in bits.
gpio_num_t getPin() const
Get the GPIO pin used for the dimmer.
virtual void end()
Disable the dimmer.
uint32_t getFrequency() const
Get the PWM frequency in Hz.