-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIRIG.cpp
283 lines (272 loc) · 7.79 KB
/
IRIG.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
#include "IRIG.h"
/* __ ______________ ____________ __
/ / / /_ __/ _/ / / _/_ __/\ \/ /
/ / / / / / / // / / / / / \ /
/ /_/ / / / _/ // /____/ / / / / /
\____/ /_/ /___/_____/___/ /_/ /_/ */
void to_bcd2(uint8_t dat, uint8_t* ones, uint8_t* tens) {
uint8_t t = 0;
while(dat > 9) { t++; dat -= 10; }
*tens = t; *ones = dat;
}
void to_bcd3(uint16_t dat, uint8_t* ones, uint8_t* tens, uint8_t* hdrs) {
uint8_t t = 0, h = 0;
while(dat > 99) { h++; dat -= 100; }
while(dat > 9) { t++; dat -= 10; }
*hdrs = h; *tens = t; *ones = dat;
}
/* ________ __________ ____ _______________________ ________
/ _/ __ \/ _/ ____/ / __ \/ ____/ ____/ ____/ _/ | / / ____/
/ // /_/ // // / __ / /_/ / __/ / / / __/ / / | | / / __/
_/ // _, _// // /_/ / / _, _/ /___/ /___/ /____/ / | |/ / /___
/___/_/ |_/___/\____/ /_/ |_/_____/\____/_____/___/ |___/_____/ */
IRIG_RX::IRIG_RX(uint8_t mode) {
uint16_t time0, time1, timeI, timeE;
switch(mode) {
case IRIG_A:
time0 = 200; time1 = 500;
timeI = 800; timeE = 150;
timeout = 3000;
break;
default:
time0 = 2000; time1 = 5000;
timeI = 8000; timeE = 1500;
timeout = 30000;
break;
}
time1A = time1 - timeE; time1B = time1 + timeE;
time0A = time0 - timeE; time0B = time0 + timeE;
timeIA = timeI - timeE; timeIB = timeI + timeE;
}
void IRIG_RX::begin(int16_t pin) {
this->pin = pin;
pinMode(pin, INPUT);
}
uint8_t from_bcd(uint8_t bcd) {
return (bcd & 0xF) + 10 * (bcd >> 4);
}
void IRIG_RX::process_buf(irig_time_t* into) {
into->secs = from_bcd(recv_buf[0]);
into->mins = from_bcd(recv_buf[1]);
into->hours = from_bcd(recv_buf[2]);
into->day_of_year = from_bcd(recv_buf[3]) + 100 * (recv_buf[4] & 0b11);
into->tenths_of_secs = recv_buf[4] >> 4;
}
uint8_t IRIG_RX::recv(irig_time_t* into) { return recv(into, timeout * 10, NULL); }
uint8_t IRIG_RX::recv(irig_time_t* into, uint32_t timeout_us) { return recv(into, timeout_us, NULL); }
uint8_t IRIG_RX::recv(irig_time_t* into, uint32_t timeout_us, uint8_t (*int_func)()) {
uint64_t lt, start_time = micros();
uint8_t bit5 = 1, dat = 0, i = 1;
uint8_t pulse = 0, lpulse = 0, started = 0;
recv_buf_pos = 0;
do {
if(int_func && int_func()) return 0;
if(!started && micros() - start_time > timeout_us) break;
lt = pulseIn(pin, HIGH, timeout);
lpulse = pulse; pulse = 3;
if(lt > time0A && lt < time0B)
pulse = 0;
else if(lt > time1A && lt < time1B)
pulse = 1;
else if(lt > timeIA && lt < timeIB)
pulse = 2;
else if(lt == 0 && recv_buf_pos >= IRIG_MIN_FRAME_LEN)
goto recv;
if(started && pulse == 3) return 0;
if(pulse == 2) {
if(started) {
_DBG_PRINT("recv_buf[");
_DBG_PRINT(recv_buf_pos);
_DBG_PRINT("]: ");
_DBG_PRINTLN(dat);
recv_buf[recv_buf_pos++] = dat;
dat = 0; i = 1; bit5 = 1;
} else if(lpulse == 2)
started = 1;
} else if(i == 0b10000 && bit5) { // Fuck the 5th bit, that guy always ruins the party
bit5 = 0;
} else if(started) {
if(pulse) dat |= i;
else dat &= ~i;
i <<= 1;
}
if(recv_buf_pos >= IRIG_FRAME_LEN)
goto recv;
continue;
recv:
process_buf(into);
_DBG_PRINTLN("recv(): exit recv")
return 1;
} while(1);
return 0;
}
/*uint8_t IRIG_RX::recv(irig_time_t* into, uint32_t timeout_us) {
if(micros() - last_pulse < timeout)
return 0;
unsigned long lt;
lt = pulseIn(pin, HIGH, timeout_us);
if(lt < timeIA || lt > timeIB)
return 0;
lt = pulseIn(pin, HIGH, timeout);
if(lt > timeIA && lt < timeIB) {
_DBG_PRINTLN("recv(): init pulse");
uint8_t bit5 = 1, pulse = 0, dat = 0, i = 1;
recv_buf_pos = 0;
while(1) {
lt = pulseIn(pin, HIGH, timeout);
if(lt > time0A && lt < time0B)
pulse = 0;
else if(lt > time1A && lt < time1B)
pulse = 1;
else if(lt > timeIA && lt < timeIB)
pulse = 2;
else if(lt == 0 || recv_buf_pos >= 6) {
process_buf(into);
return 1;
}
if(pulse == 2){
_DBG_PRINT("recv_buf[");
_DBG_PRINT(recv_buf_pos);
_DBG_PRINT("]: ");
_DBG_PRINTLN(dat);
recv_buf[recv_buf_pos++] = dat;
dat = 0; i = 1; bit5 = 1;
} else if(i == 0b10000 && bit5) { // Fuck the 5th bit, that guy always ruins the party
bit5 = 0;
} else {
if(pulse) dat |= i;
else dat &= ~i;
i <<= 1;
}
}
last_pulse = micros();
}
return 0;
}*/
/* ________ __________ __________ ___ _ _______ __ _____________
/ _/ __ \/ _/ ____/ /_ __/ __ \/ | / | / / ___// |/ / _/_ __/
/ // /_/ // // / __ / / / /_/ / /| | / |/ /\__ \/ /|_/ // / / /
_/ // _, _// // /_/ / / / / _, _/ ___ |/ /| /___/ / / / // / / /
/___/_/ |_/___/\____/ /_/ /_/ |_/_/ |_/_/ |_//____/_/ /_/___/ /_/ */
IRIG_TX::IRIG_TX(uint8_t mode) {
uint16_t mul = 1000;
switch(mode) {
case IRIG_A: mul = 100; break;
}
time0A = 2 * mul; time0B = 8 * mul;
time1A = 5 * mul; time1B = 5 * mul;
timeIA = 8 * mul; timeIB = 2 * mul;
ftimeMS = 1 * mul;
}
uint16_t IRIG_TX::getSendRate() {
return ftimeMS;
}
void IRIG_TX::begin(int16_t pin) {
this->pin = pin;
pinMode(pin, OUTPUT);
}
//TODO: Improve speed of function, ie. calculate send buffer, and send it
void IRIG_TX::send(irig_time_t tc) {
tc.fixup();
_DBG_PRINT("Sending: ");
// START FRAME
send_pos_ind();
send_pos_ind();
// SECONDS FRAME
to_bcd2(tc.secs, &bcd1, &bcd10);
send_nibble(bcd1);
send_nibble(bcd10 << 1);
send_pos_ind();
// MINUTES FRAME
send_bcd9(tc.mins);
// HOURS FRAME
send_bcd9(tc.hours);
// DOY FRAME
to_bcd3(tc.day_of_year, &bcd1, &bcd10, &bcd100);
send_nibble(bcd1);
send_bit(0);
send_nibble(bcd10);
send_pos_ind();
send_nibble(bcd100);
send_bit(0);
// TOS FRAME
send_nibble(tc.tenths_of_secs);
send_pos_ind();
_DBG_PRINTLN();
}
void IRIG_TX::send_bcd9(uint8_t num) {
to_bcd2(num, &bcd1, &bcd10);
send_nibble(bcd1);
send_bit(0);
send_nibble(bcd10);
send_pos_ind();
}
void IRIG_TX::send_nibble(uint8_t nibb) {
send_bit(nibb & 1);
send_bit(nibb & 2);
send_bit(nibb & 4);
send_bit(nibb & 8);
}
inline void IRIG_TX::send_pos_ind() {
_DBG_PRINT('P');
pulse(timeIA, timeIB);
}
inline void IRIG_TX::send_bit(uint8_t bit) {
_DBG_PRINT(bit ? '1' : '0');
if(bit) pulse(time1A, time1B);
else pulse(time0A, time0B);
}
inline void IRIG_TX::pulse(uint16_t a, uint16_t b) {
digitalWrite(pin, 1);
delayMicroseconds(a);
digitalWrite(pin, 0);
delayMicroseconds(b);
}
/* ________ __________ ____________ ___________________ ____ ______
/ _/ __ \/ _/ ____/ /_ __/ _/ |/ / ____/ ____/ __ \/ __ \/ ____/
/ // /_/ // // / __ / / / // /|_/ / __/ / / / / / / / / / __/
_/ // _, _// // /_/ / / / _/ // / / / /___/ /___/ /_/ / /_/ / /___
/___/_/ |_/___/\____/ /_/ /___/_/ /_/_____/\____/\____/_____/_____/ */
void irig_time_t::fixup() {
while(tenths_of_secs > 9) { tenths_of_secs -= 10; secs++; }
while(secs > 59) { secs -= 60; mins++; }
while(mins > 59) { mins -= 60; hours++; }
while(hours > 23) { hours -= 24; day_of_year++; }
while(day_of_year > 364) { day_of_year -= 365; }
}
uint16_t irig_time_t::to_strn(char* str, uint16_t n) {
return snprintf(str, n, "%d:%d:%d.%d day %d", hours, mins, secs, tenths_of_secs, day_of_year);
}
void irig_time_t::uptime() {
uint64_t ms = millis();
day_of_year = ms / MS_PER_DAY;
ms -= day_of_year * MS_PER_DAY;
hours = ms / MS_PER_HOUR;
ms -= hours * MS_PER_HOUR;
mins = ms / MS_PER_MINUTE;
ms -= mins * MS_PER_MINUTE;
secs = ms / 1000;
ms -= secs * 1000;
tenths_of_secs = ms / 100;
}
void irig_time_t::add_ms(uint32_t tdiff_ms) {
uint8_t amo = tdiff_ms % 100;
tenths_of_secs += amo / 10; tdiff_ms -= amo;
add_s(tdiff_ms / 1000);
}
void irig_time_t::add_s(uint32_t tdiff) {
secs += tdiff % 60;
add_m(tdiff / 60);
}
void irig_time_t::add_m(uint16_t tdiff) {
mins += tdiff % 60;
add_h(tdiff / 60);
}
void irig_time_t::add_h(uint16_t tdiff) {
hours += tdiff % 24;
add_d(tdiff / 24);
}
void irig_time_t::add_d(uint8_t tdiff) {
day_of_year += tdiff;
fixup();
}