-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartButtonMonitor.cpp
More file actions
157 lines (121 loc) · 4.87 KB
/
StartButtonMonitor.cpp
File metadata and controls
157 lines (121 loc) · 4.87 KB
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
#include "StartButtonMonitor.h"
#define POLL_DELAY_MS 33
#include "AttinySoftwareSerial.h" //for debug, hard coded to write serial to PB1 @ 9600 baud
// extern AttinySoftwareSerial mySerial;//debug!
// Public Functions
// ============================================================================
StartButtonMonitor::StartButtonMonitor(TaskScheduler& scheduler, LedFuse& fuse, PhotoCountSelectMonitor& photoCountMonitor, ColorSelectMonitor& colorMonitor,uint8_t monitorPin, uint8_t triggerPin):
m_scheduler(scheduler),
m_currentScheduledTask(NULL),
m_ledFuse(fuse),
m_photoCountSelector(photoCountMonitor),
m_colorSelector(colorMonitor),
m_burnTimeMs(5000), //default the burn time to 5 seconds
m_ledColor(0xFF), //default to blue
m_photosToTake(1), //take 1 photo by default
m_photosTaken(0),
m_pinToMonitor(monitorPin),
m_triggerPin(triggerPin)
{
//start enabled by default.
enable();
}//end constructor
StartButtonMonitor::~StartButtonMonitor()
{
if(m_currentScheduledTask)
{
m_scheduler.removeScheduledTask(m_currentScheduledTask);
}
}//end destructor
//Enable (if disabled) monitoring of the digital pin
void StartButtonMonitor::enable()
{
if(m_currentScheduledTask == 0)
{
m_currentScheduledTask = m_scheduler.scheduleDelayedTask(checkDigitalPin0, this, POLL_DELAY_MS);
}
}//end enable
void StartButtonMonitor::setCountDownTime(uint16_t newBurnTimeMs)
{
m_burnTimeMs = newBurnTimeMs;
}//end setCountDownTime
//Disable monitoring of the digital pin.
void StartButtonMonitor::disable()
{
if(m_currentScheduledTask)
{
m_scheduler.removeScheduledTask(m_currentScheduledTask);
m_currentScheduledTask = 0;
}
}//end disable
void StartButtonMonitor::checkDigitalPin0(void* clientData)
{
((StartButtonMonitor*)clientData)->checkDigitalPin();
}//end checkDigitalPin0
void StartButtonMonitor::triggerCamera0(void* clientData)
{
((StartButtonMonitor*)clientData)->triggerCamera();
}//end triggerCamera0
void StartButtonMonitor::reactivate0(void* clientData)
{
((StartButtonMonitor*)clientData)->reactivate();
}//end reactivate0
// Private functions
// ============================================================================
void StartButtonMonitor::checkDigitalPin()
{
//Check if the button is being pressed
if(digitalRead(m_pinToMonitor) == LOW)
{
//The button is pressed!
//Tell the photoCountSelector and the colorSelectMonitor to stop monitoring their pins
m_photoCountSelector.disable();
m_colorSelector.disable();
m_ledColor = m_colorSelector.getColor();
m_photosToTake = m_photoCountSelector.getCount();
m_photosTaken = 0; //reset how many shots we've taken
// mySerial.print("m_photosToTake = ");
// mySerial.print(m_photosToTake);
// mySerial.print(", m_ledColor = ");
// mySerial.print(m_ledColor);
m_ledFuse.ignite(m_burnTimeMs, m_ledColor);
//Schedule the camera to be triggered a little bit after the countdown animation ends.
m_currentScheduledTask = m_scheduler.scheduleDelayedTask(triggerCamera0, this, m_burnTimeMs + 10);
}
else
{
//The button is not being pressed
//Set up a delayed task so that we will check again in a little bit.
m_currentScheduledTask = m_scheduler.scheduleDelayedTask(checkDigitalPin0, this, POLL_DELAY_MS);
}
}//end checkDigitalPin
void StartButtonMonitor::triggerCamera()
{
digitalWrite(m_triggerPin, HIGH);
const uint16_t TRIGGER_HIGH_TIME_MS = 2000; //Length of trigger pulse
// We assume 2 seconds is long enough for all cameras to auto-focus and take the picture.
// If the pin goes low before auto-focus is finished, some cameras will abort taking the picture.
m_currentScheduledTask = m_scheduler.scheduleDelayedTask(reactivate0, this, TRIGGER_HIGH_TIME_MS);
}//end triggerCamera
void StartButtonMonitor::reactivate()
{
digitalWrite(m_triggerPin, LOW);
m_photosTaken++;
// Do we have more photos to take?
if(m_photosTaken < m_photosToTake)
{
//Take one more photo.
//Start the led count down again.
m_ledFuse.ignite(m_burnTimeMs, m_ledColor);
//schedule ourselves to be reactivated after the burnTime + coolDownDelay
m_currentScheduledTask = m_scheduler.scheduleDelayedTask(triggerCamera0, this, m_burnTimeMs + 10);
}
else
{
//We are done taking all the pictures, reactive monitoring of the start button
m_currentScheduledTask = 0; //Needed for our enable function to work.
enable();
m_photoCountSelector.enable();
m_colorSelector.enable();
}
}//end reactivate