summaryrefslogtreecommitdiff
path: root/ThrottleQuadrant.hpp
blob: 8776aab2f1e3dfc9ee59d540314f84f3b077bfb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef SAITEK_THROTTLE_QUADRANT_HPP
#define SAITEK_THROTTLE_QUADRANT_HPP

#include <limits.h>
#include <stddef.h>
#include <stdint.h>

struct SaitekThrottleQuadrant {

  //! The data read from the throttle quadrant.
  struct [[gnu::packed]] Data {
    uint8_t left;
    uint8_t right;
    uint8_t middle;
    uint16_t buttons : 9;
    uint16_t : 0;
  };

  //! The minimum delay between successive read operations.
  auto constexpr static readDelayMillis = 25u;

  //! Initialize the throttle quadrant interface.
  auto begin(unsigned clock_pin, unsigned latch_pin, unsigned data_pin) noexcept -> void;

  //! Read the current state of the throttle quadrant.
  auto read() noexcept -> Data;

private:
  //! The time for the clock to propagate.
  auto constexpr static clockDelayMicros = 25u;

  //! The time for the latch to settle.
  auto constexpr static latchDelayMicros = 5u;

  //! The number of buttons on the throttle quadrant.
  auto constexpr static numberOfButtons = 9u;

  auto beginTransaction() -> void;
  auto endTransaction() -> void;

  template<typename T, size_t Bits = sizeof(T) * CHAR_BIT>
  auto doRead() -> T;

  unsigned clock_pin{};
  unsigned latch_pin{};
  unsigned data_pin{};
} static ThrottleQuadrant;

#endif