forked from maniacbug/TheCameraMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtcEvTable.cpp
214 lines (172 loc) · 5.45 KB
/
RtcEvTable.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
/*
Copyright (C) 2012 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
// STL includes
// C includes
// Framework includes
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
// Library includes
#include <AnyRtc.h>
#include <Updater.h>
// Project includes
#include "RtcEvTable.h"
#include "objects.h" // for logger
#if 1
void* operator new(size_t, void* __p)
{
return __p;
}
#endif
/****************************************************************************/
uint32_t eventtime(const RtcEvTable::evline& prog_event)
{
RtcEvTable::evline event;
memcpy_P(&event,&prog_event,sizeof(event));
return DateTime(event[0],event[1],event[2],event[3],event[4],event[5]).unixtime();
}
/****************************************************************************/
void RtcEvTable::begin(Updater& up)
{
up.add(this);
begin();
}
/****************************************************************************/
bool RtcEvTable::is_time_now(void) const
{
return is_valid() && RTC.now() >= whenNext();
}
/****************************************************************************/
void RtcEvTable::update(void)
{
// We actually want to fire if now is AT or after 'when'
if ( is_time_now() )
{
channel(current_channel())->emit(current_signal());
current++;
}
}
/****************************************************************************/
RtcEvTable::ev_t RtcEvTable::current_channel(void) const
{
ev_t result;
memcpy_P(&result,*current+6,sizeof(result));
return result;
}
/****************************************************************************/
RtcEvTable::ev_t RtcEvTable::current_signal(void) const
{
ev_t result;
memcpy_P(&result,*current+7,sizeof(result));
return result;
}
/****************************************************************************/
RtcEvTable::RtcEvTable(Connector& _conn,const evline* _table,uint8_t _num_lines,
uint8_t _num_channels):
table(_table), current(_table), num_lines(_num_lines), num_channels(_num_channels)
{
// Placement new called over an array. Very nasty. But C++ affords no
// other way to call constructors on array members.
channels = reinterpret_cast<Channel*>( malloc( num_channels * sizeof(Connectable) ) );
int i = num_channels;
while (i--)
new (channels + i) Channel(_conn);
}
/****************************************************************************/
RtcEvTable::~RtcEvTable()
{
free(channels);
}
/****************************************************************************/
void RtcEvTable::begin(void)
{
char buf[32];
// First, print all out for debugging
printf_P(PSTR("REVT %u events\n\r"),num_lines);
while ( is_valid() )
{
printf_P(PSTR("REVT %s on %u %S\n\r"),DateTime(whenNext()).toString(buf,sizeof(buf)),current_channel(),logger.find_symbol(current_signal()));
current++;
}
// Reset to the top
current = table;
// seek the current pointer to the right place
while ( is_time_now() )
current++;
if ( is_valid() )
{
int signal = current_signal();
printf_P(PSTR("REVT next event is %s on %u %S\n\r"),DateTime(whenNext()).toString(buf,sizeof(buf)),current_channel(),logger.find_symbol(signal));
}
else
printf_P(PSTR("REVT No more events remain.\n\r"));
}
/****************************************************************************/
uint32_t RtcEvTable::whenNext(void) const
{
if ( is_valid() )
return eventtime(*current);
else
return -1;
}
/****************************************************************************/
void RtcEvTable::invalidate(void)
{
current = table + num_lines;
}
/****************************************************************************/
bool RtcEvTable::is_valid(void) const
{
return (current < table + num_lines);
}
/****************************************************************************/
RtcEvTable::Channel* RtcEvTable::channel(uint8_t _channel)
{
if ( _channel < num_channels )
return channels + _channel;
else
return NULL;
}
/****************************************************************************/
void SignalEvTable::onNotify(const Connectable* ,uint8_t signal )
{
if ( signal == signal_start )
{
// Started a second ago, so the first event fires immediately
started_at = RTC.now() - 1;
reset();
}
}
/****************************************************************************/
SignalEvTable::SignalEvTable(Connector& _conn,uint8_t _signal_start,const evline* events,uint8_t num_lines, uint8_t num_channels): RtcEvTable(_conn,events,num_lines,num_channels), Connectable(_conn), signal_start(_signal_start)
{
// The initialized state of a signal ev table is the INVALID state
invalidate();
}
/****************************************************************************/
void SignalEvTable::begin(Updater& up,Connectable* _who)
{
RtcEvTable::begin(up);
listen(_who);
}
/****************************************************************************/
void SignalEvTable::listen(Connectable* _who)
{
Connectable::listen(_who,signal_start);
}
/****************************************************************************/
uint32_t SignalEvTable::whenNext(void) const
{
if ( is_valid() )
return RtcEvTable::whenNext() + started_at;
else
return -1;
}
/****************************************************************************/
// vim:cin:ai:sts=2 sw=2 ft=cpp