Different, less efficient way of checking if buffer overflowed

This commit is contained in:
redfast00 2020-08-27 03:29:16 +02:00
parent a62562b716
commit e5bf82352f
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
3 changed files with 22 additions and 14 deletions

View file

@ -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) {

View file

@ -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

View file

@ -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
}