diff options
| -rw-r--r-- | lib/pbdrv/pb-types.h | 6 | ||||
| -rw-r--r-- | main/i2c.c | 72 | 
2 files changed, 75 insertions, 3 deletions
| diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index ef3df54..44b590b 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -131,6 +131,12 @@ typedef struct {  	pb_global_state_t state; //!< global state  } pb_cmd_state_t; +//! PB_MOD_STATE data +typedef struct { +	i2c_addr_t sender; //!< i2c address of sender +	pb_global_state_t state; //!< global state +} pb_puzzle_module_t; +  //! \ref PB_CMD_MAGIC data  typedef struct {  	char * magic; //!< magic value @@ -12,10 +12,10 @@  #include "pb-buf.h"  #include "pb-send.h" +static pb_global_state_t _global_state = PB_GS_IDLE;  +pb_puzzle_module_t modules[CFG_PB_MOD_MAX];  // stolen from lib/pico-sdk/src/rp2_common/hardware_i2c/i2c.c  #define i2c_reserved_addr(addr) (((addr) & 0x78) == 0 || ((addr) & 0x78) == 0x78) - -i2c_addr_t modules[CFG_PB_MOD_MAX];  size_t modules_size = 0;  static void bus_scan() { @@ -34,6 +34,8 @@ static void bus_scan() {  }  static void state_exchange() { +	 +  	for (size_t i = 0; i < modules_size; i++) {  		pb_buf_t buf = pb_send_state_req(); @@ -41,6 +43,49 @@ static void state_exchange() {  	}  } +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++) { +		module_state = modules[i].state; +		if (module_state != PB_GS_SOLVED) +			solved == false;	 +		 +		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; +		} +	} +} +  void bus_task() {  	// do a scan of the bus  	bus_scan(); @@ -67,7 +112,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; +	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; +} + +void pb_hook_mod_state_write(pb_global_state_t state) { +	_global_state = state; +} |