18 *dest = (
static_cast<uint8_t
>(sys_type)
19 | static_cast<uint8_t>(Magics::SYS::SYS_BASE));
24 const uint8_t* orig_dest { dest };
25 *(dest++) = (static_cast<uint8_t>(Magics::CMD::CMD_BASE)
26 |
static_cast<uint8_t
>(Magics::CMD::TYPE)
34 const uint8_t modes_available) {
35 const uint8_t* orig_dest { dest };
36 *(dest++) = (static_cast<uint8_t>(Magics::CMD::CMD_BASE)
37 |
static_cast<uint8_t
>(Magics::CMD::MODES)
39 *(dest++) = (0x07 & modes);
40 *(dest++) = (0x07 & modes_available);
46 const uint8_t* orig_dest { dest };
47 *(dest++) = (static_cast<uint8_t>(Magics::CMD::CMD_BASE)
48 |
static_cast<uint8_t
>(Magics::CMD::SPEED)
50 uint32_t speed_conv = SimpleEndian::htole32(speed);
51 memcpy(reinterpret_cast<void*>(dest),
52 reinterpret_cast<const void*>(&speed_conv),
sizeof(speed_conv));
59 const uint8_t* orig_dest { dest };
60 *(dest++) = (static_cast<uint8_t>(Magics::CMD::CMD_BASE)
61 |
static_cast<uint8_t
>(Magics::CMD::SELECT)
63 *(dest++) = (0x07 & mode);
73 const uint8_t* orig_dest { dest };
74 *(dest++) = (static_cast<uint8_t>(Magics::CMD::CMD_BASE)
75 |
static_cast<uint8_t
>(Magics::CMD::WRITE)
77 memcpy(reinterpret_cast<void*>(dest),
78 reinterpret_cast<const void*>(data), len);
82 *dest =
checksum(orig_dest, 0x01 + len + padding);
83 return (0x02 + len + padding);
89 const size_t name_length { name !=
nullptr ? strlen(name) : 0 };
94 const uint8_t* orig_dest { dest };
95 *(dest++) = (static_cast<uint8_t>(Magics::INFO::INFO_BASE)
99 memcpy(reinterpret_cast<void*>(dest),
100 reinterpret_cast<const void*>(name), name_length);
104 *dest =
checksum(orig_dest, 0x02 + name_length + padding);
105 return (0x03 + name_length + padding);
111 const uint8_t* orig_dest { dest };
112 *(dest++) = (static_cast<uint8_t>(Magics::INFO::INFO_BASE)
115 *(dest++) = static_cast<uint8_t>(span_type);
120 memcpy(reinterpret_cast<void*>(&temp),
121 reinterpret_cast<const void*>(&lower),
sizeof(lower));
122 temp = SimpleEndian::htole32(temp);
123 memcpy(reinterpret_cast<void*>(&tempf),
124 reinterpret_cast<const void*>(&temp),
sizeof(temp));
125 memcpy(reinterpret_cast<void*>(dest),
126 reinterpret_cast<const void*>(&tempf),
sizeof(tempf));
127 dest +=
sizeof(tempf);
129 memcpy(reinterpret_cast<void*>(&temp),
130 reinterpret_cast<const void*>(&upper),
sizeof(upper));
131 temp = SimpleEndian::htole32(temp);
132 memcpy(reinterpret_cast<void*>(&tempf),
133 reinterpret_cast<const void*>(&temp),
sizeof(temp));
134 memcpy(reinterpret_cast<void*>(dest),
135 reinterpret_cast<const void*>(&tempf),
sizeof(tempf));
136 dest +=
sizeof(tempf);
143 const char* symbol) {
144 const size_t symbol_length { symbol !=
nullptr ? strlen(symbol) : 0 };
149 const uint8_t* orig_dest { dest };
150 *(dest++) = (static_cast<uint8_t>(Magics::INFO::INFO_BASE)
154 memcpy(reinterpret_cast<void*>(dest),
155 reinterpret_cast<const void*>(symbol), symbol_length);
156 dest += symbol_length;
158 const uint8_t padding { 0x08 - symbol_length };
159 for (uint8_t i = 0; i < padding; i++)
162 *dest =
checksum(orig_dest, 0x02 + symbol_length + padding);
163 return (0x03 + symbol_length + padding);
170 const uint8_t decimals) {
171 const uint8_t* orig_dest { dest };
172 *(dest++) = (static_cast<uint8_t>(Magics::INFO::INFO_BASE)
176 *(dest++) = (0x3f & elems);
177 *(dest++) = (0x03 & static_cast<uint8_t>(data_type));
178 *(dest++) = (0x0f & width);
179 *(dest++) = (0x0f & decimals);
180 *dest =
checksum(orig_dest, 0x02 + 0x04);
185 const uint8_t* data,
const uint8_t len) {
189 const uint8_t* orig_dest { dest };
190 *(dest++) = (static_cast<uint8_t>(Magics::DATA::DATA_BASE)
193 memcpy(reinterpret_cast<void*>(dest),
194 reinterpret_cast<const void*>(data), len);
198 *dest =
checksum(orig_dest, 0x01 + len + padding);
199 return (0x02 + len + padding);
203 uint8_t
checksum(
const uint8_t* buf,
const uint8_t len) {
204 uint8_t acc { 0xff };
205 for (uint8_t i = 0; i < len; i++) {
212 const uint8_t padding = { ((0x01 <<
log2(len)) - len) };
213 for (uint8_t i = 0; i < padding; i++) {
int8_t frame_data_message(uint8_t *dest, const uint8_t mode, const uint8_t *data, const uint8_t len)
uint8_t checksum(const uint8_t *buf, const uint8_t len)
int8_t frame_info_message_format(uint8_t *dest, const uint8_t mode, const uint8_t elems, Magics::INFO_DTYPE data_type, const uint8_t width, const uint8_t decimals)
int8_t frame_cmd_modes_message(uint8_t *dest, const uint8_t modes, const uint8_t modes_available)
constexpr uint8_t log2(uint8_t val)
int8_t frame_cmd_write_message(uint8_t *dest, const uint8_t *data, const uint8_t len)
constexpr uint8_t PAYLOAD_EV3_TO_SENSOR_MAX
Maximum size of any payload sent in the EV3 UART sensor protocol, in bytes, from the EV3 to the senso...
int8_t frame_info_message_name(uint8_t *dest, const uint8_t mode, const char *name)
int8_t frame_cmd_select_message(uint8_t *dest, const uint8_t mode)
constexpr uint8_t PAYLOAD_SENSOR_TO_EV3_MAX
Maximum size of any payload sent in the EV3 UART sensor protocol, in bytes, to the EV3...
int8_t frame_sys_message(uint8_t *dest, Magics::SYS sys_type)
int8_t frame_info_message_symbol(uint8_t *dest, const uint8_t mode, const char *symbol)
constexpr uint8_t PAYLOAD_MIN
Minimum size of any payload sent in the EV3 UART sensor protocol, regardless of direction, in bytes.
int8_t frame_info_message_span(uint8_t *dest, const uint8_t mode, Magics::INFO_SPAN span_type, const float lower, const float upper)
int8_t frame_cmd_speed_message(uint8_t *dest, const uint32_t speed)
constexpr uint8_t length_code(uint8_t len)
int8_t frame_cmd_type_message(uint8_t *dest, const uint8_t type)
constexpr uint8_t SYMBOL_MAX
Maximum length of the string representation (ASCII) of any symbol referencing a the SI unit used to r...
uint8_t insert_padding(uint8_t *dest, uint8_t len)