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 virtual void begin();
87
93 virtual void end();
94
95 virtual const char* type() const { return "dfrobot"; }
96
97#ifdef MYCILA_JSON_SUPPORT
103 void toJson(const JsonObject& root) const override {
104 Dimmer::toJson(root);
105 root["dfrobot_sku"] = _sku == SKU::DFR1071_GP8211S ? "DFR1071_GP8211S" : _sku == SKU::DFR1073_GP8413 ? "DFR1073_GP8413"
106 : _sku == SKU::DFR0971_GP8403 ? "DFR0971_GP8403"
107 : "UNKNOWN";
108 root["dfrobot_output"] = _output == Output::RANGE_0_5V ? "0-5V" : "0-10V";
109 root["dfrobot_i2c_address"] = _deviceAddress;
110 root["dfrobot_channel"] = _channel;
111 root["dfrobot_resolution"] = getResolution();
112 }
113#endif
114
115 protected:
116 virtual bool _apply() override {
117 if (!_online) {
118 return _sendDutyCycle(_deviceAddress, 0) == ESP_OK;
119 }
120 uint16_t duty = _dutyCycleFire * ((1 << getResolution()) - 1);
121 return _sendDutyCycle(_deviceAddress, duty) == ESP_OK;
122 }
123
124 virtual bool _calculateHarmonics(float* array, size_t n) const override {
125 return _calculatePhaseControlHarmonics(_dutyCycleFire, array, n);
126 }
127
128 private:
129 SKU _sku = SKU::UNKNOWN;
130 Output _output = Output::RANGE_0_10V;
131 TwoWire* _wire = &Wire;
132 uint8_t _deviceAddress;
133 uint8_t _channel = 0;
134
135 uint8_t _sendDutyCycle(uint8_t address, uint16_t duty);
136 uint8_t _sendOutput(uint8_t address, Output output);
137 uint8_t _send(uint8_t address, uint8_t reg, uint8_t* buffer, size_t size);
138 uint8_t _test(uint8_t address);
139 };
140} // 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.
virtual void end()
Disable the dimmer.
void setChannel(uint8_t channel)
Set channel number.
void setOutput(Output output)
Set output mode of the device: 0-5V or 0-10V.
uint8_t getResolution() const
Get the PWM resolution in bits.
virtual void begin()
Enable a dimmer on a specific GPIO pin.