API Documentation v0.0.1
Loading...
Searching...
No Matches
Panel.h
1/**********************************************************************************************************************
2 * ____ _ _ _
3 * / __ \ | | | | | |
4 * | | | |_ __ ___ _ __ | |__| | ___ _ __ _ __ ___| |_
5 * | | | | '_ \ / _ \ '_ \| __ |/ _ \| '__| '_ \ / _ \ __|
6 * | |__| | |_) | __/ | | | | | | (_) | | | | | | __/ |_
7 * \____/| .__/ \___|_| |_|_| |_|\___/|_| |_| |_|\___|\__|
8 * | |
9 * |_|
10 * ----------------------------------------------------------------------------------
11 *
12 * @file Panel.h
13 * @author Ulukaii
14 * @date 24.05.2025
15 * @version t 0.3.2
16 * @copyright Copyright 2016-2025 OpenHornet. See 2A13-BACKLIGHT_CONTROLLER.ino for details.
17 * @brief Abstract base class for all panels. Each panel must be a derived class from this base class.
18 * @details It provides functions that are repeatedly required across all panels:
19 * setInstrLights(), setIndicatorColor(), setFloodlights().
20 * Panels are added to Channels. For memory efficiency, panels are organized in a linked list within each
21 * channel. This conserves stack memory.
22 * This approach avoids allocating fixed-size arrays for panel pointers in each channel,
23 * which would exhaust the limited stack space on the Arduino Mega 2560 (I tested it).
24 * Instead, this class provides a pointer to the next panel in its channel.
25 * Thus,the parent channels still can iterate through all of their panels.
26 *********************************************************************************************************************/
27
28
29#ifndef __PANEL_H
30#define __PANEL_H
31
32#include <Arduino.h>
33#include <avr/pgmspace.h>
34#include "DcsBios.h"
35#include "FastLED.h"
36#include "LedRole.h"
37#include "LedStruct.h"
38#include "LedUpdateState.h"
39#include "Colors.h"
40
41class Panel {
42public:
47 virtual int getStartIndex() const { return panelStartIndex; }
48
53 virtual int getLedCount() const { return ledCount; }
54
59 virtual const Led* getLedTable() const { return ledTable; }
60
65 virtual CRGB* getLedStrip() const { return ledStrip; }
66
67 // Add friend declaration to allow Channel to access nextPanel and its protected methods
68 friend class Channel;
69
70protected:
76 current_backl_brightness = 0;
77 current_console_brightness = 0;
78 current_flood_brightness = 0;
79 nextPanel = nullptr; // Initialize next panel pointer
80 }
81
82
83 int panelStartIndex; // Start index of the panel on the LED strip
84 int ledCount; // Number of LEDs in the panel
85 const Led* ledTable; // Pointer to the LED table
86 CRGB* ledStrip; // Pointer to the LED strip
87 uint16_t current_backl_brightness; // Current br. value for backlights (0-65535)
88 uint16_t current_console_brightness; // Current br. value for console lights (0-65535)
89 uint16_t current_flood_brightness; // Current br. value for floodlights (0-65535)
90 Panel* nextPanel; // Pointer to next panel in the channel
91
92
99 void setInstrLights(uint16_t newValue, const CRGB& color = NVIS_GREEN_A) {
100 if (!getLedStrip() || !getLedTable()) return; // Safety checks
101 if (newValue == current_backl_brightness) return; // Exit if no brightness change
102 int scale = map(newValue, 0, 65535, 0, 255); // Map the brightness scale factor to a range of 0-255
103 CRGB target = color;
104 target.nscale8_video(scale); // Use FastLED's nscale8_video to apply the scale factor
105 CRGB target2 = NVIS_CGRB_GREEN_A; // For GRB LEDs (e.g. Radar Altimeter and Standby Instruments)
106 target2.nscale8_video(scale);
107 current_backl_brightness = newValue; // Update and save the current brightness value
108
109 int n = getLedCount();
110 for (int i = 0; i < n; i++) { // For each LED, read info from PROGMEM; if LED is BACKLIGHT, set color
111 Led led;
112 memcpy_P(&led, &getLedTable()[i], sizeof(Led)); // getLedTable() accesses the panel's LED table
113 uint16_t ledIndex = led.index + getStartIndex();
114 if (led.role == LED_INSTR_BL) {
115 getLedStrip()[ledIndex] = target;
116 }
117 if (led.role == LED_INSTR_BL_CGRB) {
118 getLedStrip()[ledIndex] = target2;
119 }
120 }
121 LedUpdateState::getInstance()->setUpdateFlag(true); // Inform that LEDs need to be updated
122 }
123
130 void setConsoleLights(uint16_t newValue, const CRGB& color = NVIS_GREEN_A) { // Set the color of all LEDs with role LED_CONSOLE_BL
131 if (!getLedStrip() || !getLedTable()) return; // Safety checks
132 if (newValue == current_console_brightness) return; // Exit if no brightness change
133 int scale = map(newValue, 0, 65535, 0, 255); // Map the brightness scale factor to a range of 0-255
134 CRGB target = color;
135 target.nscale8_video(scale); // Use FastLED's nscale8_video to apply the scale factor
136 current_console_brightness = newValue; // Update and save the current brightness value
137
138 int n = getLedCount();
139 for (int i = 0; i < n; i++) { // For each LED, read info from PROGMEM; if LED is BACKLIGHT, set color
140 Led led;
141 memcpy_P(&led, &getLedTable()[i], sizeof(Led)); // getLedTable() accesses the panel's LED table
142 uint16_t ledIndex = led.index + getStartIndex();
143 if (led.role == LED_CONSOLE_BL) {
144 getLedStrip()[ledIndex] = target;
145 }
146 }
147 LedUpdateState::getInstance()->setUpdateFlag(true); // Inform that LEDs need to be updated
148 }
149
156 void setIndicatorColor(LedRole role, const CRGB& color) { // Set color of specific LEDs ("role" parameter)
157 if (!getLedStrip() || !getLedTable()) return;
158 int n = getLedCount();
159 for (int i = 0; i < n; i++) {
160 Led led;
161 memcpy_P(&led, &getLedTable()[i], sizeof(Led));
162 uint16_t ledIndex = led.index + getStartIndex();
163 if (led.role == role) {
164 getLedStrip()[ledIndex] = color;
165 }
166 }
167 LedUpdateState::getInstance()->setUpdateFlag(true); // Inform that LEDs need to be updated
168 }
169
175 void setFloodlights(uint16_t newValue) { // Set the brightness of LEDs with role LED_FLOOD
176 if (!getLedStrip() || !getLedTable()) return; // Same structure as setInstrLights()
177 if (newValue == current_flood_brightness) return;
178 current_flood_brightness = newValue;
179
180 uint8_t scale = map(newValue, 0, 65535, 0, 255);
181
182 CRGB target = NVIS_WHITE;
183 target.nscale8_video(scale);
184
185 int n = getLedCount();
186 for (int i = 0; i < n; i++) {
187 Led led;
188 memcpy_P(&led, &getLedTable()[i], sizeof(Led));
189 uint16_t ledIndex = led.index + getStartIndex();
190 if (led.role == LED_FLOOD) {
191 getLedStrip()[ledIndex] = target;
192 }
193 }
194 LedUpdateState::getInstance()->setUpdateFlag(true); // Inform that LEDs need to be updated
195 }
196
201 void setAllLightsOff() { // Turn off all lights and reset brightness state
202 if (!getLedStrip() || !getLedTable()) return; // Safety checks
203
204 CRGB* ledArray = getLedStrip();
205 int startIndex = getStartIndex();
206 int panelLedCount = getLedCount();
207
208 for (int i = 0; i < panelLedCount; i++) {
209 ledArray[startIndex + i] = NVIS_BLACK;
210 }
211
212 // Reset all brightness variables to 0
213 current_backl_brightness = 0;
214 current_console_brightness = 0;
215 current_flood_brightness = 0;
216
217 LedUpdateState::getInstance()->setUpdateFlag(true); // Inform that LEDs need to be updated
218 }
219};
220
221#endif
void setUpdateFlag(bool requireUpdate)
Sets the LED update flag in an atomic operation.
static LedUpdateState * getInstance()
Gets the singleton instance of the LedUpdateState class.
Definition Panel.h:41
virtual const Led * getLedTable() const
Gets the LED table for this panel.
Definition Panel.h:59
void setAllLightsOff()
Turns off all lights in this panel, irrespective of their role, and resets brightness state.
Definition Panel.h:201
void setInstrLights(uint16_t newValue, const CRGB &color=NVIS_GREEN_A)
Set the color of all instrument backlight LEDs.
Definition Panel.h:99
virtual int getLedCount() const
Gets the number of LEDs in this panel.
Definition Panel.h:53
void setFloodlights(uint16_t newValue)
Sets the brightness of floodlight LEDs.
Definition Panel.h:175
Panel()
Protected constructor to prevent direct instantiation.
Definition Panel.h:75
virtual CRGB * getLedStrip() const
Gets the LED strip for this panel.
Definition Panel.h:65
virtual int getStartIndex() const
Gets the start index of this panel on the LED strip.
Definition Panel.h:47
void setConsoleLights(uint16_t newValue, const CRGB &color=NVIS_GREEN_A)
Sets the color of all console backlight LEDs.
Definition Panel.h:130
void setIndicatorColor(LedRole role, const CRGB &color)
Sets the color of LEDs with a specific role.
Definition Panel.h:156