fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
2.9 KiB102 linesraw
1#ifndef _INPUT_SWITCHMATRIXINPUT_HPP
2#define _INPUT_SWITCHMATRIXINPUT_HPP
3
4#include "core/InputSource.hpp"
5#include "core/state.hpp"
6#include "gpio.hpp"
7#include "util/state_util.hpp"
8
9#include <config.pb.h>
10
11#define NA BTN_UNSPECIFIED
12
13enum class DiodeDirection {
14    ROW2COL,
15    COL2ROW,
16};
17
18template <size_t num_rows, size_t num_cols> class SwitchMatrixInput : public InputSource {
19  public:
20    SwitchMatrixInput(
21        const uint row_pins[num_rows],
22        const uint col_pins[num_cols],
23        const Button (&matrix)[num_rows][num_cols],
24        DiodeDirection direction
25    )
26        : _matrix(matrix) {
27        _direction = direction;
28
29        if (_direction == DiodeDirection::ROW2COL) {
30            _num_outputs = num_cols;
31            _num_inputs = num_rows;
32            _output_pins = col_pins;
33            _input_pins = row_pins;
34        } else {
35            _num_outputs = num_rows;
36            _num_inputs = num_cols;
37            _output_pins = row_pins;
38            _input_pins = col_pins;
39        }
40
41        // Initialize output pins.
42        for (size_t i = 0; i < _num_outputs; i++) {
43            gpio::init_pin(_output_pins[i], gpio::GpioMode::GPIO_INPUT_PULLUP);
44        }
45
46        // Initialize input pins.
47        for (size_t i = 0; i < _num_inputs; i++) {
48            gpio::init_pin(_input_pins[i], gpio::GpioMode::GPIO_INPUT_PULLUP);
49        }
50    }
51
52    ~SwitchMatrixInput() {
53        // Make sure all pins are set back to inputs.
54        for (size_t i = 0; i < _num_outputs; i++) {
55            gpio::init_pin(_output_pins[i], gpio::GpioMode::GPIO_INPUT_PULLUP);
56        }
57    }
58
59    InputScanSpeed ScanSpeed() { return InputScanSpeed::FAST; }
60
61    void UpdateInputs(InputState &inputs) {
62        for (size_t i = 0; i < _num_outputs; i++) {
63            // Activate the column/row.
64            gpio::init_pin(_output_pins[i], gpio::GpioMode::GPIO_OUTPUT);
65            gpio::write_digital(_output_pins[i], 0);
66
67            // Read each cell in the column/row.
68            for (size_t j = 0; j < _num_inputs; j++) {
69                UpdateButtonState(
70                    inputs,
71                    _direction == DiodeDirection::ROW2COL ? j : i,
72                    _direction == DiodeDirection::ROW2COL ? i : j,
73                    !gpio::read_digital(_input_pins[j])
74                );
75            }
76
77            // Deactivate the column/row.
78            gpio::init_pin(_output_pins[i], gpio::GpioMode::GPIO_INPUT_PULLUP);
79        }
80    }
81
82  protected:
83    size_t _num_outputs;
84    size_t _num_inputs;
85    const uint *_output_pins;
86    const uint *_input_pins;
87    const Button (&_matrix)[num_rows][num_cols];
88    DiodeDirection _direction;
89
90  private:
91    virtual void UpdateButtonState(
92        InputState &inputs,
93        size_t col_index,
94        size_t row_index,
95        bool pressed
96    ) {
97        Button button = _matrix[col_index][row_index];
98        set_button(inputs.buttons, button, pressed);
99    };
100};
101
102#endif