-
Notifications
You must be signed in to change notification settings - Fork 1
/
LED.cpp
144 lines (115 loc) · 2.78 KB
/
LED.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @brief LED controller singleton for Seeed XIAO nRF52840 BLE (Sense)
* @file led.cpp
* @author TANAHASHI, Jiro (aka jtFuruhata) <[email protected]>
* @version 1.2.0
* @date 2023-03-22
*
* @copyright Copyright (c) 2023 jtLab, Hokkaido Information University,
* Release under the MIT License.
* See LICENSE.
*/
#include "LED.h"
LED::LED() {
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
this->setColor(LED::BLACK);
}
void LED::writeLED() {
digitalWrite(LEDR, this->getR() ? LOW : HIGH);
digitalWrite(LEDG, this->getG() ? LOW : HIGH);
digitalWrite(LEDB, this->getB() ? LOW : HIGH);
}
LED* LED::getInstance() {
if (instance == nullptr) {
instance = new LED();
}
return instance;
}
void LED::setColor(LED::COLOR color) {
this->color = color;
this->writeLED();
}
void LED::setLED(LED::BRG brg) {
this->color = (LED::COLOR)((byte)this->color | (byte)brg);
this->writeLED();
}
void LED::setR() {
this->setLED(LED::R);
}
void LED::setG() {
this->setLED(LED::G);
}
void LED::setB() {
this->setLED(LED::B);
}
void LED::flashLED(LED::COLOR color, int delayTime, int times) {
for (int i = 0; i < times; i++) {
this->setColor(color);
delay(delayTime);
this->setColor(LED::BLACK);
delay(delayTime);
}
}
void LED::flashLED(LED::BRG brg, int delayTime, int times) {
for (int i = 0; i < times; i++) {
this->setLED(brg);
delay(delayTime);
this->resetLED(brg);
delay(delayTime);
}
}
void LED::flashR(int delayTime, int times) {
this->flashLED(LED::R, delayTime, times);
}
void LED::flashG(int delayTime, int times) {
this->flashLED(LED::G, delayTime, times);
}
void LED::flashB(int delayTime, int times) {
this->flashLED(LED::B, delayTime, times);
}
void LED::resetLED(LED::BRG brg) {
this->color = (LED::COLOR)((byte)this->color & ~(byte)brg);
this->writeLED();
}
void LED::resetR() {
this->resetLED(LED::R);
}
void LED::resetG() {
this->resetLED(LED::G);
}
void LED::resetB() {
this->resetLED(LED::B);
}
void LED::flipLED(LED::BRG brg) {
this->color = (LED::COLOR)((byte)this->color ^ (byte)brg);
this->writeLED();
}
void LED::flipR() {
this->flipLED(LED::R);
}
void LED::flipG() {
this->flipLED(LED::G);
}
void LED::flipB() {
this->flipLED(LED::B);
}
LED::COLOR LED::getColor() {
return this->color;
}
bool LED::getLED(LED::BRG brg) {
return (this->color & (byte)brg) ? true : false;
}
bool LED::getR() {
return this->getLED(LED::R);
}
bool LED::getG() {
return this->getLED(LED::G);
}
bool LED::getB() {
return this->getLED(LED::B);
}
LED* LED::instance = nullptr;
LED* LEDobject = LED::getInstance();
LED& led = *LEDobject;