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
src
MycilaDimmerPWM.cpp
1
// SPDX-License-Identifier: MIT
2
/*
3
* Copyright (C) 2023-2025 Mathieu Carbou
4
*/
5
#include <MycilaDimmerPWM.h>
6
7
#include <driver/ledc.h>
8
9
// logging
10
#include <esp32-hal-log.h>
11
12
#ifndef GPIO_IS_VALID_OUTPUT_GPIO
13
#define GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) ((gpio_num >= 0) && \
14
(((1ULL << (gpio_num)) & SOC_GPIO_VALID_OUTPUT_GPIO_MASK) != 0))
15
#endif
16
17
#ifndef GPIO_IS_VALID_GPIO
18
#define GPIO_IS_VALID_GPIO(gpio_num) ((gpio_num >= 0) && \
19
(((1ULL << (gpio_num)) & SOC_GPIO_VALID_GPIO_MASK) != 0))
20
#endif
21
22
#define TAG "PWM"
23
24
void
Mycila::PWMDimmer::begin
() {
25
if
(_enabled)
26
return
;
27
28
if
(!GPIO_IS_VALID_OUTPUT_GPIO(_pin)) {
29
ESP_LOGE(TAG,
"Invalid pin: %"
PRId8, _pin);
30
return
;
31
}
32
33
ESP_LOGI(TAG,
"Enable dimmer on pin %"
PRId8, _pin);
34
35
pinMode(_pin, OUTPUT);
36
digitalWrite(_pin, LOW);
37
38
if
(ledcAttach(_pin, _frequency, _resolution) && ledcWrite(_pin, 0)) {
39
_enabled =
true
;
40
}
else
{
41
ESP_LOGE(TAG,
"Failed to attach ledc driver on pin %"
PRId8, _pin);
42
return
;
43
}
44
45
// restart with last saved value
46
setDutyCycle
(_dutyCycle);
47
}
48
49
void
Mycila::PWMDimmer::end
() {
50
if
(!_enabled)
51
return
;
52
_enabled =
false
;
53
_online =
false
;
54
ESP_LOGI(TAG,
"Disable dimmer on pin %"
PRId8, _pin);
55
_apply();
56
ledcDetach(_pin);
57
pinMode(_pin, OUTPUT);
58
digitalWrite(_pin, LOW);
59
}
Mycila::Dimmer::setDutyCycle
bool setDutyCycle(float dutyCycle)
Set the power duty.
Definition
MycilaDimmer.h:201
Mycila::PWMDimmer::end
void end() override
Disable the dimmer.
Definition
MycilaDimmerPWM.cpp:49
Mycila::PWMDimmer::begin
void begin() override
Enable a dimmer on a specific GPIO pin.
Definition
MycilaDimmerPWM.cpp:24
Generated by
1.15.0