Different, less efficient way of checking if buffer overflowed
This commit is contained in:
parent
a62562b716
commit
e5bf82352f
|
@ -74,7 +74,6 @@ void init() {
|
||||||
mcp2515.reset();
|
mcp2515.reset();
|
||||||
mcp2515.setBitrate(CAN_50KBPS);
|
mcp2515.setBitrate(CAN_50KBPS);
|
||||||
mcp2515.setNormalMode();
|
mcp2515.setNormalMode();
|
||||||
mcp2515.setRegister(MCP_CANINTE, CANINTF_ERRIF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,6 +139,9 @@ bool receive(struct message *msg) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_error_condition() {
|
||||||
|
return mcp2515.getInterrupts() & MCP2515::CANINTF_ERRIF;
|
||||||
|
}
|
||||||
|
|
||||||
void send(struct message *msg) {
|
void send(struct message *msg) {
|
||||||
if (!is_init) {
|
if (!is_init) {
|
||||||
|
|
|
@ -77,7 +77,7 @@ void init();
|
||||||
/**
|
/**
|
||||||
* Receive a message
|
* 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
|
* @return true if a message was received, false otherwise
|
||||||
*/
|
*/
|
||||||
bool receive(struct message *msg);
|
bool receive(struct message *msg);
|
||||||
|
@ -90,6 +90,8 @@ bool receive(struct message *msg);
|
||||||
*/
|
*/
|
||||||
void send(struct message *msg);
|
void send(struct message *msg);
|
||||||
|
|
||||||
|
bool is_error_condition();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For internal use only
|
* For internal use only
|
||||||
|
|
|
@ -12,13 +12,8 @@ namespace obus_module {
|
||||||
struct obus_can::module this_module;
|
struct obus_can::module this_module;
|
||||||
uint8_t strike_count;
|
uint8_t strike_count;
|
||||||
bool running;
|
bool running;
|
||||||
bool error;
|
|
||||||
uint32_t time_stop_strike_led;
|
uint32_t time_stop_strike_led;
|
||||||
|
|
||||||
void interrupt_can_error() {
|
|
||||||
error = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup(uint8_t type, uint8_t id) {
|
void setup(uint8_t type, uint8_t id) {
|
||||||
this_module.type = type;
|
this_module.type = type;
|
||||||
this_module.id = id;
|
this_module.id = id;
|
||||||
|
@ -27,18 +22,22 @@ void setup(uint8_t type, uint8_t id) {
|
||||||
|
|
||||||
strike_count = 0;
|
strike_count = 0;
|
||||||
running = false;
|
running = false;
|
||||||
error = false;
|
|
||||||
pinMode(RED_LED, OUTPUT);
|
pinMode(RED_LED, OUTPUT);
|
||||||
pinMode(GREEN_LED, OUTPUT);
|
pinMode(GREEN_LED, OUTPUT);
|
||||||
digitalWrite(RED_LED, LOW);
|
digitalWrite(RED_LED, LOW);
|
||||||
digitalWrite(GREEN_LED, LOW);
|
digitalWrite(GREEN_LED, LOW);
|
||||||
attachInterrupt(digitalPinToInterrupt(MCP_INT), interrupt_can_error, RISING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loop(obus_can::message* message) {
|
bool loop(obus_can::message* message) {
|
||||||
// Check if the message buffer overflowed
|
// Check if the message buffer overflowed
|
||||||
if (error) {
|
if (time_stop_strike_led && time_stop_strike_led > millis()) {
|
||||||
// Loop forever while blinking status led orange
|
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;
|
bool blink = false;
|
||||||
while (true) {
|
while (true) {
|
||||||
digitalWrite(RED_LED, blink);
|
digitalWrite(RED_LED, blink);
|
||||||
|
@ -47,10 +46,15 @@ bool loop(obus_can::message* message) {
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (time_stop_strike_led && time_stop_strike_led > millis()) {
|
bool received = receive(message);
|
||||||
digitalWrite(RED_LED, LOW);
|
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
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue