5#include <MycilaDimmerDFRobot.h>
8#include <esp32-hal-log.h>
10#ifdef MYCILA_LOGGER_SUPPORT
11 #include <MycilaLogger.h>
12extern Mycila::Logger logger;
13 #define LOGD(tag, format, ...) logger.debug(tag, format, ##__VA_ARGS__)
14 #define LOGI(tag, format, ...) logger.info(tag, format, ##__VA_ARGS__)
15 #define LOGW(tag, format, ...) logger.warn(tag, format, ##__VA_ARGS__)
16 #define LOGE(tag, format, ...) logger.error(tag, format, ##__VA_ARGS__)
18 #define LOGD(tag, format, ...) ESP_LOGD(tag, format, ##__VA_ARGS__)
19 #define LOGI(tag, format, ...) ESP_LOGI(tag, format, ##__VA_ARGS__)
20 #define LOGW(tag, format, ...) ESP_LOGW(tag, format, ##__VA_ARGS__)
21 #define LOGE(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__)
24#define TAG "DFR_DIMMER"
32 LOGE(TAG,
"Disable DFRobot Dimmer: SKU not set!");
37 if (_sku == SKU::DFR1071_GP8211S) {
39 LOGW(TAG,
"DFRobot DFR1071 (GP8211S) has only one channel: switching to channel 0");
45 LOGE(TAG,
"Disable DFRobot Dimmer: invalid channel %d", _channel);
52 LOGI(TAG,
"Searching for DFRobot Dimmer @ 0x%02x...", _deviceAddress);
53 for (
int i = 0; i < 3; i++) {
54 uint8_t err = _test(_deviceAddress);
56 LOGD(TAG,
"DFRobot Dimmer @ 0x%02x: TwoWire communication error: %d", _deviceAddress, err);
65 LOGI(TAG,
"Searching for DFRobot Dimmer @ 0x58 up to 0x5F...");
66 for (uint8_t addr = 0x58; !found && addr <= 0x5F; addr++) {
67 if (_test(addr) == ESP_OK) {
68 _deviceAddress = addr;
76 LOGI(TAG,
"Found DFRobot Dimmer @ 0x%02x and channel %d", _deviceAddress, _channel);
77 }
else if (_deviceAddress) {
78 LOGW(TAG,
"DFRobot Dimmer @ 0x%02x: Unable to communicate with device", _deviceAddress);
80 _deviceAddress = 0x58;
81 LOGW(TAG,
"DFRobot Dimmer no found! Using default address 0x58");
85 uint8_t err = _sendOutput(_deviceAddress, _output);
87 LOGE(TAG,
"Disable DFRobot Dimmer @ 0x%02x: Unable to set output voltage: TwoWire communication error: %d", _deviceAddress, err);
102 LOGI(TAG,
"Disable DFRobot Dimmer @ 0x%02x", _deviceAddress);
106uint8_t Mycila::DFRobotDimmer::_sendDutyCycle(uint8_t address, uint16_t duty) {
107 duty = duty << (16 - getResolution());
110 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
111 return _send(address, 0x02, buffer, 2) == 0;
114 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
115 return _send(address, 0x04, buffer, 2) == 0;
118 uint8_t buffer[4] = {uint8_t(duty & 0xff), uint8_t(duty >> 8), uint8_t(duty & 0xff), uint8_t(duty >> 8)};
119 return _send(address, 0x02, buffer, 4) == 0;
127uint8_t Mycila::DFRobotDimmer::_sendOutput(uint8_t address, Output output) {
129 case Output::RANGE_0_5V: {
130 LOGI(TAG,
"Set output range to 0-5V");
132 return _send(address, 0x01, &data, 1);
134 case Output::RANGE_0_10V: {
135 LOGI(TAG,
"Set output range to 0-10V");
137 return _send(address, 0x01, &data, 1);
145uint8_t Mycila::DFRobotDimmer::_send(uint8_t address, uint8_t reg, uint8_t* buffer,
size_t size) {
146 _wire->beginTransmission(address);
148 for (uint16_t i = 0; i < size; i++) {
149 _wire->write(buffer[i]);
151 return _wire->endTransmission();
154uint8_t Mycila::DFRobotDimmer::_test(uint8_t address) {
156 _wire->beginTransmission(address);
157 delayMicroseconds(100);
158 return _wire->endTransmission();
void begin() override
Enable a dimmer on a specific GPIO pin.
uint8_t getResolution() const
Get the PWM resolution in bits.
void end() override
Disable the dimmer.
bool setDutyCycle(float dutyCycle)
Set the power duty.