-
Notifications
You must be signed in to change notification settings - Fork 186
/
MultipleButtons.ino
72 lines (60 loc) · 2.2 KB
/
MultipleButtons.ino
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* This code programs a number of pins on an ESP32 as buttons on a BLE gamepad
*
* It uses arrays to cut down on code
*
* Before using, adjust the numOfButtons, buttonPins and physicalButtons to suit your senario
*
*/
#include <Arduino.h>
#include <BleGamepad.h> // https://github.com/lemmingDev/ESP32-BLE-Gamepad
BleGamepad bleGamepad;
#define numOfButtons 10
byte previousButtonStates[numOfButtons];
byte currentButtonStates[numOfButtons];
byte buttonPins[numOfButtons] = {0, 35, 17, 18, 19, 23, 25, 26, 27, 32};
byte physicalButtons[numOfButtons] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
void setup()
{
for (byte currentPinIndex = 0; currentPinIndex < numOfButtons; currentPinIndex++)
{
pinMode(buttonPins[currentPinIndex], INPUT_PULLUP);
previousButtonStates[currentPinIndex] = HIGH;
currentButtonStates[currentPinIndex] = HIGH;
}
BleGamepadConfiguration bleGamepadConfig;
bleGamepadConfig.setAutoReport(false);
bleGamepadConfig.setButtonCount(numOfButtons);
bleGamepad.begin(&bleGamepadConfig);
// changing bleGamepadConfig after the begin function has no effect, unless you call the begin function again
}
void loop()
{
if (bleGamepad.isConnected())
{
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
{
currentButtonStates[currentIndex] = digitalRead(buttonPins[currentIndex]);
if (currentButtonStates[currentIndex] != previousButtonStates[currentIndex])
{
if (currentButtonStates[currentIndex] == LOW)
{
bleGamepad.press(physicalButtons[currentIndex]);
}
else
{
bleGamepad.release(physicalButtons[currentIndex]);
}
}
}
if (currentButtonStates != previousButtonStates)
{
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
{
previousButtonStates[currentIndex] = currentButtonStates[currentIndex];
}
bleGamepad.sendReport();
}
delay(20);
}
}