Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Last active September 7, 2017 14:08
Show Gist options
  • Save MatrixManAtYrService/06e06b2941f45698ac90bcb7864ec9e6 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/06e06b2941f45698ac90bcb7864ec9e6 to your computer and use it in GitHub Desktop.
Decoding an old (2014?) SPaT
#include <exception>
#include <array>
#include <stdint.h>
#define BUFFER_SIZE 10240
struct ControllerState
{
uint16_t PedCallActive = 0;
uint16_t Green = 0;
uint16_t Yellow = 0;
uint16_t Red = 0xFFFF;
uint16_t Flash = 0;
uint16_t OvlpGreen = 0;
uint16_t OvlpYellow = 0;
uint16_t OvlpRed = 0;
uint16_t OvlpFlash = 0;
uint16_t Walk = 0;
uint16_t PedClear = 0;
uint16_t DontWalk = 0xFFFF;
};
ControllerState DecodeSpat(array<char, BUFFER_SIZE>& buffer, size_t bytes)
{
ControllerState result;
unsigned char* ubuffer = reinterpret_cast<unsigned char*>(buffer.data());
if ((uint8_t)buffer[0] != 0xcd)
throw std::runtime_error("Incompatible SPaT");
#define READ_TWO_BYTES_AT_LOCATION(X) (uint16_t)((ubuffer[X] << 8) | ubuffer[X + 1]);
result.Red = READ_TWO_BYTES_AT_LOCATION(210);
result.Yellow = READ_TWO_BYTES_AT_LOCATION(212);
result.Green = READ_TWO_BYTES_AT_LOCATION(214);
result.DontWalk = READ_TWO_BYTES_AT_LOCATION(216);
result.PedClear = READ_TWO_BYTES_AT_LOCATION(218);
result.Walk = READ_TWO_BYTES_AT_LOCATION(220);
result.OvlpRed = READ_TWO_BYTES_AT_LOCATION(222);
result.OvlpYellow = READ_TWO_BYTES_AT_LOCATION(224);
result.OvlpGreen = READ_TWO_BYTES_AT_LOCATION(226);
result.Flash = READ_TWO_BYTES_AT_LOCATION(228);
result.OvlpFlash = READ_TWO_BYTES_AT_LOCATION(230);
result.PedCallActive = READ_TWO_BYTES_AT_LOCATION(241);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment