API Documentation v0.0.1
Loading...
Searching...
No Matches
Channel.h
1/**********************************************************************************************************************
2 * ____ _ _ _
3 * / __ \ | | | | | |
4 * | | | |_ __ ___ _ __ | |__| | ___ _ __ _ __ ___| |_
5 * | | | | '_ \ / _ \ '_ \| __ |/ _ \| '__| '_ \ / _ \ __|
6 * | |__| | |_) | __/ | | | | | | (_) | | | | | | __/ |_
7 * \____/| .__/ \___|_| |_|_| |_|\___/|_| |_| |_|\___|\__|
8 * | |
9 * |_|
10 * ----------------------------------------------------------------------------------
11 *
12 * @file Channel.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 Channels are the logical representation of LED strips.
18 * @details Each channel maintains an array of LEDs, and a pointer to the first panel on the channel.
19 * The channel class provides methods to add panels to the channel and to update the backlights of all
20 * panels in the channel. The channel class does not hold an array of panels. Instead, panels are organized
21 * in a linked list within each channel. This conserves stack memory. This approach avoids allocating
22 * fixed-size arrays for panels in each channel, which would exhaust the limited stack space on the
23 * Arduino Mega 2560 (I tested it). Instead, this class provides a pointer to its first panel.
24 * Thus,the channels are still able to iterate through all of their panels.
25 *********************************************************************************************************************/
26
27#ifndef __CHANNEL_H
28#define __CHANNEL_H
29
30#include "FastLED.h"
31#include "Panel.h"
32#include "Colors.h"
33
34
35class Channel {
36
37private:
38 uint8_t pin; // Hardware pin number
39 const char* pcbName; // Changed from char* to const char*
40 uint16_t ledCount; // Number of LEDs
41 CRGB* leds; // Pointer to LED array
42 uint16_t currentIndex; // Index of the next available LED
43 Panel* firstPanel; // Pointer to first panel in the channel
44 uint8_t panelCount; // Number of panels in the channel
45
46public:
54 Channel(uint8_t p, const char* pcb, CRGB* ledArray, uint16_t count) {
55 pin = p;
56 pcbName = pcb;
57 leds = ledArray; // Store pointer to the static array
58 ledCount = count;
59 currentIndex = 0; // Initialize currentIndex to 0
60 firstPanel = nullptr; // Initialize first panel pointer
61 panelCount = 0; // Initialize panel count
62 }
63
68 void initialize() {
69 // Use a switch statement to overcome strange behaviour of FastLED to have a pin number at compile time
70 switch(pin) {
71 case 4: FastLED.addLeds<WS2812B, 4, GRB>(leds, ledCount); break;
72 case 5: FastLED.addLeds<WS2812B, 5, GRB>(leds, ledCount); break;
73 case 6: FastLED.addLeds<WS2812B, 6, GRB>(leds, ledCount); break;
74 case 7: FastLED.addLeds<WS2812B, 7, GRB>(leds, ledCount); break;
75 case 8: FastLED.addLeds<WS2812B, 8, GRB>(leds, ledCount); break;
76 case 9: FastLED.addLeds<WS2812B, 9, GRB>(leds, ledCount); break;
77 case 10: FastLED.addLeds<WS2812B, 10, GRB>(leds, ledCount); break;
78 case 11: FastLED.addLeds<WS2812B, 11, GRB>(leds, ledCount); break;
79 case 12: FastLED.addLeds<WS2812B, 12, GRB>(leds, ledCount); break;
80 case 13: FastLED.addLeds<WS2812B, 13, GRB>(leds, ledCount); break;
81 default: break; // Handle invalid pin
82 }
83
84 fill_solid(leds, ledCount, NVIS_BLACK);
85 }
86
92 template<typename PanelType>
93 void addPanel() {
94 PanelType* panel = PanelType::getInstance(currentIndex, leds);
95
96 // Safety check: Ensure we don't exceed the channel's LED capacity
97 if (currentIndex + panel->getLedCount() > ledCount) {
98 // Halt execution and indicate error with LED pattern
99 while(1) {
100 digitalWrite(LED_BUILTIN, HIGH);
101 delay(100);
102 digitalWrite(LED_BUILTIN, LOW);
103 delay(100);
104 }
105 }
106
107 // Add panel to linked list
108 if (firstPanel == nullptr) {
109 firstPanel = panel; // First panel in the channel
110 } else {
111 // Find the last panel
112 Panel* currentPanel = firstPanel;
113 while (currentPanel->nextPanel != nullptr) {
114 currentPanel = currentPanel->nextPanel;
115 }
116 currentPanel->nextPanel = panel; // Add new panel at the end
117 }
118
119 panelCount++;
120 currentIndex += panel->getLedCount();
121 }
122
123 // Getters
128 uint8_t getPin() const { return pin; }
129
134 const char* getPcbName() const { return pcbName; }
135
140 uint16_t getLedCount() const { return ledCount; }
141
146 CRGB* getLeds() const { return leds; }
147
152 Panel* getFirstPanel() const { return firstPanel; }
153
158 uint8_t getPanelCount() const { return panelCount; }
159
166 void updateInstrLights(uint16_t brightness, const CRGB& color = NVIS_GREEN_A) {
167 Panel* current = firstPanel;
168 while (current != nullptr) {
169 current->setInstrLights(brightness, color);
170 current = current->nextPanel;
171 }
172 }
173
180 void updateConsoleLights(uint16_t brightness, const CRGB& color = NVIS_GREEN_A) {
181 Panel* current = firstPanel;
182 while (current != nullptr) {
183 current->setConsoleLights(brightness, color);
184 current = current->nextPanel;
185 }
186 }
187
193 void updateFloodLights(uint16_t brightness) {
194 Panel* current = firstPanel;
195 while (current != nullptr) {
196 current->setFloodlights(brightness);
197 current = current->nextPanel;
198 }
199 }
200
206 // Clear all LEDs in the entire channel array (not just panel-tracked ones)
207 fill_solid(leds, ledCount, NVIS_BLACK);
208
209 // Also clear panel-tracked LEDs and reset brightness state
210 Panel* current = firstPanel;
211 while (current != nullptr) {
212 current->setAllLightsOff();
213 current = current->nextPanel;
214 }
215 }
216
217};
218
219#endif
uint8_t getPin() const
Gets the pin number for this channel.
Definition Channel.h:128
void updateInstrLights(uint16_t brightness, const CRGB &color=NVIS_GREEN_A)
Updates backlights for all panels in this channel.
Definition Channel.h:166
uint8_t getPanelCount() const
Gets the number of panels in this channel.
Definition Channel.h:158
void initialize()
Initializes the LED strip with FastLED.
Definition Channel.h:68
CRGB * getLeds() const
Gets the LED array for this channel.
Definition Channel.h:146
Channel(uint8_t p, const char *pcb, CRGB *ledArray, uint16_t count)
Constructor for the Channel class.
Definition Channel.h:54
void updateConsoleLights(uint16_t brightness, const CRGB &color=NVIS_GREEN_A)
Updates console lights for all panels in this channel.
Definition Channel.h:180
void addPanel()
Adds a panel to the channel's linked list.
Definition Channel.h:93
void updateFloodLights(uint16_t brightness)
Updates flood lights for all panels in this channel.
Definition Channel.h:193
const char * getPcbName() const
Gets the PCB name for this channel.
Definition Channel.h:134
void setAllLightsOff()
Turns off all lights in all panels of this channel and resets brightness state.
Definition Channel.h:205
Panel * getFirstPanel() const
Gets the first panel in the channel's linked list.
Definition Channel.h:152
uint16_t getLedCount() const
Gets the number of LEDs in this channel.
Definition Channel.h:140
Definition Panel.h:41
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
void setFloodlights(uint16_t newValue)
Sets the brightness of floodlight LEDs.
Definition Panel.h:175
void setConsoleLights(uint16_t newValue, const CRGB &color=NVIS_GREEN_A)
Sets the color of all console backlight LEDs.
Definition Panel.h:130