diff --git a/lib/obus_can.cpp b/lib/obus_can.cpp index c923b93..d242019 100644 --- a/lib/obus_can.cpp +++ b/lib/obus_can.cpp @@ -74,7 +74,6 @@ void init() { mcp2515.reset(); mcp2515.setBitrate(CAN_50KBPS); mcp2515.setNormalMode(); - mcp2515.setRegister(MCP_CANINTE, CANINTF_ERRIF); } @@ -140,6 +139,9 @@ bool receive(struct message *msg) { return true; } +bool is_error_condition() { + return mcp2515.getInterrupts() & MCP2515::CANINTF_ERRIF; +} void send(struct message *msg) { if (!is_init) { diff --git a/lib/obus_can.h b/lib/obus_can.h index dee7d9e..2994a9c 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -77,7 +77,7 @@ void init(); /** * Receive a message * - * @param msg Pointer to memory where the received message will be wriitten + * @param msg Pointer to memory where the received message will be written * @return true if a message was received, false otherwise */ bool receive(struct message *msg); @@ -90,6 +90,8 @@ bool receive(struct message *msg); */ void send(struct message *msg); +bool is_error_condition(); + /** * For internal use only diff --git a/lib/obus_module.cpp b/lib/obus_module.cpp index acb13f1..34efbc7 100644 --- a/lib/obus_module.cpp +++ b/lib/obus_module.cpp @@ -12,13 +12,8 @@ namespace obus_module { struct obus_can::module this_module; uint8_t strike_count; bool running; -bool error; uint32_t time_stop_strike_led; -void interrupt_can_error() { - error = true; -} - void setup(uint8_t type, uint8_t id) { this_module.type = type; this_module.id = id; @@ -27,18 +22,22 @@ void setup(uint8_t type, uint8_t id) { strike_count = 0; running = false; - error = false; pinMode(RED_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT); digitalWrite(RED_LED, LOW); digitalWrite(GREEN_LED, LOW); - attachInterrupt(digitalPinToInterrupt(MCP_INT), interrupt_can_error, RISING); } bool loop(obus_can::message* message) { // Check if the message buffer overflowed - if (error) { - // Loop forever while blinking status led orange + if (time_stop_strike_led && time_stop_strike_led > millis()) { + digitalWrite(RED_LED, LOW); + } + // TODO this can be more efficient by only enabling error interrupts and + // reacting to the interrupt instead of checking if the flag is set in a loop + // We will need to fork our CAN library for this, because the needed functions + // are private + if (obus_can::is_error_condition()) { bool blink = false; while (true) { digitalWrite(RED_LED, blink); @@ -47,10 +46,15 @@ bool loop(obus_can::message* message) { delay(500); } } - if (time_stop_strike_led && time_stop_strike_led > millis()) { - digitalWrite(RED_LED, LOW); + bool received = receive(message); + if (received) { + if (message->msg_type == OBUS_MSGTYPE_C_GAMESTART) { + callback_game_start(); + } /* TODO extend this for all messages */ else { + return true; + } + return false; } - // TODO receive CAN frame and call callback functions }