![](/media/cache/profiles/aa03f7d585b4ae5554378b5843a73d11.jpg.50x50_q85.png)
Experiment of serial command protocol
Dependencies: RingBuffer SerialInterfaceProtocol duinotech_16x2_LCD mbed
You can edit this area
SerialInterfaceProtocol.h@2:54932809c7b2, 2016-06-09 (annotated)
- Committer:
- rba90
- Date:
- Thu Jun 09 12:28:31 2016 +0000
- Revision:
- 2:54932809c7b2
- Child:
- 3:0c4aa3cec685
Add wrapper for protocol
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 2:54932809c7b2 | 1 | // SerialInterfaceProtocol (SIP) is a protocol which help to exchange |
rba90 | 2:54932809c7b2 | 2 | // information between host and slave devices |
rba90 | 2:54932809c7b2 | 3 | #ifndef SERIALINTERFACEPROTOCOL_H_ |
rba90 | 2:54932809c7b2 | 4 | #define SERIALINTERFACEPROTOCOL_H_ |
rba90 | 2:54932809c7b2 | 5 | |
rba90 | 2:54932809c7b2 | 6 | #include "mbed.h" |
rba90 | 2:54932809c7b2 | 7 | #include "stdint.h" |
rba90 | 2:54932809c7b2 | 8 | #include "CommandPacket.h" |
rba90 | 2:54932809c7b2 | 9 | #include "buffer.h" |
rba90 | 2:54932809c7b2 | 10 | |
rba90 | 2:54932809c7b2 | 11 | #define SIP_CMD_VECTOR_TABLE_SZ 256 |
rba90 | 2:54932809c7b2 | 12 | #define SIP_MAX_RESP_LEN 256 |
rba90 | 2:54932809c7b2 | 13 | #define SIP_MAX_PAYLOAD_LEN 256 |
rba90 | 2:54932809c7b2 | 14 | |
rba90 | 2:54932809c7b2 | 15 | typedef int (*callback_func)(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length); |
rba90 | 2:54932809c7b2 | 16 | typedef CircularBuffer<uint8_t> SerialBuffer_t; |
rba90 | 2:54932809c7b2 | 17 | |
rba90 | 2:54932809c7b2 | 18 | class SerialInterfaceProtocol |
rba90 | 2:54932809c7b2 | 19 | { |
rba90 | 2:54932809c7b2 | 20 | public: |
rba90 | 2:54932809c7b2 | 21 | // namespace |
rba90 | 2:54932809c7b2 | 22 | |
rba90 | 2:54932809c7b2 | 23 | private: |
rba90 | 2:54932809c7b2 | 24 | // internal variable |
rba90 | 2:54932809c7b2 | 25 | callback_func CommandVectorTable[SIP_CMD_VECTOR_TABLE_SZ]; |
rba90 | 2:54932809c7b2 | 26 | |
rba90 | 2:54932809c7b2 | 27 | // buffered interface |
rba90 | 2:54932809c7b2 | 28 | SerialBuffer_t *SerialInputBuffer; |
rba90 | 2:54932809c7b2 | 29 | SerialBuffer_t *SerialOutputBuffer; |
rba90 | 2:54932809c7b2 | 30 | CommandPacket PacketBuffer; |
rba90 | 2:54932809c7b2 | 31 | |
rba90 | 2:54932809c7b2 | 32 | // state machine |
rba90 | 2:54932809c7b2 | 33 | CommandPacket::State_t state; |
rba90 | 2:54932809c7b2 | 34 | |
rba90 | 2:54932809c7b2 | 35 | // internal state |
rba90 | 2:54932809c7b2 | 36 | bool isChecksumEnabled; |
rba90 | 2:54932809c7b2 | 37 | |
rba90 | 2:54932809c7b2 | 38 | private: |
rba90 | 2:54932809c7b2 | 39 | // internal methods |
rba90 | 2:54932809c7b2 | 40 | int execute(uint8_t *response, uint8_t *response_length); |
rba90 | 2:54932809c7b2 | 41 | |
rba90 | 2:54932809c7b2 | 42 | int assemble(uint8_t *response, uint8_t response_length); |
rba90 | 2:54932809c7b2 | 43 | |
rba90 | 2:54932809c7b2 | 44 | |
rba90 | 2:54932809c7b2 | 45 | public: |
rba90 | 2:54932809c7b2 | 46 | // public methods |
rba90 | 2:54932809c7b2 | 47 | |
rba90 | 2:54932809c7b2 | 48 | // constructor |
rba90 | 2:54932809c7b2 | 49 | SerialInterfaceProtocol(SerialBuffer_t *in, SerialBuffer_t *out); |
rba90 | 2:54932809c7b2 | 50 | ~SerialInterfaceProtocol(); |
rba90 | 2:54932809c7b2 | 51 | |
rba90 | 2:54932809c7b2 | 52 | // member function |
rba90 | 2:54932809c7b2 | 53 | void poll(); |
rba90 | 2:54932809c7b2 | 54 | void registerCommand(uint8_t command, callback_func f); |
rba90 | 2:54932809c7b2 | 55 | void deRegisterCommand(uint8_t command); |
rba90 | 2:54932809c7b2 | 56 | |
rba90 | 2:54932809c7b2 | 57 | void disableChecksum(); |
rba90 | 2:54932809c7b2 | 58 | void enableChecksum(); |
rba90 | 2:54932809c7b2 | 59 | |
rba90 | 2:54932809c7b2 | 60 | }; |
rba90 | 2:54932809c7b2 | 61 | |
rba90 | 2:54932809c7b2 | 62 | |
rba90 | 2:54932809c7b2 | 63 | |
rba90 | 2:54932809c7b2 | 64 | |
rba90 | 2:54932809c7b2 | 65 | #endif |