diff -r 162fe523c37d -r 4fd621551d55 src/hp34comm.h --- a/src/hp34comm.h Wed Oct 26 22:41:16 2016 +0200 +++ b/src/hp34comm.h Sat Oct 29 23:44:31 2016 +0200 @@ -10,17 +10,13 @@ #define MAX_BUFF 16 #define BUF_SIZE 16 + + class HPSerial { public: - enum TrState { - Idle = 0, - Tx, - Rx, - }; typedef struct _CMD { - TrState direction; uint8_t cmd; uint8_t size; char value[MAX_BUFF+1]; @@ -29,7 +25,7 @@ - HPSerial(PinName rx); + HPSerial(PinName tx, PinName rx); bool cmd_available(void); bool pop(CMD& cmd); @@ -38,26 +34,57 @@ private: - void reset(uint8_t errorno=0xFF); - void handleAck(uint8_t val); - void pushCmd(TrState direction, uint8_t cmd, uint8_t size, char *payload); - void handleChar(uint8_t val); - void setstatedbg(void); + void pushCmd(uint8_t cmd, uint8_t size, char *payload); void rxIrq(void); void timeout(void); private: - RawSerial serial_rx; + RawSerial serial; uint8_t buf[BUF_SIZE]; uint8_t head; - uint8_t tx_state; - uint8_t tx_cmd; - uint8_t tx_len; - bool tx_ack; CircularBuffer cmdbuf; unsigned long ncmd; 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; + + + typedef struct instance_data { + uint8_t cmd; + uint8_t size; + uint8_t received; + char payload[MAX_BUFF]; + } instance_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 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); + + state_t cur_state; + instance_data rx_data; }; #endif