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 {
16 class PWMDimmer : public Dimmer {
17 public:
18 virtual ~PWMDimmer() { end(); }
19
23 void setPin(gpio_num_t pin) { _pin = pin; }
24
28 gpio_num_t getPin() const { return _pin; }
29
33 void setFrequency(uint32_t frequency) { this->_frequency = frequency; }
34
38 uint32_t getFrequency() const { return _frequency; }
39
43 void setResolution(uint8_t resolution) { this->_resolution = resolution; }
44
48 uint8_t getResolution() const { return _resolution; }
49
56 virtual void begin();
57
63 virtual void end();
64
65 virtual const char* type() const { return "pwm"; }
66
67#ifdef MYCILA_JSON_SUPPORT
73 void toJson(const JsonObject& root) const override {
74 Dimmer::toJson(root);
75 root["pwm_pin"] = static_cast<int>(_pin);
76 root["pwm_frequency"] = _frequency;
77 root["pwm_resolution"] = _resolution;
78 }
79#endif
80
81 protected:
82 virtual bool _apply() override {
83 if (!_online) {
84 return ledcWrite(_pin, 0);
85 }
86 uint32_t duty = _dutyCycleFire * ((1 << _resolution) - 1);
87 return ledcWrite(_pin, duty);
88 }
89
90 virtual bool _calculateHarmonics(float* array, size_t n) const override {
91 return _calculatePhaseControlHarmonics(_dutyCycleFire, array, n);
92 }
93
94 private:
95 gpio_num_t _pin = GPIO_NUM_NC;
96 uint32_t _frequency = MYCILA_DIMMER_PWM_FREQUENCY;
97 uint8_t _resolution = MYCILA_DIMMER_PWM_RESOLUTION;
98 };
99} // namespace Mycila
PWM based dimmer implementation for voltage regulators controlled by a PWM signal to 0-10V analog con...
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.