-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainstruct_2.ino
273 lines (225 loc) · 8.35 KB
/
mainstruct_2.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "modeChanger.h"
#include <FastLED.h>
#include "DualFunctionButton.h"
#define startingLEDs 4 // Number of backlight LEDs BEFORE the strip
#define numLEDs 60 // Number of LEDs in strip
#define LEDOffset 30 // First LED in strip corresponds to 30-th second
struct CRGB _leds[startingLEDs+numLEDs];
// Pin definitions:
// #define ARDUINO_NANO
#ifdef ARDUINO_NANO
#define rotaryLeft 3
#define rotaryRight 4
#define LEDStripPin 9 // Data pin
#define menuPin 7 //
//#define reedSwitchPin 3 // Switches off the display to reduce power consumption (before re-flashing)
// Arduino Nano i2c: SDA = A4, SCL = A5.
#endif
#ifndef ARDUINO_NANO
#define rotaryLeft 4
#define rotaryRight 5
#define LEDStripPin 3 // Data pin
#define menuPin 2 // Arduino Pro Mini supports external interrupts only on pins 2 and 3
#define reedSwitchPin 3 // Switches off the display to reduce power consumption (before re-flashing)
// Arduino Pro Mini i2c: SDA = A4, SCL = A5.
#endif
DualFunctionButton button(menuPin, 1000, INPUT_PULLUP);
// CONVERSIONS
// Which LED (in [0..59] range) corresponds to given hh:mm time
uint8_t _hourPos (int hour, int minute) {
while (hour < 0) { // Foolproof check :)
hour += 12;
}
while (minute < 0) { // Foolproof check :)
minute += numLEDs;
}
uint8_t _hp = (hour%12)*5 + (minute+6)/12;
return (_hp == 60) ? 0 : _hp;
}
// Which offset (in [4..63] range) corresponds to given [0..59] mm time
// First [0..3] offsets are reserved for backlight LEDs (==startingLEDs)
struct CRGB* findLED (int minute) {
while (minute < 0) { // Foolproof check :)
minute += numLEDs;
}
return &_leds[(LEDOffset+minute)%numLEDs+startingLEDs];
}
// END CONVERSIONS
int f1 (long dummy) {
Serial.println ("\nMode: f1");
return 0;
}
int f2 (long dummy) {
Serial.println ("Mode: f2");
return 0;
}
int f3 (long dummy) {
Serial.println ("Mode: f3");
return 0;
}
int (*modeFuncArray[])(long) = {fColorDemo1, fColorDemo2, fColorDemo3};
ModeChanger *mode = new ModeChanger (modeFuncArray, sizeof(modeFuncArray)/sizeof(modeFuncArray[0]));
void setup () {
initDevices ();
readEEPROM ();
Serial.println ("\n\nStarting...");
}
int numPresses = 0;
void loop () {
//static long _currentCallNumber = 0;
//mode->applyMode (fColorDemo10sec);
mode->loopThruModeFunc (100);
if (button.shortPress()) {
Serial.println (++numPresses);
Serial.println ("mode->nextMode();");
mode->nextMode();
}
if (button.longPress()) {
Serial.println ("mode->applyMode (fColorDemo1);");
mode->applyMode (fColorDemo1);
}
//backlightLEDs ();
LEDS.show ();
}
void initDevices (void) {
// Start LEDs
LEDS.addLeds<WS2812B, LEDStripPin, GRB>(_leds, startingLEDs+numLEDs);
LEDS.clear(true);
Serial.begin (9600);
}
void readEEPROM (void) {}
/*extern uint8_t NeoPixel_sine8(uint8_t x);
extern uint8_t NeoPixel_gamma8(uint8_t x);*/
int fColorDemo1 (long currentCallNumber) {
static unsigned long millisAtStart;
//Serial.println ("Mode: fColorDemo1");
//Serial.println (currentCallNumber);
if (currentCallNumber == 0) {
millisAtStart = millis ();
LEDS.clear(true);
}
unsigned long deltaT = millis () - millisAtStart;
unsigned long timeStep = 5;
float ledBrightness = 0.8;
int direction = 1;
int wavelen = 20;
if (deltaT > 3000) { wavelen = 20; timeStep = 4; };
if (deltaT > 6000) { wavelen = 20; timeStep = 3; };
if (deltaT > 9000) { wavelen = 20; timeStep = 2; };
if (deltaT > 12000) { wavelen = 20; timeStep = 1; };
if (deltaT > 15000) { wavelen = 20; direction = -1; };
if (deltaT > 18000) { LEDS.clear (); return 1; };
//Serial.print ("deltaT: "); Serial.println (deltaT);
for (int led = 0; led < numLEDs; led++) {
// uint8_t firstBrightness = NeoPixel_gamma8(NeoPixel_sine8(static_cast<uint8_t>(deltaT/timeStep-direction*led*wavelen)%256));
uint8_t firstBrightness = NeoPixel_gamma8(NeoPixel_sine8((deltaT/timeStep-direction*led*wavelen)%256));
findLED(led)->r = firstBrightness;
//Serial.print (led); Serial.print (": "); Serial.println (firstBrightness);
}
//delay (10000);
return 0;
}
int fColorDemo2 (long currentCallNumber) {
static unsigned long millisAtStart;
//Serial.println ("Mode: fColorDemo2");
//Serial.println (currentCallNumber);
if (currentCallNumber == 0) {
millisAtStart = millis ();
}
unsigned long deltaT = millis () - millisAtStart;
int timeStep = 80;
int ledBrightness = 20;
int direction = -1;
int wavelen = 12;
if (deltaT > 3000) wavelen = 10;
if (deltaT > 6000) wavelen = 8;
if (deltaT > 9000) {wavelen = 6; timeStep = 40; ledBrightness += 80;};
if (deltaT > 12000) wavelen = 4;
if (deltaT > 15000) {wavelen = 4; ledBrightness = 255;};
if (deltaT > 18000) { LEDS.clear (); return 1;};
// Serial.print ("deltaT: "); Serial.println (deltaT);
for (int led = 0; led < numLEDs; led++) {
// =ОСТАТ(время/timeStep1-направление*диод; waveLen)<1
//Serial.print ("deltaT/timeStep-direction*led)%wavelen: "); Serial.println ((deltaT/timeStep-direction*led)%wavelen);
if ((deltaT/timeStep-direction*led)%wavelen < 1) {
// switch this led on
// Хорошо бы и предыдущий led несильно зажигать, чтобы выглядело как затухающий след
// И встречную волну пустить другого цвета
//findLED(led-direction)->b = 25;
findLED(led)->g = ledBrightness;
} else {
// switch this led off
findLED(led)->r = 0;
findLED(led)->g = 0;
findLED(led)->b = 0;
//findLED(led-1)->b = 000;
}
}
return 0;
}
int fColorDemo3 (long currentCallNumber) {
static unsigned long millisAtStart;
//Serial.println ("Mode: fColorDemo3");
//Serial.println (currentCallNumber);
if (currentCallNumber == 0) {
millisAtStart = millis ();
}
unsigned long deltaT = millis () - millisAtStart;
int timeStep = 80;
int ledBrightness = 10;
int direction = 1;
int wavelen = 12;
if (deltaT > 3000) wavelen = 10;
if (deltaT > 6000) {wavelen = 8; timeStep = 40; ledBrightness = 80;};
if (deltaT > 9000) {wavelen = 6; timeStep = 40; ledBrightness = 120;};
if (deltaT > 12000) wavelen = 4;
if (deltaT > 15000) {wavelen = 4; ledBrightness = 255;};
if (deltaT > 18000) { LEDS.clear (); return 1;};
// Serial.print ("deltaT: "); Serial.println (deltaT);
for (int led = 0; led < numLEDs; led++) {
// =ОСТАТ(время/timeStep1-направление*диод; waveLen)<1
//Serial.print ("deltaT/timeStep-direction*led)%wavelen: "); Serial.println ((deltaT/timeStep-direction*led)%wavelen);
if ((deltaT/timeStep-direction*led)%wavelen < 1) {
// switch this led on
// Хорошо бы и предыдущий led несильно зажигать, чтобы выглядело как затухающий след
// И встречную волну пустить другого цвета
//findLED(led-direction)->b = 25;
findLED(led)->b = ledBrightness;
} else {
// switch this led off
findLED(led)->r = 0;
findLED(led)->g = 0;
findLED(led)->b = 0;
//findLED(led-1)->b = 000;
}
}
return 0;
}
//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g - b - back to r
void Wheel (uint16_t WheelPos, byte &r, byte &g, byte &b) {
switch(WheelPos / 128) {
case 0:
r = 127 - WheelPos % 128; // red down
g = WheelPos % 128; // green up
b = 0; // blue off
break;
case 1:
g = 127 - WheelPos % 128; // green down
b = WheelPos % 128; // blue up
r = 0; // red off
break;
case 2:
b = 127 - WheelPos % 128; // blue down
r = WheelPos % 128; // red up
g = 0; // green off
break;
}
}
void backlightLEDs (void) {
for (int i = 0; i < startingLEDs; i++) {
_leds[i].g = 255;
_leds[i].r = 5;
_leds[i].b = 5;
}
}