Skip to content

Commit d0f9914

Browse files
committed
Examples
1 parent 6d7d15a commit d0f9914

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <can.h>
2+
#include <mcp2515.h>
3+
4+
#include <CanHacker.h>
5+
#include <CanHackerLineReader.h>
6+
#include <lib.h>
7+
8+
#include <SPI.h>
9+
#include <SoftwareSerial.h>
10+
11+
const int SPI_CS_PIN = 10;
12+
const int INT_PIN = 2;
13+
14+
const int SS_RX_PIN = 3;
15+
const int SS_TX_PIN = 4;
16+
17+
CanHackerLineReader *lineReader = NULL;
18+
CanHacker *canHacker = NULL;
19+
20+
SoftwareSerial softwareSerial(SS_RX_PIN, SS_TX_PIN);
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
SPI.begin();
25+
softwareSerial.begin(115200);
26+
27+
Stream *interfaceStream = &Serial;
28+
Stream *debugStream = &softwareSerial;
29+
30+
31+
canHacker = new CanHacker(interfaceStream, debugStream, SPI_CS_PIN);
32+
canHacker->setLoopbackEnabled(true);
33+
lineReader = new CanHackerLineReader(canHacker);
34+
35+
pinMode(INT_PIN, INPUT);
36+
}
37+
38+
void loop() {
39+
if (digitalRead(INT_PIN) == LOW) {
40+
CanHacker::ERROR error = canHacker->processInterrupt();
41+
handleError(error);
42+
}
43+
}
44+
45+
void serialEvent() {
46+
CanHacker::ERROR error = lineReader->process();
47+
handleError(error);
48+
}
49+
50+
void handleError(const CanHacker::ERROR error) {
51+
52+
switch (error) {
53+
case CanHacker::ERROR_OK:
54+
case CanHacker::ERROR_UNKNOWN_COMMAND:
55+
case CanHacker::ERROR_NOT_CONNECTED:
56+
case CanHacker::ERROR_MCP2515_ERRIF:
57+
case CanHacker::ERROR_INVALID_COMMAND:
58+
return;
59+
60+
default:
61+
break;
62+
}
63+
64+
softwareSerial.print("Failure (code ");
65+
softwareSerial.print((int)error);
66+
softwareSerial.println(")");
67+
68+
while (1) {
69+
delay(2000);
70+
} ;
71+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <can.h>
2+
#include <mcp2515.h>
3+
4+
#include <CanHacker.h>
5+
#include <CanHackerLineReader.h>
6+
#include <lib.h>
7+
8+
#include <SPI.h>
9+
10+
const int SPI_CS_PIN = 10;
11+
const int INT_PIN = 2;
12+
13+
CanHackerLineReader *lineReader = NULL;
14+
CanHacker *canHacker = NULL;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
SPI.begin();
19+
20+
canHacker = new CanHacker(&Serial, NULL, SPI_CS_PIN);
21+
lineReader = new CanHackerLineReader(canHacker);
22+
23+
pinMode(INT_PIN, INPUT);
24+
}
25+
26+
void loop() {
27+
if (digitalRead(INT_PIN) == LOW) {
28+
canHacker->processInterrupt();
29+
}
30+
}
31+
32+
void serialEvent() {
33+
lineReader->process();
34+
}

0 commit comments

Comments
 (0)