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.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
25 if (_enabled)
26 return true;
27
28 if (!GPIO_IS_VALID_OUTPUT_GPIO(_pin)) {
29 ESP_LOGE(TAG, "Invalid pin: %" PRId8, _pin);
30 return false;
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 false;
43 }
44
45 // restart with last saved value
46 setDutyCycle(_dutyCycle);
47 return true;
48}
49
51 if (!_enabled)
52 return;
53 _enabled = false;
54 _online = false;
55 ESP_LOGI(TAG, "Disable dimmer on pin %" PRId8, _pin);
56 _apply();
57 ledcDetach(_pin);
58 pinMode(_pin, OUTPUT);
59 digitalWrite(_pin, LOW);
60}
bool setDutyCycle(float dutyCycle)
Set the power duty.
bool begin() override
Enable a dimmer on a specific GPIO pin.
void end() override
Disable the dimmer.