blob: 8b16d5c930234e2065a62630a7671b687bf38826 [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"
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <optional>
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(
Brian Silvermana10b87e2019-02-24 15:20:07 -080031 gsl::span<const char, spi_transfer_size()> transfer);
32SpiTransfer SpiPackToTeensy(const RoborioToTeensy &message);
James Kuszmaul3ae42262019-11-08 12:33:41 -080033std::optional<RoborioToTeensy> SpiUnpackToTeensy(
Brian Silvermana10b87e2019-02-24 15:20:07 -080034 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_