From 2c994f2c60467223c94407822fbe91212be4cef1 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Fri, 11 Sep 2020 21:55:43 +0200 Subject: [PATCH 1/6] Add info phase in controller --- lib/obus_can.h | 9 +++++++++ src/controller/controller.ino | 33 +++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/obus_can.h b/lib/obus_can.h index 1f70d99..a07574d 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -203,6 +203,15 @@ inline void send_c_timeout( /** * Send a controller "strikeout" OBUS message */ +inline void send_c_infostart(struct module from) { + assert(from.type == OBUS_TYPE_CONTROLLER); + struct message msg = _msg(from, false, OBUS_MSGTYPE_C_INFOSTART); + send(&msg); +} + +/** + * Send a controller "info start" OBUS message + */ inline void send_c_strikeout( struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { diff --git a/src/controller/controller.ino b/src/controller/controller.ino index b0a4008..2d86a71 100644 --- a/src/controller/controller.ino +++ b/src/controller/controller.ino @@ -3,19 +3,22 @@ #define STATE_INACTIVE 0 -#define STATE_HELLO 1 -#define STATE_GAME 2 -#define STATE_GAMEOVER 3 +#define STATE_INFO 1 +#define STATE_HELLO 2 +#define STATE_GAME 3 +#define STATE_GAMEOVER 4 #define OBUS_MAX_STRIKES 3 // Number of strikes allowed until game over #define OBUS_GAME_DURATION 60 // Duration of the game in seconds #define OBUS_MAX_MODULES 16 +#define OBUS_INFO_DURATION 3 // Duration of discovery round in seconds #define OBUS_DISC_DURATION 5 // Duration of discovery round in seconds #define OBUS_UPDATE_INTERVAL 500 // Number of milliseconds between game updates #define OBUS_GAME_DURATION_MS ((uint32_t) OBUS_GAME_DURATION*1000) #define OBUS_DISC_DURATION_MS ((uint32_t) OBUS_DISC_DURATION*1000) +#define OBUS_INFO_DURATION_MS ((uint32_t) OBUS_INFO_DURATION*1000) #define DIVIDE_CEIL(dividend, divisor) ((dividend + (divisor - 1)) / divisor) #define MAX_AMOUNT_PUZZLES 256 // The ID of a puzzle is uint8 @@ -34,6 +37,7 @@ uint8_t unsolved_puzzles[N_UNSOLVED_PUZZLES]; // Timers uint32_t hello_round_start; +uint32_t info_round_start; uint32_t game_start; uint32_t last_draw; uint32_t last_update; @@ -85,6 +89,23 @@ void solve_puzzle_in_bit_vector(uint8_t module_id) { unsolved_puzzles[byte_index] &= ~(0x1 << bit_index); } +void start_info() { + state = STATE_INFO; + info_round_start = millis(); + obus_can::send_c_infostart(this_module); + Serial.println(F(" Start of info round")); + tm.displayText("InFO"); +} + +void wait_info() { + struct obus_can::message msg; + obus_can::receive(&msg); + + if (millis() - info_round_start > OBUS_INFO_DURATION_MS) { + start_hello(); + } +} + void start_hello() { state = STATE_HELLO; @@ -258,7 +279,11 @@ void game_loop() { void loop() { switch (state) { case STATE_INACTIVE: - start_hello(); + start_info(); + break; + + case STATE_INFO: + wait_info(); break; case STATE_HELLO: From b7fd5b404a3d12a31c15386f26b67cd0bfc97b23 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Fri, 11 Sep 2020 21:10:47 +0200 Subject: [PATCH 2/6] Add ACK address to protocol, implement in obus_can --- docs/protocol.txt | 6 ++++-- lib/obus_can.cpp | 16 ++++++++++++++++ lib/obus_can.h | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/protocol.txt b/docs/protocol.txt index 3c2c543..b1b5185 100644 --- a/docs/protocol.txt +++ b/docs/protocol.txt @@ -38,8 +38,10 @@ Types for controller: - 0 acknowledge valid message [ X B B B B B B B ] - -------------- - reserved + - - ---------- + | ↓ reserved + ↓ module ID + type - 1 hello [ X B B B B B B B ] diff --git a/lib/obus_can.cpp b/lib/obus_can.cpp index 3d851be..51b6b51 100644 --- a/lib/obus_can.cpp +++ b/lib/obus_can.cpp @@ -36,6 +36,7 @@ uint8_t payload_type(uint8_t module_type, uint8_t module_id, uint8_t msg_type) { if (module_type == OBUS_TYPE_CONTROLLER && module_id == OBUS_CONTROLLER_ID) { switch (msg_type) { case OBUS_MSGTYPE_C_ACK: + return OBUS_PAYLDTYPE_MODULEADDR; case OBUS_MSGTYPE_C_HELLO: return OBUS_PAYLDTYPE_EMPTY; @@ -140,6 +141,16 @@ bool receive(struct message *msg) { msg->infomessage.len = data_len; } break; + case OBUS_PAYLDTYPE_MODULEADDR: + { + if (receive_frame.can_dlc < 3) { + Serial.println(F("W Received illegal count msg: payload <3")); + return false; + } + msg->payload_address.type = receive_frame.data[1]; + msg->payload_address.id = receive_frame.data[2]; + } + break; default: Serial.println(F("W Couldn't determine payload type")); return false; @@ -194,6 +205,11 @@ void send(struct message *msg) { length = msg->infomessage.len + 1; break; + case OBUS_PAYLDTYPE_MODULEADDR: + send_frame.data[1] = msg->payload_address.type; + send_frame.data[2] = msg->payload_address.id; + break; + default: Serial.print(F("E Unknown payload type ")); Serial.println(pyld_type); diff --git a/lib/obus_can.h b/lib/obus_can.h index a07574d..94f527a 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -35,6 +35,7 @@ #define OBUS_PAYLDTYPE_GAMESTATUS 1 #define OBUS_PAYLDTYPE_COUNT 2 #define OBUS_PAYLDTYPE_INFO 3 +#define OBUS_PAYLDTYPE_MODULEADDR 4 #define OBUS_PAYLD_INFO_MAXLEN (OBUS_MSG_LENGTH - 1) @@ -66,6 +67,7 @@ struct message { struct payld_gamestatus gamestatus; uint8_t count; struct payld_infomessage infomessage; + struct module payload_address; }; }; From bfcdd19e9b60a78d55defc4c58704a97c54eb82c Mon Sep 17 00:00:00 2001 From: redfast00 Date: Fri, 11 Sep 2020 21:14:40 +0200 Subject: [PATCH 3/6] Implement sending controller ACKs --- lib/obus_can.h | 3 ++- src/controller/controller.ino | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/obus_can.h b/lib/obus_can.h index 94f527a..149ad78 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -142,9 +142,10 @@ inline void _send_payld_gamestatus( /** * Send a controller "ACK" OBUS message */ -inline void send_c_ack(struct module from) { +inline void send_c_ack(struct module from, struct module payload_address) { assert(from.type == OBUS_TYPE_CONTROLLER); struct message msg = _msg(from, false, OBUS_MSGTYPE_C_ACK); + msg.payload_address = payload_address; send(&msg); } diff --git a/src/controller/controller.ino b/src/controller/controller.ino index 2d86a71..d439ec9 100644 --- a/src/controller/controller.ino +++ b/src/controller/controller.ino @@ -157,7 +157,7 @@ void receive_hello() { Serial.println(F("W Max # modules reached")); } - obus_can::send_c_ack(this_module); + obus_can::send_c_ack(this_module, msg.from); Serial.println(" ACK"); } From ff272fc841b2114c184338441b826c9e0287ae9f Mon Sep 17 00:00:00 2001 From: redfast00 Date: Fri, 11 Sep 2020 21:20:01 +0200 Subject: [PATCH 4/6] Parse ACKs in debugger --- debugging_tool/server.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/debugging_tool/server.py b/debugging_tool/server.py index 4a580bb..c8984e2 100644 --- a/debugging_tool/server.py +++ b/debugging_tool/server.py @@ -42,8 +42,9 @@ class Message: def sender_id(self): return (self.received_from >> 0) & 0b1111_1111 - def human_readable_type(self): - return [('controller' if self.sender_id() == 0 else 'info'), 'puzzle', 'needy', 'RESERVED TYPE'][self.sender_type()] + @staticmethod + def human_readable_type(sender_type, sender_id): + return [('controller' if sender_id == 0 else 'info'), 'puzzle', 'needy', 'RESERVED TYPE'][sender_type] def _parse_state_update(self): timeleft = self.payload[1] << 0x18 | self.payload[2] << 0x10 | self.payload[3] << 0x08 | self.payload[4] @@ -58,7 +59,7 @@ class Message: try: if sender_type == 0b00 and self.sender_id() == 0: # controller if message_type == 0: - return "ACK" + return f"ACK {Message.human_readable_type(self.payload[1], self.payload[2])} {self.payload[2]}" elif message_type == 1: return "HELLO" elif message_type == 2: @@ -99,7 +100,7 @@ class Message: 'parsed': self.parse_message(), 'pretty_raw_sender_id': f'{self.priority_bit():01b} {self.sender_type():02b} {self.sender_id():08b}', 'raw_message': f"{self.payload.hex(' ')}", - 'human_readable_type': self.human_readable_type(), + 'human_readable_type': Message.human_readable_type(self.sender_type(), self.sender_id()), 'sender_id': self.sender_id(), 'internal_id': self.internal_id } From c51513ebfaeba016f76ff7ed90af208b1b3f03ea Mon Sep 17 00:00:00 2001 From: redfast00 Date: Sat, 30 Jan 2021 13:58:45 +0100 Subject: [PATCH 5/6] Fix whitespace --- docs/protocol.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/protocol.txt b/docs/protocol.txt index b1b5185..0ef8eef 100644 --- a/docs/protocol.txt +++ b/docs/protocol.txt @@ -40,8 +40,8 @@ Types for controller: [ X B B B B B B B ] - - ---------- | ↓ reserved - ↓ module ID - type + ↓ module ID + type - 1 hello [ X B B B B B B B ] @@ -66,10 +66,10 @@ Types for controller: end time ↓ ↓ reserved #strikes #max strikes - - 7 info start + - 7 info start [ X B B B B B B B ] -------------- - reserved + reserved - 8-255 reserved From 2b44309ed8ac99d1fdfdfa0af607c443cf8f8d5a Mon Sep 17 00:00:00 2001 From: redfast00 Date: Sat, 30 Jan 2021 14:04:40 +0100 Subject: [PATCH 6/6] Put comments in correct order --- lib/obus_can.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/obus_can.h b/lib/obus_can.h index 149ad78..bd36dfa 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -204,7 +204,7 @@ inline void send_c_timeout( } /** - * Send a controller "strikeout" OBUS message + * Send a controller "info start" OBUS message */ inline void send_c_infostart(struct module from) { assert(from.type == OBUS_TYPE_CONTROLLER); @@ -213,7 +213,7 @@ inline void send_c_infostart(struct module from) { } /** - * Send a controller "info start" OBUS message + * Send a controller "strikeout" OBUS message */ inline void send_c_strikeout( struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {