Line data Source code
1 : #pragma once 2 : 3 : #include "AP_LightWareSerial_config.h" 4 : 5 : #if AP_LIGHTWARESERIAL_ENABLED 6 : 7 : #include <AP_HAL/AP_HAL.h> 8 : #define LIGHTWARE_PAYLOAD_LEN_MAX 256 // maximum payload size we can accept (in some configurations sensor may send as large as 1023) 9 : 10 : class AP_LightWareSerial 11 : { 12 : public: 13 2 : AP_LightWareSerial(AP_HAL::UARTDriver *&uart_ref) : 14 2 : _uart(uart_ref) 15 : { 16 2 : } 17 : 18 : protected: 19 : 20 : // initialise sensor 21 : void initialise(); 22 : 23 : // send message to sensor 24 : void send_message(uint8_t msgid, bool write, const uint8_t *payload, uint16_t payload_len); 25 : 26 : // process one byte received on serial port 27 : // returns true if a complete message has been received 28 : // state is stored in _msg structure 29 : bool parse_byte(uint8_t b); 30 : 31 : enum class ParseState { 32 : HEADER = 0, 33 : FLAGS_L, 34 : FLAGS_H, 35 : MSG_ID, 36 : PAYLOAD, 37 : CRC_L, 38 : CRC_H 39 : } _parse_state; // state of incoming message processing 40 : uint16_t _payload_recv; // number of message's payload bytes received so far 41 : uint16_t _crc_expected; // latest message's expected crc 42 : 43 : // structure holding latest message contents 44 : struct { 45 : uint8_t flags_low; // flags low byte 46 : uint8_t flags_high; // flags high byte 47 : uint16_t payload_len; // latest message payload length (1+ bytes in payload) 48 : uint8_t payload[LIGHTWARE_PAYLOAD_LEN_MAX]; // payload 49 : uint8_t msgid; // latest message's message id 50 : uint8_t crc_low; // crc low byte 51 : uint8_t crc_high; // crc high byte 52 : } _msg; 53 : 54 : private: 55 : AP_HAL::UARTDriver *& _uart; 56 : }; 57 : #endif // AP_LIGHTWARESERIAL_ENABLED