OpenSourceSimWheelESP32
Open-source wireless steering wheel/button box for ESP32 boards
Loading...
Searching...
No Matches
Testing.hpp
Go to the documentation of this file.
1
12#pragma once
13
14// ------------------------------------------------------------
15// Imports
16// ------------------------------------------------------------
17
19
21
22// ------------------------------------------------------------
23// ------------------------------------------------------------
24// Pin-out plan
25// ------------------------------------------------------------
26// ------------------------------------------------------------
27
28// -------------------------------------------------------------
29#if defined(CONFIG_IDF_TARGET_ESP32)
30// ESP32 -------------------------------------------------------
31
32// Button matrix pins for testing
33#define TEST_BTNMTX_ROW1 GPIO_NUM_12
34#define TEST_BTNMTX_ROW2 GPIO_NUM_13
35#define TEST_BTNMTX_COL1 GPIO_NUM_26
36#define TEST_BTNMTX_COL2 GPIO_NUM_27
37#define TEST_BTNMTX_COL3 GPIO_NUM_14
38
39// Relative rotary encoder pins for testing
40#define TEST_ROTARY_SW GPIO_NUM_34
41#define TEST_ROTARY_CLK GPIO_NUM_35
42#define TEST_ROTARY_DT GPIO_NUM_32
43#define TEST_ROTARY_ALPS_A GPIO_NUM_33
44#define TEST_ROTARY_ALPS_B GPIO_NUM_25
45
46// Analog multiplexer pins for testing
47#define TEST_AMTXER_SEL1 GPIO_NUM_5
48#define TEST_AMTXER_SEL2 GPIO_NUM_18
49#define TEST_AMTXER_SEL3 GPIO_NUM_19
50#define TEST_AMTXER_IN1 GPIO_NUM_4
51#define TEST_AMTXER_IN2 GPIO_NUM_16
52
53// Analog clutch paddles for testing
54#define TEST_ANALOG_PIN1 GPIO_NUM_36
55#define TEST_ANALOG_PIN2 GPIO_NUM_39
56
57// Battery monitor pins for testing
58#define TEST_BATTERY_READ_ENABLE GPIO_NUM_17
59#define TEST_BATTERY_READ GPIO_NUM_15
60
61// Latch circuit pins for testing
62#define TEST_LATCH_PIN GPIO_NUM_23
63
64// PISO shift registers testing
65#define TEST_SR_SERIAL GPIO_NUM_36
66#define TEST_SR_LOAD GPIO_NUM_32
67#define TEST_SR_NEXT GPIO_NUM_33
68
69// Wake up
70#define TEST_POWER_PIN TEST_ROTARY_SW
71
72// Simple shift light UI
73#define TEST_SIMPLE_SHIFT_LIGHT_PIN GPIO_NUM_33
74
75// LED strip
76#define TEST_D_OUT GPIO_NUM_21
77#define TEST_LEVEL_SHIFTER true
78
79// Secondary I2C bus
80#define TEST_SECONDARY_SDA SDA
81#define TEST_SECONDARY_SCL SCL
82
83// -------------------------------------------------------------
84#elif defined(CONFIG_IDF_TARGET_ESP32S3)
85// -------------------------------------------------------------
86
87// Button matrix pins for testing
88#define TEST_BTNMTX_ROW1 GPIO_NUM_7
89#define TEST_BTNMTX_ROW2 GPIO_NUM_15
90#define TEST_BTNMTX_COL1 GPIO_NUM_4
91#define TEST_BTNMTX_COL2 GPIO_NUM_5
92#define TEST_BTNMTX_COL3 GPIO_NUM_6
93
94// Relative rotary encoder pins for testing
95#define TEST_ROTARY_SW GPIO_NUM_10
96#define TEST_ROTARY_CLK GPIO_NUM_12
97#define TEST_ROTARY_DT GPIO_NUM_11
98#define TEST_ROTARY_ALPS_A GPIO_NUM_1
99#define TEST_ROTARY_ALPS_B GPIO_NUM_38
100
101// Analog multiplexer pins for testing
102#define TEST_AMTXER_SEL1 GPIO_NUM_21
103#define TEST_AMTXER_SEL2 GPIO_NUM_47
104#define TEST_AMTXER_SEL3 GPIO_NUM_48
105#define TEST_AMTXER_IN1 GPIO_NUM_16
106#define TEST_AMTXER_IN2 GPIO_NUM_17
107
108// Analog clutch paddles for testing
109#define TEST_ANALOG_PIN1 GPIO_NUM_13
110#define TEST_ANALOG_PIN2 GPIO_NUM_14
111
112// Battery monitor pins for testing
113#define TEST_BATTERY_READ_ENABLE GPIO_NUM_42
114#define TEST_BATTERY_READ GPIO_NUM_2
115
116// Latch circuit pins for testing
117#define TEST_LATCH_PIN GPIO_NUM_18
118
119// PISO shift registers testing
120#define TEST_SR_SERIAL GPIO_NUM_41
121#define TEST_SR_LOAD GPIO_NUM_39
122#define TEST_SR_NEXT GPIO_NUM_40
123
124// Wake up
125#define TEST_POWER_PIN TEST_ROTARY_SW
126
127// Simple shift light UI
128#define TEST_SIMPLE_SHIFT_LIGHT_PIN GPIO_NUM_40
129
130// Secondary I2C bus
131#define TEST_SECONDARY_SDA GPIO_NUM_21
132#define TEST_SECONDARY_SCL GPIO_NUM_47
133
134// LED strip
135#define TEST_D_OUT GPIO_NUM_39
136#define TEST_LEVEL_SHIFTER false
137
138// -------------------------------------------------------------
139#elif defined(CONFIG_IDF_TARGET_ESP32C3)
140// -------------------------------------------------------------
141
142#warning The ESP32C3 board is not configured for most test
143
144// Button matrix pins for testing
145#define TEST_BTNMTX_ROW1 GPIO_NUM_NC
146#define TEST_BTNMTX_ROW2 GPIO_NUM_NC
147#define TEST_BTNMTX_COL1 GPIO_NUM_NC
148#define TEST_BTNMTX_COL2 GPIO_NUM_NC
149#define TEST_BTNMTX_COL3 GPIO_NUM_NC
150
151// Relative rotary encoder pins for testing
152#define TEST_ROTARY_SW GPIO_NUM_NC
153#define TEST_ROTARY_CLK GPIO_NUM_NC
154#define TEST_ROTARY_DT GPIO_NUM_NC
155#define TEST_ROTARY_ALPS_A GPIO_NUM_NC
156#define TEST_ROTARY_ALPS_B GPIO_NUM_NC
157
158// Analog multiplexer pins for testing
159#define TEST_AMTXER_SEL1 GPIO_NUM_NC
160#define TEST_AMTXER_SEL2 GPIO_NUM_NC
161#define TEST_AMTXER_SEL3 GPIO_NUM_NC
162#define TEST_AMTXER_IN1 GPIO_NUM_NC
163#define TEST_AMTXER_IN2 GPIO_NUM_NC
164
165// Analog clutch paddles for testing
166#define TEST_ANALOG_PIN1 GPIO_NUM_NC
167#define TEST_ANALOG_PIN2 GPIO_NUM_NC
168
169// Battery monitor pins for testing
170#define TEST_BATTERY_READ_ENABLE GPIO_NUM_NC
171#define TEST_BATTERY_READ GPIO_NUM_NC
172
173// Latch circuit pins for testing
174#define TEST_LATCH_PIN GPIO_NUM_NC
175
176// PISO shift registers testing
177#define TEST_SR_SERIAL GPIO_NUM_NC
178#define TEST_SR_LOAD GPIO_NUM_NC
179#define TEST_SR_NEXT GPIO_NUM_NC
180
181// Wake up
182#define TEST_POWER_PIN GPIO_NUM_2
183
184// Simple shift light UI
185#define TEST_SIMPLE_SHIFT_LIGHT_PIN GPIO_NUM_NC
186
187// -------------------------------------------------------------
188#else
189// -------------------------------------------------------------
190#error Board not configured for testing in this project
191#endif
192
193// ------------------------------------------------------------
194// ------------------------------------------------------------
195// Globals
196// ------------------------------------------------------------
197// ------------------------------------------------------------
198
199// Power latch
200#define TEST_LATCH_MODE PowerLatchMode::POWER_OPEN_DRAIN
201#define TEST_LATCH_DELAY 5000
202
203// Shift registers
204#define TEST_SR_BUTTONS_COUNT 17
205
206// GPIO expanders
207#define MCP23017_I2C_ADDR3 7
208#define PCF8574_I2C_ADDR3 0
209
210// ------------------------------------------------------------
211// ------------------------------------------------------------
212// Macros
213// ------------------------------------------------------------
214// ------------------------------------------------------------
215
216#if !CD_CI
217#include <Arduino.h> // For conditional compilation
218#include <stdarg.h> // For variadic functions
219
220inline void debugPrintBegin()
221{
222 Serial0.begin(115200);
223#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
224 USBSerial.begin(115200);
225#endif
226#if ARDUINO_USB_CDC_ON_BOOT && ARDUINO_USB_MODE
227 HWCDCSerial.begin(115200);
228#endif
229}
230
231inline void debugPrintf(const char *format, ...)
232{
233 va_list args;
234 va_start(args, format);
235 Serial0.vprintf(format, args);
236#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
237 USBSerial.vprintf(format, args);
238#endif
239#if ARDUINO_USB_CDC_ON_BOOT && ARDUINO_USB_MODE
240 HWCDCSerial.vprintf(format, args);
241#endif
242 va_end(args);
243}
244
245inline void debugPrintBool(uint64_t state, uint8_t bitCount = 0)
246{
247 int maxBitCount = (sizeof(uint64_t) * 8);
248 if ((bitCount == 0) || (bitCount > maxBitCount))
249 bitCount = maxBitCount;
250 for (int i = (bitCount - 1); i >= 0; i--)
251 {
252 if ((1ULL << i) & state)
253 debugPrintf("1");
254 else
255 debugPrintf("0");
256 }
257}
258#endif // !CD_CI
259
260void setDebugInputNumbers(ButtonMatrix &instance)
261{
262 instance[TEST_BTNMTX_ROW1][TEST_BTNMTX_COL3] = 2;
263 instance[TEST_BTNMTX_ROW2][TEST_BTNMTX_COL3] = 3;
264 instance[TEST_BTNMTX_ROW2][TEST_BTNMTX_COL2] = 4;
265 instance[TEST_BTNMTX_ROW1][TEST_BTNMTX_COL2] = 5;
266 instance[TEST_BTNMTX_ROW1][TEST_BTNMTX_COL1] = 6;
267 instance[TEST_BTNMTX_ROW2][TEST_BTNMTX_COL1] = 7;
268}
269
270OutputGPIOCollection getDebugMuxSelectors()
271{
272 return {TEST_AMTXER_SEL1, TEST_AMTXER_SEL2, TEST_AMTXER_SEL3};
273}
274
275void setDebugInputNumbers(AnalogMultiplexerGroup<Mux8Pin> &instance)
276{
277 AnalogMultiplexerChip8 chip1(TEST_AMTXER_IN1), chip2(TEST_AMTXER_IN2);
278 chip1[Mux8Pin::A0] = 20;
279 chip1[Mux8Pin::A7] = 21;
280 chip2[Mux8Pin::A3] = 22;
281 chip2[Mux8Pin::A5] = 23;
282 instance = {chip1, chip2};
283}
284
285void setDebugInputNumbers(ShiftRegisterChain &instance, InputNumber &SER)
286{
287 ShiftRegisterChip chip1, chip2;
288 chip1[SR8Pin::E] = 2;
289 chip1[SR8Pin::B] = 4;
290 chip2[SR8Pin::H] = 3;
291 chip2[SR8Pin::C] = 5;
292 SER = 6;
293 instance = {chip1, chip2};
294}
295
296void setDebugInputNumbers(MCP23017Expander &instance)
297{
298 instance[MCP23017Pin::GPA0] = 10;
299 instance[MCP23017Pin::GPA1] = 11;
300 instance[MCP23017Pin::GPA2] = 12;
301 instance[MCP23017Pin::GPA3] = 13;
302 instance[MCP23017Pin::GPA4] = 14;
303 instance[MCP23017Pin::GPA5] = 15;
304 instance[MCP23017Pin::GPA6] = 16;
305 instance[MCP23017Pin::GPA7] = 17;
306 instance[MCP23017Pin::GPB0] = 20;
307 instance[MCP23017Pin::GPB1] = 21;
308 instance[MCP23017Pin::GPB2] = 22;
309 instance[MCP23017Pin::GPB3] = 23;
310 instance[MCP23017Pin::GPB4] = 24;
311 instance[MCP23017Pin::GPB5] = 25;
312 instance[MCP23017Pin::GPB6] = 26;
313 instance[MCP23017Pin::GPB7] = 27;
314}
315
316void setDebugInputNumbers(PCF8574Expander &instance)
317{
318 instance[PCF8574Pin::P0] = 30;
319 instance[PCF8574Pin::P1] = 31;
320 instance[PCF8574Pin::P2] = 32;
321 instance[PCF8574Pin::P3] = 33;
322 instance[PCF8574Pin::P4] = 34;
323 instance[PCF8574Pin::P5] = 35;
324 instance[PCF8574Pin::P6] = 36;
325 instance[PCF8574Pin::P7] = 37;
326}
327
Configure input hardware and specify input numbers.
std::map< SR8Pin, InputNumber > ShiftRegisterChip
PISO shift register chip.
GPIOExpanderChip< PCF8574Pin > PCF8574Expander
PCF8574 GPIO Expander for switches.
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.
GPIOExpanderChip< MCP23017Pin > MCP23017Expander
MCP23017 GPIO Expander for switches.
std::vector< OutputGPIO > OutputGPIOCollection
Collection of output GPIOs.
Generic analog multiplexer chip.
Firmware-defined input numbers in the range [0,63] or unspecified.