-
Notifications
You must be signed in to change notification settings - Fork 1
/
rally.ino
222 lines (199 loc) · 47 KB
/
rally.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright (c) 2022, Ununnilium, https://github.com/Ununnilium
Copyright (c) 2021, NordicRally, https://www.instructables.com/Rally-Controller-With-Bluetooth/
Written by NordicRally, modified by Ununnilium.
For all code written by Ununnilium, the ISC license applies:
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
For code written by NordicRally ask him for license details.
*/
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include"esp_gap_bt_api.h"
#include "esp_err.h"
#include <Bounce2.h>
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;//("BT-Media", "ESP32-duke24", 74);
int switch_mode_btn = 18;
int c_play_pause_btn = 24;
int up_volume_up_btn = 30;
int down_volume_down_btn = 29;
int left_btn = 26;
int right_btn = 25;
int plus_next_track_btn = 10;
int minus_previous_track_btn = 13;
int last_switch_mode_state = 0;
#define REMOVE_BONDED_DEVICES 0 // <- Set to 0 to view all bonded devices addresses, set to 1 to remove
#define PAIR_MAX_DEVICES 9
uint8_t pairedDeviceBtAddr[PAIR_MAX_DEVICES][6];
char bda_str[18];
#define NUM_BUTTONS 7
const uint8_t BUTTON_PINS[NUM_BUTTONS] = {c_play_pause_btn, up_volume_up_btn, down_volume_down_btn, left_btn, right_btn, plus_next_track_btn, minus_previous_track_btn};
Bounce * buttons = new Bounce[NUM_BUTTONS];
unsigned long buttonPressTimeStampu;
unsigned long buttonPressTimeStampd;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
pinMode(switch_mode_btn, INPUT_PULLUP);
checkReset() ;
bleKeyboard.begin();
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].attach( BUTTON_PINS[i] , INPUT_PULLUP ); //setup the bounce instance for the current button
buttons[i].interval(100); // interval in ms
}
}
void loop() {
int switch_mode_state = digitalRead(switch_mode_btn);
if (switch_mode_state != last_switch_mode_state) {
last_switch_mode_state = switch_mode_state;
Serial.print("New mode: ");
Serial.println(switch_mode_state);
}
for (int i = 0; i < NUM_BUTTONS; i++) {
// Update the Bounce instance :
buttons[i].update();
// If it fell, flag the need to toggle the LED
}
if ( buttons[0].fell() ) {
Serial.println("C or play/pause button pushed");
if (bleKeyboard.isConnected()) {
if (last_switch_mode_state) {
Serial.println("Pressing the play/pause button via the Bluetooth keyboard");
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
} else {
Serial.println("Pressing the C key via the Bluetooth keyboard");
bleKeyboard.write('C');
}
}
}
if ( buttons[1].fell() ) {
Serial.println("up arrow or volume up button pushed");
if (bleKeyboard.isConnected()) {
if (last_switch_mode_state) {
Serial.println("Pressing the volume up button via the Bluetooth keyboard");
bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
} else {
Serial.println("Pressing the arrow up key via the Bluetooth keyboard");
bleKeyboard.write(KEY_UP_ARROW);
}
}
}
if ( buttons[2].fell() ) {
Serial.println("down arrow or volume down button pushed");
if (bleKeyboard.isConnected()) {
if (last_switch_mode_state) {
Serial.println("Pressing the volume down button via the Bluetooth keyboard");
bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
} else {
Serial.println("Pressing the arrow down key via the Bluetooth keyboard");
bleKeyboard.write(KEY_DOWN_ARROW);
}
}
}
if ( buttons[3].fell() ) {
Serial.println("left arrow button pushed");
if (bleKeyboard.isConnected()) {
if (!last_switch_mode_state) {
Serial.println("Pressing the arrow left key via the Bluetooth keyboard");
bleKeyboard.write(KEY_LEFT_ARROW);
}
}
}
if ( buttons[4].fell() ) {
Serial.println("right arrow button pushed");
if (bleKeyboard.isConnected()) {
if (!last_switch_mode_state) {
Serial.println("Pressing the arrow right key via the Bluetooth keyboard");
bleKeyboard.write(KEY_RIGHT_ARROW);
}
}
}
if ( buttons[5].fell() ) {
Serial.println("+ or next track button pushed");
if (bleKeyboard.isConnected()) {
if (last_switch_mode_state) {
Serial.println("Pressing the vnext track button via the Bluetooth keyboard");
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
} else {
Serial.println("Pressing the + key via the Bluetooth keyboard");
bleKeyboard.write('+');
}
}
}
if ( buttons[6].fell() ) {
Serial.println("- or previous track button pushed");
if (bleKeyboard.isConnected()) {
if (last_switch_mode_state) {
Serial.println("Pressing the previous track button via the Bluetooth keyboard");
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
} else {
Serial.println("Pressing the - key via the Bluetooth keyboard");
bleKeyboard.write('-');
}
}
}
}
bool initBluetooth() {
if (!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if (esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if (esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
return true;
}
char *bda2str(const uint8_t* bda, char *str, size_t size) {
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
return str;
}
void checkReset() {
//initBluetooth();
Serial.print("ESP32 bluetooth address: "); Serial.println(bda2str(esp_bt_dev_get_address(), bda_str, 18));
// Get the numbers of bonded/paired devices in the BT module
int count = esp_bt_gap_get_bond_device_num();
if (!count) {
Serial.println("No bonded device found.");
} else {
Serial.print("Bonded device count: "); Serial.println(count);
if ( abs(count) > PAIR_MAX_DEVICES) {
//count = PAIR_MAX_DEVICES;
Serial.print("Reset bonded device count: "); Serial.println(count);
REMOVE_BONDED_DEVICES == 1;
}
esp_err_t tError = esp_bt_gap_get_bond_device_list(&count, pairedDeviceBtAddr);
if (ESP_OK == tError) {
for (int i = 0; i < count; i++) {
Serial.print("Found bonded device # "); Serial.print(i); Serial.print(" -> ");
Serial.println(bda2str(pairedDeviceBtAddr[i], bda_str, 18));
if (REMOVE_BONDED_DEVICES) {
esp_err_t tError = esp_bt_gap_remove_bond_device(pairedDeviceBtAddr[i]);
if (ESP_OK == tError) {
Serial.print("Removed bonded device # ");
} else {
Serial.print("Failed to remove bonded device # ");
}
Serial.println(i);
}
}
}
}
}