blob: 62e5ea719d7bedbc3493d8de33fa66b2bd673ce8 [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
Austin Schuhb72be802022-01-02 12:26:28 -08008#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07009
Brian Silverman246cb222019-02-02 16:38:18 -080010#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);
James Kuszmaul3ae42262019-11-08 12:33:41 -080030std::optional<TeensyToRoborio> SpiUnpackToRoborio(
Austin Schuhb72be802022-01-02 12:26:28 -080031 absl::Span<const char> transfer);
Brian Silvermana10b87e2019-02-24 15:20:07 -080032SpiTransfer SpiPackToTeensy(const RoborioToTeensy &message);
James Kuszmaul3ae42262019-11-08 12:33:41 -080033std::optional<RoborioToTeensy> SpiUnpackToTeensy(
Austin Schuhb72be802022-01-02 12:26:28 -080034 absl::Span<const char> transfer);
Brian Silverman246cb222019-02-02 16:38:18 -080035
36} // namespace jevois
37} // namespace frc971
38
39#endif // Y2019_JEVOIS_SPI_H_