-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ino
103 lines (94 loc) · 2.26 KB
/
parser.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
#include <SPI.h>
#include "mcp2515_can.h"
#include <math.h>
const int SPI_CS_PIN = 10;
mcp2515_can CAN(SPI_CS_PIN);
long lastCanActive = 0;
bool isCanDead = true;
void setup() {
Serial.begin(115200);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
initCan();
}
void initCan() {
while (CAN_OK != CAN.begin(CAN_50KBPS)) {}
delay(2000);
}
void parseCan() {
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive()) {
lastCanActive = millis();
if (isCanDead) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
Serial.println("BOOT RPI!");
}
isCanDead = false;
CAN.readMsgBuf(&len, buf);
unsigned long canId = CAN.getCanId();
switch(canId) {
case 0x3C3:
Serial.print("brightness:");
Serial.println((int) ((float) buf[2] / (float) 112 * (float) 255));
Serial.print("dayTime:");
Serial.println(buf[5] != 32);
break;
case 0x380:
Serial.print("inReverse:");
Serial.println(buf[2] == 76);
break;
case 0x3C4:
String activeButton;
switch (buf[0]) {
case 128:
activeButton = "volUp";
break;
case 64:
activeButton = "volDown";
break;
case 32:
activeButton = "mute";
break;
case 16:
activeButton = "nextTrack";
break;
case 8:
activeButton = "previousTrack";
break;
case 4:
activeButton = "source";
}
if (activeButton == NULL) {
switch(buf[1]) {
case 128:
activeButton = "phone";
break;
case 64:
activeButton = "voice";
break;
case 2:
activeButton = "up";
break;
case 1:
activeButton = "down";
}
}
if (activeButton != NULL) {
Serial.print("activeSwcButton:");
Serial.println(activeButton);
}
}
} else {
if (millis() - lastCanActive >= 5000 && !isCanDead) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println("KILL RPI");
isCanDead = true;
}
}
}
void loop() {
parseCan();
}