OpenSourceSimWheelESP32
Open-source wireless steering wheel/button box for ESP32 boards
Loading...
Searching...
No Matches
InputValidation.hpp
Go to the documentation of this file.
1
12#pragma once
13
14//-------------------------------------------------------------------
15// Imports
16//-------------------------------------------------------------------
17
19
20//-------------------------------------------------------------------
21// API
22//-------------------------------------------------------------------
23
24namespace internals
25{
26 namespace inputs
27 {
32 namespace validate
33 {
40 template <typename GPIOtype>
41 inline void reserve(const std::set<GPIOtype> &collection)
42 {
43 for (auto item : collection)
44 item.reserve();
45 }
46
52 inline void buttonMatrix(const ButtonMatrix &matrix)
53 {
54 OutputGPIOCollection selectors;
56 uint128_t previousInputNumbers = InputNumber::booked();
57 for (ButtonMatrix::const_iterator row = matrix.cbegin();
58 row != matrix.cend();
59 row++)
60 {
61 OutputGPIO selectorPin = row->first;
62 selectors.insert(selectorPin);
63 for (std::map<InputGPIO, InputNumber>::const_iterator col = row->second.begin(); col != row->second.end(); col++)
64 {
65 InputGPIO inputPin = col->first;
66 inputs.insert(inputPin);
67 InputNumber inputNumber = col->second;
68 inputNumber.book();
69 }
70 }
71 reserve<OutputGPIO>(selectors);
73 if (previousInputNumbers == InputNumber::booked())
74 throw empty_input_number_set("button matrix");
75 }
76
84 template <typename PinTags>
86 {
87 uint128_t previousInputNumbers = InputNumber::booked();
88 reserve<OutputGPIO>(selectors);
89 for (auto chip : chips)
90 chip.reserve_and_book();
91 if (previousInputNumbers == InputNumber::booked())
92 throw empty_input_number_set("analog multiplexers");
93 }
94
104 OutputGPIO loadPin,
105 OutputGPIO nextPin,
106 InputGPIO inputPin,
107 const ShiftRegisterChain &chain)
108 {
109 uint128_t previousInputNumbers = InputNumber::booked();
110 loadPin.reserve();
111 nextPin.reserve();
112 inputPin.reserve();
113 for (auto chip : chain)
114 {
115 for (ShiftRegisterChip::const_iterator i = chip.begin(); i != chip.end(); i++)
116 (i->second).book();
117 }
118 if (previousInputNumbers == InputNumber::booked())
119 throw empty_input_number_set("PISO shift registers");
120 }
121
128 template <typename PinTags>
130 {
131 uint128_t previousInputNumbers = InputNumber::booked();
132 for (auto i = chip.begin(); i != chip.end(); i++)
133 (i->second).book();
134 if (previousInputNumbers == InputNumber::booked())
135 throw empty_input_number_set("GPIO expander");
136 }
137
146 inline void rotaryEncoder(
147 InputGPIO dtPin,
148 InputGPIO clkPin,
149 InputNumber cw,
150 InputNumber ccw)
151 {
152 uint128_t previousInputNumbers = InputNumber::booked();
153 dtPin.reserve();
154 clkPin.reserve();
155 cw.book();
156 ccw.book();
157 if (previousInputNumbers == InputNumber::booked())
158 throw empty_input_number_set("rotary encoder");
159 if (cw == UNSPECIFIED::VALUE)
160 throw std::runtime_error("Useless rotary encoder: no input number for clockwise rotation");
161 if (ccw == UNSPECIFIED::VALUE)
162 throw std::runtime_error("Useless rotary encoder: no input number for counter-clockwise rotation");
163 if (cw == ccw)
164 throw std::runtime_error("Useless rotary encoder: same input numbers for clockwise and counter-clockwise");
165 }
166
173 inline void button(InputGPIO pin, InputNumber inputNumber)
174 {
175 pin.reserve();
176 if (inputNumber == UNSPECIFIED::VALUE)
177 throw empty_input_number_set("single button");
178 inputNumber.book();
179 }
180
191 inline void joystick(
192 ADC_GPIO xAxisPin,
193 ADC_GPIO yAxisPin,
194 InputNumber up,
195 InputNumber down,
196 InputNumber left,
197 InputNumber right)
198 {
199 if (xAxisPin == yAxisPin)
200 throw std::runtime_error(
201 "inputs::addJoystick() is using the same pin for both axes");
202 xAxisPin.reserve();
203 yAxisPin.reserve();
204 up.book();
205 down.book();
206 left.book();
207 right.book();
208 }
209
210 } // namespace validate
211 } // namespace inputs
212} // namespace internals
Configure input hardware and specify input numbers.
std::map< PinTags, InputNumber > GPIOExpanderChip
Generic GPIO expander chip.
std::vector< AnalogMultiplexerChip< PinTags > > AnalogMultiplexerGroup
Group of analog multiplexer chips sharing the same selector pins.
std::vector< ShiftRegisterChip > ShiftRegisterChain
Chain of PISO shift registers for switches.
std::map< OutputGPIO, std::map< InputGPIO, InputNumber > > ButtonMatrix
Button matrix specification.
@ VALUE
Unspecified value.
std::set< InputGPIO > InputGPIOCollection
Collection of input GPIOs.
std::set< OutputGPIO > OutputGPIOCollection
Collection of output GPIOs.
Exception for empty input number specifications.
Everything related to hardware inputs and their events.
Definition SimWheel.hpp:31
void analogMultiplexer(const OutputGPIOCollection &selectors, const AnalogMultiplexerGroup< PinTags > chips)
Validate a group of analog multiplexers.
void buttonMatrix(const ButtonMatrix &matrix)
Validate a button matrix.
void joystick(ADC_GPIO xAxisPin, ADC_GPIO yAxisPin, InputNumber up, InputNumber down, InputNumber left, InputNumber right)
Validate a joystick.
void shiftRegisterChain(OutputGPIO loadPin, OutputGPIO nextPin, InputGPIO inputPin, const ShiftRegisterChain &chain)
Validate a chain of PISO shift registers.
void GPIOExpander(const GPIOExpanderChip< PinTags > &chip)
Validate a GPIO expander.
void reserve(const std::set< GPIOtype > &collection)
Reserve a collection of GPIO pins.
void rotaryEncoder(InputGPIO dtPin, InputGPIO clkPin, InputNumber cw, InputNumber ccw)
Validate a rotary encoder.
void button(InputGPIO pin, InputNumber inputNumber)
Validate a single button.
ADC-capable GPIO pin number.
void reserve() const
Reserve this GPIO for exclusive use.
Input-capable GPIO pin number.
Firmware-defined input numbers in the range [0,127] or unspecified.
void book() const
Book as in use.
static uint128_t booked()
Get a bitmap of all booked input numbers.
Output-capable GPIO pin number.
128-bit integer