forked from TheRo0T/can-usb
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcan-usb.ino
83 lines (62 loc) · 1.89 KB
/
can-usb.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
#include <can.h>
#include <mcp2515.h>
#include <CanHacker.h>
#include <CanHackerLineReader.h>
#include <lib.h>
#include <SPI.h>
#include <SoftwareSerial.h>
const int SPI_CS_PIN = 10;
const int INT_PIN = 2;
const int SS_RX_PIN = 3;
const int SS_TX_PIN = 4;
CanHackerLineReader *lineReader = NULL;
CanHacker *canHacker = NULL;
SoftwareSerial softwareSerial(SS_RX_PIN, SS_TX_PIN);
void setup() {
Serial.begin(115200);
while (!Serial);
SPI.begin();
softwareSerial.begin(115200);
Stream *interfaceStream = &Serial;
Stream *debugStream = &softwareSerial;
canHacker = new CanHacker(interfaceStream, debugStream, SPI_CS_PIN);
//canHacker->enableLoopback(); // uncomment this for loopback
lineReader = new CanHackerLineReader(canHacker);
pinMode(INT_PIN, INPUT);
}
void loop() {
CanHacker::ERROR error;
if (digitalRead(INT_PIN) == LOW) {
error = canHacker->processInterrupt();
handleError(error);
}
error = lineReader->process();
handleError(error);
}
void handleError(const CanHacker::ERROR error) {
switch (error) {
case CanHacker::ERROR_OK:
case CanHacker::ERROR_UNKNOWN_COMMAND:
case CanHacker::ERROR_NOT_CONNECTED:
case CanHacker::ERROR_MCP2515_ERRIF:
case CanHacker::ERROR_INVALID_COMMAND:
return;
default:
break;
}
softwareSerial.print("Failure (code ");
softwareSerial.print((int)error);
softwareSerial.println(")");
digitalWrite(SPI_CS_PIN, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
while (1) {
int c = (int)error;
for (int i=0; i<c; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
delay(2000);
} ;
}