Loading [MathJax]/extensions/tex2jax.js
SensESP 3.1.0
Universal Signal K sensor toolkit ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rgb_led.cpp
Go to the documentation of this file.
1#include "rgb_led.h"
2
3#include "pwm_output.h"
4
5namespace sensesp {
6
7RgbLed::RgbLed(int led_r_pin, int led_g_pin, int led_b_pin, String config_path,
8 long int led_on_rgb, long int led_off_rgb, bool common_anode)
9 : FileSystemSaveable(config_path),
10 led_on_rgb_{led_on_rgb},
11 led_off_rgb_{led_off_rgb},
12 common_anode_{common_anode},
13 led_r_pin_{led_r_pin},
14 led_g_pin_{led_g_pin},
15 led_b_pin_{led_b_pin} {
16 this->load();
17
18#if ESP_ARDUINO_VERSION_MAJOR > 2
19 ledcAttach(led_r_pin, 5000, kPWMResolution);
20 ledcAttach(led_g_pin, 5000, kPWMResolution);
21 ledcAttach(led_b_pin, 5000, kPWMResolution);
22#endif
23}
24
25// Calculate the PWM value to send to analogWrite() based on the specified
26// color value. When using analogWrite(), the closer to zero, the
27// brighter the color. The closer to PWMRANGE, the darker the
28// color.
29static float get_pwm(int64_t rgb, int shift_right, bool common_anode) {
30 int color_val = (rgb >> shift_right) & 0xFF;
31 float const color_pct = color_val / 255.0;
32
33 if (common_anode) {
34 return 1.0 - color_pct;
35 }
36 return color_pct;
37}
38
39bool RgbLed::to_json(JsonObject& root) {
40 root["led_on_rgb"] = led_on_rgb_;
41 root["led_off_rgb"] = led_off_rgb_;
42 return true;
43}
44
45bool RgbLed::from_json(const JsonObject& config) {
46 String expected[] = {"led_on_rgb", "led_off_rgb"};
47 for (auto str : expected) {
48 if (!config[str].is<JsonVariant>()) {
49 return false;
50 }
51 }
52 led_on_rgb_ = config["led_on_rgb"];
53 led_off_rgb_ = config["led_off_rgb"];
54 return true;
55}
56
57void RgbLed::set_color(long new_value) {
58 float r = get_pwm(new_value, 16, common_anode_);
59 const int max_brightness = 1 << kPWMResolution;
60 float g = get_pwm(new_value, 8, common_anode_);
61 float b = get_pwm(new_value, 0, common_anode_);
62
63#if ESP_ARDUINO_VERSION_MAJOR > 2
64 ledcWrite(led_r_pin_, static_cast<int>(r * max_brightness));
65 ledcWrite(led_g_pin_, static_cast<int>(g * max_brightness));
66 ledcWrite(led_b_pin_, static_cast<int>(b * max_brightness));
67#else
71#endif
72}
73
74const String ConfigSchema(const RgbLed& obj) {
75 return R"({"type":"object","properties":{"led_on_rgb":{"title":"RGB color for led ON","type":"integer"},"led_off_rgb":{"title":"RGB color for led OFF","type":"integer"}}})";
76}
77
78} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
virtual void set(const float &new_value) override
Definition pwm_output.h:62
A special device object that can be used to control a multi-channel color rgb LED light using up to 3...
Definition rgb_led.h:31
RgbLed(int led_r_pin=-1, int led_g_pin=-1, int led_b_pin=-1, String config_path="", long int led_on_rgb=0x00FF00, long int led_off_rgb=0xFF0000, bool common_anode=true)
Definition rgb_led.cpp:7
PWMOutput pwm_output_g_
Definition rgb_led.h:91
long led_off_rgb_
Definition rgb_led.h:87
long led_on_rgb_
Definition rgb_led.h:86
void set_color(long new_value)
Definition rgb_led.cpp:57
bool common_anode_
Definition rgb_led.h:88
virtual bool from_json(const JsonObject &config) override
Definition rgb_led.cpp:45
PWMOutput pwm_output_r_
Definition rgb_led.h:90
virtual bool to_json(JsonObject &root) override
Definition rgb_led.cpp:39
PWMOutput pwm_output_b_
Definition rgb_led.h:92
const String ConfigSchema(const SmartSwitchController &obj)
constexpr int kPWMResolution
Definition rgb_led.h:12