/* CAN Bus Monitor - see http://www.technoblogy.com/show?4H7H David Johnson-Davies - www.technoblogy.com - 30th August 2023 CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include #define TFT_CS 32 #define TFT_DC 14 #define TFT_RST -1 #include // Core graphics library #include // Hardware-specific library for ST7789 Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST); void setup() { tft.init(135, 240); tft.setRotation(1); tft.fillScreen(ST77XX_BLACK); CAN.setPins(RX, TX); // Start the CAN bus at 500 kbps if (!CAN.begin(500000)) { tft.print("Starting CAN failed!"); while (1); } tft.print("No PacketID Data Hexadecimal"); tft.setCursor(15*6 + 7*14, 0); tft.println("ASCII"); } int Line = 0; void loop() { uint8_t packet[8]; int packetsize = CAN.parsePacket(); if (packetsize) { // Received a packet for (int i=0; i<8; i++) { if (CAN.available()) packet[i] = CAN.read(); else packet[i] = 0; } if (!CAN.packetRtr()) { // Not RTR; packet contains data tft.printf("%2d %8x", Line++, (uint32_t)CAN.packetId()); for (int i=0; i<8; i++) { // Print data in hexadecimal tft.setCursor(12*6 + i*14, Line*8); if (i < packetsize) tft.printf("%02x", packet[i]); else tft.print(" "); } tft.print(' '); for (int i=0; i<8; i++) { // Print data in ASCII char c = packet[i]; if (c>=32 && c<=127) tft.print(c); else tft.print('.'); } } else { // RTR packet; show DLC tft.printf("%2d %8x %d", Line++, (uint32_t)CAN.packetId(), CAN.packetDlc()); } tft.println(); } if (Line == 16) for(;;); }