From 14fce40f9f92a123d149d8d30110711e50bef9b9 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Tue, 25 Jan 2022 21:16:29 +0100 Subject: [PATCH] Only enable module if it has been acked --- lib/obus_module.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/obus_module.cpp b/lib/obus_module.cpp index a695987..9ef04de 100644 --- a/lib/obus_module.cpp +++ b/lib/obus_module.cpp @@ -19,6 +19,7 @@ namespace obus_module { struct obus_can::module this_module; uint8_t strike_count; bool active; +bool acked_after_last_hello = false; uint32_t next_loop_call_deadline; // Current LED status @@ -91,6 +92,7 @@ void _resetState() { strike_count = 0; active = false; next_loop_call_deadline = 0; + acked_after_last_hello = false; if (this_module.type == OBUS_TYPE_PUZZLE || this_module.type == OBUS_TYPE_NEEDY) { pinMode(PIN_LED_RED, OUTPUT); @@ -145,9 +147,11 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(uint8_t if (is_from_controller(message->from)) { switch (message->msg_type) { case OBUS_MSGTYPE_C_GAMESTART: - active = true; - _setLed(COLOR_YELLOW); - callback_game_start(message->gamestatus.puzzle_modules_left); + if (acked_after_last_hello) { + active = true; + _setLed(COLOR_YELLOW); + callback_game_start(message->gamestatus.puzzle_modules_left); + } break; case OBUS_MSGTYPE_C_HELLO: _resetState(); @@ -156,18 +160,23 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(uint8_t case OBUS_MSGTYPE_C_SOLVED: case OBUS_MSGTYPE_C_TIMEOUT: case OBUS_MSGTYPE_C_STRIKEOUT: - active = false; - _setLed(COLOR_OFF); - callback_game_stop(); + if (acked_after_last_hello) { + active = false; + _setLed(COLOR_OFF); + callback_game_stop(); + } break; case OBUS_MSGTYPE_C_ACK: + if (message->payload_address.type == this_module.type && message->payload_address.id == this_module.id) { + acked_after_last_hello = true; + } break; case OBUS_MSGTYPE_C_STATE: callback_state(message->gamestatus.time_left, message->gamestatus.strikes, message->gamestatus.max_strikes, message->gamestatus.puzzle_modules_left); break; case OBUS_MSGTYPE_C_INFOSTART: // Add module type and id to seed, to remove correlation in randomness between modules - uinst32_t seed = message->infostart.seed + ((uint32_t) this_module.type << 8) + ((uint32_t) this_module.id); + uint32_t seed = message->infostart.seed + ((uint32_t) this_module.type << 8) + ((uint32_t) this_module.id); // randomSeed has no effect when called with 0 as seed, so we use // a fallback value that is unlikely to collide with other frequently used seeds if (seed == 0) {