3
3
#include " constants.h"
4
4
5
5
/* *
6
- * Code that controls the two led strips
6
+ * Code for controlling the two LED strips.
7
+ * Enables display of various states like speed,
8
+ * battery level and warnings through LED animations.
7
9
*/
8
10
9
11
/*
10
12
* CONSTANTS
11
13
*/
12
- const int LedBar_Num = 10 ; // Number of LEDs in the strip
13
- const int LEDBar_Brightness = 15 ;
14
- const int LEDBar_BrightnessSecond = 3 ;
14
+ const int LedBar_Num = 10 ; // Number of LEDs per strip
15
+ const int LEDBar_Brightness = 15 ; // Default brightness for active LEDs
16
+ const int LEDBar_BrightnessSecond = 3 ; // Reduced brightness for secondary displays
15
17
16
18
/*
17
19
* GLOBAL VARIABLES
18
20
*/
21
+ // NeoPixel object for both LED strips
19
22
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LedBar_Num + LedBar2_Num, PIN_LEDBAR, NEO_GRB + NEO_KHZ800);
20
23
21
- unsigned long lastOverrideTime = 0 ;
22
- bool isOverridden = false ;
23
- int lastSpeedValue = 0 ;
24
- const unsigned long OVERRIDE_TIMEOUT = 30000 ; // 30 Sekunden in Millisekunden
24
+ unsigned long lastOverrideTime = 0 ; // Timestamp of last override
25
+ bool isOverridden = false ; // Flag for active override
26
+ int lastSpeedValue = 0 ; // Stores last speed value
27
+ const unsigned long OVERRIDE_TIMEOUT = 30000 ; // Timeout for overrides (30 seconds)
25
28
29
+ /* *
30
+ * Initializes the LED strips and sets them to standby mode
31
+ */
26
32
void ledBarSetup (){
27
- // Neopixel
28
33
strip.begin ();
29
- strip.show (); // Alle LEDs ausschalten
34
+ strip.show (); // Turn off all LEDs
30
35
setBarStandby ();
31
36
}
32
37
33
-
38
+ /* *
39
+ * Sets the LEDs of a strip with defined colors and brightness
40
+ * @param stripNumber Number of LED strip (1 or 2)
41
+ * @param numLEDsOn Number of active LEDs
42
+ * @param hexColorOn Hex color code for active LEDs
43
+ * @param brightnessOn Brightness of active LEDs (0-100)
44
+ * @param hexColorOff Hex color code for inactive LEDs
45
+ * @param brightnessOff Brightness of inactive LEDs (0-100)
46
+ */
34
47
void setBar (int stripNumber, int numLEDsOn, String hexColorOn, int brightnessOn, String hexColorOff, int brightnessOff) {
35
- // Make sure that stripNumber is valid (1 for the first strip, 2 for the second strip)
36
48
if (stripNumber != 1 && stripNumber != 2 ) {
37
- return ; // Unauthorized value, do nothing
49
+ return ; // Invalid value, abort function
38
50
}
39
51
40
- // Berechne den Startindex basierend auf stripNumber
41
52
int startIndex = (stripNumber == 1 ) ? 0 : LedBar_Num;
42
-
43
- // Calculate the end index based on stripNumber
44
53
int endIndex = (stripNumber == 1 ) ? LedBar_Num : LedBar_Num + LedBar2_Num;
45
54
46
- // Convert the hex color value to RGB color values for the switched-on color
55
+ // Convert hex color code to RGB for active LEDs
47
56
long numberOn = (long )strtol (&hexColorOn[1 ], NULL , 16 );
48
57
int redOn = numberOn >> 16 ;
49
58
int greenOn = (numberOn >> 8 ) & 0xFF ;
50
59
int blueOn = numberOn & 0xFF ;
51
60
52
- // Set the LEDs according to the specified brightness and colors
61
+ // Set active LEDs
53
62
for (int i = startIndex; i < startIndex + numLEDsOn; i++) {
54
63
int dimmed_color_r = redOn * brightnessOn / 100 ;
55
64
int dimmed_color_g = greenOn * brightnessOn / 100 ;
56
65
int dimmed_color_b = blueOn * brightnessOn / 100 ;
57
66
strip.setPixelColor (i, strip.Color (dimmed_color_r, dimmed_color_g, dimmed_color_b));
58
67
}
59
68
60
- // Set the LEDs for the side that is switched off
69
+ // Set inactive LEDs
61
70
for (int i = startIndex + numLEDsOn; i < endIndex; i++) {
62
- // Convert the hex color value to RGB color values for the switched off color
63
71
long numberOff = (long )strtol (&hexColorOff[1 ], NULL , 16 );
64
72
int redOff = numberOff >> 16 ;
65
73
int greenOff = (numberOff >> 8 ) & 0xFF ;
@@ -70,29 +78,46 @@ void setBar(int stripNumber, int numLEDsOn, String hexColorOn, int brightnessOn,
70
78
strip.show (); // Update LED strips
71
79
}
72
80
81
+ /* *
82
+ * Sets LED strips to standby mode
83
+ */
73
84
void setBarStandby () {
74
85
setBar (1 ,10 ," #e38f09" , LEDBar_BrightnessSecond, " #000000" , 0 );
75
86
}
76
87
88
+ /* *
89
+ * Checks and handles timeouts of overrides
90
+ */
77
91
void handleBarOverride () {
78
92
if (isOverridden && (millis () - lastOverrideTime > OVERRIDE_TIMEOUT)) {
79
93
isOverridden = false ;
80
- setBarSpeed (lastSpeedValue); // Stelle Speed-Anzeige wieder her
94
+ setBarSpeed (lastSpeedValue); // Restore speed display
81
95
}
82
96
}
83
97
98
+ /* *
99
+ * Displays current speed on LED strip
100
+ * @param num Number of LEDs to activate (speed)
101
+ */
84
102
void setBarSpeed (int num) {
85
- lastSpeedValue = num; // Speichern des letzten Speed-Wertes
103
+ lastSpeedValue = num;
86
104
if (!isOverridden) {
87
105
setBar (1 ,num," #cb1bf2" , LEDBar_Brightness, " #000000" , 0 );
88
106
}
89
107
}
90
108
109
+ /* *
110
+ * Displays battery level on second LED strip
111
+ * @param num Battery level (0-10)
112
+ */
91
113
void setBarBattery (int num) {
92
114
int calc = LedBar_Num-num;
93
115
setBar (2 ,calc," #e30b0b" , LEDBar_BrightnessSecond, " #0a9e08" , LEDBar_Brightness);
94
116
}
95
117
118
+ /* *
119
+ * Shows leak warnings through LED animations
120
+ */
96
121
void setBarLeak () {
97
122
int frontLeakState = digitalRead (PIN_LEAK_FRONT);
98
123
int backLeakState = digitalRead (PIN_LEAK_BACK);
@@ -102,34 +127,46 @@ void setBarLeak() {
102
127
lastOverrideTime = millis ();
103
128
104
129
if (backLeakState == LOW && frontLeakState == LOW) {
105
- setBar (1 ,10 ," #0000FF" , LEDBar_Brightness, " #0000FF" , 0 );
130
+ setBar (1 ,10 ," #0000FF" , LEDBar_Brightness, " #0000FF" , 0 ); // Both leaks
106
131
} else if (backLeakState == LOW) {
107
- setBar (1 ,5 ," #0000FF" , LEDBar_Brightness, " #0000FF" , 0 );
132
+ setBar (1 ,5 ," #0000FF" , LEDBar_Brightness, " #0000FF" , 0 ); // Back leak
108
133
} else if (frontLeakState == LOW) {
109
- setBar (1 ,5 ," #0000FF" , 0 , " #0000FF" , LEDBar_Brightness);
134
+ setBar (1 ,5 ," #0000FF" , 0 , " #0000FF" , LEDBar_Brightness); // Front leak
110
135
}
111
136
}
112
137
}
113
138
139
+ /* *
140
+ * Shows powerbank status
141
+ * @param status true for active, false for inactive
142
+ */
114
143
void setBarPowerBank (bool status) {
115
144
isOverridden = true ;
116
145
lastOverrideTime = millis ();
117
146
118
147
if (status){
119
- setBar (1 ,9 ," #000000" , 0 , " #036ffc" , LEDBar_Brightness);
148
+ setBar (1 ,9 ," #000000" , 0 , " #036ffc" , LEDBar_Brightness); // Blue for active
120
149
}
121
150
else {
122
- setBar (1 ,9 ," #000000" , 0 , " #ff0000" , LEDBar_Brightness);
151
+ setBar (1 ,9 ," #000000" , 0 , " #ff0000" , LEDBar_Brightness); // Red for inactive
123
152
}
124
153
}
125
154
155
+ /* *
156
+ * Sets a specific number of LEDs to white
157
+ * @param num Number of white LEDs
158
+ */
126
159
void setBarLED (int num) {
127
160
isOverridden = true ;
128
161
lastOverrideTime = millis ();
129
162
int calc = LedBar_Num-num;
130
163
setBar (1 ,calc," #000000" , 0 , " #FFFFFF" , LEDBar_Brightness);
131
164
}
132
165
166
+ /* *
167
+ * Activates/Deactivates flasher mode
168
+ * @param status true for active, false for inactive
169
+ */
133
170
void setBarFlasher (bool status) {
134
171
if (status){
135
172
isOverridden = true ;
0 commit comments