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 void reserve(const std::vector<GPIOtype> &collection)
42 {
43 for (auto item : collection)
44 item.reserve();
45 }
46
52 void buttonMatrix(const ButtonMatrix &matrix)
53 {
54 OutputGPIOCollection selectors;
56 uint64_t previousInputNumbers = InputNumber::booked();
57 for (ButtonMatrix::const_iterator row = matrix.begin(); row != matrix.end(); row++)
58 {
59 OutputGPIO selectorPin = row->first;
60 addIfNotExists<OutputGPIO>(selectorPin, selectors);
61 for (std::map<InputGPIO, InputNumber>::const_iterator col = row->second.begin(); col != row->second.end(); col++)
62 {
63 InputGPIO inputPin = col->first;
65 InputNumber inputNumber = col->second;
66 inputNumber.book();
67 }
68 }
69 reserve<OutputGPIO>(selectors);
71 if (previousInputNumbers == InputNumber::booked())
72 throw empty_input_number_set("button matrix");
73 }
74
82 template <typename PinTags>
84 {
85 uint64_t previousInputNumbers = InputNumber::booked();
86 reserve<OutputGPIO>(selectors);
87 for (auto chip : chips)
88 chip.reserve_and_book();
89 if (previousInputNumbers == InputNumber::booked())
90 throw empty_input_number_set("analog multiplexers");
91 }
92
102 OutputGPIO loadPin,
103 OutputGPIO nextPin,
104 InputGPIO inputPin,
105 const ShiftRegisterChain &chain)
106 {
107 uint64_t previousInputNumbers = InputNumber::booked();
108 loadPin.reserve();
109 nextPin.reserve();
110 inputPin.reserve();
111 for (auto chip : chain)
112 {
113 for (ShiftRegisterChip::const_iterator i = chip.begin(); i != chip.end(); i++)
114 (i->second).book();
115 }
116 if (previousInputNumbers == InputNumber::booked())
117 throw empty_input_number_set("PISO shift registers");
118 }
119
126 template <typename PinTags>
128 {
129 uint64_t previousInputNumbers = InputNumber::booked();
130 for (auto i = chip.begin(); i != chip.end(); i++)
131 (i->second).book();
132 if (previousInputNumbers == InputNumber::booked())
133 throw empty_input_number_set("GPIO expander");
134 }
135
145 {
146 uint64_t previousInputNumbers = InputNumber::booked();
147 dtPin.reserve();
148 clkPin.reserve();
149 cw.book();
150 ccw.book();
151 if (previousInputNumbers == InputNumber::booked())
152 throw empty_input_number_set("rotary encoder");
153 if (cw == UNSPECIFIED::VALUE)
154 throw std::runtime_error("Useless rotary encoder: no input number for clockwise rotation");
155 if (ccw == UNSPECIFIED::VALUE)
156 throw std::runtime_error("Useless rotary encoder: no input number for counter-clockwise rotation");
157 if (cw == ccw)
158 throw std::runtime_error("Useless rotary encoder: same input numbers for clockwise and counter-clockwise");
159 }
160
167 void button(InputGPIO pin, InputNumber inputNumber)
168 {
169 pin.reserve();
170 if (inputNumber == UNSPECIFIED::VALUE)
171 throw empty_input_number_set("single button");
172 inputNumber.book();
173 }
174
182 {
183 uint8_t pinCount = inputs.size();
184 if ((pinCount < 2) || (pinCount > 8))
185 throw std::runtime_error("Wrong count of input pins in a coded rotary switch");
187 uint8_t maxIndex = (1 << pinCount); // = 2^pinCount
188 for (RotaryCodedSwitch::const_iterator i = spec.begin(); i != spec.end(); i++)
189 {
190 if (i->first >= maxIndex)
191 throw std::runtime_error(
192 "Invalid position ()" +
193 std::to_string(i->first) +
194 ") in a coded rotary switch. Valid range is [0," +
195 std::to_string(maxIndex) +
196 ")");
197 (i->second).book();
198 }
199 }
200 } // namespace validate
201 } // namespace inputs
202} // 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< uint8_t, InputNumber > RotaryCodedSwitch
Rotary coded switch.
std::map< OutputGPIO, std::map< InputGPIO, InputNumber > > ButtonMatrix
Button matrix specification.
bool addIfNotExists(T item, std::vector< T > &vector)
Add an item to a collection without duplicates.
@ VALUE
Unspecified value.
std::vector< InputGPIO > InputGPIOCollection
Collection of input GPIOs.
std::vector< 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 codedRotarySwitch(const RotaryCodedSwitch &spec, const InputGPIOCollection &inputs)
Validate a coded rotary switch.
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 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 rotaryEncoder(InputGPIO dtPin, InputGPIO clkPin, InputNumber cw, InputNumber ccw)
Validate a rotary encoder.
void reserve(const std::vector< GPIOtype > &collection)
Reserve a collection of GPIO pins.
void button(InputGPIO pin, InputNumber inputNumber)
Validate a single button.
void reserve() const
Reserve this GPIO for exclusive use.
Input-capable GPIO pin number.
Firmware-defined input numbers in the range [0,63] or unspecified.
void book() const
Book as in use.
static uint64_t booked()
Get a bitmap of all booked input numbers.
Output-capable GPIO pin number.