@@ -38,7 +38,7 @@ Testes with Arduino Nano Only.
3838
3939## Usage
4040
41- Example
41+ Upload the below sketch to work with CANHacker Windows:
4242
4343```
4444#include <can.h>
@@ -49,29 +49,80 @@ Example
4949#include <lib.h>
5050
5151#include <SPI.h>
52+ #include <SoftwareSerial.h>
5253
5354const int SPI_CS_PIN = 10;
5455const int INT_PIN = 2;
5556
57+ const int SS_RX_PIN = 3;
58+ const int SS_TX_PIN = 4;
59+
5660CanHackerLineReader *lineReader = NULL;
5761CanHacker *canHacker = NULL;
5862
63+ SoftwareSerial softwareSerial(SS_RX_PIN, SS_TX_PIN);
64+
5965void setup() {
6066 Serial.begin(115200);
6167 while (!Serial);
6268 SPI.begin();
69+ softwareSerial.begin(115200);
6370
6471 Stream *interfaceStream = &Serial;
72+ Stream *debugStream = &softwareSerial;
73+
6574
66- canHacker = new CanHacker(interfaceStream, NULL, SPI_CS_PIN);
75+ canHacker = new CanHacker(interfaceStream, debugStream, SPI_CS_PIN);
76+ //canHacker->enableLoopback(); // uncomment this for loopback
6777 lineReader = new CanHackerLineReader(canHacker);
6878
6979 pinMode(INT_PIN, INPUT);
7080}
7181
7282void loop() {
73- canHacker->processInterrupt();
74- lineReader->process();
83+ CanHacker::ERROR error;
84+
85+ if (digitalRead(INT_PIN) == LOW) {
86+ error = canHacker->processInterrupt();
87+ handleError(error);
88+ }
89+
90+ error = lineReader->process();
91+ handleError(error);
92+ }
93+
94+ void handleError(const CanHacker::ERROR error) {
95+
96+ switch (error) {
97+ case CanHacker::ERROR_OK:
98+ case CanHacker::ERROR_UNKNOWN_COMMAND:
99+ case CanHacker::ERROR_NOT_CONNECTED:
100+ case CanHacker::ERROR_MCP2515_ERRIF:
101+ case CanHacker::ERROR_INVALID_COMMAND:
102+ return;
103+
104+ default:
105+ break;
106+ }
107+
108+ softwareSerial.print("Failure (code ");
109+ softwareSerial.print((int)error);
110+ softwareSerial.println(")");
111+
112+ digitalWrite(SPI_CS_PIN, HIGH);
113+ pinMode(LED_BUILTIN, OUTPUT);
114+
115+ while (1) {
116+ int c = (int)error;
117+ for (int i=0; i<c; i++) {
118+ digitalWrite(LED_BUILTIN, HIGH);
119+ delay(500);
120+ digitalWrite(LED_BUILTIN, LOW);
121+ delay(500);
122+ }
123+
124+ delay(2000);
125+ } ;
75126}
76127```
77128
0 commit comments