This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeoPixel.ino
175 lines (150 loc) · 3.53 KB
/
NeoPixel.ino
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <Adafruit_NeoPixel.h>
#include <esp32-hal.h>
#define LED_PIN 13
#define RESTART_BUTTON_PIN 15
#define SELECTOR_BUTTON 25
#define LED_COUNT 16
#define BRIGHTNESS 60
#define CONFERENCE_DURATION 2400
#define QUICKY_DURATION 1200
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_KHZ800 + NEO_GRB);
class TimeKeeper
{
private:
int totalTime, remainingTime;
bool started;
public:
TimeKeeper(int seconds)
{
this->totalTime = seconds;
this->remainingTime = seconds;
this->started = false;
}
/**
* Retrieve the totalTime set for this timeKeeper.
*/
int getTotalTime() const
{
return this->totalTime;
}
/**
* Retrieve the remainingTime for this timeKeeper.
*/
int getRemainingTime() const
{
return this->remainingTime;
}
/**
* Start the TimeKeeper.
*/
void start() {
this->started = true;
}
/**
* Stop the TimeKeeper.
*/
void stop() {
this->started = false;
}
/**
* Is the TimeKeeper started ?
*/
bool isStarted(){
return this->started;
}
/**
* wait for the given number of seconds.
*/
void wait(int seconds)
{
delay(seconds * 1000);
this->remainingTime -= seconds;
}
};
class TimeSelector{
private:
int index;
int PRESETS [2] = {CONFERENCE_DURATION, QUICKY_DURATION};
public:
TimeSelector(){
this->index = 0;
}
/**
* Switch to next preset.
*/
void next(){
this->index=(this->index+1) % 2;
}
/**
* Get preset value.
*/
int getPreset(){
return this->PRESETS[this->index];
}
};
TimeSelector timeSelector = TimeSelector();
TimeKeeper timeKeeper = TimeKeeper(timeSelector.getPreset());
void restart() {
timeKeeper.stop();
timeKeeper = TimeKeeper(timeSelector.getPreset());
timeKeeper.start();
}
void select() {
timeKeeper.stop();
timeSelector.next();
}
void setup()
{
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS); // Set BRIGHTNESS to about 1/5 (max = 255)
pinMode(RESTART_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(RESTART_BUTTON_PIN, restart, FALLING);
pinMode(SELECTOR_BUTTON, INPUT_PULLUP);
attachInterrupt(SELECTOR_BUTTON, select, FALLING);
}
void loop()
{
display();
}
void display() {
log_printf("time keeper is started %b \n", timeKeeper.isStarted());
while (timeKeeper.getRemainingTime() > 0 && timeKeeper.isStarted())
{
log_printf("New loop is starting with %d \n", timeKeeper.getRemainingTime());
displayTimeKeeper();
}
log_printf("Selector with %d \n", timeSelector.getPreset());
displayTimeSelector();
}
void displayTimeSelector(){
strip.clear();
int coeff = (CONFERENCE_DURATION / timeSelector.getPreset());
for (int i = 0; i < int((strip.numPixels()) / coeff); i++)
{
strip.setPixelColor(i, strip.Color(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS));
}
strip.show();
delay(1000);
}
void displayTimeKeeper()
{
int seconds = strip.numPixels() - 1;
while (seconds >= 0 && timeKeeper.isStarted())
{
displayRemainingTime();
strip.setPixelColor(seconds, strip.Color(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS));
strip.show();
timeKeeper.wait(1);
seconds--;
}
}
void displayRemainingTime()
{
strip.clear();
int coeff = (timeKeeper.getTotalTime() * 10 / timeKeeper.getRemainingTime());
for (int i = 0; i <= int((strip.numPixels()) * 10 / coeff); i++)
{
strip.setPixelColor(i, strip.Color(coeff > 85 ? BRIGHTNESS : 0, coeff <= 85 ? BRIGHTNESS : 0, 0));
}
}