-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathb_TimeAlarms.ino
119 lines (102 loc) · 3.51 KB
/
b_TimeAlarms.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
// Time alarm video/photo driven by MewPro
#ifdef USE_TIME_ALARMS
#define delay(a) Alarm.delay(a)
boolean alarmEntered = false; // in order to avoid timeAdjust() or alarmPowerOn() functions called twice
const int syncDelay = 600; // more than 8 minute. workaround for time drift on Arduino
const int powerOnDelay = 60; // delay in second after camera power on
const int powerOffDelay = 5; // delay in second before camera power off
// Since the time measured by Arduino's Time library gains or loses maximum 20 seconds per hour, we need to sync the time with GoPro's RTC at least once a day.
void timeAdjust()
{
if (!alarmEntered) {
alarmEntered = true;
queueIn(F("@"));
}
}
void alarmFlagClear()
{
alarmEntered = false;
}
// Alarm tasks
void alarmPowerOn()
{
queueIn(F("@"));
Alarm.timerOnce(powerOnDelay, alarmStartRecording);
}
void alarmStartRecording()
{
startRecording();
/***********************
Example alarm: Shooting duration in seconds
*** EDIT NEXT LINE ****/
Alarm.timerOnce(5 * 60, alarmStopRecording); // 5 minutes
/***********************/
}
void alarmStopRecording()
{
stopRecording();
Alarm.timerOnce(5, alarmSuspend); // power off at powerOffDelay seconds after stop
}
void alarmSuspend()
{
queueIn(F("PW0"));
// alarmEntered == true for a long enough time > 8 minute (> 20 * 24 seconds).
if (alarmEntered) {
Alarm.timerOnce(syncDelay, alarmFlagClear);
}
}
void _setTime() // This function is called after every time when the camera power on
{
TimeElements tm;
tm.Hour = td[TD_DATE_TIME_hour]; tm.Minute = td[TD_DATE_TIME_minute]; tm.Second = td[TD_DATE_TIME_second];
tm.Day = td[TD_DATE_TIME_day]; tm.Month = td[TD_DATE_TIME_month]; tm.Year = y2kYearToTm(td[TD_DATE_TIME_year]);
setTime(makeTime(tm));
for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
// delete all alarms
if (Alarm.isAlarm(id)) {
Alarm.free(id);
}
}
/***********************
Example alarm: Shooting video from 09:00
*** EDIT NEXT LINE ****/
tm.Hour = 9; tm.Minute = 0; tm.Second = 0; // 09:00:00 Recording start time
/***********************/
time_t t = makeTime(tm), s;
// Arduino's time is adjusted whenever GoPro power on.
// Sync the time once a day. Take care the time before power-on has drifted a maximum of 8 minute!
s = t - 2 * syncDelay - 3 * powerOnDelay;
Alarm.alarmRepeat(hour(s), minute(s), second(s), timeAdjust);
//
// just after sync with GoPro, the time is accurate enough. let's power on again and shoot video.
t -= powerOnDelay; // powerOnDelay seconds before recording start
/***********************
*** EDIT NEXT AND CHOOSE EITHER ALARM 1 or 2 or 3 ****/
/***********************
Example alarm 1: Shooting video everyday */
Alarm.alarmRepeat(hour(t), minute(t), second(t), alarmPowerOn);
/***********************
Example alarm 2: Shooting video weekly on Tuesday */
// Alarm.alarmRepeat(dowTuesday, hour(t), minute(t), second(t), alarmPowerOn);
/***********************
Example alarm 3: Shooting video on Monday and Wednesday and Friday */
// Alarm.alarmRepeat(dowMonday, hour(t), minute(t), second(t), alarmPowerOn);
// Alarm.alarmRepeat(dowWednesday, hour(t), minute(t), second(t), alarmPowerOn);
// Alarm.alarmRepeat(dowFriday, hour(t), minute(t), second(t), alarmPowerOn);
/***********************/
if (alarmEntered) {
Alarm.timerOnce(powerOnDelay, alarmSuspend);
}
}
void checkTimeAlarms()
{
delay(0); // Don't delete. Alarms are serviced from here.
}
#else
void _setTime()
{
}
void checkTimeAlarms()
{
}
#endif