5#include <MycilaDimmerDFRobot.h>
8#include <esp32-hal-log.h>
18 ESP_LOGE(TAG,
"SKU not set!");
23 if (_sku == SKU::DFR1071_GP8211S) {
25 ESP_LOGW(TAG,
"DFRobot DFR1071 (GP8211S) has only one channel: switching to channel 0");
31 ESP_LOGE(TAG,
"Invalid channel %d", _channel);
38 ESP_LOGI(TAG,
"Searching for DFRobot @ 0x%02x...", _deviceAddress);
39 for (
int i = 0; i < 3; i++) {
40 uint8_t err = _test(_deviceAddress);
42 ESP_LOGD(TAG,
"DFRobot @ 0x%02x: TwoWire communication error: %d", _deviceAddress, err);
51 ESP_LOGI(TAG,
"Searching for DFRobot @ 0x58 up to 0x5F...");
52 for (uint8_t addr = 0x58; !found && addr <= 0x5F; addr++) {
53 if (_test(addr) == ESP_OK) {
54 _deviceAddress = addr;
62 ESP_LOGI(TAG,
"Found DFRobot @ 0x%02x and channel %d", _deviceAddress, _channel);
63 }
else if (_deviceAddress) {
64 ESP_LOGW(TAG,
"DFRobot @ 0x%02x: Unable to communicate with device", _deviceAddress);
66 _deviceAddress = 0x58;
67 ESP_LOGW(TAG,
"DFRobot not found! Using default address 0x58");
71 uint8_t err = _sendOutput(_deviceAddress, _output);
73 ESP_LOGE(TAG,
"Disable DFRobot @ 0x%02x: Unable to set output voltage: TwoWire communication error: %d", _deviceAddress, err);
88 ESP_LOGI(TAG,
"Disable DFRobot @ 0x%02x", _deviceAddress);
92uint8_t Mycila::DFRobotDimmer::_sendDutyCycle(uint8_t address, uint16_t duty) {
93 duty = duty << (16 - getResolution());
96 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
97 return _send(address, 0x02, buffer, 2) == 0;
100 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
101 return _send(address, 0x04, buffer, 2) == 0;
104 uint8_t buffer[4] = {uint8_t(duty & 0xff), uint8_t(duty >> 8), uint8_t(duty & 0xff), uint8_t(duty >> 8)};
105 return _send(address, 0x02, buffer, 4) == 0;
113uint8_t Mycila::DFRobotDimmer::_sendOutput(uint8_t address, Output output) {
115 case Output::RANGE_0_5V: {
116 ESP_LOGI(TAG,
"Set output range to 0-5V");
118 return _send(address, 0x01, &data, 1);
120 case Output::RANGE_0_10V: {
121 ESP_LOGI(TAG,
"Set output range to 0-10V");
123 return _send(address, 0x01, &data, 1);
131uint8_t Mycila::DFRobotDimmer::_send(uint8_t address, uint8_t reg, uint8_t* buffer,
size_t size) {
132 _wire->beginTransmission(address);
134 for (uint16_t i = 0; i < size; i++) {
135 _wire->write(buffer[i]);
137 return _wire->endTransmission();
140uint8_t Mycila::DFRobotDimmer::_test(uint8_t address) {
142 _wire->beginTransmission(address);
143 delayMicroseconds(100);
144 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.