TeensyNanoExoCode
Loading...
Searching...
No Matches
CAN.h
Go to the documentation of this file.
1/*
2 * Using the FlexCAN library included by Teensyduino.
3 * This class uses the singleton design pattern. Read about it here: https://www.tutorialspoint.com/Explain-Cplusplus-Singleton-design-pattern
4 */
5
6#ifndef CAN_H
7#define CAN_H
8
9#include "Logger.h"
10#include "Arduino.h"
11// Arduino compiles everything in the src folder even if not included so it causes and error for the nano if this is not included.
12#if defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY41)
13
14#include "FlexCAN_T4.h"
15#if defined(ARDUINO_TEENSY36)
16static FlexCAN_T4<CAN0, RX_SIZE_256, TX_SIZE_16> Can0;
17#elif defined(ARDUINO_TEENSY41)
18static FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can0;
19#endif
20
21class CAN
22{
23 public:
24 static CAN* getInstance()
25 {
26 static CAN* instance = new CAN;
27 return instance;
28 }
29
30 void send(CAN_message_t msg)
31 {
32 if(!Can0.write(msg))
33 {
34 logger::println("Error Sending" + String(msg.id), LogLevel::Error);
35 }
36 }
37
38 CAN_message_t read()
39 {
40 CAN_message_t msg;
41 Can0.read(msg);
42 return msg;
43 }
44
45 private:
46 CAN()
47 {
48 Can0.begin();
49 Can0.setBaudRate(1000000);
50 }
51};
52
53#endif
54#endif
void msg(float *data, int len)
Definition RealTimeI2C.cpp:55