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);
90 ESP_LOGI(TAG,
"Disable DFRobot @ 0x%02x", _deviceAddress);
94uint8_t Mycila::DFRobotDimmer::_sendDutyCycle(uint8_t address, uint16_t duty) {
95 duty = duty << (16 - getResolution());
98 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
99 return _send(address, 0x02, buffer, 2) == 0;
102 uint8_t buffer[2] = {uint8_t(duty & 0xff), uint8_t(duty >> 8)};
103 return _send(address, 0x04, buffer, 2) == 0;
106 uint8_t buffer[4] = {uint8_t(duty & 0xff), uint8_t(duty >> 8), uint8_t(duty & 0xff), uint8_t(duty >> 8)};
107 return _send(address, 0x02, buffer, 4) == 0;
115uint8_t Mycila::DFRobotDimmer::_sendOutput(uint8_t address, Output output) {
117 case Output::RANGE_0_5V: {
118 ESP_LOGI(TAG,
"Set output range to 0-5V");
120 return _send(address, 0x01, &data, 1);
122 case Output::RANGE_0_10V: {
123 ESP_LOGI(TAG,
"Set output range to 0-10V");
125 return _send(address, 0x01, &data, 1);
133uint8_t Mycila::DFRobotDimmer::_send(uint8_t address, uint8_t reg, uint8_t* buffer,
size_t size) {
134 _wire->beginTransmission(address);
136 for (uint16_t i = 0; i < size; i++) {
137 _wire->write(buffer[i]);
139 return _wire->endTransmission();
142uint8_t Mycila::DFRobotDimmer::_test(uint8_t address) {
144 _wire->beginTransmission(address);
145 delayMicroseconds(100);
146 return _wire->endTransmission();
bool 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.