-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLED_Controller.cpp
More file actions
60 lines (48 loc) · 1.34 KB
/
LED_Controller.cpp
File metadata and controls
60 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "LED_Controller.h"
#include "Arduino.h"
#include "DEBUG.h"
namespace LEDAnimation {
LED_Controller* LED_Controller::instance;
LED_Controller::LED_Controller(uint8_t firstPin, uint8_t lastPin) : firstPin(firstPin), lastPin(lastPin) {
for (uint8_t i = firstPin; i <= lastPin; i++) {
pinMode(i, OUTPUT);
}
if (LED_Controller::instance == nullptr) {
LED_Controller::instance = this;
} else {
DEBUG_PRINT("WARN: multiple controllers. ignoring\n")
delete this;
}
}
LED_Controller::~LED_Controller() {}
void LED_Controller::set(uint8_t LED, LEDstate state) {
DEBUG_PRINT("LED ")
DEBUG_PRINT(LED)
DEBUG_PRINT(" set: ")
DEBUG_PRINT(state == LEDstate::on ? "on" : "off")
DEBUG_PRINT("\n")
digitalWrite(transformToPin(LED), resolveToSignal(state));
}
void LED_Controller::setAll(LEDstate state) {
for (int i = firstPin; i <= lastPin; i++) {
digitalWrite(i, resolveToSignal(state));
}
DEBUG_PRINT("All set: ")
DEBUG_PRINT(state == LEDstate::on ? "on\n" : "off\n")
}
LED_Controller* LED_Controller::getInstance()
{
return LED_Controller::instance;
}
uint8_t LED_Controller::transformToPin(unsigned int LED) {
return firstPin - 1 + LED;
}
uint8_t LED_Controller::resolveToSignal(LEDstate state) {
switch (state) {
case LEDstate::on:
return LOW;
case LEDstate::off:
return HIGH;
}
}
}