forked from GadgetNutt/AVC-LAN-Module-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavclan.ino
360 lines (315 loc) · 10.2 KB
/
avclan.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Name: avclan.ino
Created: 20-12-04 8:46:15 PM
Author: Greg Nutt
*/
#include "src/config.h"
#include "src/avclan-drv.h"
#include "src/avclan-serial.h"
#include "src/avclan-registers.h"
#if defined AVC_MASTER
#include "src/avclan-master.h"
#endif
#ifdef AVC_DEVICE
#include "src/avclan-device.h"
#include "src/avclan-messages.h"
#endif
#include "src/avclan-router.h"
//#define local_debug
uint8_t readSeq = 0;
uint8_t s_len = 0;
uint8_t s_dig = 0;
uint8_t s_c[2];
uint8_t data_tmp[32];
uint8_t i;
// Define Functions below here or use other .ino or cpp files
bool time_started;
unsigned long StartTime;
unsigned long CurrentTime;
bool message_timeout;
unsigned long last_message_time;
//unsigned long CurrentTime2;
void(*resetFunc) (void) = 0;//declare reset function at address 0
void CheckForMessage(int WaitPeriod) {
StartTime = millis();
while (CurrentTime - StartTime < WaitPeriod) {
CurrentTime = millis();
CheckForMessage();
}
}
void CheckForMessage() {
avclan_frame_t msg_frame;
if (INPUT_IS_SET) {
RX_LED_ON;
uint8_t res = avclan.readMessage(&msg_frame);
RX_LED_OFF;
if (!res) {
last_message_time = millis();
switch (msg_frame.slave) {
#ifndef AVC_SNIFFER
case ADDR_ME: // Define global_master in config.h
#ifdef local_debug
avcSerial.println("Received message for ADDR_ME");
#endif
#ifdef AVC_MASTER
master.processMessage(&msg_frame);
#endif
#ifdef AVC_DEVICE
device.processMessage(&msg_frame);
#endif
break;
#endif
/* If broadcast messages*/
case ADDR_BROADCAST_1FF:
case ADDR_BROADCAST_FFF:
#ifdef local_debug
avcSerial.println("Received broadcast message");
#endif
#ifdef AVC_MASTER
master.processMessage(&msg_frame);
if (msg_frame.master != ADDR_ME) {
}
#endif
#ifdef AVC_DEVICE
device.processMessage(&msg_frame);
if (msg_frame.master != ADDR_ME) {
}
#endif
break;
}
}
} else {
CurrentTime = millis();
if (CurrentTime - last_message_time > 120000) {
#ifdef local_debug
avcSerial.println();
avcSerial.println("No communication received in 2 mins. Reset");
avcSerial.println();
delay(100);
#endif
resetFunc(); // Didn't receive any communications for 2 minutes! Restart AVCLAN
message_timeout = true;
}
}
}
#ifdef AVC_MASTER
void PingTimer() {
// Basic timer to send heart beat once every minute
if (!time_started) {
StartTime = millis();
time_started = true;
}
else {
CurrentTime = millis();
if (CurrentTime - StartTime > 60000) {
master.ping(); // Time's up! Send ping!
time_started = false;
}
}
}
#endif
void avclan_startup() {
// AVC-LAN Start up
#ifdef AVC_MASTER
delay(3000);
master.lan_start(0x00); // Master inviting devices to advertise their logical ID's
CheckForMessage(1000);
master.lan_start(0x00); // Master inviting devices to advertise their logical ID's
CheckForMessage(DEFAULT_MSG_DELAY);
master.lan_start(0x01); // Master inviting devices to advertise their logical ID's
CheckForMessage(20);
if (master.msg10_count > 0) {
#ifdef local_debug
avcSerial.println("cmd10s received, sending cmd01");
#endif
master.lan_start(0x01);
}else{
avcSerial.println("AVCLAN Startup Failed!");
resetFunc();
}
#endif
//master.ping_address(ADDR_BROADCAST_FFF);
//CheckForMessage(DEFAULT_MSG_DELAY);
#ifdef AVC_MASTER
master.lan_start(0x58);
CheckForMessage(DEFAULT_MSG_DELAY);
// What is this? It is seems to be sent with 'b 190 FFF 09 A401DB000000000000' following it.
master.lan_start(0x46);
CheckForMessage(DEFAULT_MSG_DELAY);
#endif
}
void setup() {
avcSerial.begin( 115200 );
avcSerial.println( "AVC-Lan Start" );
#if defined AVC_MASTER
avcSerial.println( "Mode: MASTER");
#elif defined AVC_DEVICE
avcSerial.println( "Mode: DEVICE");
#elif defined AVC_SNIFFER
avcSerial.println( "Mode: SNIFFER");
#else
avcSerial.println( "Mode: unknown...");
avcSerial.println( "Error: mode not set. Check configuration." );
exit( 0 );
#endif
#if ( CRYSTAL == 3 )
avcSerial.print( "Crystal spec: ");
avcSerial.println( "16MHz");
#elif ( CRYSTAL == 1 )
avcSerial.print( "Crystal spec: ");
avcSerial.println( "8MHz");
#else
avcSerial.println( "Error: crystal specification not set. Check configuration." );
exit( 0 );
#endif
avcSerial.print( "DATAOUT / TX- : ");
avcSerial.printDec( DATAOUT );
avcSerial.println();
avcSerial.print( "DATAIN / TX+ : ");
avcSerial.printDec( DATAIN );
avcSerial.println();
avcSerial.print( "Arduino address : ");
avcSerial.printHex16( ADDR_ME );
avcSerial.println();
avcSerial.print( "AVC_NORMAL_BIT_LENGTH: " ); avcSerial.printDec( AVC_NORMAL_BIT_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_BIT_1_HOLD_ON_LENGTH: " ); avcSerial.printDec( AVC_BIT_1_HOLD_ON_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_BIT_0_HOLD_ON_LENGTH: " ); avcSerial.printDec( AVC_BIT_0_HOLD_ON_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_BIT_0_HOLD_ON_MIN_LENGTH: " ); avcSerial.printDec( AVC_BIT_0_HOLD_ON_MIN_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_START_BIT_HOLD_ON_LENGTH: " ); avcSerial.printDec( AVC_START_BIT_HOLD_ON_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_START_BIT_HOLD_ON_MIN_LENGTH: " );avcSerial.printDec( AVC_START_BIT_HOLD_ON_MIN_LENGTH );
avcSerial.println();
avcSerial.print( "AVC_1U_LENGTH: " ); avcSerial.printDec( AVC_1U_LENGTH );
avcSerial.println();
delay(3000);
// Setup LED
sbi(TX_LED_DDR, TX_LED_OUT);
cbi(TX_LED_PORT, TX_LED_OUT);
sbi(RX_LED_DDR, RX_LED_OUT);
cbi(RX_LED_PORT, RX_LED_OUT);
ENABLE_TIMER1_INT;
avclan.begin();
//audio_hu.begin();
//display.begin();
avclan_startup();
}
void loop()
{
avclan_frame_t msg_frame;
CheckForMessage();
#ifdef AVC_MASTER
master.update();
#endif
#ifdef AVC_DEVICE
device.update();
#endif
#ifdef AVC_MASTER
PingTimer();
#endif
if (avcSerial.available()) {
uint8_t readkey = avcSerial.read();
switch (readkey) {
case 'R':
resetFunc(); //call reset
break;
#ifdef AVC_MASTER
case 'L':
master.printAddress2DeviceList();
break;
#endif
case 'S': // start command
readSeq = 1;
s_len = 0;
s_dig = 0;
s_c[0] = s_c[1] = 0;
break;
case 'P':
case 'Y': // end of remote command
readSeq = 1;
// Get Message Type
uint8_t broadcast;
broadcast = data_tmp[0];
if (broadcast == 0x2D) // If broadcast received a "d", hex value 2D then set broadcast bit to 1
msg_frame.direct = 1;
else if (broadcast == 0x2B) // If broadcast received a "b", hex value 2B then set broadcast bit to 0
msg_frame.direct = 0;
// Get Master Address bits
msg_frame.master = ((data_tmp[1] << 4) + (data_tmp[2] >> 4)) & 0x0FFF;
#ifdef local_debug
avcSerial.print("S0 msg Master: ");
avcSerial.printHex8((data_tmp[1] << 4) + (data_tmp[2] >> 4)>>4);
avcSerial.printHex8((data_tmp[1] << 4) + (data_tmp[2] >> 4));
avcSerial.println();
#endif
// Get Slave Address bits
msg_frame.slave = ((data_tmp[2] << 8) + (data_tmp[3])) & 0x0FFF;
#ifdef local_debug
avcSerial.print("S0 msg Slave: ");
avcSerial.printHex8((data_tmp[2] << 8) + (data_tmp[3])>>4);
avcSerial.printHex8((data_tmp[2] << 8) + (data_tmp[3]));
avcSerial.println();
#endif
// Get DataSize bits
msg_frame.size = (data_tmp[4]);
// Get the rest of the message after the data size byte
for (i = 5; i < s_len; i++)
msg_frame.data[i - 5] = data_tmp[i];
if (readkey == 'Y') {
router.sendMessage(&msg_frame);
}
#ifdef AVC_MASTER
else {
master.processMessage(&msg_frame);
}
#endif
break;
default:
if (readSeq == 1) {
if (readkey != ' ') { // Key was a character
s_c[s_dig] = readkey; // Read the key entered
s_dig++; // Increase the data counter
if (s_dig == 2) { // If the data counter = 2 then
if (s_c[0] < ':') { // if the first key is < ':' then subtract 48, else subtract 55
s_c[0] -= 48;
}
else {
s_c[0] -= 55;
}
data_tmp[s_len] = 16 * s_c[0]; // ???
if (s_c[1] < ':') {
s_c[1] -= 48;
}
else {
s_c[1] -= 55;
}
data_tmp[s_len] += s_c[1];
s_len++;
s_dig = 0;
s_c[0] = s_c[1] = 0;
}
}
}
}
}
}
ISR(TIMER1_OVF_vect) {
TCNT1H = TI1_H; // Load counter value hi
TCNT1L = TI1_L; // Load counter value lo
/*
avclanDevice.cd_sec = avclanDevice.hexInc(avclanDevice.cd_sec);
if (avclanDevice.cd_sec == 0x60) {
avclanDevice.cd_sec = 0;
avclanDevice.cd_min = avclanDevice.hexInc(avclanDevice.cd_min);
if (avclanDevice.cd_min == 0xA0) {
avclanDevice.cd_min = 0x0;
}
}
avclan.event = EV_STATUS;
*/
/* Trigger broadcast of timed interval status request messages for all devices
*/
}