-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cpp
493 lines (424 loc) · 9.37 KB
/
Program.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Project: UV PCB Exposure Box
* URL: https://lytrax.io/blog/projects/diy-uv-exposure-box
* Youtube Video: https://www.youtube.com/watch?v=ZAlxNNI-BVM
* Author: Christos Lytras <christos.lytras@gmail.com>
* Description: I'm using an older scanner as a box to create a UV PCB exposure box. I'll go through all steps of the hardware and software design emphasizing more to software logic.
* Version: 1.1
*
* 1.1
* - Change EEPROM write, to write only after 5 seconds the encoder has stopped and when the program goes at running mode
*
* */
#include "Program.h"
Program *Program::self;
Program::Program() :
display(DISPLAY_PIN_CLK, DISPLAY_PIN_DIO),
encoder(ENCODER_PIN_A, ENCODER_PIN_B),
m_mode(ModeSetD4),
m_colonTimer(0),
m_beepBeepsCount(0),
m_colon(true),
m_eepromWriteTimer(0)
{
Program::self = this;
}
void Program::initialize()
{
pinMode(ENCODER_SWITCH_PIN, INPUT);
digitalWrite(ENCODER_SWITCH_PIN, HIGH);
pinMode(STRIP_MOSFET_PIN, OUTPUT);
m_ledUVPower = false;
m_startBtn.onButtonPress(&this->onButtonStartPush);
m_startBtn.onDoEvents(&this->run_btnDoEvents, (void*)this);
m_startBtn.init(START_SWITCH_PIN, HIGH);
m_stopBtn.onButtonPress(&this->onButtonStopPush);
m_stopBtn.onDoEvents(&this->run_btnDoEvents, (void*)this);
m_stopBtn.init(STOP_SWITCH_PIN, HIGH);
readEEPROMData();
display.setBrightness(LIGHT_BRIGHTEST);
display.showNumberInt(m_secsCountdown);
encoder.setDataArg(this);
encoder.enable();
encoder.onLeft(&this->onEncoderLeft);
encoder.onRight(&this->onEncoderRight);
}
void Program::run(bool encoderSwitchHandler /*= true*/)
{
encoder.update();
m_startBtn.update(true);
m_stopBtn.update(true);
playMode();
runToneHandler();
if(encoderSwitchHandler)
runEncoderSwitchHandler();
}
void Program::run_btnDoEvents(Program* pProgram) {
pProgram->encoder.update();
pProgram->runToneHandler();
}
void Program::onButtonStartPush(CL_Button *button) {
Program::self->doModeStart();
}
void Program::onButtonStopPush(CL_Button *button) {
Program::self->doModeStop();
}
void Program::setMode(Modes mode)
{
m_mode = mode;
}
void Program::nextSetMode()
{
switch(m_mode)
{
case ModeSetD1:
setMode(ModeSetD4);
break;
case ModeSetD2:
setMode(ModeSetD1);
break;
case ModeSetD3:
setMode(ModeSetD2);
break;
case ModeSetD4:
setMode(ModeSetD3);
break;
}
}
void Program::playMode()
{
switch(m_mode)
{
case ModeSetD1:
case ModeSetD2:
case ModeSetD3:
case ModeSetD4: // Default
runSetMode();
break;
case ModeRunning:
runRunningMode();
break;
case ModePaused:
runPausedMode();
break;
case ModeFinished:
runFinishedMode();
break;
}
}
void Program::runSetMode(bool forceUpdate /*= false*/)
{
char digits[5];
char padDigits[5];
itoa(m_secsCountdown, digits, 10);
size_t digitsLen = strlen(digits);
memset(padDigits, ' ', sizeof(padDigits));
padDigits[4 - digitsLen] = 0;
strcat(padDigits, digits);
for(int i = 4; i >= m_mode; i--) {
if(padDigits[i] == ' ')
padDigits[i] = '0';
}
uint32_t currentMillis = millis();
if(currentMillis - m_setTimeBlinkTimer >= 499)
{
m_setTimeBlinkState = !m_setTimeBlinkState;
m_setTimeBlinkTimer = millis();
}
if(m_mode != ModeSetD4 && !m_setTimeBlinkState)
padDigits[m_mode] = 0x20;
if(forceUpdate || strcmp(m_currentDigits, padDigits) != 0)
{
display.showString(padDigits);
strcpy(m_currentDigits, padDigits);
}
if(m_eepromWriteTimer != 0 && (currentMillis - m_eepromWriteTimer) >= (EEPROM_WRITE_AFTER_ENCODER_SECONDS * 1000))
{
writeEEPROMData();
m_eepromWriteTimer = 0;
}
}
void Program::doModeSet()
{
if(m_mode != ModeRunning)
{
stopBeep();
m_mode = ModeSetD4;
runSetMode(true);
}
}
void Program::doModeStart()
{
if(m_mode == ModeRunning)
doModePause();
else if(m_mode == ModePaused)
doModeResume();
else if(m_mode == ModeFinished)
doModeSet();
else
{
stopBeep();
m_setTimeBlinkState = true;
m_setTimeBlinkTimer = millis();
m_mode = ModeSetD4;
runSetMode(true);
m_mode = ModeRunning;
m_countdownSecs = m_secsCountdown;
m_countdownTimer = millis();
ledUVPower(true);
writeEEPROMData();
}
}
void Program::doModeStop()
{
if(m_mode == ModeRunning)
{
m_mode = ModeFinished;
ledUVPower(false);
}
else if(m_mode == ModePaused || m_mode == ModeFinished)
{
doModeSet();
}
}
void Program::doModePause()
{
if(m_mode == ModeRunning)
{
m_mode = ModePaused;
ledUVPower(false);
}
}
void Program::doModeResume()
{
if(m_mode == ModePaused)
{
runPausedMode(true); // Set display to on immediately; Eliminates on/off delay
m_mode = ModeRunning;
m_countdownTimer = millis();
ledUVPower(true);
}
}
void Program::doModeFinish()
{
if(m_mode == ModeRunning)
{
m_mode = ModeFinished;
ledUVPower(false);
}
}
void Program::ledUVPower(bool bSet /*= true*/)
{
m_ledUVPower = bSet;
digitalWrite(STRIP_MOSFET_PIN, m_ledUVPower ? HIGH : LOW);
}
void Program::ledUVToggle()
{
ledUVPower(!m_ledUVPower);
}
void Program::runFinishedMode()
{
static bool displayState = false;
static uint32_t finishedTimer = millis() + 500;
static uint32_t finishedBeepTimer = millis() + 7000;
if(millis() - finishedTimer > 499) {
displayState = !displayState;
if(displayState)
display.showString("----");
else
display.showString("");
finishedTimer = millis();
}
if(millis() - finishedBeepTimer > 6999)
{
finishedBeep();
finishedBeepTimer = millis();
}
}
void Program::runRunningMode()
{
if(millis() - m_countdownTimer > 999)
{
m_countdownSecs--;
if(m_countdownSecs <= 0)
{
doModeFinish();
}
else
{
display.showNumberInt(m_countdownSecs);
}
m_countdownTimer = millis();
}
}
void Program::runPausedMode(bool forcePausedState /*= false*/)
{
static uint32_t pausedTimer = millis() + 1200;
static bool pausedState = false;
if(forcePausedState || millis() - pausedTimer > (pausedState ? 1199 : 499))
{
pausedState = !forcePausedState ? !pausedState : true;
if(pausedState)
display.showNumberInt(m_countdownSecs);
else
display.showString("");
pausedTimer = millis();
}
}
void Program::writeEEPROMData()
{
int addr = EEPROM_START_ADDRESS;
EEPROM.put(addr, m_secsCountdown);
}
void Program::readEEPROMData()
{
int addr = EEPROM_START_ADDRESS;
EEPROM.get(addr, m_secsCountdown);
if(m_secsCountdown < 0)
m_secsCountdown = 1;
else if(m_secsCountdown > 9999)
m_secsCountdown = 9999;
}
void Program::beep()
{
this->tone(BEEP_FREQUENCY, BEEP_DURATION);
}
void Program::hourChangeBeep()
{
this->tone(BEEP_FREQUENCY, BEEP_DURATION, 2);
}
void Program::finishedBeep()
{
this->tone(3500, 500, 2, 500);
}
void Program::tone(uint16_t frequency,
uint32_t duration,
uint8_t beepsCount /*= 1*/,
uint32_t pauseDuration /*= 0*/ /* 0 = duration */)
{
m_beepFrequency = frequency;
m_beepDuration = duration;
m_beepBeepsCount = beepsCount;
m_beepPauseDuration = pauseDuration == 0 ? duration : pauseDuration;
m_beepActive = false;
m_beepPauseCycle = millis() - m_beepPauseDuration;
}
void Program::runToneHandler()
{
if(m_beepBeepsCount > 0)
{
if(!m_beepActive)
{
if(millis() - m_beepPauseDuration >= m_beepPauseCycle)
{
toneAC(m_beepFrequency);
m_beepCycle = millis();
m_beepActive = true;
}
}
else
{
if(millis() - m_beepCycle >= m_beepDuration)
{
noToneAC();
m_beepPauseCycle = millis();
m_beepActive = false;
m_beepBeepsCount--;
}
}
}
}
void Program::stopBeep()
{
m_beepBeepsCount = 0;
noToneAC();
}
void Program::runEncoderSwitchHandler()
{
unsigned long decouplingTimer = millis() + 50;
if((millis() - decouplingTimer) < 50)
return;
if (digitalRead(ENCODER_SWITCH_PIN) == LOW)
{
//unsigned long timeBegan = millis();
bool modeChanged = false;
while(digitalRead(ENCODER_SWITCH_PIN) == LOW)
{
run(false);
/*if(millis() - timeBegan > ENCODER_MODE_CHANGE_INTERVAL)
{
beep();
nextMode();
timeBegan = millis();
modeChanged = true;
}*/
}
decouplingTimer = millis();
if(!modeChanged && digitalRead(ENCODER_SWITCH_PIN) == HIGH)
onEncoderSwitchPush();
}
}
void Program::onEncoderSwitchPush()
{
if(m_mode == ModeRunning)
doModePause();
else if(m_mode == ModePaused)
doModeResume();
else if(m_mode == ModeFinished)
doModeStop();
else
{
nextSetMode();
beep();
}
}
void Program::onEncoderSwitchLongPush()
{
}
void Program::onEncoderChange(int steps)
{
if(m_mode == ModeRunning)
return;
if(m_mode == ModeFinished)
{
m_mode = ModeSetD4;
return;
}
m_setTimeBlinkState = true;
m_setTimeBlinkTimer = millis();
m_eepromWriteTimer = millis();
runSetMode();
bool leadingZeros = true;
int16_t secsCountdown = m_secsCountdown;
uint8_t numbers[4];
int i;
for(i = 0; i < 4; i++)
{
if(m_currentDigits[i] > '0' && m_currentDigits[i] <= '9') {
numbers[i] = m_currentDigits[i] - 0x30;
if(i < (m_mode - 0))
leadingZeros = false;
} else
numbers[i] = 0;
}
if(leadingZeros && steps <= 0 && numbers[m_mode] == 0) {
if(steps != 0 && m_mode < 3) {
m_mode = (Modes)m_mode + 1;
}
return;
}
int power = ceil(pow(10, 4 - m_mode - 1));
m_secsCountdown += steps * power;
if(m_secsCountdown < 1)
m_secsCountdown = 1;
else if(m_secsCountdown > 9999)
m_secsCountdown = secsCountdown;
}
void Program::onEncoderLeft(CL_RotaryEncoder *enc, Program *prog)
{
Program::self->onEncoderChange(-enc->getSteps());
}
void Program::onEncoderRight(CL_RotaryEncoder *enc, Program *prog)
{
Program::self->onEncoderChange(enc->getSteps());
}