From 41c01b6aae8545c5afebcd1d29b00fecc6b63be9 Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 8 Jun 2023 14:12:15 -0700 Subject: [PATCH] add new example --- .../.feather_m4_can.test.only | 0 .../feather_m4can_neotran.ino | 111 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 examples/feather_m4can_neotran/.feather_m4_can.test.only create mode 100644 examples/feather_m4can_neotran/feather_m4can_neotran.ino diff --git a/examples/feather_m4can_neotran/.feather_m4_can.test.only b/examples/feather_m4can_neotran/.feather_m4_can.test.only new file mode 100644 index 0000000..e69de29 diff --git a/examples/feather_m4can_neotran/feather_m4can_neotran.ino b/examples/feather_m4can_neotran/feather_m4can_neotran.ino new file mode 100644 index 0000000..dc8ce0a --- /dev/null +++ b/examples/feather_m4can_neotran/feather_m4can_neotran.ino @@ -0,0 +1,111 @@ +/* + * Adafruit Feather M4 CAN Transceiver Example + */ + +#include +#include + +CANSAME5x CAN; + +Adafruit_NeoPixel strip(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); + +#define MY_PACKET_ID 0xAF + +uint32_t timestamp; + +void setup() { + Serial.begin(115200); + //while (!Serial) delay(10); + + Serial.println("CAN NeoPixel Potentiometer RX/TX demo"); + + pinMode(PIN_CAN_STANDBY, OUTPUT); + digitalWrite(PIN_CAN_STANDBY, false); // turn off STANDBY + pinMode(PIN_CAN_BOOSTEN, OUTPUT); + digitalWrite(PIN_CAN_BOOSTEN, true); // turn on booster + + strip.begin(); + strip.setBrightness(50); + + // start the CAN bus at 250 kbps + if (!CAN.begin(250000)) { + Serial.println("Starting CAN failed!"); + while (1) delay(10); + } + + timestamp = millis(); +} + +void loop() { + // every 100 ms send out a packet + if ((millis() - timestamp) > 100) { + uint16_t pot = analogRead(A5); + // send a packet with the potentiometer value + Serial.print("Sending packet with value "); + Serial.print(pot); + + CAN.beginPacket(MY_PACKET_ID); + CAN.write(pot >> 8); + CAN.write(pot & 0xFF); + CAN.endPacket(); + + Serial.println("...sent!"); + timestamp = millis(); + } + + // try to parse any incoming 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); + + uint8_t receivedData[packetSize]; + for (int i=0; i