SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
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_r_output_{std::make_shared<PWMOutput>(led_r_pin)},
11 led_g_output_{std::make_shared<PWMOutput>(led_g_pin)},
12 led_b_output_{std::make_shared<PWMOutput>(led_b_pin)},
13 led_on_rgb_{led_on_rgb},
14 led_off_rgb_{led_off_rgb},
15 common_anode_{common_anode} {
16 this->load();
17}
18
19// Calculate the PWM value to send to analogWrite() based on the specified
20// color value. When using analogWrite(), the closer to zero, the
21// brighter the color. The closer to PWMRANGE, the darker the
22// color.
23static float get_pwm(int64_t rgb, int shift_right, bool common_anode) {
24 int color_val = (rgb >> shift_right) & 0xFF;
25 float const color_pct = color_val / 255.0;
26
27 if (common_anode) {
28 return 1.0 - color_pct;
29 }
30 return color_pct;
31}
32
33bool RgbLed::to_json(JsonObject& root) {
34 root["led_on_rgb"] = led_on_rgb_;
35 root["led_off_rgb"] = led_off_rgb_;
36 return true;
37}
38
39bool RgbLed::from_json(const JsonObject& config) {
40 String expected[] = {"led_on_rgb", "led_off_rgb"};
41 for (auto str : expected) {
42 if (!config[str].is<JsonVariant>()) {
43 return false;
44 }
45 }
46 led_on_rgb_ = config["led_on_rgb"];
47 led_off_rgb_ = config["led_off_rgb"];
48 return true;
49}
50
51void RgbLed::set_color(long new_value) {
52 float r = get_pwm(new_value, 16, common_anode_);
53 led_r_output_->set(r);
54
55 float g = get_pwm(new_value, 8, common_anode_);
56 led_g_output_->set(g);
57
58 float b = get_pwm(new_value, 0, common_anode_);
59 led_b_output_->set(b);
60}
61
62const String ConfigSchema(const RgbLed& obj) {
63 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"}}})";
64}
65
66} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Provides a cross platform mechanism for generating Pulse Width Modulation signals over one or more GP...
Definition pwm_output.h:32
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:29
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
long led_off_rgb_
Definition rgb_led.h:84
long led_on_rgb_
Definition rgb_led.h:83
void set_color(long new_value)
Definition rgb_led.cpp:51
bool common_anode_
Definition rgb_led.h:85
std::shared_ptr< PWMOutput > led_g_output_
Definition rgb_led.h:81
virtual bool from_json(const JsonObject &config) override
Definition rgb_led.cpp:39
virtual bool to_json(JsonObject &root) override
Definition rgb_led.cpp:33
std::shared_ptr< PWMOutput > led_b_output_
Definition rgb_led.h:82
std::shared_ptr< PWMOutput > led_r_output_
Definition rgb_led.h:80
const String ConfigSchema(const SmartSwitchController &obj)