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 {
12 class DFRobotDimmer : public Dimmer {
13 public:
14 enum class SKU {
15 UNKNOWN,
16 // 0-5V/10V output, 1-channel, I2C, 15-bit resolution, 99.99% accuracy
17 DFR1071_GP8211S,
18 // 0-5V/10V output, 2-channel, I2C, 15-bit resolution, 99.99% accuracy
19 DFR1073_GP8413,
20 // 0-5V/10V output, 2-channel, I2C, 12-bit resolution, 99.90% accuracy
21 DFR0971_GP8403,
22 };
23
24 enum class Output {
25 RANGE_0_5V,
26 RANGE_0_10V,
27 };
28
29 virtual ~DFRobotDimmer() { end(); }
30
31 void setWire(TwoWire& wire) { _wire = &wire; }
32 TwoWire& getWire() const { return *_wire; }
33
34 void setSKU(SKU sku) { _sku = sku; }
35 SKU getSKU() const { return _sku; }
36
41 void setOutput(Output output) { _output = output; }
42 Output getOutput() const { return _output; }
43
48 void setDeviceAddress(uint8_t deviceAddress) { _deviceAddress = deviceAddress; }
49 uint8_t getDeviceAddress() const { return _deviceAddress; }
50
58 void setChannel(uint8_t channel) { _channel = channel; }
59 uint8_t getChannel() const { return _channel; }
60
64 uint8_t getResolution() const {
65 switch (_sku) {
66 case SKU::DFR1071_GP8211S:
67 return 15;
68 case SKU::DFR1073_GP8413:
69 return 15;
70 case SKU::DFR0971_GP8403:
71 return 12;
72 default:
73 return 0;
74 }
75 }
76
83 virtual void begin();
84
90 virtual void end();
91
92 virtual const char* type() const { return "dfrobot"; }
93
94#ifdef MYCILA_JSON_SUPPORT
100 void toJson(const JsonObject& root) const override {
101 Dimmer::toJson(root);
102 root["dfrobot_sku"] = _sku == SKU::DFR1071_GP8211S ? "DFR1071_GP8211S" : _sku == SKU::DFR1073_GP8413 ? "DFR1073_GP8413"
103 : _sku == SKU::DFR0971_GP8403 ? "DFR0971_GP8403"
104 : "UNKNOWN";
105 root["dfrobot_output"] = _output == Output::RANGE_0_5V ? "0-5V" : "0-10V";
106 root["dfrobot_i2c_address"] = _deviceAddress;
107 root["dfrobot_channel"] = _channel;
108 root["dfrobot_resolution"] = getResolution();
109 }
110#endif
111
112 protected:
113 virtual bool _apply() {
114 if (!_online) {
115 return _sendDutyCycle(_deviceAddress, 0) == ESP_OK;
116 }
117 uint16_t duty = _dutyCycleFire * ((1 << getResolution()) - 1);
118 return _sendDutyCycle(_deviceAddress, duty) == ESP_OK;
119 }
120
121 private:
122 SKU _sku = SKU::UNKNOWN;
123 Output _output = Output::RANGE_0_10V;
124 TwoWire* _wire = &Wire;
125 uint8_t _deviceAddress;
126 uint8_t _channel = 0;
127
128 uint8_t _sendDutyCycle(uint8_t address, uint16_t duty);
129 uint8_t _sendOutput(uint8_t address, Output output);
130 uint8_t _send(uint8_t address, uint8_t reg, uint8_t* buffer, size_t size);
131 uint8_t _test(uint8_t address);
132 };
133} // namespace Mycila
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.