{"id":24489791,"url":"https://github.com/ryleeharrison/canfetti-arduino","last_synced_at":"2026-05-04T16:37:36.369Z","repository":{"id":273305303,"uuid":"919234468","full_name":"RyLeeHarrison/CANfetti-arduino","owner":"RyLeeHarrison","description":"A lightweight library for cross-platform CAN communications on Teensy 4.x and ESP32.","archived":false,"fork":false,"pushed_at":"2025-01-20T04:29:43.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-20T05:26:17.634Z","etag":null,"topics":["arduino","canbus","communication-protocol","esp32s3","flexcan"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RyLeeHarrison.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-20T02:31:49.000Z","updated_at":"2025-01-20T04:29:45.000Z","dependencies_parsed_at":"2025-01-20T05:26:44.458Z","dependency_job_id":"57cc1c06-881c-481b-931f-4afbf2e2a968","html_url":"https://github.com/RyLeeHarrison/CANfetti-arduino","commit_stats":null,"previous_names":["ryleeharrison/canfetti-arduino"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyLeeHarrison%2FCANfetti-arduino","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyLeeHarrison%2FCANfetti-arduino/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyLeeHarrison%2FCANfetti-arduino/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyLeeHarrison%2FCANfetti-arduino/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RyLeeHarrison","download_url":"https://codeload.github.com/RyLeeHarrison/CANfetti-arduino/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243668920,"owners_count":20328171,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","canbus","communication-protocol","esp32s3","flexcan"],"created_at":"2025-01-21T17:15:55.389Z","updated_at":"2026-05-04T16:37:36.327Z","avatar_url":"https://github.com/RyLeeHarrison.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CANfetti :tada:\n\n**CANfetti** is a lightweight Arduino library designed to simplify CAN (Controller Area Network) communications across **Teensy 4.x** and **ESP32** platforms. Whether you're sending periodic messages or building robust data logging systems, CANfetti offers a unified API that masks platform complexities.\n\n---\n\n## Features\n\n1. **Cross-Platform CAN Handling**  \n   - Automatically detects **Teensy 4.x** (FlexCAN) or **ESP32** (TWAI) and configures the appropriate backend.\n\n2. **Simple Initialization**  \n   - Easily set up the CAN bus with a single function call, specifying your desired bit rate.\n\n3. **Versatile Frame Building**  \n   - Build standard or extended CAN frames, set the RTR flag, and manage up to 8 bytes of data.\n\n4. **Reliable Sending \u0026 Receiving**  \n   - Send and receive CAN frames with configurable timeouts. Includes built-in debug outputs for quick diagnostics.\n\n5. **Minimal Footprint**  \n   - Designed with efficiency in mind, using minimal resources while handling all core CAN operations.\n\n---\n\n## Installation\n\n1. **Library Manager (Recommended)**  \n   - In the Arduino IDE, go to **Sketch** \u003e **Include Library** \u003e **Manage Libraries...**  \n   - Search for **CANfetti** and click **Install**.\n\n2. **Manual Installation**  \n   - Clone or download [CANfetti-arduino](https://github.com/RyLeeHarrison/CANfetti-arduino) from GitHub.  \n   - Move the `CANfetti-arduino` folder into your `Documents/Arduino/libraries/` directory.\n\n---\n\n## API Breakdown\n\nThe **CANfetti** library offers three main classes:\n\n1. **CANfettiManager**: Manages the CAN controller (initialization, start/stop, send/receive).  \n2. **CANfettiFrame**: Represents a CAN message (ID, flags, data).  \n3. **CANfetti**: A helper class for building and customizing **CANfettiFrame** objects.\n\n### 1. **CANfettiManager Class**\n\nResponsible for initializing and managing CAN operations.\n\n- **`bool init(uint32_t bitrate = 500000)`**  \n  Initializes the CAN controller with the specified bit rate. Returns `true` if successful.\n\n- **`void stop()`**  \n  Stops the CAN controller, releasing any resources.\n\n- **`bool sendMessage(const CANfettiFrame\u0026 message)`**  \n  Sends a CAN message. Returns `true` on success.\n\n- **`bool receiveMessage(CANfettiFrame\u0026 message, uint32_t timeout = 0)`**  \n  Attempts to receive a CAN message. Optionally specify a `timeout` in milliseconds (ESP32 only). Returns `true` if a message was received.\n\n- **`bool isRunning() const`**  \n  Returns `true` if the CAN controller is currently running.\n\n### 2. **CANfettiFrame Class**\n\nA simple container for CAN message data.\n\n- **Members**  \n  - `uint32_t id`: The CAN ID (standard or extended).  \n  - `uint8_t len`: Length of the CAN data payload (0–8).  \n  - `uint8_t buf[8]`: The 8-byte CAN data buffer.  \n  - `struct flags { bool extended; bool remote; }`: Frame type flags:\n    - `extended` — set `true` for extended (29-bit) IDs.  \n    - `remote` — set `true` for remote transmission request (RTR) frames.\n\n### 3. **CANfetti Class**\n\nA utility class to build **CANfettiFrame** objects with a fluent interface.\n\n- **`CANfetti\u0026 setId(uint32_t id)`**  \n  Sets the CAN ID.\n\n- **`CANfetti\u0026 setExtendedFrame(bool isExtended)`**  \n  Marks the frame as extended or standard.\n\n- **`CANfetti\u0026 setRemoteTransmissionRequest(bool isRtr)`**  \n  Marks the frame as an RTR frame if `true`.\n\n- **`CANfetti\u0026 setDataLength(uint8_t length)`**  \n  Sets the length of the CAN data (up to 8).\n\n- **`CANfetti\u0026 setData(const uint8_t* data, uint8_t length)`**  \n  Copies the provided data into the frame buffer. Automatically caps at 8 bytes.\n\n- **`CANfettiFrame build() const`**  \n  Finalizes and returns the built frame.\n\n---\n\n## Supported Platforms\n\n- **Teensy 4.0 / 4.1**  \n  - Uses [FlexCAN_T4](https://github.com/tonton81/FlexCAN_T4).  \n  - Automatically handled via the **`USE_FLEXCAN`** define.\n\n- **ESP32**  \n  - Uses [TWAI driver](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html).  \n  - Automatically handled via the **`USE_TWAI`** define.\n\n\u003e **Note:** Other boards are currently **not** supported.\n\n---\n\n## Examples\n\nBelow are minimal examples to get you started. For more detailed use cases, check out the [examples folder](https://github.com/RyLeeHarrison/CANfetti-arduino/examples).\n\n### 1. Basic Initialization\n\n```cpp\n#include \u003cArduino.h\u003e\n#include \u003cCANfetti.hpp\u003e\n\nCANfettiManager canManager;\n\nvoid setup() {\n  Serial.begin(115200);\n\n  // Initialize CAN at 500 kbps\n  if (canManager.init(500000)) {\n    Serial.println(\"CAN initialized successfully!\");\n  } else {\n    Serial.println(\"Failed to initialize CAN.\");\n  }\n}\n\nvoid loop() {\n  // ...\n}\n```\n\n---\n\n### 2. Sending a CAN Message\n\n```cpp\n#include \u003cArduino.h\u003e\n#include \u003cCANfetti.hpp\u003e\n\nCANfettiManager canManager;\n\nvoid setup() {\n  Serial.begin(115200);\n  canManager.init(500000);\n}\n\nvoid loop() {\n  // Build a simple CAN message with ID 0x123 and 3 bytes of data\n  CANfettiFrame frame = CANfetti()\n                          .setId(0x123)\n                          .setDataLength(3)\n                          .setData((const uint8_t*)\"\\x01\\x02\\x03\", 3)\n                          .build();\n\n  bool success = canManager.sendMessage(frame);\n\n  Serial.println(success ? \"Message sent!\" : \"Message failed!\");\n\n  delay(1000);\n}\n```\n\n---\n\n### 3. Receiving a CAN Message\n\n```cpp\n#include \u003cArduino.h\u003e\n#include \u003cCANfetti.hpp\u003e\n\nCANfettiManager canManager;\nCANfettiFrame incomingFrame;\n\nvoid setup() {\n  Serial.begin(115200);\n  canManager.init(500000);\n}\n\nvoid loop() {\n  // Non-blocking check for a new CAN message\n  if (canManager.receiveMessage(incomingFrame)) {\n    Serial.print(\"Received CAN ID: 0x\");\n    Serial.println(incomingFrame.id, HEX);\n\n    Serial.print(\"Data Length: \");\n    Serial.println(incomingFrame.len);\n\n    Serial.print(\"Data Bytes: \");\n    for (int i = 0; i \u003c incomingFrame.len; i++) {\n      Serial.print(incomingFrame.buf[i], HEX);\n      Serial.print(\" \");\n    }\n    Serial.println();\n  }\n\n  // Continue with other tasks...\n  delay(10);\n}\n```\n\n---\n\n### 4. Sending an Extended Frame\n\n```cpp\n#include \u003cArduino.h\u003e\n#include \u003cCANfetti.hpp\u003e\n\nCANfettiManager canManager;\n\nvoid setup() {\n  Serial.begin(115200);\n  canManager.init(500000);\n}\n\nvoid loop() {\n  // Build an extended CAN message with ID 0x1ABCDE\n  CANfettiFrame frame = CANfetti()\n                          .setId(0x1ABCDE)\n                          .setExtendedFrame(true)\n                          .setDataLength(4)\n                          .setData((const uint8_t*)\"\\xAA\\xBB\\xCC\\xDD\", 4)\n                          .build();\n\n  canManager.sendMessage(frame);\n\n  delay(1000);\n}\n```\n\n---\n\n### 5. Stopping the CAN Interface\n\n```cpp\n#include \u003cArduino.h\u003e\n#include \u003cCANfetti.hpp\u003e\n\nCANfettiManager canManager;\n\nvoid setup() {\n  Serial.begin(115200);\n  if (canManager.init(500000)) {\n    Serial.println(\"CAN running...\");\n  }\n}\n\nvoid loop() {\n  // Stop CAN after 5 seconds\n  if (millis() \u003e 5000 \u0026\u0026 canManager.isRunning()) {\n    canManager.stop();\n    Serial.println(\"CAN has been stopped.\");\n  }\n}\n```\n\n---\n\n## Troubleshooting\n\n- **Message Not Sending**  \n  - Ensure your transceiver wiring is correct and that the bus has proper termination (120Ω resistor on each end).  \n  - Check that the bit rate matches all other devices on the CAN bus.\n\n- **Platform Unrecognized**  \n  - This library requires Teensy 4.x or ESP32. If you're on an unsupported board, compilation will fail with an error.\n\n- **Timeout or No Messages Received**  \n  - Confirm that another CAN node is actively sending data and that your wiring is correct.  \n  - On ESP32, verify the TX/RX pin assignments (`GPIO_NUM_9` / `GPIO_NUM_10` by default in the library, update as needed).\n\n---\n\n## License\n\nThis library is distributed under the MIT License. See the [LICENSE](https://github.com/RyLeeHarrison/CANfetti-arduino/blob/main/LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryleeharrison%2Fcanfetti-arduino","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryleeharrison%2Fcanfetti-arduino","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryleeharrison%2Fcanfetti-arduino/lists"}