18#define CAN_BAUD 1000000
58 void attach (Callback<
void()> func, CAN::IrqType type = CAN::IrqType::RxIrq){
59 can.attach(func,type);
79 exact_.push_back(exact_id);
90 RangeCallback range_ids = {start_id, end_id, std::move(func)};
91 range_.push_back(range_ids);
100 while(
can.read(rxMsg)) {
106 for (
auto callbacks :
exact_) {
107 if (rxMsg.id == callbacks.id) {
108 callbacks.func(&rxMsg);
113 for (
auto callbacks :
range_) {
114 if (rxMsg.id >= callbacks.start_id && rxMsg.id <= callbacks.end_id) {
115 callbacks.func(&rxMsg);
130 if (
can.read(rxMsg)) {
131 int err =
can.rderror();
133 printf(
"[%d CAN Read Errors] on CANBus_%d\n", err, canbus + 1);
138 for(
int i = 0; i < 8; i ++){
148 bool rawRead(
int *
id, uint8_t bytes[],
int length){
152 if (
can.read(rxMsg)) {
153 int err =
can.rderror();
155 printf(
"[%d CAN Read Errors]\n", err);
160 for(
int i = 0; i < rxMsg.len; i ++){
177 printf(
" ID = 0x%.3x\t", msg.id);
178 for (
int i = 0; i < msg.len; i++)
179 printf(
" 0x%.2X", msg.data[i]);
190 bool rawSend(
int id, int8_t bytes[],
int length = 8){
191 static int errorCount = 0;
195 for(
int i = 0; i < length; i++){
196 txMsg << int8_t(bytes[i]);
201 bool isWrite =
can.write(txMsg);
211 if (errorCount > 1000){
#define CAN_BAUD
__/ _ / // (_-</ -_)\ \/ _ \/ _/ _ ‘/ _ \/ -_) __/ __/ / /__/ _ \/ ’ \/ -_|_-</ /_/ / _ \/ __/ -_)/ /...
Definition CANHandler.h:18
Definition CANHandler.h:33
CAN can
Definition CANHandler.h:39
CANBus
Definition CANHandler.h:45
@ CANBUS_2
Definition CANHandler.h:45
@ CANBUS_1
Definition CANHandler.h:45
@ NOBUS
Definition CANHandler.h:45
void attach(Callback< void()> func, CAN::IrqType type=CAN::IrqType::RxIrq)
Definition CANHandler.h:58
void updateCANs(PinName canRx, PinName canTx)
Definition CANHandler.h:62
static void printMsg(CANMessage &msg)
Prints a CANMessage nicely.
Definition CANHandler.h:175
std::vector< ExactCallback > exact_
Definition CANHandler.h:42
CANHandler(PinName canRx, PinName canTx)
Definition CANHandler.h:54
void registerCallback(uint32_t id, std::function< void(const CANMsg *)> func)
Add a callback for a specific id.
Definition CANHandler.h:77
std::vector< RangeCallback > range_
Definition CANHandler.h:43
void readAllCan()
Get feedback from all CAN devices, and send to relevant callbacks.
Definition CANHandler.h:97
CANHandler()
Definition CANHandler.h:50
void registerCallback(uint32_t start_id, uint32_t end_id, std::function< void(const CANMsg *)> func)
Add a callback for multiple ids.
Definition CANHandler.h:89
bool rawRead(int *id, uint8_t bytes[], int length)
Definition CANHandler.h:148
bool rawSend(int id, int8_t bytes[], int length=8)
Raw sending of CAN Messages.
Definition CANHandler.h:190
bool getFeedback(int *id, uint8_t bytes[], int canbus)
Get feedback back from the motor.
Definition CANHandler.h:126
CAN * getCAN()
Definition CANHandler.h:67
bool exists
Definition CANHandler.h:47
void clear(void)
Definition CANMsg.h:35
Definition CANHandler.h:21
uint32_t id
Definition CANHandler.h:22
std::function< void(const CANMsg *)> func
Definition CANHandler.h:23
Definition CANHandler.h:27
uint32_t start_id
Definition CANHandler.h:28
uint32_t end_id
Definition CANHandler.h:29
std::function< void(const CANMsg *)> func
Definition CANHandler.h:30