Mon, 11 Mar 2024 15:35:36 +0100
Add links to sr.ht repos and add an image in the README file
5 | 1 | #ifndef HP34COMM_H |
2 | #define HP34COMM_H | |
3 | ||
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
4 | #include <mbed.h> |
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
5 | #include <CircularBuffer.h> |
5 | 6 | |
7 | /***** HP 34970A communication class ***/ | |
8 | ||
9 | #define MAX_ERRS 10 | |
8 | 10 | #define MAX_BUFF 16 |
11 | #define BUF_SIZE 16 | |
5 | 12 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
13 | |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
14 | |
5 | 15 | class HPSerial { |
16 | ||
17 | public: | |
18 | typedef struct _CMD | |
19 | { | |
20 | uint8_t cmd; | |
21 | uint8_t size; | |
22 | char value[MAX_BUFF+1]; | |
23 | unsigned long id; | |
24 | } CMD; | |
21 | 25 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
26 | HPSerial(PinName tx, PinName rx); |
19 | 27 | |
66
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
28 | void reset(void); |
5 | 29 | bool cmd_available(void); |
30 | bool pop(CMD& cmd); | |
31 | bool cmd_buf_full(void); | |
32 | unsigned int nerrors(uint8_t errorno); | |
21 | 33 | |
19 | 34 | void sendkey(uint8_t keycode); |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
35 | void send_startup_seq(); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
36 | void send_startup_seq(uint8_t keycode); |
21 | 37 | |
5 | 38 | private: |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
66
diff
changeset
|
39 | void push_cmd(uint8_t cmd, uint8_t size, char *payload); |
44 | 40 | void rx_irq(void); |
5 | 41 | void timeout(void); |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
42 | void set_timer(Kernel::Clock::duration_u32 v=0ms) { |
19 | 43 | timeouter.detach(); |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
44 | if (v > 0ms) |
19 | 45 | timeouter.attach(callback(this, &HPSerial::timeout), v); |
44 | 46 | }; |
5 | 47 | |
48 | private: | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
49 | UnbufferedSerial serial; |
5 | 50 | uint8_t buf[BUF_SIZE]; |
51 | uint8_t head; | |
52 | CircularBuffer<CMD, 32> cmdbuf; | |
44 | 53 | CircularBuffer<uint8_t, 32> sendbuf; |
5 | 54 | unsigned long ncmd; |
55 | unsigned int errs[MAX_ERRS]; | |
56 | Ticker timeouter; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
57 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
58 | Thread send_thread; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
59 | void send_pending_key(); |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
66
diff
changeset
|
60 | void do_send_key(); |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
61 | bool wait_for(uint8_t); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
62 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
63 | |
19 | 64 | public: |
65 | // global state machine | |
66 | typedef enum { | |
67 | GSTATE_STARTING, | |
68 | GSTATE_IDLE, | |
69 | GSTATE_TX, | |
70 | GSTATE_RX, | |
71 | NUM_GSTATES} gstate_t; | |
72 | gstate_t gstate() {return cur_gstate;}; | |
73 | /* gstate_t do_start(); */ | |
74 | /* gstate_t do_send(); */ | |
75 | /* gstate_t do_receive(); */ | |
21 | 76 | |
19 | 77 | /* typedef gstate_t(HPSerial::*gstatemethod)(); */ |
78 | /* statemethod const gstate_table[NUM_GSTATES] = { */ | |
79 | /* &HPSerial::do_start, */ | |
80 | /* &HPSerial::do_send, */ | |
81 | /* &HPSerial::do_receive, */ | |
82 | /* }; */ | |
21 | 83 | |
19 | 84 | /* gstate_t run_gstate(gstate_t cur_state); */ |
85 | private: | |
86 | gstate_t cur_gstate; | |
21 | 87 | |
19 | 88 | public: |
89 | // transmission state machine | |
90 | typedef enum { | |
91 | STATE_IDLE, | |
92 | STATE_COMMAND, | |
93 | STATE_PAYLOAD_SIZE, | |
94 | STATE_PAYLOAD, | |
95 | STATE_SENDING, | |
96 | NUM_STATES} state_t; | |
97 | state_t state() {return cur_state;}; | |
21 | 98 | |
19 | 99 | private: |
100 | typedef struct state_data { | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
101 | uint8_t cmd; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
102 | uint8_t size; |
19 | 103 | uint8_t pos; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
104 | char payload[MAX_BUFF]; |
19 | 105 | } state_data_t; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
106 | |
19 | 107 | state_t do_state_initial(uint8_t c=0x00); |
108 | state_t do_state_command(uint8_t c); | |
109 | state_t do_state_payload_size(uint8_t c); | |
110 | state_t do_state_payload(uint8_t c); | |
111 | state_t do_state_sending(uint8_t c=0x00); | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
112 | |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
113 | void send_ack(uint8_t c); |
21 | 114 | |
19 | 115 | typedef state_t(HPSerial::*statemethod)(uint8_t c); |
21 | 116 | static statemethod state_table[NUM_STATES]; |
19 | 117 | state_t run_state(state_t cur_state, uint8_t c); |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
118 | |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
10
diff
changeset
|
119 | state_t cur_state; |
19 | 120 | state_data_t tr_data; |
5 | 121 | }; |
122 | ||
19 | 123 | |
5 | 124 | #endif |