Skip to content

Commit bb7bf8e

Browse files
committed
HC-05 Example
1 parent 868820b commit bb7bf8e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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; // 10 by default, 53 on mega
11+
const int INT_PIN = 2;
12+
13+
CanHackerLineReader *lineReader = NULL;
14+
CanHacker *canHacker = NULL;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while (!Serial);
19+
Serial1.begin(57600);
20+
while (!Serial1);
21+
SPI.begin();
22+
23+
Stream *interfaceStream = &Serial1;
24+
Stream *debugStream = &Serial;
25+
26+
27+
canHacker = new CanHacker(interfaceStream, debugStream, SPI_CS_PIN);
28+
canHacker->enableLoopback(); // uncomment this for loopback
29+
lineReader = new CanHackerLineReader(canHacker);
30+
31+
pinMode(INT_PIN, INPUT);
32+
}
33+
34+
void loop() {
35+
CanHacker::ERROR error;
36+
37+
if (digitalRead(INT_PIN) == LOW) {
38+
error = canHacker->processInterrupt();
39+
handleError(error);
40+
}
41+
42+
error = lineReader->process();
43+
handleError(error);
44+
}
45+
46+
void handleError(const CanHacker::ERROR error) {
47+
48+
switch (error) {
49+
case CanHacker::ERROR_OK:
50+
case CanHacker::ERROR_UNKNOWN_COMMAND:
51+
case CanHacker::ERROR_NOT_CONNECTED:
52+
case CanHacker::ERROR_MCP2515_ERRIF:
53+
case CanHacker::ERROR_INVALID_COMMAND:
54+
return;
55+
56+
default:
57+
break;
58+
}
59+
60+
Serial.print("Failure (code ");
61+
Serial.print((int)error);
62+
Serial.println(")");
63+
64+
digitalWrite(SPI_CS_PIN, HIGH);
65+
pinMode(LED_BUILTIN, OUTPUT);
66+
67+
while (1) {
68+
int c = (int)error;
69+
for (int i=0; i<c; i++) {
70+
digitalWrite(LED_BUILTIN, HIGH);
71+
delay(500);
72+
digitalWrite(LED_BUILTIN, LOW);
73+
delay(500);
74+
}
75+
76+
delay(2000);
77+
} ;
78+
}

0 commit comments

Comments
 (0)