-
Notifications
You must be signed in to change notification settings - Fork 15
/
Event_Printer.ino
294 lines (248 loc) · 8.38 KB
/
Event_Printer.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
#include <SPI.h>
#include <mcp_can.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include "BMW_R1200_GS_K25_CAN_Bus_Defines.h"
#include "BMW_R1200_GS_K25_State.h"
/* Compile time Flags */
#define DEBUG 0
#define PRINT_STATUS_TO_SERIAL_CONSOLE 0
#define PRINT_STATUS_TO_TFT 1
/* Helper Macros */
#define HI_NIBBLE(b) (((b) >> 4) & 0x0F)
#define LO_NIBBLE(b) ((b) & 0x0F)
/* Debug Macros */
#ifdef DEBUG
#define DEBUG_PRINT(...) Serial.print(__VA_ARGS__)
#define DEBUG_PRINT_LN(...) Serial.println(__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#define DEBUG_PRINT_LN(...)
#endif
/* TFT Pin Defines */
#define TFT_CS 10
#define TFT_RST 7
#define TFT_DC 4
/* Globals */
MCP_CAN CAN(9);
K25_State_t motorcycle_state;
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
bool status_changed = true;
/* Arduino Functions */
void setup()
{
Serial.begin(115200);
init_CAN_bus();
init_display();
}
void loop()
{
process_CAN_Messages();
if (status_changed)
{
print_status();
status_changed = false;
}
}
/* Init Helpers */
void init_display()
{
tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
tft.setRotation(3); // rotate 90 degrees
tft.fillScreen(ST7735_BLACK);
}
void init_CAN_bus()
{
while (CAN_OK != CAN.begin(CAN_500KBPS)) {
DEBUG_PRINT_LN("Failed to initialize CAN");
DEBUG_PRINT_LN("Retrying..");
delay(100);
}
DEBUG_PRINT_LN("Starting to initialize CAN");
// Set filter masks
CAN.init_Mask(0, 0, 0xfff);
CAN.init_Mask(1, 0, 0xfff);
// Set filters
CAN.init_Filt(0, 0, MSG_ID_BMSK_Control_Module);
CAN.init_Filt(1, 0, MSG_ID_BMSK_Control_Module_2);
CAN.init_Filt(2, 0, MSG_ID_ZFE_Control_Module);
CAN.init_Filt(3, 0, MSG_ID_ZFE_Control_Module_2);
CAN.init_Filt(4, 0, MSG_ID_ABS_Control_Module);
CAN.init_Filt(5, 0, MSG_ID_ABS_Control_Module_2);
CAN.init_Filt(6, 0, MSG_ID_Instrument_Cluster);
CAN.init_Filt(7, 0, MSG_ID_Instrument_Cluster_2);
Serial.println("CAN Initialized");
}
/* Event Processors */
void process_CAN_Messages()
{
byte length = 0;
byte data[8];
byte new_value = 0;
if(CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&length, data);
switch(CAN.getCanId())
{
case MSG_ID_BMSK_Control_Module:
{
/*
float new_throttle = K25_THROTTLE_PERCENTAGE(data[1]);
if ( new_throttle != motorcycle_state.throttle_position )
{
status_changed = true;
motorcycle_state.throttle_position = (K25_Throttle_Position_t)new_throttle;
}
*/
new_value = LO_NIBBLE(data[5]);
if ( new_value != motorcycle_state.abs_button )
{
status_changed = true;
motorcycle_state.abs_button = (K25_ABS_Button_State)new_value;
}
new_value = LO_NIBBLE(data[4]);
if ( new_value != motorcycle_state.clutch )
{
status_changed = true;
motorcycle_state.clutch = (K25_Clutch_Lever_State)new_value;
}
} break;
case MSG_ID_BMSK_Control_Module_2:
{
} break;
case MSG_ID_ZFE_Control_Module:
{
new_value = LO_NIBBLE(data[6]);
if ( new_value != motorcycle_state.high_beam )
{
status_changed = true;
motorcycle_state.high_beam = (K25_High_Beam_State)new_value;
}
new_value = data[7];
if ( new_value != motorcycle_state.turn_signals )
{
status_changed = true;
motorcycle_state.turn_signals = (K25_Turn_Signals_State)new_value;
}
} break;
case MSG_ID_ZFE_Control_Module_2:
{
new_value = HI_NIBBLE(data[7]);
if ( new_value != motorcycle_state.heated_grips )
{
status_changed = true;
motorcycle_state.heated_grips = (K25_Heated_Grips_State)new_value;
}
new_value = LO_NIBBLE(data[5]);
if ( new_value != motorcycle_state.info_button )
{
status_changed = true;
motorcycle_state.info_button = (K25_Info_Button_State)new_value;
}
} break;
case MSG_ID_ABS_Control_Module:
{
new_value = LO_NIBBLE(data[6]);
if ( new_value != motorcycle_state.brake_levers )
{
status_changed = true;
motorcycle_state.brake_levers = (K25_Brake_Lever_State)new_value;
}
new_value = HI_NIBBLE(data[1]);
if ( new_value != motorcycle_state.abs_system )
{
status_changed = true;
motorcycle_state.abs_system = (K25_ABS_State)new_value;
}
} break;
case MSG_ID_ABS_Control_Module_2:
{
} break;
case MSG_ID_Instrument_Cluster:
{
} break;
case MSG_ID_Instrument_Cluster_2:
{
new_value = LO_NIBBLE(data[1]);
if ( new_value != motorcycle_state.als )
{
status_changed = true;
motorcycle_state.als = (K25_ALS_State)new_value;
}
} break;
}
}
}
/* Print Functions */
void print_status()
{
String text = "";
/*
text += "Throttle: ";
text += motorcycle_state.throttle_position;
text += "% \n";
*/
text += "Heated Grips: ";
if ( motorcycle_state.heated_grips == K25_Heated_Grips_State_off ) text += "OFF";
else if ( motorcycle_state.heated_grips == K25_Heated_Grips_State_low ) text += "LOW";
else if ( motorcycle_state.heated_grips == K25_Heated_Grips_State_high ) text += "HIGH";
else text += "Unknown";
text += " \n";
text += "Turn Signals: ";
if ( motorcycle_state.turn_signals == K25_Turn_Signals_State_off ) text += "OFF";
else if ( motorcycle_state.turn_signals == K25_Turn_Signals_State_left ) text += "LEFT";
else if ( motorcycle_state.turn_signals == K25_Turn_Signals_State_right ) text += "RIGHT";
else if ( motorcycle_state.turn_signals == K25_Turn_Signals_State_both ) text += "HAZARDS";
else text += "Unknown";
text += " \n";
text += "High Beam: ";
if ( motorcycle_state.high_beam == K25_High_Beam_State_off ) text += "OFF";
else if ( motorcycle_state.high_beam == K25_High_Beam_State_on ) text += "ON";
else text += "Unknown";
text += " \n";
text += "Info Button: ";
if ( motorcycle_state.info_button == K25_Info_Button_State_short_press ) text += "SHORT";
else if ( motorcycle_state.info_button == K25_Info_Button_State_long_press ) text += "LONG";
else text += "Unknown";
text += "\n";
text += "Clutch: ";
if ( motorcycle_state.clutch == K25_Clutch_Lever_State_out ) text += "OUT";
else if ( motorcycle_state.clutch == K25_Clutch_Lever_State_in ) text += "IN";
else text += "Unknown";
text += " \n";
text += "ABS Button: ";
if ( motorcycle_state.abs_button == K25_ABS_Button_State_off ) text += "OFF";
else if ( motorcycle_state.abs_button == K25_ABS_Button_State_on ) text += "ON";
else text += "Unknown";
text += " \n";
text += "Brakes: ";
if ( motorcycle_state.brake_levers == K25_Brake_Lever_State_none ) text += "NONE";
else if ( motorcycle_state.brake_levers == K25_Brake_Lever_State_front ) text += "FRONT";
else if ( motorcycle_state.brake_levers == K25_Brake_Lever_State_rear ) text += "REAR";
else text += "Unknown";
text += " \n";
text += "ABS: ";
if ( motorcycle_state.abs_system == K25_ABS_State_off ) text += "OFF";
else if ( motorcycle_state.abs_system == K25_ABS_State_on ) text += "ON";
else text += "Unknown";
text += " \n";
/*
text += "ALS: ";
if ( motorcycle_state.als == K25_ALS_State_dark ) text += "DARK";
else if ( motorcycle_state.als == K25_ALS_State_light ) text += "LIGHT";
else text += "Unknown";
text += " \n";
*/
#if PRINT_STATUS_TO_SERIAL_CONSOLE
Serial.println(text);
#endif
#if PRINT_STATUS_TO_TFT
draw_text(&text, ST7735_WHITE, ST7735_BLACK);
#endif
}
void draw_text(String *text, uint16_t color, uint16_t background_color) {
tft.setCursor(0, 0);
tft.setTextColor(color, background_color);
tft.setTextWrap(true);
tft.print(text->c_str());
}