-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.ino
403 lines (302 loc) · 7.68 KB
/
controller.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
// CAN Transmitter Code (Sparkfun Pro Micro 5v/16MHz): //
// CAN document is in HEX, Code uses DEC. Must convert.
// --------------------------------------------------- //
// Library for using SPI Communication
#include <SPI.h>
// Library for using CAN Communication
// Library source: https://github.com/autowp/arduino-mcp2515
#include <mcp2515.h>
// HID-Projects includes media key (also called consumer keys) functions
// Library source: https://github.com/NicoHood/HID
#include <HID-Project.h>
#include <HID-Settings.h>
#define HID_CUSTOM_LAYOUT;
#define LAYOUT_US_ENGLISH;
// Library for a timer to keep the module alive
// Library source: https://github.com/natnqweb/Simpletimer
#include <Simpletimer.h>
struct can_frame canMsg;
// Chip select Pin?
MCP2515 mcp2515(17); // CS pin on MCP2515
// CAN message bits
int bit0 = 0;
int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
int bit4 = 0;
int bit5 = 0;
int bit6 = 0;
int bit7 = 0;
// Display button panel values
const int PWR = A0;
const int SOURCE = 8;
const int UP = 10;
const int DOWN = 9;
const int MENU = 5;
const int STATUS = 13;
// If OPMODE is 0, button panel operates in navigation mode
// If OPMODE is 1, button panel operates in media mode
int OPMODE = 0;
// Create the global timer object
Simpletimer timerPulse{};
void setup() {
// Begins SPI communication
SPI.begin();
Serial.begin(115200);
mcp2515.reset();
pinMode(PWR, OUTPUT);
pinMode(SOURCE, OUTPUT);
pinMode(UP, OUTPUT);
pinMode(DOWN, OUTPUT);
pinMode(MENU, OUTPUT);
pinMode(STATUS, OUTPUT);
digitalWrite(PWR, LOW);
digitalWrite(SOURCE, LOW);
digitalWrite(UP, LOW);
digitalWrite(DOWN, LOW);
digitalWrite(MENU, LOW);
digitalWrite(STATUS, LOW);
//Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
canMsg.can_id = 0x5dd;
canMsg.can_dlc = 8;
canMsg.data[0] = 93;
canMsg.data[1] = 0;
canMsg.data[2] = 255;
canMsg.data[3] = 255;
canMsg.data[4] = 255;
canMsg.data[5] = 255;
canMsg.data[6] = 255;
canMsg.data[7] = 255;
// Necessary to establish media key comm function
Keyboard.begin();
// Keeps Ford Button panel awake
timerPulse.register_callback(keepAlive);
}
void loop() {
mcp2515.readMessage( & canMsg);
// Set the duration of the timer (in ms)
timerPulse.run(500);
// Places captured packet data in variables
if (canMsg.can_id == 0x2a0) {
bit6 = canMsg.data[6]; // Volume knob events
bit0 = canMsg.data[0]; // All the other buttons events
// Eject button
if ((canMsg.data[0]) == 52) {
// Set media mode
if (OPMODE == 0) {
OPMODE = 1;
delay(20);
}
// Set navigation mode
else if (OPMODE == 1) {
OPMODE = 0;
delay(20);
}
}
// Rotary knob right
else if ((canMsg.data[6]) == 31) {
// Navigate up
if (OPMODE == 0) {
Keyboard.write("down");
delay(20);
}
// Volume up
else if (OPMODE == 1) {
Keyboard.write("+");
delay(20);
}
}
// Rotary knob left
else if ((canMsg.data[6]) == 29) {
// Navigate down
if (OPMODE == 0) {
Keyboard.write("up");
delay(20);
}
// Volume down
else if (OPMODE == 1) {
Keyboard.write("-");
delay(20);
}
}
// Rotary knob center button
else if (bit0 == 31) {
// Navigation select item
if (OPMODE == 0) {
Keyboard.write("enter");
delay(20);
}
// Media play/pause
else if (OPMODE == 1) {
Keyboard.write("space");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Forward button
else if (bit0 == 51) {
// Navigation next view
if (OPMODE == 0) {
Keyboard.write("tab");
delay(20);
}
// Media next track
else if (OPMODE == 1) {
Keyboard.write(">");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Previous button
else if (bit0 == 50) {
// Navigation previous view
if (OPMODE == 0) {
Keyboard.write("tab");
delay(20);
}
// Media previous track
else if (OPMODE == 1) {
Keyboard.write("<");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Tune+ button
else if (bit0 == 74) {
// Navigation move cursor right (no effect)
if (OPMODE == 0) {
Keyboard.write("right");
delay(20);
}
// Media seek track forward
else if (OPMODE == 1) {
Keyboard.write("]");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Tune- button
else if (bit0 == 75) {
// Navigation move cursor left (no effect)
if (OPMODE == 0) {
Keyboard.write("left");
delay(20);
}
// Media seek track backwards
else if (OPMODE == 1) {
Keyboard.write("[");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Sound button event
else if (bit0 == 24) {
// Media random track
else if (OPMODE == 0) {
Keyboard.write("s");
delay(20);
}
// Navigation random track
else if (OPMODE == 1) {
Keyboard.write("s");
delay(20);
}
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
delay(10);
}
// Power off display
else if (bit0 == 80) { // DISP Button
digitalWrite(PWR, HIGH);
delay(100);
digitalWrite(PWR, LOW);
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
}
// Change input source
else if (bit0 == 79) { // SOURCE Button
digitalWrite(SOURCE, HIGH);
delay(100);
digitalWrite(SOURCE, LOW);
while (bit0 != 255) {
delay(100);
mcp2515.readMessage( & canMsg);
if (canMsg.can_id == 0x2a0) {
bit0 = canMsg.data[0];
}
}
}
} // End parent if block
} // End loop
void keepAlive() {
Serial.println("Pulse");
digitalWrite(STATUS, HIGH);
delay(100);
digitalWrite(STATUS, LOW);
canMsg.can_id = 0x5dd;
canMsg.can_dlc = 8;
canMsg.data[0] = 93;
canMsg.data[1] = 0;
canMsg.data[2] = 255;
canMsg.data[3] = 255;
canMsg.data[4] = 255;
canMsg.data[5] = 255;
canMsg.data[6] = 255;
canMsg.data[7] = 255;
mcp2515.sendMessage( & canMsg);
bit0 = 0;
bit1 = 0;
bit2 = 0;
bit3 = 0;
bit4 = 0;
bit5 = 0;
bit6 = 0;
bit7 = 0;
}