SensESP 2.7.2
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
19
20// Calculate the PWM value to send to analogWrite() based on the specified
21// color value. When using analogWrite(), the closer to zero, the
22// brighter the color. The closer to PWMRANGE, the darker the
23// color.
24static float get_pwm(long rgb, int shift_right, bool common_anode) {
25 int color_val = (rgb >> shift_right) & 0xFF;
26 float color_pct = color_val / 255.0;
27
28 if (common_anode) {
29 return 1.0 - color_pct;
30 } else {
31 return color_pct;
32 }
33}
34
36 if (led_r_channel_ >= 0) {
37 float r = get_pwm(new_value, 16, common_anode_);
39 }
40
41 if (led_g_channel_ >= 0) {
42 float g = get_pwm(new_value, 8, common_anode_);
44 }
45
46 if (led_b_channel_ >= 0) {
47 float b = get_pwm(new_value, 0, common_anode_);
49 }
50}
51
59
61 root["led_on_rgb"] = led_on_rgb_;
62 root["led_off_rgb"] = led_off_rgb_;
63}
64
65static const char SCHEMA[] PROGMEM = R"({
66 "type": "object",
67 "properties": {
68 "led_on_rgb": { "title": "RGB color for led ON", "type": "integer" },
69 "led_off_rgb": { "title": "RGB color for led OFF", "type": "integer" }
70 }
71 })";
72
74
76 String expected[] = {"led_on_rgb", "led_off_rgb"};
77 for (auto str : expected) {
78 if (!config.containsKey(str)) {
79 return false;
80 }
81 }
82 led_on_rgb_ = config["led_on_rgb"];
83 led_off_rgb_ = config["led_off_rgb"];
84 return true;
85}
86
87} // namespace sensesp
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
virtual void load_configuration()
Construct a new transform based on a single function.
static int assign_channel(int pin, int pwm_channel=-1)
static void set_pwm(int pwm_channel, float value)
long led_off_rgb_
Definition rgb_led.h:76
int led_g_channel_
Definition rgb_led.h:73
virtual void set_input(long new_value, uint8_t input_channel=0) override
Definition rgb_led.cpp:35
int led_b_channel_
Definition rgb_led.h:74
virtual void get_configuration(JsonObject &doc) override
Definition rgb_led.cpp:60
long led_on_rgb_
Definition rgb_led.h:75
bool common_anode_
Definition rgb_led.h:77
virtual bool set_configuration(const JsonObject &config) override
Definition rgb_led.cpp:75
virtual String get_config_schema() override
Definition rgb_led.cpp:73
RgbLed(int led_r_pin=-1, int led_g_pin=-1, int led_b_pin=-1, String config_path="", long led_on_rgb=0x00FF00, long led_off_rgb=0xFF0000, bool common_anode=true)
Definition rgb_led.cpp:7
int led_r_channel_
Definition rgb_led.h:72
const uint8_t PAGE_css_bootstrap[] PROGMEM