-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcanEmulator.ino
193 lines (174 loc) · 8.21 KB
/
canEmulator.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
#define SPI_CS_PIN 10
#define SERIAL_SPEED 9600 //baud rate for arduino serial console debug output
#define ENABLE_CANBUS //comment out to avoid usage of mcp-board (for testing)
#define SPEEDUP 500 //the smaller, the faster (default is 1000)
#include <SPI.h>
#include <mcp_can.h> //https://github.com/coryjfowler/MCP_CAN_lib
MCP_CAN CAN(SPI_CS_PIN); //global init of can-library
unsigned long startTime;
struct msgStruct {
byte duration; //how long to wait before next msg is send
short id; //the CAN-ID
INT8U dlc; //Number of data-bytes in this msg (currently <= 8 bytes)
INT8U data[8]; //the data which is send to the dash
};
/**
* the following struct contains the messages which are send to the dash
* for initialization
* feel free to add or remove messages.
* {duration, id, dlc {byte1, byte2, ... byte_dlc}}
**/
const struct msgStruct initMessages[] PROGMEM = {
{10, 0x35d, 8, { 0x10, 0x03, 0x20, 0x00, 0x00, 0x00, 0x50, 0x00}} //dash on
,{4, 0x60d, 8, { 0x00, 0x10, 0x00, 0x00, 0x27, 0x73, 0x21, 0x71}} //reset display state
,{20, 0x60d, 8, { 0x08, 0x10, 0x00, 0x00, 0x32, 0xFF, 0x2f, 0x10}} //show saved km
//,{10, 0x60d, 8, { 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, 0x21, 0x70}} //disp normal
//,{10, 0x625, 4, { 0x82, 0x00, 0xC0, 0x00}} //engine on
//,{10, 0x75E, 4, { 0x01, 0x80, 0xC4, 0x00}} //parking brake
//,{10, 0x60D, 4, {0x00, 0x00, 0x00, 0x00, 0x63, 0x00, 0x21, 0x71}} //disp button
};
/**
* the following stuct contains the messages which are send to the dash
* within a loop
* feel free to add or remove messages.
* {duration, id, dlc {byte1, byte2, ... byte_dlc}}
**/
const struct msgStruct messages[] PROGMEM = { //load into flash-memory (sram was to small)
{1, 0x743, 8, {0x02, 0x10, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00}} //enable indicators
,{3, 0x743, 8, {0x04, 0x30, 0x06, 0x20, 0xFF, 0x00, 0x00, 0x00}} //left ind. lights
,{3, 0x743, 8, {0x04, 0x30, 0x07, 0x20, 0xFF, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x01, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x07, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x06, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x05, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x18, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x12, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x11, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x0A, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x0B, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x04, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x14, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x17, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00}} //colant to hot
,{7, 0x743, 8, {0x02, 0x10, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00}} //full left alpha
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x08, 0x00, 0x00, 0x00}} //middle ind. lights
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x02, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x15, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x19, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x0F, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x09, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x0C, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x0E, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x13, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x10, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x17, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x07, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x16, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x11, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x0F, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x08, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x1E, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x19, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x06, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x01, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x0E, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x09, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x1F, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x03, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x1B, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x12, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x0A, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x20, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x0B, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x13, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x1A, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x02, 0x00, 0x00, 0x00}}
,{1, 0x743, 8, {0x04, 0x30, 0x02, 0x20, 0x1D, 0x00, 0x00, 0x00}}
,{2, 0x743, 8, { 0x04, 0x30, 0x05, 0x20, 0x46, 0x00, 0x00, 0x00}} //rpm full
,{2, 0x743, 8, { 0x04, 0x30, 0x05, 0x20, 0x44, 0x00, 0x00, 0x00}} //rpm -1
,{2, 0x743, 8, { 0x04, 0x30, 0x05, 0x20, 0x42, 0x00, 0x00, 0x00}} //rpm -2
,{2, 0x743, 8, {0x04, 0x30, 0x01, 0x20, 0x16, 0x00, 0x00, 0x00}} //push breake indicator
,{2, 0x743, 8, {0x04, 0x30, 0x04, 0x20, 0xC8, 0x00, 0x00, 0x00}} //200kmh
,{2, 0x743, 8, {0x04, 0x30, 0x04, 0x20, 0xBC, 0x00, 0x00, 0x00}} //188kmh
,{7, 0x743, 8, {0x04, 0x30, 0x03, 0x11, 0x01, 0x00, 0x00, 0x00}} //full small
,{15, 0x60d, 8, { 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, 0x21, 0x70}} //disp normal
};
/**
* Convenient function which prints debug output to the serial console
*/
void printMsg(int i, msgStruct msg){
unsigned long d = ((millis()-startTime)/1000);
byte s = d % 60;
byte m = (d/60) % 60;
byte h = (d/3600);
if (msg.duration==0) Serial.print("==>");
Serial.print(h); Serial.print(":"); Serial.print(m); Serial.print(":"); Serial.print(s);
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print("{");
Serial.print(msg.duration);
Serial.print(", ");
Serial.print(msg.id, HEX);
Serial.print(", ");
Serial.print(msg.dlc, HEX);
Serial.print(", {");
for(int i=0; i<msg.dlc; i++) {
Serial.print(msg.data[i], HEX);
if (i<msg.dlc) Serial.print(", ");
}
Serial.println("}}");
}
/*
* Send message to CAN-Bus
* i = number of message
* m = the message itself
*/
void sendMsg(int i, msgStruct m){
printMsg(i, m);
#if defined(ENABLE_CANBUS)
while (CAN_OK != CAN.sendMsgBuf(m.id, 0, m.dlc, m.data)){ //here we send the CAN-Messages
Serial.println(F("Error sending message... repeating"));
delay(((m.duration==0)?1:m.duration)*SPEEDUP); //wait for <duration> secs.
}
#endif
delay(((m.duration==0)?1:m.duration)*SPEEDUP); //wait for <duration> secs.
}
/**
* Setup and initialization
*/
void setup(){
Serial.begin(SERIAL_SPEED); //speed for arduino serial monitor
while (!Serial);
#if defined(ENABLE_CANBUS)
Serial.println(F("CAN setup.."));
while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ)){ //CAN-Bus setup
delay(500);
Serial.println(F("CAN BUS: starting error! Trying again..."));
}
CAN.setMode(MCP_NORMAL);
Serial.println(F("Please wait for disp startup..."));
delay(5000); //just wait for a while...
#endif
startTime = millis();
//send display init commands
msgStruct m;
for(int i=0; i<(sizeof(initMessages)/sizeof(msgStruct)); i++){ //loop through all messages
memcpy_P(&m, &initMessages[i], sizeof(msgStruct)); //read msg from flash-memory into sram
sendMsg(i,m);
}
Serial.println(F("setup finished."));
}
/**
* For ever loop through our msg and send them to the dash
*/
void loop(){
msgStruct m;
for(int i=0; i<(sizeof(messages)/sizeof(msgStruct)); i++){ //loop through all messages
memcpy_P(&m, &messages[i], sizeof(msgStruct)); //read msg from flash-memory into sram
sendMsg(i,m);
}
}