Add deadline for loop function so users are forced to call it frequently

This commit is contained in:
redfast00 2021-02-01 17:00:15 +01:00
parent b7c2e85682
commit 07fd7ed1c8
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
3 changed files with 25 additions and 11 deletions

View file

@ -8,6 +8,8 @@
#define BLINK_DELAY_NORMAL 500
#define BLINK_DELAY_FAST 300
#define MAX_TIME_BETWEEN_CALLS 100
// Not used normally
#define MCP_INT 2
@ -21,6 +23,7 @@ namespace obus_module {
struct obus_can::module this_module;
uint8_t strike_count;
bool active;
uint32_t next_loop_call_deadline;
// Current LED status
struct color { bool red; bool green; };
@ -81,6 +84,7 @@ void _setLedBlink(struct color color, uint16_t delay) {
void _resetState() {
strike_count = 0;
active = false;
next_loop_call_deadline = 0;
if (this_module.type == OBUS_TYPE_PUZZLE || this_module.type == OBUS_TYPE_NEEDY) {
pinMode(RED_LED, OUTPUT);
@ -113,21 +117,31 @@ void empty_callback_state(uint32_t time_left, uint8_t strikes, uint8_t max_strik
(void)puzzle_modules_solved;
}
void blink_error(String message) {
bool blink = false;
while (true) {
digitalWrite(RED_LED, blink);
digitalWrite(GREEN_LED, blink);
blink = !blink;
delay(BLINK_DELAY_NORMAL);
Serial.println(message);
}
}
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]), void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved)) {
// 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.
// Also, we can't do this by default, because the INT pin is normally not connected to the board
if (obus_can::is_error_condition()) {
bool blink = false;
while (true) {
digitalWrite(RED_LED, blink);
digitalWrite(GREEN_LED, blink);
blink = !blink;
delay(BLINK_DELAY_NORMAL);
}
blink_error(F("E CAN error"));
}
if (next_loop_call_deadline != 0 && millis() > next_loop_call_deadline) {
blink_error(F("E missed deadline"));
}
next_loop_call_deadline = millis() + MAX_TIME_BETWEEN_CALLS;
bool received_message = false;
if (obus_can::receive(message)) {
received_message = true;

View file

@ -18,7 +18,7 @@ uint8_t shiftInFixed(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
else
value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, HIGH);
delay(1);
delayMicroseconds(3);
digitalWrite(clockPin, LOW);
}
return value;
@ -50,7 +50,7 @@ void loop() // run over and over
{
// read data in
digitalWrite(READ_PIN, HIGH);
delay(200);
delayMicroseconds(3);
digitalWrite(READ_PIN, LOW);
// shift data bit by bit

View file

@ -82,7 +82,7 @@ uint8_t read_value_from_date_module(uint8_t bit_order) {
else
value |= read_bit << (7 - i);
digitalWrite(DATE_CLOCK_PIN, HIGH);
delay(1);
delayMicroseconds(3);
digitalWrite(DATE_CLOCK_PIN, LOW);
}
return value;
@ -90,7 +90,7 @@ uint8_t read_value_from_date_module(uint8_t bit_order) {
void read_from_date_module(uint8_t* data_out) {
digitalWrite(DATE_READ_PIN, HIGH);
delay(200);
delayMicroseconds(3);
digitalWrite(DATE_READ_PIN, LOW);
for (int i = 0; i < 4; i++) {