-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG27-Gamepad.ino
155 lines (132 loc) · 4.07 KB
/
G27-Gamepad.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <BleGamepad.h>
#include <Rotary.h>
#include <EasyButton.h>
BleGamepad bleGamepad;
//Tasker: handle Bluetooth an Rotary's on different Cores
TaskHandle_t TaskBT;
TaskHandle_t TaskBTN;
//Global Variables Thread
int nSendBtn = 0;
//KnobLeft-Pins
const int pKnobLeftCLK = 25;
const int pKnobLeftDT = 05;
const int pKnobLeftSW = 12; //Change this PIN to 11 or 13 since it's a bootstrapping-pin, see: https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card#note-about-gpio12-esp32-only
//KnobRight-Pins
const int pKnobRightCLK = 19;
const int pKnobRightDT = 27;
const int pKnobRightSW = 02;
//Define Rotary Object
Rotary KnobLeft = Rotary(pKnobLeftCLK,pKnobLeftDT);
Rotary KnobRight = Rotary(pKnobRightCLK,pKnobRightDT);
EasyButton ButtonLeft(pKnobLeftSW);
EasyButton ButtonRight(pKnobRightSW);
void fKnobLeft_Pressed(void) {
Serial.println("KnobLeft: clicked");
nSendBtn = (BUTTON_1);
}
void fKnobLeft_CClock(void) {
Serial.println("KnobLeft: rotating right");
nSendBtn = (BUTTON_2);
}
void fKnobLeft_Clock(void) {
Serial.println("KnobLeft: rotating left");
nSendBtn = (BUTTON_3);
}
void fKnobRight_Pressed(void) {
Serial.println("KnobRight: clicked");
nSendBtn = (BUTTON_4);
}
void fKnobRight_CClock(void) {
Serial.println("KnobRight: rotating right");
nSendBtn = (BUTTON_5);
}
void fKnobRight_Clock(void) {
Serial.println("KnobRight: rotating left");
nSendBtn = (BUTTON_6);
}
void ButtonLeftISR()
{
//When button is being used through external interrupts, parameter INTERRUPT must be passed to read() function
ButtonLeft.read(INTERRUPT);
}
void ButtonRightISR()
{
//When button is being used through external interrupts, parameter INTERRUPT must be passed to read() function
ButtonRight.read(INTERRUPT);
}
void setup() {
Serial.begin(115200);
//#### GamePad Bluetooth
Serial.println("Starting BLE work!");
bleGamepad.begin();
//!!!! GamePad Bluetooth
//#### Setup Buttons
ButtonLeft.begin();
ButtonRight.begin();
ButtonLeft.onPressed(fKnobLeft_Pressed);
ButtonRight.onPressed(fKnobRight_Pressed);
if (ButtonLeft.supportsInterrupt())
{
ButtonLeft.enableInterrupt(ButtonLeftISR);
Serial.println("Button will be used through interrupts");
}
if (ButtonRight.supportsInterrupt())
{
ButtonRight.enableInterrupt(ButtonRightISR);
Serial.println("Button will be used through interrupts");
}
//!!!! Setup Buttons
//#### Set Rotary to CPU 1
xTaskCreatePinnedToCore(
loopRotary, /* Task function. */
"BTNSend", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&TaskBTN, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
//#### Set Bluetooth to CPU 0
xTaskCreatePinnedToCore(
procBTCMD, /* Task function. */
"BTSend", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&TaskBT, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
}
//Send Bluetooth Button on Change of nSendBtn
void procBTCMD(void * pvParameters ) {
while (1) {
if (nSendBtn != 0) {
int btn = nSendBtn;
if(bleGamepad.isConnected()) {
bleGamepad.press(btn);
delay(150);
bleGamepad.release(btn);
}
nSendBtn = 0;
}
delay(1); // <- Watchdog needs this
}
}
//Process rotarys
void loopRotary(void * pvParameters ) {
//Proccess Rotray
while (true) {
unsigned char KLres = KnobLeft.process();
if (KLres == DIR_CW) {
fKnobLeft_Clock();
} else if (KLres == DIR_CCW) {
fKnobLeft_CClock();
}
unsigned char KRres = KnobRight.process();
if (KRres == DIR_CW) {
fKnobRight_Clock();
} else if (KRres == DIR_CCW) {
fKnobRight_CClock();
}
}
}
void loop() {
}