-
Notifications
You must be signed in to change notification settings - Fork 6
/
idrv.ino
366 lines (319 loc) · 11.2 KB
/
idrv.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
#include <Canbus.h>
#include <string.h>
#define CAN_INT_PIN 2 //in
//Debug defines
#define CAN_ACTIVE true
#define DEBUG_LOOP false
//iDrive knob defines
#define KNOB_ROTATE_RIGHT 20
#define KNOB_ROTATE_LEFT 21
#define KNOB_UP 22
#define KNOB_DOWN 23
#define KNOB_RIGHT 24
#define KNOB_LEFT 25
#define KNOB_PRESS 26
#define MENU_BUTTON 27
#define IDRIVE_KNOB 0x1B8
// define CPU frequency in Mhz here if not defined in Makefile or other includes, compiler will throw a warning, ignore
#ifndef F_CPU
#define F_CPU 16000000UL // 28636360UL, 14318180UL, 21477270 UL
#endif
//CANBUS variables
unsigned char read_buffer[19]; //2 cmd words + 1 length wd + 16 data wds = 19, truncate if > 16 data wds
//unsigned char read_buffer_temp[10][19];
unsigned char read_buffer_temp[19];
unsigned short can_cmd = 0;
byte can_count = 0;
//Timer vars
byte timer_active = false;
unsigned long start_time = 0;
unsigned long end_time = 0;
unsigned long sniff_start = 0;
unsigned long idrive_timer = 0;
unsigned long log_timer = 0;
unsigned long log_start = 0;
//iDrive menu globals
byte window_focus = 0;
short menu_command = 0;
boolean menu_changed = false;
unsigned int last_rotate = 0;
byte bt_activity = 0;
//Misc functions
//float read_std_obd2(unsigned short);
int get_update_param(int);
unsigned long start_stop_timer(unsigned short,unsigned short);
void find_can_id(unsigned short);
int check_for_shutdown();
void verify_config();
void command_memorycheck();
void canbus_to_serial(unsigned char *);
//Volatile variables (anything used by the ISR should go here)
volatile boolean fresh_rx = false;
//Interrupt Service Routine (ISR)
void Canbus_ISR();
void setup() // run once, when the sketch starts
{
//Attach interrupt to CAN-bus interrupt line (DIO pin 2)
attachInterrupt(0,Canbus_ISR,FALLING);
} // end setup
void loop(){
if (CAN_ACTIVE == true)
{
if (Canbus.init(CANSPEED_100A,FILTER_ON)) // Initialize MCP2515 CAN controller at the specified speed
{
Serial.println("CAN Init OK");
}
else
{
Serial.println("Cannot init CAN!/");
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check for CAN Shield interrupts knob changes //////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Canbus_ISR()
{
if (CAN_ACTIVE == false)
{
fresh_rx = false;
return;
}
fresh_rx = true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read CANBUS message if interrupt fired ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void read_canbus_message()
{
unsigned int rotate_num = 0;
if (fresh_rx == true)
{
//Read CAN message
if (Canbus.message_rx((unsigned char*)read_buffer) == 0)
{
//NTSC_Term_Print("?");
fresh_rx = false;
return;
}
/*
//Route CAN message to serial (BT)
if (active_screen == CAN_SNIFFER)
{
for (byte i=0;i<19;i++)
{
//read_buffer_temp[can_count][i] = read_buffer[i];
read_buffer_temp[i] = read_buffer[i];
}
//canbus_to_serial(&read_buffer_temp[can_count][0]);
canbus_to_serial(&read_buffer_temp[0]);
//Serial.print(can_count);
if (can_count < 9)
{
//can_count++;
}
}
*/
can_cmd = (read_buffer[1] << 8) + read_buffer[0];
//sprintf(buff,"buf1 = 0x%x, buf0 = 0x%x, sum=0x%x",read_buffer[1],read_buffer[0],can_cmd);
//NTSC_Term_Print(buff);
//sprintf(buff,"2 0x%x/",read_buffer[2]);
//NTSC_Term_Print(buff);
//sprintf(buff,"3 0x%x/",read_buffer[3]);
//NTSC_Term_Print(buff);
//sprintf(buff,"4 0x%x/",read_buffer[4]);
//NTSC_Term_Print(buff);
//sprintf(buff,"5 0x%x/",read_buffer[5]);
//NTSC_Term_Print(buff);
//sprintf(buff,"6 0x%x/",read_buffer[6]);
//NTSC_Term_Print(buff);
//sprintf(buff,"7 0x%x/",read_buffer[7]);
//NTSC_Term_Print(buff);
//sprintf(buff,"8 0x%x/",read_buffer[8]);
//NTSC_Term_Print(buff);
//http://translate.google.com/translate?u=http%3A%2F%2Fwww.xolmatic.com%2Fxprojects%2FXE65%2FCAN.htm&sl=es&tl=en&hl=&ie=UTF-8
//Look for specific commands, anything else exit ISR
switch (can_cmd)
{
//iDrive knob commands
//I-drive Menu button: 1B8 6 0FC5nnnn206F (or 0FC4nnnn206F, sometimes C4 sometimes C5)
//I-drive knob press: 1B8 6 0FC1nnnn206F
//I-drive knob up: 1B8 6 00C0nnnn206F
//I-drive knob down: 1B8 6 04C0nnnn206F
//I-drive knob left: 1B8 6 06C0nnnn206F
//I-drive knob right: 1B8 6 02C0nnnn206F
//I-drive knob rotate right: 1B8 6 0FC0nnnn206F (nn is increasing)
//I-drive knob rotate left: 1B8 6 0FC0nnnn206F (nn is decreasing)
// nnnn min = 0000
// nnnn max = FF|FF (where buffer 5 is lower and 6 is the higher byte)
case IDRIVE_KNOB: //Command word 0x1B8
//Knob rotation or return to center
if ((read_buffer[3] == 0x0F) && (read_buffer[4] == 0xC0)
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
rotate_num = (read_buffer[6] << 8) + read_buffer[5];
//I-drive knob rotate right: 1B8 6 0FC0nnnn206F (nn is increasing)
//if (read_buffer[5] > last_rotate)
if (rotate_num > last_rotate)
{
menu_command = KNOB_ROTATE_RIGHT;
if (window_focus != 0)
menu_changed = true;
//NTSC_Term_Print("Knob r right/");
Serial.println("Knob r rt");
}
//I-drive knob rotate left: 1B8 6 0FC0nnnn206F (nn is decreasing)
//else if (read_buffer[5] < last_rotate)
else if (rotate_num < last_rotate)
{
menu_command = KNOB_ROTATE_LEFT;
if (window_focus != 0)
menu_changed = true;
//NTSC_Term_Print("Knob r left/");
Serial.println("Knob r lf");
}
//I-drive knob return to center since no rotation increase
else
{
menu_changed = true;
//NTSC_Term_Print("debnc");
Serial.println("Return");
}
//last_rotate = read_buffer[5];
last_rotate = rotate_num;
}
//I-drive knob up: 1B8 6 00C0nnnn206F
else if ((read_buffer[3] == 0x00) && (read_buffer[4] == 0xC0)
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = KNOB_UP;
//menu_changed = true;
//NTSC_Term_Print("Knob up/");
Serial.println("Knob up");
}
//I-drive knob down: 1B8 6 04C0nnnn206F
else if ((read_buffer[3] == 0x04) && (read_buffer[4] == 0xC0)
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = KNOB_DOWN;
//menu_changed = true;
//NTSC_Term_Print("Knob down/");
Serial.println("Knob down");
}
//I-drive knob right: 1B8 6 02C0nnnn206F
else if ((read_buffer[3] == 0x02) && (read_buffer[4] == 0xC0)
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = KNOB_RIGHT;
//menu_changed = true;
//NTSC_Term_Print("Knob right/");
//Serial.println("Knob right");
}
//I-drive knob left: 1B8 6 06C0nnnn206F
else if ((read_buffer[3] == 0x06) && (read_buffer[4] == 0xC0)
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = KNOB_LEFT;
//menu_changed = true;
//NTSC_Term_Print("Knob left/");
Serial.println("Knob left");
}
//I-drive knob press: 1B8 6 0FC1nnnn206F
else if ((read_buffer[3] == 0x0F) && (read_buffer[4] == 0xC1) //0xc0 is press up
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = KNOB_PRESS;
//menu_changed = true;
//NTSC_Term_Print("Knob press/");
Serial.println("Knob press");
}
//I-drive Menu button: 1B8 6 0FC5nnnn206F (or 0FC4nnnn206F, sometimes C4 sometimes C5)
else if ((read_buffer[3] == 0x0F) && ((read_buffer[4] == 0xC5) || (read_buffer[4] == 0xC4))
&& (read_buffer[7] == 0x20) && (read_buffer[8] == 0x6F))
{
menu_command = MENU_BUTTON;
//menu_changed = true;
//NTSC_Term_Print("Menu btn/");
Serial.println("Menu btn");
}
break;
//Other custom BMW commands, put here
// ....
default:
menu_command = 0;
menu_changed = false;
break;
}
fresh_rx = false;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read Standard OBD2 messages ///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*float read_std_obd2(unsigned short param)
{
if (CAN_ACTIVE == false)
return -999;
if (Canbus.ecu_req(param,can_buffer) == 1)
{
return atof(can_buffer);
}
else
return -999;
}*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read CAN bus for Specific Command ID //////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void find_can_id(unsigned short cmd_to_find)
{
if (CAN_ACTIVE == false)
return;
//find_flag = true;
//find_id = cmd_to_find;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void canbus_to_serial(unsigned char *buff_in)
{
//char buff_temp[7] = {0};
char buff_out[16] = {0};
sprintf(buff_out,"%dms, 0x",(int)(millis()-sniff_start));
Serial.print(buff_out);
//strcpy(buff_out,buff_temp);
for (byte i=1;i<buff_in[2]+3;i+=2)
{
if (i == 3)
{
sprintf(buff_out,"%02x ",buff_in[2]);
//strcat(buff_out,buff_temp);
Serial.print(buff_out);
i = 2;
}
else if (i > 18)
{
//truncate message if greater than 16 data words (19 total words)
//Serial.println("");
break;
}
else
{
sprintf(buff_out,"%02x %02x ",buff_in[i],buff_in[i-1]);
Serial.print(buff_out);
//strcat(buff_out,buff_temp);
}
//sprintf(buff_out,"%x ",buff_in[i]);
if (can_count > 0)
{
can_count = 0;
break;
}
}
Serial.println("");
/* if (can_count >= 1)
{
--can_count;
canbus_to_serial(&read_buffer_temp[can_count][0]);
}
*/
}