diff -r 4fd621551d55 -r a52d60613cf7 src/hp34comm.h --- a/src/hp34comm.h Sat Oct 29 23:44:31 2016 +0200 +++ b/src/hp34comm.h Wed Jan 18 23:19:13 2017 +0100 @@ -22,21 +22,27 @@ char value[MAX_BUFF+1]; unsigned long id; } CMD; - - HPSerial(PinName tx, PinName rx); - + bool cmd_available(void); bool pop(CMD& cmd); bool cmd_buf_full(void); unsigned int nerrors(uint8_t errorno); - + + void startup(void); + void send(const uint8_t *buf, uint8_t size); + void sendkey(uint8_t keycode); private: void pushCmd(uint8_t cmd, uint8_t size, char *payload); void rxIrq(void); void timeout(void); + void set_timer(float v=0.0) { + timeouter.detach(); + if (v > 0.0) + timeouter.attach(callback(this, &HPSerial::timeout), v); + } private: RawSerial serial; @@ -47,44 +53,69 @@ unsigned int errs[MAX_ERRS]; Ticker timeouter; - // state machine stuff - typedef enum {STATE_IDLE, - STATE_HANDCHEKED, - STATE_COMMAND, - STATE_PAYLOAD_SIZE, - STATE_PAYLOAD, - NUM_STATES} state_t; - + public: + // global state machine + typedef enum { + GSTATE_STARTING, + GSTATE_STARTING2, + GSTATE_IDLE, + GSTATE_TX, + GSTATE_RX, + NUM_GSTATES} gstate_t; + gstate_t gstate() {return cur_gstate;}; + /* gstate_t do_start(); */ + /* gstate_t do_send(); */ + /* gstate_t do_receive(); */ - typedef struct instance_data { + /* typedef gstate_t(HPSerial::*gstatemethod)(); */ + /* statemethod const gstate_table[NUM_GSTATES] = { */ + /* &HPSerial::do_start, */ + /* &HPSerial::do_send, */ + /* &HPSerial::do_receive, */ + /* }; */ + + /* gstate_t run_gstate(gstate_t cur_state); */ + private: + gstate_t cur_gstate; + + public: + // transmission state machine + typedef enum { + STATE_IDLE, + STATE_COMMAND, + STATE_PAYLOAD_SIZE, + STATE_PAYLOAD, + STATE_SENDING, + STATE_EOT, + NUM_STATES} state_t; + state_t state() {return cur_state;}; + + private: + typedef struct state_data { uint8_t cmd; uint8_t size; - uint8_t received; + uint8_t pos; char payload[MAX_BUFF]; - } instance_data_t; + } state_data_t; - state_t do_state_initial(instance_data_t *data, uint8_t c); - state_t do_state_handcheck(instance_data_t *data, uint8_t c); - state_t do_state_command(instance_data_t *data, uint8_t c); - state_t do_state_payload_size(instance_data_t *data, uint8_t c); - state_t do_state_payload(instance_data_t *data, uint8_t c); + void _startup(void); + void _startup2(void); + state_t do_state_initial(uint8_t c=0x00); + state_t do_state_command(uint8_t c); + state_t do_state_payload_size(uint8_t c); + state_t do_state_payload(uint8_t c); + state_t do_state_sending(uint8_t c=0x00); + state_t do_state_eot(uint8_t c=0x00); void send_ack(uint8_t c); - // pointer to "state method" (ie. one of the just above) - typedef state_t(HPSerial::*statemethod)(instance_data_t *data, uint8_t c); - statemethod const state_table[NUM_STATES] = { - &HPSerial::do_state_initial, - &HPSerial::do_state_handcheck, - &HPSerial::do_state_command, - &HPSerial::do_state_payload_size, - &HPSerial::do_state_payload, - }; - - state_t run_state(state_t cur_state, instance_data_t *data, uint8_t c); + typedef state_t(HPSerial::*statemethod)(uint8_t c); + static statemethod state_table[NUM_STATES]; + state_t run_state(state_t cur_state, uint8_t c); state_t cur_state; - instance_data rx_data; + state_data_t tr_data; }; + #endif