-
Notifications
You must be signed in to change notification settings - Fork 0
/
iSaab.ino
executable file
·421 lines (366 loc) · 8.78 KB
/
iSaab.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* iSaab -- A CD Changer replacement for Saab 9-3 OG and 9-5 OG
*
*/
#include <SPI.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include "CAN.h"
#include "CDC.h"
#include "iSaab.h"
uint8_t tag = AudioFile::NUM_TAGS;
bool newText;
// one-time setup
void setup() {
// setup sound card
CDC.setup();
#ifndef SERIALMODE
// open I-Bus @ 47.619Kbps
CAN.begin(47, high_filters, low_filters);
// use IRQ for incoming messages
SPI.usingInterrupt(MCP2515_INT);
attachInterrupt(MCP2515_INT, processMessage, LOW);
// reduce power
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
power_usart0_disable();
power_timer1_disable();
#else
// open serial
Serial.begin(115200);
// use timer to read serial
SPI.usingInterrupt(255);
TCCR1A = 0;
TCCR1B = _BV(WGM12) | _BV(CS12);
OCR1A = F_CPU / 256 / 2 - 1;
TIMSK1 |= _BV(OCIE1A);
// reduce power
set_sleep_mode(SLEEP_MODE_IDLE);
#endif
power_timer2_disable();
power_adc_disable();
power_twi_disable();
}
// main loop
void loop() {
sleep_mode();
CDC.loop();
}
// interrupt handler for incoming message
void processMessage() {
static uint8_t gap = 0;
// starting to receive CDC messages, wake up
if (gap == 0) {
CAN.setMode(CANClass::Normal);
}
// act on message
CANClass::msg msg;
if (receiveMessage(msg)) {
switch (msg.id) {
case RX_CDC_CONTROL:
controlRequest(msg);
gap = 15;
break;
case RX_CDC_POWER:
powerRequest(msg);
gap = 15;
break;
case RX_SID_REQUEST:
displayRequest(msg); // comment out for vehicles equipped with Park Assist
gap--;
break;
}
}
// no more CDC messages, go to sleep
if (gap == 0) {
CAN.setMode(CANClass::ListenOnly);
}
}
void powerRequest(CANClass::msg &msg) {
uint8_t action = msg.data[3] & 0x0f;
// reply id
msg.id = TX_CDC_POWER;
// handle power request
switch (action) {
case 0x2: // active
msg.data[3] = 0x16;
break;
case 0x3: // power on
msg.data[3] = 0x03;
CDC.on();
break;
case 0x8: // power off
msg.data[3] = 0x19;
CDC.off();
break;
}
msg.data[0] = 0x32;
msg.data[1] = 0x00;
msg.data[2] = 0x00;
msg.data[4] = 0x01;
msg.data[5] = 0x02;
msg.data[6] = 0x00;
msg.data[7] = 0x00;
sendMessage(msg);
// send rest of sequence
switch (action) {
case 0x2: // active
msg.data[3] = 0x36;
break;
case 0x3: // power on
msg.data[3] = 0x22;
break;
case 0x8: // power off
msg.data[3] = 0x38;
break;
}
msg.data[4] = 0x00;
msg.data[5] = 0x00;
while (msg.data[0] < 0x62) {
msg.data[0] += 0x10;
sendMessage(msg);
}
}
void controlRequest(CANClass::msg &msg) {
static int8_t repeatCount;
// reply id
msg.id = TX_CDC_CONTROL;
// ready, reply
msg.data[0] = 0x60;
// map CDC commands to methods
repeatCount++;
switch (msg.data[1]) {
case 0x00: // no command
msg.data[0] = 0x20;
repeatCount = 0;
CDC.normal();
break;
case 0x35: // TRACK >>
CDC.skipTrack(repeatCount);
break;
case 0x36: // TRACK <<
CDC.skipTrack(1 - repeatCount);
break;
case 0x45: // PLAY >>
CDC.skipTime(repeatCount);
break;
case 0x46: // PLAY <<
CDC.skipTime(-repeatCount);
break;
case 0x59: // NXT
if (repeatCount == 1) {
if (CDC.isShuffled()) {
// rotate display tag
tag = (tag + 1) % (AudioFile::NUM_TAGS + 1);
newText = true;
} else {
CDC.nextDisc();
}
}
break;
case 0x68: // 1 - 6
if (repeatCount == 1) {
uint8_t preset = msg.data[2] - 1;
if (CDC.isShuffled()) {
// hide/show display tag
tag = (preset == tag) ? AudioFile::NUM_TAGS : preset;
newText = true;
} else {
CDC.preset(preset);
}
}
break;
case 0x76: // RDM toggle
if (repeatCount == 1) {
msg.data[0] |= 0x80;
CDC.shuffle();
}
break;
case 0xb0: // MUTE on
case 0x14: // deselect CDC
case 0x84: // SCAN disc
CDC.pause();
break;
case 0xb1: // MUTE off
case 0x24: // select CDC
case 0x88: // SCAN magazine
CDC.resume();
break;
}
msg.data[1] = 0x00;
// magazine
msg.data[2] = 0b00111111;
// play status, disc
msg.data[3] = CDC.getState() & 0xf0;
msg.data[3] |= CDC.getDisc() % 6 + 1;
// track
uint8_t track = CDC.getTrack() + 1;
msg.data[4] = (track / 10) % 10;
msg.data[4] <<= 4;
msg.data[4] |= track % 10;
// minutes
uint16_t time = CDC.getTime();
uint8_t min = time / 60;
msg.data[5] = (min / 10) % 10;
msg.data[5] <<= 4;
msg.data[5] |= min % 10;
// seconds
uint8_t sec = time % 60;
msg.data[6] = sec / 10;
msg.data[6] <<= 4;
msg.data[6] |= sec % 10;
// married, RDM
msg.data[7] = 0xd0;
if (CDC.isShuffled()) msg.data[7] |= 0x20;
// flag disc, track, or time change
static uint8_t last[4] = {0x02, 0x01, 0x00, 0x00};
if (memcmp(last, (msg.data + 3), sizeof(last))) {
msg.data[0] |= 0x80;
if (time == 0) newText = true;
memcpy(last, (msg.data + 3), sizeof(last));
}
sendMessage(msg);
}
void displayRequest(CANClass::msg &msg) {
// check row
if (msg.data[0] == 0x00) {
// check if display is wanted
const String text = CDC.getText(tag);
if (CDC.getState() == VS1053::Playing && text.length() > 0) {
// check owner
switch (msg.data[1]) {
case 0x12: // iSaab
// send text
msg.id = TX_SID_TEXT;
msg.data[1] = 0x96;
for (int8_t id = 5, i = 0; id >= 0; id--) {
// sequence id, start of sequence flag
msg.data[0] = id;
if (id == 5) msg.data[0] |= 0x40;
// row id, new text flag
msg.data[2] = (id < 3) ? 2 : 1;
if (newText) msg.data[2] |= 0x80;
// copy text
switch (id) {
default:
msg.data[3] = text[i++];
msg.data[4] = text[i++];
msg.data[5] = text[i++];
msg.data[6] = text[i++];
msg.data[7] = text[i++];
break;
case 3:
msg.data[3] = text[i++];
msg.data[4] = text[i++];
if (text[i] == ' ') i++;
msg.data[5] = 0x00;
msg.data[6] = 0x00;
msg.data[7] = 0x00;
break;
case 0:
msg.data[3] = text[i++];
msg.data[4] = tag + 1;
msg.data[5] = 0x00;
msg.data[6] = 0x00;
msg.data[7] = 0x00;
break;
}
sendMessage(msg);
}
newText = false;
msg.data[2] = 0x05; // keep
break;
case 0xff: // available
newText = true;
msg.data[2] = 0x03; // request
break;
default: // taken
msg.data[2] = 0xff; // decline
break;
}
} else {
msg.data[2] = 0xff; // decline
}
// display request
msg.id = TX_SID_REQUEST;
msg.data[0] = 0x1f; // device
msg.data[1] = 0x00; // row
msg.data[3] = 0x12; // iSaab
msg.data[4] = 0x00;
msg.data[5] = 0x00;
msg.data[6] = 0x00;
msg.data[7] = 0x00;
sendMessage(msg);
}
}
void sendMessage(const CANClass::msg &msg) {
#ifndef SERIALMODE
while (!CAN.send(msg));
#else
Serial.print(msg.id, HEX);
Serial.print(' ');
for (uint8_t i = 0; i < 8; i++) {
Serial.print(msg.data[i], HEX);
Serial.print(' ');
}
Serial.println();
#endif
}
bool receiveMessage(CANClass::msg &msg) {
#ifndef SERIALMODE
return CAN.receive(msg);
#else
msg.id = RX_CDC_CONTROL;
if (Serial.available()) {
char c = Serial.read();
switch (c) {
case 'b':
msg.id = RX_CDC_POWER;
msg.data[3] = 0x03;
break;
case 'e':
msg.id = RX_CDC_POWER;
msg.data[3] = 0x08;
break;
case 't':
msg.id = RX_SID_REQUEST;
msg.data[0] = 0x00;
msg.data[1] = 0x12;
break;
case 'p':
msg.data[1] = 0xb0;
break;
case 'r':
msg.data[1] = 0xb1;
break;
case '*':
msg.data[1] = 0x76;
break;
case '>':
msg.data[1] = 0x35;
break;
case '<':
msg.data[1] = 0x36;
break;
case 'n':
msg.data[1] = 0x59;
break;
case '-':
msg.data[1] = 0x46;
break;
case '+':
msg.data[1] = 0x45;
break;
case '1': case '2': case '3': case '4': case '5': case '6':
msg.data[1] = 0x68;
msg.data[2] = c - '0';
break;
}
} else {
msg.data[1] = 0x00;
}
return true;
#endif
}
#ifdef SERIALMODE
ISR(TIMER1_COMPA_vect) { processMessage(); }
#endif