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
MycilaDimmerDFRobot.h
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright (C) 2023-2025 Mathieu Carbou
4 */
5#pragma once
6
7#include "MycilaDimmer.h"
8
9#include <Wire.h>
10
11namespace Mycila {
15 class DFRobotDimmer : public Dimmer {
16 public:
17 enum class SKU {
18 UNKNOWN,
19 // 0-5V/10V output, 1-channel, I2C, 15-bit resolution, 99.99% accuracy
20 DFR1071_GP8211S,
21 // 0-5V/10V output, 2-channel, I2C, 15-bit resolution, 99.99% accuracy
22 DFR1073_GP8413,
23 // 0-5V/10V output, 2-channel, I2C, 12-bit resolution, 99.90% accuracy
24 DFR0971_GP8403,
25 };
26
27 enum class Output {
28 RANGE_0_5V,
29 RANGE_0_10V,
30 };
31
32 virtual ~DFRobotDimmer() { end(); }
33
34 void setWire(TwoWire& wire) { _wire = &wire; }
35 TwoWire& getWire() const { return *_wire; }
36
37 void setSKU(SKU sku) { _sku = sku; }
38 SKU getSKU() const { return _sku; }
39
44 void setOutput(Output output) { _output = output; }
45 Output getOutput() const { return _output; }
46
51 void setDeviceAddress(uint8_t deviceAddress) { _deviceAddress = deviceAddress; }
52 uint8_t getDeviceAddress() const { return _deviceAddress; }
53
61 void setChannel(uint8_t channel) { _channel = channel; }
62 uint8_t getChannel() const { return _channel; }
63
67 uint8_t getResolution() const {
68 switch (_sku) {
69 case SKU::DFR1071_GP8211S:
70 return 15;
71 case SKU::DFR1073_GP8413:
72 return 15;
73 case SKU::DFR0971_GP8403:
74 return 12;
75 default:
76 return 0;
77 }
78 }
79
86 bool begin() override;
87
93 void end() override;
94
95 const char* type() const override { return "dfrobot"; }
96
97 bool calculateMetrics(Metrics& metrics, float gridVoltage, float loadResistance) const override {
98 return isEnabled() && _calculatePhaseControlMetrics(metrics, _dutyCycleFire, gridVoltage, loadResistance);
99 }
100
101#ifdef MYCILA_JSON_SUPPORT
107 void toJson(const JsonObject& root) const override {
108 Dimmer::toJson(root);
109 root["sku"] = _sku == SKU::DFR1071_GP8211S ? "DFR1071_GP8211S" : _sku == SKU::DFR1073_GP8413 ? "DFR1073_GP8413"
110 : _sku == SKU::DFR0971_GP8403 ? "DFR0971_GP8403"
111 : "UNKNOWN";
112 root["output"] = _output == Output::RANGE_0_5V ? "0-5V" : "0-10V";
113 root["i2c_address"] = _deviceAddress;
114 root["channel"] = _channel;
115 root["resolution"] = getResolution();
116 }
117#endif
118
119 protected:
120 bool _apply() override {
121 if (!_online) {
122 return _sendDutyCycle(_deviceAddress, 0) == ESP_OK;
123 }
124 uint16_t duty = _dutyCycleFire * ((1 << getResolution()) - 1);
125 return _sendDutyCycle(_deviceAddress, duty) == ESP_OK;
126 }
127
128 bool _calculateHarmonics(float* array, size_t n) const override {
129 return _calculatePhaseControlHarmonics(_dutyCycleFire, array, n);
130 }
131
132 private:
133 SKU _sku = SKU::UNKNOWN;
134 Output _output = Output::RANGE_0_10V;
135 TwoWire* _wire = &Wire;
136 uint8_t _deviceAddress;
137 uint8_t _channel = 0;
138
139 uint8_t _sendDutyCycle(uint8_t address, uint16_t duty);
140 uint8_t _sendOutput(uint8_t address, Output output);
141 uint8_t _send(uint8_t address, uint8_t reg, uint8_t* buffer, size_t size);
142 uint8_t _test(uint8_t address);
143 };
144} // namespace Mycila
DFRobot DFR1071/DFR1073/DFR0971 I2C controlled 0-10V/0-5V dimmer implementation for voltage regulator...
void setDeviceAddress(uint8_t deviceAddress)
I2C address of the device.
void setChannel(uint8_t channel)
Set channel number.
void setOutput(Output output)
Set output mode of the device: 0-5V or 0-10V.
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 isEnabled() const
Check if the dimmer is enabled (if it was able to initialize correctly).