blob: 8244fdb492537df557e2522b57963db04eb9dcb4 [file] [log] [blame]
Brian Silvermanbfbbe872019-02-10 18:00:57 -08001#ifndef Y2019_JEVOIS_UART_H_
2#define Y2019_JEVOIS_UART_H_
3
4#include "aos/containers/sized_array.h"
5#include "third_party/optional/tl/optional.hpp"
Brian Silverman2eb89762019-02-17 15:16:37 -08006#include "y2019/jevois/cobs.h"
Brian Silvermanbfbbe872019-02-10 18:00:57 -08007#include "y2019/jevois/structures.h"
8
9// This file manages serializing and deserializing the various structures for
10// transport via UART.
11
12namespace frc971 {
13namespace jevois {
14
Brian Silverman2eb89762019-02-17 15:16:37 -080015constexpr size_t uart_to_teensy_size() {
Brian Silvermana1e4d332019-02-17 22:53:13 -080016 return 1 /* number of targets */ +
17 3 /* targets */ * (sizeof(float) * 4 /* fields */) + 1 /* age */ +
Brian Silverman2eb89762019-02-17 15:16:37 -080018 2 /* CRC-16 */;
Brian Silvermanbfbbe872019-02-10 18:00:57 -080019}
Brian Silverman2eb89762019-02-17 15:16:37 -080020using UartToTeensyBuffer =
21 aos::SizedArray<char, CobsMaxEncodedSize(uart_to_teensy_size())>;
Brian Silvermanbfbbe872019-02-10 18:00:57 -080022
Brian Silverman2eb89762019-02-17 15:16:37 -080023constexpr size_t uart_to_camera_size() {
24 return sizeof(float) * 3 * 4 /* calibration */ +
25 sizeof(int64_t) /* teensy_now */ + sizeof(int64_t) /* realtime_now */ +
26 1 /* camera_command */ + 2 /* CRC-16 */;
27}
28using UartToCameraBuffer =
29 aos::SizedArray<char, CobsMaxEncodedSize(uart_to_camera_size())>;
30
31UartToTeensyBuffer UartPackToTeensy(const Frame &message);
32tl::optional<Frame> UartUnpackToTeensy(const UartToTeensyBuffer &buffer);
33
34UartToCameraBuffer UartPackToCamera(const CameraCalibration &message);
35tl::optional<CameraCalibration> UartUnpackToCamera(
36 const UartToCameraBuffer &buffer);
Brian Silvermanbfbbe872019-02-10 18:00:57 -080037
38} // namespace jevois
39} // namespace frc971
40
41#endif // Y2019_JEVOIS_UART_H_