19 #ifndef SNARK_ACTUATORS_QUICKSET_ptcr_PACKET_H_
20 #define SNARK_ACTUATORS_QUICKSET_ptcr_PACKET_H_
22 #include <comma/packed/byte.h>
23 #include <comma/packed/little_endian.h>
24 #include <comma/packed/struct.h>
26 namespace snark {
namespace quickset {
namespace ptcr {
30 static const unsigned char stx = 0x02;
31 static const unsigned char etx = 0x03;
32 static const unsigned char ack = 0x06;
33 static const unsigned char nak = 0x15;
34 static const unsigned char esc = 0x1B;
37 template <
typename B >
38 struct header :
public comma::packed::packed_struct< header< B >, 3 >
40 comma::packed::byte type;
41 comma::packed::byte address;
42 comma::packed::const_byte< B::id > id;
44 header(
unsigned char t ) { type = t; address = 0; }
47 template <
typename B >
48 struct lrc :
public comma::packed::packed_struct< lrc< B >, 1 >
50 comma::packed::byte value;
56 template <
typename B >
57 struct footer :
public comma::packed::packed_struct< footer< B >, 2 >
60 comma::packed::const_byte< 0x03 > etx;
63 template <
typename B >
64 struct packet :
public comma::packed::packed_struct< packet< B >, 3 + 2 + B::size >
66 header< B > packet_header;
68 footer< B > packet_footer;
70 packet() : packet_header( constants::stx ) {}
71 packet(
unsigned char type ) : packet_header( type ) {}
72 packet(
const B& body ) : packet_header( constants::stx ), body( body ) { packet_footer.footer_lrc.set(); }
73 packet(
const B& body,
unsigned char type ) : packet_header( type ), body( body ) { packet_footer.footer_lrc.set(); }
76 inline static unsigned char get_lrc(
const char* begin,
const char* end )
79 for(
const char* p = begin; p < end; ++p ) { c ^=
static_cast< unsigned char >( *p ); }
83 template <
typename B >
84 inline void lrc< B >::set() { value = get_lrc( value.data() - B::size - 1, value.data() ); }
86 template <
typename B >
87 inline bool lrc< B >::ok()
const
89 return value() == get_lrc( value.data() - B::size - 1, value.data() );
94 #endif // #ifndef SNARK_ACTUATORS_QUICKSET_ptcr_PACKET_H_