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
system_status_led.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_SYSTEM_SYSTEM_STATUS_LED_H_
2#define SENSESP_SRC_SENSESP_SYSTEM_SYSTEM_STATUS_LED_H_
3
4#include <memory>
5#include <vector>
6#include <vector>
7
8#include "lambda_consumer.h"
9#include "led_blinker.h"
11
12#include "esp_arduino_version.h"
13#include "esp_idf_version.h"
14
15#if ESP_ARDUINO_VERSION_MAJOR < 3
16#include "pwm_output.h"
17#define rgbLedWrite neopixelWrite
18#endif
19
20
21namespace sensesp {
22
23// LED patterns. It isn't strictly speaking necessary to have these public,
24// but it makes it easier to test the LED functionality.
25extern LEDPattern no_ap_pattern;
26extern LEDPattern wifi_disconnected_pattern;
27extern LEDPattern wifi_connected_pattern;
28extern LEDPattern ws_connecting_pattern;
29extern LEDPattern ws_connected_pattern;
30extern LEDPattern ws_disconnected_pattern;
31extern LEDPattern ws_authorizing_pattern;
32
41 protected:
42 CRGB leds_[1] = {CRGB::Black};
43
44 std::unique_ptr<LEDBlinker> blinker_;
45
46 virtual void show() = 0;
47
48 virtual void set_brightness(uint8_t brightness) = 0;
49
50 virtual void set_wifi_no_ap();
51 virtual void set_wifi_disconnected();
52 virtual void set_ws_disconnected();
53 virtual void set_ws_authorizing();
54 virtual void set_ws_connecting();
55 virtual void set_ws_connected();
56
57 public:
59 blinker_ = std::unique_ptr<LEDBlinker>(
60 new LEDBlinker(leds_[0], no_ap_pattern, [this]() { this->show(); }));
61
62 event_loop()->onRepeat(5, [this]() {
63 this->blinker_->tick();
64 this->show();
65 });
66 }
67
69 [this](SystemStatus status) {
70 switch (status) {
72 this->set_wifi_no_ap();
73 break;
76 break;
78 this->set_ws_disconnected();
79 break;
81 this->set_ws_connecting();
82 break;
84 this->set_ws_authorizing();
85 break;
87 this->set_ws_connected();
88 break;
89 default:
90 break;
91 }
92 }};
93
97
99};
100
101// TODO: Instead of setting PWM output manually, it would be great to
102// instead implement a single-LED FastLED controller class. That would
103// allow supporting features like dithering etc.
104
110 public:
111 SystemStatusLed(uint8_t pin, uint8_t brightness = 255)
113 pin_{pin},
114 brightness_{brightness} {
115#if ESP_ARDUINO_VERSION_MAJOR >= 3
116 ledcAttach(pin, 2000, 8);
117#endif
118 }
119
120 protected:
121 uint8_t pin_;
122#if ESP_ARDUINO_VERSION_MAJOR < 3
124#endif
125 uint8_t brightness_;
126 void show() override {
127 // Convert the RGB color to a single brightness value using the
128 // perceptual luminance formula.
129 // value is scaled to 0-255.
130 int value = ((0.2126 * leds_[0].r + // Red contribution
131 0.7152 * leds_[0].g + // Green contribution
132 0.0722 * leds_[0].b // Blue contribution
133 ) *
134 brightness_ / (255.0));
135#if ESP_ARDUINO_VERSION_MAJOR >= 3
136 ledcWrite(pin_, value);
137#else
138 pwm_output_.set(value / 255.0);
139#endif
140 }
141
142 void set_brightness(uint8_t brightness) override { brightness_ = brightness; }
143};
144
145// Direct use of FastLED breaks when the WiFi client is enabled. Thus,
146// use the Arduino ESP32 Core native rgbLedWrite function instead. That
147// seems to work with WiFi as well.
148
150 public:
151 RGBSystemStatusLed(uint8_t pin, uint8_t brightness = 40)
152 : BaseSystemStatusLed(), pin_{pin}, brightness_{brightness} {}
153
154 protected:
155 uint8_t pin_;
156 uint8_t brightness_;
157 void show() override {
158 rgbLedWrite(pin_, brightness_ * leds_[0].r / 255,
159 brightness_ * leds_[0].g / 255, brightness_ * leds_[0].b / 255);
160 }
161
162 void set_brightness(uint8_t brightness) override { brightness_ = brightness; }
163};
164
165} // namespace sensesp
166
167#endif
Consumes the networking and websocket states and delta counts and updates the device LED accordingly.
virtual void set_brightness(uint8_t brightness)=0
std::unique_ptr< LEDBlinker > blinker_
ValueConsumer< int > & get_delta_tx_count_consumer()
ValueConsumer< SystemStatus > & get_system_status_consumer()
LambdaConsumer< SystemStatus > system_status_consumer_
Blink the LED with a pattern specified by a vector of callback functions.
Provides an easy way of calling a function based on the output of any ValueProducer.
DEPRECATED. Use Android native ledcAttach and ledcWrite instead.
Definition pwm_output.h:33
virtual void set(const float &new_value) override
Definition pwm_output.h:62
RGBSystemStatusLed(uint8_t pin, uint8_t brightness=40)
void set_brightness(uint8_t brightness) override
Monochromatic system status LED.
SystemStatusLed(uint8_t pin, uint8_t brightness=255)
void set_brightness(uint8_t brightness) override
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
LEDPattern no_ap_pattern
LEDPattern ws_authorizing_pattern
LEDPattern ws_disconnected_pattern
LEDPattern ws_connected_pattern
LEDPattern wifi_connected_pattern
LEDPattern ws_connecting_pattern
LEDPattern wifi_disconnected_pattern
#define rgbLedWrite