-
Notifications
You must be signed in to change notification settings - Fork 1
/
Executor.cpp
180 lines (152 loc) · 5.12 KB
/
Executor.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
/*
Copyright (C) 2011-2012 William Brodie-Tyrrell
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "Executor.h"
Executor::Executor(LiquidCrystal &l, Keypad &k, ButtonDebounce &b, ButtonDebounce &fs, LEDDriver &led)
: disp(l), keys(k), button(b), footswitch(fs), leddriver(led)
{
current=NULL;
}
void Executor::begin()
{
execphase=0;
dd=false;
}
void Executor::setProgram(Program *p)
{
current=p;
changePhase(0);
}
void Executor::setDrydown(bool d)
{
dd=d;
changePhase(0);
}
void Executor::setSplitgrade(bool s)
{
sg=s;
changePhase(0);
}
/// specify that program is up to a particular exposure; display it
void Executor::changePhase(unsigned char ph)
{
if(NULL == current)
return;
execphase = ph;
(*current).getExposure(execphase).display(disp, dispbuf, true);
disp.setCursor(18, 2);
disp.print(sg ? "S" : " ");
disp.setCursor(19, 2);
disp.print(dd ? "D" : " ");
}
void Executor::expose()
{
if(NULL == current)
return;
// backup the duration; it will get overwritten for display purposes
Program::Exposure &expo=(*current).getExposure(execphase);
unsigned long msbackup=expo.ms;
unsigned long start=micros();
unsigned long now=start, dt=0, lastupdate=start;
// begin
leddriver.exposeOn(expo.hardpower, expo.softpower, expo.hardpower, expo.softpower);
// polling loop
bool cancelled=false, skipped=false;
while(!skipped){
now=micros();
dt=(now-start)/1000; // works with overflows
// time is elapsed
if(dt >= msbackup){
break;
}
// time for some IO? otherwise tighten the loop a bit
if((msbackup-dt) > 50){
if((now-lastupdate) > 100000){
// re-display with reduced time remaining
expo.ms=msbackup-dt;
expo.displayTime(disp, dispbuf, true);
lastupdate=now;
}
// pause!
keys.scan();
button.scan();
footswitch.scan();
bool buttonPressed = button.hadPress() || footswitch.hadPress();
if(keys.available() || buttonPressed){
if (keys.readRaw() == Keypad::KP_HASH || buttonPressed){
leddriver.allOff();
unsigned long pausestart=micros();
// cancel on anything but Expose buttons
buttonPressed = false;
do {
keys.scan();
button.scan();
footswitch.scan();
buttonPressed = button.hadPress() || footswitch.hadPress();
} while(!keys.available() && !buttonPressed);
if (buttonPressed){
start+=micros()-pausestart;
leddriver.exposeOn(expo.hardpower, expo.softpower, expo.hardpower, expo.softpower);
} else {
char ch = keys.readRaw();
switch(ch){
case Keypad::KP_HASH:
// adjust clock and resume exposing
// !! should account for enlarger warmup here
start+=micros()-pausestart;
leddriver.exposeOn(expo.hardpower, expo.softpower, expo.hardpower, expo.softpower);
break;
case Keypad::KP_B:
// halt and this exposure
skipped=true;
break;
default:
// halt and cancel
skipped=true;
cancelled=true;
}
}
}
}
}
}
// cease
leddriver.allOff();
// restore
expo.ms=msbackup;
if(cancelled){
// tell user to go home
disp.clear();
disp.print("Prog Cancelled");
delay(1000);
changePhase(0);
}
else{
nextPhase();
}
}
void Executor::nextPhase()
{
// decide on next exposure or reset to beginning
for(int newphase=execphase+1; newphase < Program::MAXEXPOSURES;++newphase){
if((*current).getExposure(newphase).ms != 0){
changePhase(newphase);
return;
}
}
disp.clear();
disp.print("Program Complete");
delay(1000);
changePhase(0);
}