Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 1 | #ifndef FCR971_INPUT_UART_RECEIVER_H_ |
| 2 | #define FRC971_INPUT_UART_RECEIVER_H_ |
| 3 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 4 | #include <stdint.h> |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 5 | |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 6 | #include <memory> |
| 7 | |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 8 | #define DATA_STRUCT_NAME DataStruct |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 9 | #include "cape/data_struct.h" |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 10 | #undef DATA_STRUCT_NAME |
| 11 | |
| 12 | namespace bbb { |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 13 | |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 14 | class UartReader { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 15 | public: |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 16 | UartReader(int32_t baud_rate); |
| 17 | ~UartReader(); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 18 | |
| 19 | // Returns true if it finds one or false if it gets an I/O error first. |
| 20 | // packet must be aligned to 4 bytes. |
| 21 | bool GetPacket(DataStruct *packet); |
| 22 | |
| 23 | private: |
| 24 | // Reads bytes until there are 4 zeros and then fills up buf_. |
| 25 | // Returns true if it finds one or false if it gets an I/O error first or the |
| 26 | // packet is invalid in some way. |
| 27 | bool FindPacket(); |
| 28 | |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 29 | // Processes a packet currently in buf_ and leaves the result in |
| 30 | // unstuffed_data_. |
| 31 | // Returns true if it succeeds or false if there was something wrong with the |
| 32 | // data. |
| 33 | bool ProcessPacket(); |
| 34 | |
| 35 | typedef char __attribute__((aligned(4))) AlignedChar; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 36 | |
| 37 | const int32_t baud_rate_; |
| 38 | AlignedChar *const buf_; |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 39 | AlignedChar *const unstuffed_data_; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 40 | const int fd_; |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 41 | |
| 42 | // How many bytes of the packet we've read in (or -1 if we don't know where |
| 43 | // the packet is). |
| 44 | int packet_bytes_ = -1; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | } // namespace bbb |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 48 | |
| 49 | #endif |