DIY Auto-Correlator 1.0
Auto-Correlator Card implementation using Teensy 4.x microcontrollers.
ledset.hpp
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
4#include <algorithm>
5#include "pins.hpp"
6#include <array> //For std::begin and std::end
7#include <initializer_list>
8
9
11template <unsigned int SET_SIZE>
12class LEDSet
13{
14public:
15
16 enum ledstate_t {OFF = 0, ON = 1, Analog = 2};
17
18 const int LEDs[SET_SIZE] = {SAFE_OUTPUT_DUMP_PIN};
19 unsigned int State[SET_SIZE];
20 bool Error_State = false;
21
22 //0
25 LEDSet(std::initializer_list<unsigned int> pins)
26 {
27 std::copy(std::begin(pins), std::end(pins), const_cast<int*>(LEDs));
28 for(unsigned int i = 0; i < this->size(); i++)
29 {
30 State[i] = false;
31 }
32 this->state_reload();
33 }
34
35 //1
37 unsigned int size() __attribute__((always_inline))
38 {
39 return std::distance(std::begin(LEDs), std::end(LEDs));
40 }
41
42 //2
44 inline void init()
45 {
46 for(unsigned int i = 0; i < size(); i++)
47 {
48 pinMode(LEDs[i], OUTPUT);
49 }
50 asm volatile ("dsb");
51 }
52
53 //3
57 inline void state_reload()
58 {
59 for(unsigned int i = 0; i < size(); i++)
60 {
61 if(State[i] < Analog)
62 {
63 pinMode(LEDs[i], OUTPUT);
64 digitalWrite(LEDs[i], State[i]);
65 }
66 }
67 }
68
69 //4
71 inline void ON_todigital()
72 {
73 for(unsigned int i = 0; i < size(); i++)
74 {
75 if(State[i] != OFF)
76 {
77 pinMode(LEDs[i], OUTPUT);
78 digitalWrite(LEDs[i], State[i]);
79 State[i] = ON;
80 }
81 }
82 }
83
84 //5
86 inline void toggle_all()
87 {
88 for(unsigned int i = 0; i < size(); i++)
89 {
90 State[i] = !(State[i] != OFF);
91 }
92 this->state_reload();
93 }
94
95 //6
97 inline void toggle_twice(int pin, double time_ms)
98 {
99 int index = fetch_index(pin);
100 if(index != -1)
101 {
102 pinMode(pin, OUTPUT);
103 State[index] = !(State[index] != OFF); //Collapse (ON and ANALOG) to just ON.
104 digitalWrite(pin, State[index]);
105
106 delay(time_ms);
107
108 State[index] = !(State[index] != OFF); //Collapse (ON and ANALOG) to just ON.
109 digitalWrite(pin, State[index]);
110 }
111
112 }
113
114 //7
118 inline void toggle_all_routine(int delay_ms)
119 {
120 toggle_all();
121
122 delay(delay_ms);
123
124 toggle_all();
125 }
126
127 //8
129 inline void dim(int pin, unsigned int analog_val)
130 {
131 int index = fetch_index(pin);
132 if(index != -1)
133 {
134 analogWrite(pin, analog_val);
135 State[index] = Analog;
136 }
137 }
138
139 //9
141 inline void dim_all(unsigned int analog_val)
142 {
143 for(unsigned int i = 1; i < size(); i++)
144 {
145 if(State[i] != OFF)
146 {
147 analogWrite(LEDs[i], analog_val);
148 }
149 }
150 }
151
152 //10
155 inline void dim_all_routine(unsigned int end_analog_val, double time_s)
156 {
157 int cycles = (time_s * 1000) / 50; //Dimming Resolution is 50 ms.
158 int steps = int(double(255 - end_analog_val)/cycles);
159 this->dim_all(255); //Max Brightness
160 volatile int analog_val = 255;
161 delay(50);
162 for(int j = 0; j < cycles-1; j++)
163 {
164 analog_val -= steps;
165 dim_all(analog_val);
166 delay(50);
167 }
168 this->dim_all(end_analog_val);
169 }
170
171 //11
173 inline void max_bright_all()
174 {
175 this->dim_all(255);
176 }
177
178 //12
180 inline void min_bright_all()
181 {
182 this->dim_all(1);
183 }
184
186
187 //All LEDs
188 //13
190 inline void set_all() __attribute__((always_inline))
191 {
192 for(unsigned int i = 0; i < size(); i++)
193 {
194 pinMode(LEDs[i], OUTPUT);
195 digitalWrite(LEDs[i], HIGH);
196 State[i] = ON;
197 }
198 }
199
200 //14
202 inline void unset_all() __attribute__((always_inline))
203 {
204 for(unsigned int i = 0; i < size(); i++)
205 {
206 pinMode(LEDs[i], OUTPUT);
207 digitalWrite(LEDs[i], LOW);
208 State[i] = OFF;
209 }
210 }
211
212 //Single LEDs
213 //15
215 inline void set(int led_pin) __attribute__((always_inline))
216 {
217 int index = fetch_index(led_pin);
218 if(index != -1)
219 {
220 pinMode(led_pin, OUTPUT);
221 digitalWrite(led_pin, HIGH);
222 State[index] = ON;
223 }
224 }
225
226 //16
228 void unset(int led_pin) __attribute__((always_inline))
229 {
230 int index = fetch_index(led_pin);
231 if(index != -1)
232 {
233 pinMode(led_pin, OUTPUT);
234 digitalWrite(led_pin, LOW);
235 State[index] = OFF;
236 }
237 }
238
239 //Two LEDs
240 //17
242 void set(int led_pin1, int led_pin2) __attribute__((always_inline))
243 {
244 int index1 = fetch_index(led_pin1);
245 int index2 = fetch_index(led_pin2);
246
247 if(index1 != -1 && index2 != -1)
248 {
249 pinMode(led_pin1, OUTPUT);
250 pinMode(led_pin2, OUTPUT);
251 digitalWrite(led_pin1, HIGH);
252 digitalWrite(led_pin2, HIGH);
253
254 State[index1] = ON;
255 State[index2] = ON;
256
257 }
258 }
259
260 //18
262 void unset(int led_pin1, int led_pin2) __attribute__((always_inline))
263 {
264 int index1 = fetch_index(led_pin1);
265 int index2 = fetch_index(led_pin2);
266
267 if(index1 != -1 && index2 != -1)
268 {
269 pinMode(led_pin1, OUTPUT);
270 pinMode(led_pin2, OUTPUT);
271
272 digitalWrite(led_pin1, LOW);
273 digitalWrite(led_pin2, LOW);
274
275 State[index1] = OFF;
276 State[index2] = OFF;
277
278 }
279 }
280
281 //Three LEDs
282 //19
284 void set(int led_pin1, int led_pin2, int led_pin3) __attribute__((always_inline))
285 {
286 int index1 = fetch_index(led_pin1);
287 int index2 = fetch_index(led_pin2);
288 int index3 = fetch_index(led_pin3);
289
290 if(index1 != -1 && index2 != -1 && index3 != -1)
291 {
292 pinMode(led_pin1, OUTPUT);
293 pinMode(led_pin2, OUTPUT);
294 pinMode(led_pin3, OUTPUT);
295
296 digitalWrite(led_pin1, HIGH);
297 digitalWrite(led_pin2, HIGH);
298 digitalWrite(led_pin3, HIGH);
299
300 State[index1] = ON;
301 State[index2] = ON;
302 State[index3] = ON;
303 }
304 }
305
306 //20
308 void unset(int led_pin1, int led_pin2, int led_pin3) __attribute__((always_inline))
309 {
310 int index1 = fetch_index(led_pin1);
311 int index2 = fetch_index(led_pin2);
312 int index3 = fetch_index(led_pin3);
313
314 if(index1 != -1 && index2 != -1 && index3 != -1)
315 {
316 pinMode(led_pin1, OUTPUT);
317 pinMode(led_pin2, OUTPUT);
318 pinMode(led_pin3, OUTPUT);
319
320 digitalWrite(led_pin1, LOW);
321 digitalWrite(led_pin2, LOW);
322 digitalWrite(led_pin3, LOW);
323
324 State[index1] = OFF;
325 State[index2] = OFF;
326 State[index3] = OFF;
327 }
328 }
329
330 //21
332 void inline error(int pin)
333 {
334 if(!Error_State)
335 { this->unset_all(); }
336
337 if(fetch_index(pin) != -1)
338 {
339 this->Error_State = true;
340 this->set(pin);
341 }
342
343 }
344
345 //22
347 void inline assert_errors()
348 {
349 if(this->Error_State)
350 {
351 for(unsigned int i = 0; i < 10; i++)
352 {
353 this->toggle_all_routine(500);
354 }
355
356 this->set_all();
357 this->unset(LOOP_LED);
358
359 #if ABORT_ON_ERROR == 1
360 abort();
361 #endif
362 }
363 }
364
365 //23
367 bool is_valid_pin(int pin)
368 {
369 return fetch_index(pin) != -1;
370 }
371
372private:
373 //24
376 int fetch_index(int pin) __attribute__((always_inline))
377 {
378 for(unsigned int i = 0; i < size(); i++)
379 {
380 if(pin == LEDs[i])
381 {
382 return i;
383 }
384 }
385
386 return -1;
387 }
388
389};
Definition: ledset.hpp:13
unsigned int size() __attribute__((always_inline))
Returns the size of the led sets.
Definition: ledset.hpp:37
LEDSet(std::initializer_list< unsigned int > pins)
Constructor for LEDSet class.
Definition: ledset.hpp:25
void ON_todigital()
Sets all leds with states (ON == 1) and (Analog == 2) to (ON == 1).
Definition: ledset.hpp:71
void unset(int led_pin1, int led_pin2, int led_pin3) __attribute__((always_inline))
Turn-FF three LED Pins simultaneously.
Definition: ledset.hpp:308
void dim_all_routine(unsigned int end_analog_val, double time_s)
Definition: ledset.hpp:155
int fetch_index(int pin) __attribute__((always_inline))
Retrive data structure index based on the pin number. Returns -1 if the pin was not found.
Definition: ledset.hpp:376
void state_reload()
Sets the LEDs based on the value of the corresponding state variable. This function is useful when th...
Definition: ledset.hpp:57
void set(int led_pin1, int led_pin2, int led_pin3) __attribute__((always_inline))
Turn-ON three LED Pins simultaneously.
Definition: ledset.hpp:284
void toggle_twice(int pin, double time_ms)
Toggles the state of a particular LED. This is a pure digital function and destroys all Analog dim st...
Definition: ledset.hpp:97
void toggle_all_routine(int delay_ms)
Definition: ledset.hpp:118
unsigned int State[SET_SIZE]
ON-OFF/Analog states for all LEDs.
Definition: ledset.hpp:19
void max_bright_all()
Sets maximum brightness for all ON-LEDs.
Definition: ledset.hpp:173
const int LEDs[SET_SIZE]
LED pins.
Definition: ledset.hpp:18
void dim(int pin, unsigned int analog_val)
dims a particular valid led pin.
Definition: ledset.hpp:129
void set(int led_pin) __attribute__((always_inline))
Turn-ON a particular LED Pin.
Definition: ledset.hpp:215
void min_bright_all()
Definition: ledset.hpp:180
void set(int led_pin1, int led_pin2) __attribute__((always_inline))
Turn-ON two LED Pins simultaneously. The function only works if both the Pins are valid pins.
Definition: ledset.hpp:242
void error(int pin)
Special function that sets a valid pin and sets the flag LedSet::Error_State to true....
Definition: ledset.hpp:332
void unset_all() __attribute__((always_inline))
Definition: ledset.hpp:202
void unset(int led_pin) __attribute__((always_inline))
Turn-OFF a particular LED Pin.
Definition: ledset.hpp:228
void init()
Sets up the pin mode of all LEDs.
Definition: ledset.hpp:44
void set_all() __attribute__((always_inline))
Turn on all available LEDs.
Definition: ledset.hpp:190
void assert_errors()
Special function that initiates a special routine if the device is in the Error State....
Definition: ledset.hpp:347
bool is_valid_pin(int pin)
Returns whether the given pin is a "valid pin". A valid pin is a pin that is registered with the LEDS...
Definition: ledset.hpp:367
void unset(int led_pin1, int led_pin2) __attribute__((always_inline))
Turn-OFF two LED Pins simultaneously.
Definition: ledset.hpp:262
void dim_all(unsigned int analog_val)
Dims the ON-LEDs with the provided analog voltage. Only affects LEDs that are ON or on Analog Mode.
Definition: ledset.hpp:141
ledstate_t
Definition: ledset.hpp:16
@ ON
Definition: ledset.hpp:16
@ Analog
Definition: ledset.hpp:16
@ OFF
Definition: ledset.hpp:16
void toggle_all()
Toggles the state of all the LEDs. This is a pure digital function and destroys all Analog dim states...
Definition: ledset.hpp:86
bool Error_State
Whether the LED Panel is in an error state.
Definition: ledset.hpp:20
const int LOOP_LED
Definition: pins.hpp:30
const int SAFE_OUTPUT_DUMP_PIN
Definition: pins.hpp:15