From 5152b4dbd41e0e11da32c1ba308824ba367c45a9 Mon Sep 17 00:00:00 2001 From: ThomasintAnker Date: Thu, 20 Jun 2024 14:58:17 +0200 Subject: WIP main controller global state update --- main/i2c.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'main/i2c.c') diff --git a/main/i2c.c b/main/i2c.c index 2503560..5074915 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -17,6 +17,9 @@ i2c_addr_t modules[CFG_PB_MOD_MAX]; size_t modules_size = 0; static void state_exchange() { + + // TODO: Add calculation(?) to get global state + for (size_t i = 0; i < modules_size; i++) { pb_buf_t buf = pb_send_state_req(); @@ -47,3 +50,11 @@ void pb_route_cmd_magic_res(pb_msg_t * msg) { printf("i2c: registered puzzle module w/ address 0x%02x\n", msg->sender); } +void pb_route_cmd_state_res(pb_msg_t * msg) { + pb_cmd_state_t * cmd = msg->cmd; + // return early if state has wrong size + if (cmd->_magic_size != sizeof(pb_cmd_state_t)) + return; + + // TODO: Get msg sender & update state in array struct (?) +} -- cgit v1.2.3 From 32d1e5eff57e8f75ef571cc6a929357cfac737c2 Mon Sep 17 00:00:00 2001 From: ThomasintAnker Date: Thu, 20 Jun 2024 16:27:28 +0200 Subject: WIP state_exchange --- main/i2c.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'main/i2c.c') diff --git a/main/i2c.c b/main/i2c.c index 5074915..cf23d12 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -13,12 +13,18 @@ #include "pb-buf.h" #include "pb-send.h" -i2c_addr_t modules[CFG_PB_MOD_MAX]; +static pb_global_state_t _global_state = PB_GS_IDLE; +pb_puzzle_module_t modules[CFG_PB_MOD_MAX]; size_t modules_size = 0; static void state_exchange() { - + pb_global_state_t new_state; // TODO: Add calculation(?) to get global state + // noinit -> not possible, idle -> all idle, solved -> all solved, playing -> one playing + for (size_t i = 0; i < modules_size; i++) { + + } + pb_hook_mod_state_write(new_state); for (size_t i = 0; i < modules_size; i++) { pb_buf_t buf = pb_send_state_req(); @@ -46,15 +52,28 @@ void bus_task() { void pb_route_cmd_magic_res(pb_msg_t * msg) { if (modules_size == CFG_PB_MOD_MAX) return; - modules[modules_size++] = msg->sender; + pb_puzzle_module_t tmp_module = {msg->sender, PB_GS_NOINIT}; + modules[modules_size++] = tmp_module; printf("i2c: registered puzzle module w/ address 0x%02x\n", msg->sender); } void pb_route_cmd_state_res(pb_msg_t * msg) { pb_cmd_state_t * cmd = msg->cmd; - // return early if state has wrong size - if (cmd->_magic_size != sizeof(pb_cmd_state_t)) - return; + i2c_addr_t sender = msg->sender; + + // update sender state + for( size_t i = 0; i < modules_size; i++ ) { + if (modules[i].sender == sender) { + modules[i].state = (pb_global_state_t)cmd; + break; + } + } +} + +pb_global_state_t pb_hook_mod_state_read() { + return _global_state; +} - // TODO: Get msg sender & update state in array struct (?) +void pb_hook_mod_state_write(pb_global_state_t state) { + _global_state = state; } -- cgit v1.2.3 From 381149dd7a1f4d5f48dd5ac07186c73371ff3c04 Mon Sep 17 00:00:00 2001 From: ThomasintAnker Date: Mon, 24 Jun 2024 10:53:01 +0200 Subject: WIP Added MC state handling --- main/i2c.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'main/i2c.c') diff --git a/main/i2c.c b/main/i2c.c index cf23d12..87ff204 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -18,18 +18,55 @@ pb_puzzle_module_t modules[CFG_PB_MOD_MAX]; size_t modules_size = 0; static void state_exchange() { - pb_global_state_t new_state; - // TODO: Add calculation(?) to get global state - // noinit -> not possible, idle -> all idle, solved -> all solved, playing -> one playing + + for (size_t i = 0; i < modules_size; i++) { + pb_buf_t buf = pb_send_state_req(); + pb_buf_free(&buf); } - pb_hook_mod_state_write(new_state); +} + +void update_state() { + // TODO: Add calculation(?) to get global state + // all states idle == idle -> set first address as playingz + // all states solved == solved + // any state plater == playing + + pb_global_state_t module_state; + bool playing = false; // default false -> loop through modules to see if one is playing -> set to true + bool solved = true; // default true -> loop through modules to see if any is not solved -> set to false for (size_t i = 0; i < modules_size; i++) { - pb_buf_t buf = pb_send_state_req(); + module_state = modules[i].state; + if (module_state != PB_GS_SOLVED) + solved == false; - pb_buf_free(&buf); + if (module_state == PB_GS_PLAYING) + playing == true; + } + + // set state if no further processing is needed + if (solved == true) { + pb_hook_mod_state_write(PB_GS_SOLVED); + return; + } else if (playing == true) { + pb_hook_mod_state_write(PB_GS_PLAYING); + return; + } + + // IF no module playing, get/set next module THAT IS NOT SOLVED to playing + // and set mc state to playing + // pb_i2c_send(addr, buff.msg, buff.size) + + for (size_t i = 0; i < modules_size; i++) { + module_state = modules[i].state; + if (module_state == PB_GS_IDLE) { + pb_buf_t buff = pb_send_state_set(PB_GS_PLAYING); + pb_i2c_send(modules[i].sender, (uint8_t*)buff.data, buff.size); + pb_hook_mod_state_write(PB_GS_PLAYING); + return; + } } } -- cgit v1.2.3