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