From 6437b2ab60f35b7ed1519975616a6c775c42e80e Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 26 Oct 2021 21:08:03 -0400 Subject: [PATCH] canbus baby --- README.md | 12 ++++- platformio.ini | 5 +- src/main2.cpp | 24 --------- src/main_canbus.cpp | 71 +++++++++++++++++++++++++++ src/main_canbus_send.cpp | 73 ++++++++++++++++++++++++++++ src/{main3.cpp => main_cap_test.cpp} | 0 src/main_switch_interrupt.cpp | 43 ++++++++++++++++ src/main_switch_led.cpp | 27 ++++++++++ 8 files changed, 229 insertions(+), 26 deletions(-) delete mode 100644 src/main2.cpp create mode 100644 src/main_canbus.cpp create mode 100644 src/main_canbus_send.cpp rename src/{main3.cpp => main_cap_test.cpp} (100%) create mode 100644 src/main_switch_interrupt.cpp create mode 100644 src/main_switch_led.cpp diff --git a/README.md b/README.md index 42ccdf9..fc2b434 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ -# ESP32 Playgroung +# ESP32 Playground +## Setting up the Debug + +You need the following in your `platformio.ini` order to get the debugger co + +``` +debug_tool = esp-prog +debug_init_break = tbreak setup +``` + +## Pinout Diagrams ![ESP32_NodeMCU](hiletgo_esp32_pinout.jpg) \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 08e5827..9cbabb2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,4 +19,7 @@ debug_init_break = tbreak setup ; I am using src_filter as a cheap way to have multiple main files in one project. ; To use it all you have to do is add the target main file like here we add main3.cpp as out build target: ; src_filter =+<*> - + -src_filter =+<*> - + \ No newline at end of file +src_filter =+<*> - + +lib_deps = + # The exact version + sandeepmistry/CAN @ 0.3.1 \ No newline at end of file diff --git a/src/main2.cpp b/src/main2.cpp deleted file mode 100644 index c8db78f..0000000 --- a/src/main2.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -#define LED_BUILTIN 2 -int delayTime = 100; - -void setup() -{ - // put your setup code here, to run once: - pinMode(LED_BUILTIN, OUTPUT); - Serial.begin(115200); -} - -void loop() -{ - // put your main code here, to run repeatedly: - Serial.println("Starting loop"); - digitalWrite(LED_BUILTIN, HIGH); - delay(delayTime); - digitalWrite(LED_BUILTIN, LOW); - Serial.println("Turning off LED"); - printf("This is main2.cpp!\n"); - delay(delayTime); -} \ No newline at end of file diff --git a/src/main_canbus.cpp b/src/main_canbus.cpp new file mode 100644 index 0000000..aa40214 --- /dev/null +++ b/src/main_canbus.cpp @@ -0,0 +1,71 @@ +// Copyright (c) Sandeep Mistry. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +#include +#include + +#define CAN_RX 22 +#define CAN_TX 21 + +void setup() +{ + CAN.setPins(CAN_RX, CAN_TX); + Serial.begin(115200); + while (!Serial) + ; + delay(10000); + Serial.println("CAN Receiver"); + + // start the CAN bus at 500 kbps + if (!CAN.begin(500E3)) + { + Serial.println("Starting CAN failed!"); + while (1) + ; + } +} + +void loop() +{ + // try to parse packet + int packetSize = CAN.parsePacket(); + + if (packetSize) + { + // received a packet + Serial.print("Received "); + + if (CAN.packetExtended()) + { + Serial.print("extended "); + } + + if (CAN.packetRtr()) + { + // Remote transmission request, packet contains no data + Serial.print("RTR "); + } + + Serial.print("packet with id 0x"); + Serial.print(CAN.packetId(), HEX); + + if (CAN.packetRtr()) + { + Serial.print(" and requested length "); + Serial.println(CAN.packetDlc()); + } + else + { + Serial.print(" and length "); + Serial.println(packetSize); + + // only print packet data for non-RTR packets + while (CAN.available()) + { + Serial.print((char)CAN.read()); + } + Serial.println(); + } + + Serial.println(); + } +} \ No newline at end of file diff --git a/src/main_canbus_send.cpp b/src/main_canbus_send.cpp new file mode 100644 index 0000000..063e304 --- /dev/null +++ b/src/main_canbus_send.cpp @@ -0,0 +1,73 @@ +#include +#include + +#define LED_BUILTIN 2 +#define CAN_RX 22 +#define CAN_TX 21 + +int result = 0; + +void setup() +{ + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); + CAN.setPins(CAN_RX, CAN_TX); + Serial.begin(115200); + while (!Serial) + ; + delay(10000); + Serial.println("CAN Sender"); + + // start the CAN bus at 500 kbps + if (!CAN.begin(500E3)) + { + Serial.println("Starting CAN failed!"); + while (1) + ; + } +} + +void loop() +{ + // send packet: id is 11 bits, packet can contain up to 8 bytes of data + Serial.print("Sending packet ... "); + + result = CAN.beginPacket(0x12); + if (result) + { + CAN.write('h'); + CAN.write('e'); + CAN.write('l'); + CAN.write('l'); + CAN.write('o'); + CAN.endPacket(); + Serial.println("done"); + } + else + { + Serial.println("There was a probelm beginning the packet."); + } + + delay(1000); + + // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data + Serial.print("Sending extended packet ... "); + + result = CAN.beginExtendedPacket(0xabcdef); + if (result) + { + CAN.write('h'); + CAN.write('e'); + CAN.write('l'); + CAN.write('l'); + CAN.write('o'); + CAN.endPacket(); + Serial.println("done"); + } + else + { + Serial.println("There was a probelm beginning the extended packet."); + } + + delay(1000); +} diff --git a/src/main3.cpp b/src/main_cap_test.cpp similarity index 100% rename from src/main3.cpp rename to src/main_cap_test.cpp diff --git a/src/main_switch_interrupt.cpp b/src/main_switch_interrupt.cpp new file mode 100644 index 0000000..ee9dd63 --- /dev/null +++ b/src/main_switch_interrupt.cpp @@ -0,0 +1,43 @@ +/* +Eample of using an interrupt to set global state. which is acted on during the loop. +Place a switch on Pin 4. +*/ +#include + +#define LED_BUILTIN 2 + +volatile bool switch_on = false; +volatile u_int switch_counter = 0; + +/* + * Interrupt Callback used to track switch state + */ +void switch_state() +{ + switch_on = !switch_on; + switch_counter = ++switch_counter; +} + +void setup() +{ + pinMode(LED_BUILTIN, OUTPUT); + pinMode(4, INPUT_PULLUP); + Serial.begin(115200); + int interrupt_pin_num = digitalPinToInterrupt(4); + printf("The interrupt number for pin 4 is: %d", interrupt_pin_num); + attachInterrupt(interrupt_pin_num, switch_state, FALLING); +} + +void loop() +{ + printf("Our counter is at: %d\n", switch_counter); + if (switch_on) + { + digitalWrite(LED_BUILTIN, HIGH); + } + else + { + digitalWrite(LED_BUILTIN, LOW); + } + delay(100); +} \ No newline at end of file diff --git a/src/main_switch_led.cpp b/src/main_switch_led.cpp new file mode 100644 index 0000000..25f41d4 --- /dev/null +++ b/src/main_switch_led.cpp @@ -0,0 +1,27 @@ +/* +A simple example of using switch to toggle the led builtin. +*/ +#include + +#define LED_BUILTIN 2 +#define SWITCH_INPUT 4 + +void setup() +{ + // put your setup code here, to run once: + pinMode(LED_BUILTIN, OUTPUT); + pinMode(SWITCH_INPUT, INPUT_PULLUP); +} + +void loop() +{ + if (digitalRead(SWITCH_INPUT)) + { + digitalWrite(LED_BUILTIN, HIGH); + } + else + { + digitalWrite(LED_BUILTIN, LOW); + } + delay(500); +} \ No newline at end of file