blob: 7c14a57bc77f0647bf90c9d1feb95d7a444aa6f3 [file] [log] [blame]
Brian Silverman246cb222019-02-02 16:38:18 -08001#ifndef Y2019_JEVOIS_SPI_H_
2#define Y2019_JEVOIS_SPI_H_
3
4#include <stdint.h>
5
6#include <array>
7
Brian Silvermana10b87e2019-02-24 15:20:07 -08008#include "third_party/GSL/include/gsl/gsl"
Brian Silverman246cb222019-02-02 16:38:18 -08009#include "third_party/optional/tl/optional.hpp"
10#include "y2019/jevois/structures.h"
11
12// This file manages serializing and deserializing the various structures for
13// transport via SPI.
14//
15// Our SPI transfers are fixed-size to simplify everything.
16
17namespace frc971 {
18namespace jevois {
19
20constexpr size_t spi_transfer_size() {
21 // The teensy->RoboRIO side is way bigger, so just calculate that.
22 return 3 /* 3 frames */ *
23 (1 /* age */ + 3 /* targets */ * 4 /* target size */) +
24 2 /* CRC-16 */;
25}
26static_assert(spi_transfer_size() == 41, "hand math is wrong");
27using SpiTransfer = std::array<char, spi_transfer_size()>;
28
29SpiTransfer SpiPackToRoborio(const TeensyToRoborio &message);
Brian Silvermana10b87e2019-02-24 15:20:07 -080030tl::optional<TeensyToRoborio> SpiUnpackToRoborio(
31 gsl::span<const char, spi_transfer_size()> transfer);
32SpiTransfer SpiPackToTeensy(const RoborioToTeensy &message);
33tl::optional<RoborioToTeensy> SpiUnpackToTeensy(
34 gsl::span<const char, spi_transfer_size()> transfer);
Brian Silverman246cb222019-02-02 16:38:18 -080035
36} // namespace jevois
37} // namespace frc971
38
39#endif // Y2019_JEVOIS_SPI_H_