Skip to content

Commit 68b515b

Browse files
Update sketch to use new "hasData" API
Include Bluepad32 + BTstack and missing components. Easier to diff
1 parent 70e534a commit 68b515b

File tree

801 files changed

+284491
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

801 files changed

+284491
-70
lines changed

bluepad32_files/libraries/Bluepad32_ESP32/examples/Controller/Controller.ino

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ void dumpGamepad(ControllerPtr ctl) {
4646
"idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: %4d, %4d, brake: %4d, throttle: %4d, "
4747
"misc: 0x%02x, gyro x:%6d y:%6d z:%6d, accel x:%6d y:%6d z:%6d\n",
4848
ctl->index(), // Controller Index
49-
ctl->dpad(), // DPAD
49+
ctl->dpad(), // D-pad
5050
ctl->buttons(), // bitmask of pressed buttons
5151
ctl->axisX(), // (-511 - 512) left X Axis
5252
ctl->axisY(), // (-511 - 512) left Y axis
5353
ctl->axisRX(), // (-511 - 512) right X axis
5454
ctl->axisRY(), // (-511 - 512) right Y axis
5555
ctl->brake(), // (0 - 1023): brake button
5656
ctl->throttle(), // (0 - 1023): throttle (AKA gas) button
57-
ctl->miscButtons(), // bitmak of pressed "misc" buttons
57+
ctl->miscButtons(), // bitmask of pressed "misc" buttons
5858
ctl->gyroX(), // Gyro X
5959
ctl->gyroY(), // Gyro Y
6060
ctl->gyroZ(), // Gyro Z
@@ -187,6 +187,24 @@ void processBalanceBoard(ControllerPtr ctl) {
187187
dumpBalanceBoard(ctl);
188188
}
189189

190+
void processControllers() {
191+
for (auto myController : myControllers) {
192+
if (myController && myController->isConnected() && myController->hasData()) {
193+
if (myController->isGamepad()) {
194+
processGamepad(myController);
195+
} else if (myController->isMouse()) {
196+
processMouse(myController);
197+
} else if (myController->isKeyboard()) {
198+
processKeyboard(myController);
199+
} else if (myController->isBalanceBoard()) {
200+
processBalanceBoard(myController);
201+
} else {
202+
Serial.println("Unsupported controller");
203+
}
204+
}
205+
}
206+
}
207+
190208
// Arduino setup function. Runs in CPU 1
191209
void setup() {
192210
Serial.begin(115200);
@@ -201,52 +219,31 @@ void setup() {
201219
// a "device factory reset", or similar.
202220
// Calling "forgetBluetoothKeys" in setup() just as an example.
203221
// Forgetting Bluetooth keys prevents "paired" gamepads to reconnect.
204-
// But might also fix some connection / re-connection issues.
222+
// But it might also fix some connection / re-connection issues.
205223
BP32.forgetBluetoothKeys();
206224

207225
// Enables mouse / touchpad support for gamepads that support them.
208-
// When enabled controllers like DualSense and DualShock4 generate two connected devices:
226+
// When enabled, controllers like DualSense and DualShock4 generate two connected devices:
209227
// - First one: the gamepad
210-
// - Second one, which is a "vritual device", is a mouse
211-
// By default it is disabled.
228+
// - Second one, which is a "virtual device", is a mouse.
229+
// By default, it is disabled.
212230
BP32.enableVirtualDevice(false);
213231
}
214232

215-
// Arduino loop function. Runs in CPU 1
233+
// Arduino loop function. Runs in CPU 1.
216234
void loop() {
217-
// This call fetches all the gamepad info from the NINA (ESP32) module.
218-
// Just call this function in your main loop.
219-
// The gamepads pointer (the ones received in the callbacks) gets updated
220-
// automatically.
221-
BP32.update();
222-
223-
// It is safe to always do this before using the gamepad API.
224-
// This guarantees that the gamepad is valid and connected.
225-
for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
226-
ControllerPtr myController = myControllers[i];
235+
// This call fetches all the controllers' data.
236+
// Call this function in your main loop.
237+
bool dataUpdated = BP32.update();
238+
if (dataUpdated)
239+
processControllers();
227240

228-
if (myController && myController->isConnected()) {
229-
if (myController->isGamepad()) {
230-
processGamepad(myController);
231-
} else if (myController->isMouse()) {
232-
processMouse(myController);
233-
} else if (myController->isKeyboard()) {
234-
processKeyboard(myController);
235-
} else if (myController->isBalanceBoard()) {
236-
processBalanceBoard(myController);
237-
} else {
238-
Serial.printf("Data not available yet\n");
239-
continue;
240-
}
241-
// See ArduinoController.h for all the available functions.
242-
}
243-
}
244241
// The main loop must have some kind of "yield to lower priority task" event.
245-
// Otherwise the watchdog will get triggered.
242+
// Otherwise, the watchdog will get triggered.
246243
// If your main loop doesn't have one, just add a simple `vTaskDelay(1)`.
247244
// Detailed info here:
248245
// https://stackoverflow.com/questions/66278271/task-watchdog-got-triggered-the-tasks-did-not-reset-the-watchdog-in-time
249246

250-
// vTaskDelay(1);
247+
// vTaskDelay(1);
251248
delay(150);
252249
}

bluepad32_files/package_esp32_bluepad32_index.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,90 @@
99
"online": "https://discord.gg/r5aMn6Cw5q"
1010
},
1111
"platforms": [
12+
{
13+
"name": "esp32_bluepad32",
14+
"architecture": "esp32",
15+
"version": "4.0-beta.1",
16+
"category": "Contributed",
17+
"url": "https://github.com/ricardoquesada/esp32-arduino-lib-builder/releases/download/4.0-beta.1/esp32-bluepad32-4.0-beta.1.zip",
18+
"archiveFileName": "esp32-bluepad32-4.0-beta.1.zip",
19+
"checksum": "SHA-256:3a3a9f59feb162490928a445bd28a6f1aea7381f1078bc3e3ea20f5c472b1dfa",
20+
"size": "197534696",
21+
"help": {
22+
"online": "https://discord.gg/r5aMn6Cw5q"
23+
},
24+
"boards": [
25+
{
26+
"name": "ESP32 Dev Board"
27+
},
28+
{
29+
"name": "ESP32-S3 Dev Board"
30+
},
31+
{
32+
"name": "ESP32-C3 Dev Board"
33+
},
34+
{
35+
"name": "Arduino Nano ESP32"
36+
}
37+
],
38+
"toolsDependencies": [
39+
{
40+
"packager": "esp32",
41+
"name": "xtensa-esp32-elf-gcc",
42+
"version": "esp-2021r2-patch5-8.4.0"
43+
},
44+
{
45+
"packager": "esp32",
46+
"name": "xtensa-esp32s2-elf-gcc",
47+
"version": "esp-2021r2-patch5-8.4.0"
48+
},
49+
{
50+
"packager": "esp32",
51+
"name": "xtensa-esp32s3-elf-gcc",
52+
"version": "esp-2021r2-patch5-8.4.0"
53+
},
54+
{
55+
"packager": "esp32",
56+
"name": "xtensa-esp-elf-gdb",
57+
"version": "11.2_20220823"
58+
},
59+
{
60+
"packager": "esp32",
61+
"name": "riscv32-esp-elf-gcc",
62+
"version": "esp-2021r2-patch5-8.4.0"
63+
},
64+
{
65+
"packager": "esp32",
66+
"name": "riscv32-esp-elf-gdb",
67+
"version": "11.2_20220823"
68+
},
69+
{
70+
"packager": "esp32",
71+
"name": "openocd-esp32",
72+
"version": "v0.12.0-esp32-20230419"
73+
},
74+
{
75+
"packager": "esp32",
76+
"name": "esptool_py",
77+
"version": "4.5.1"
78+
},
79+
{
80+
"packager": "esp32",
81+
"name": "mkspiffs",
82+
"version": "0.2.3"
83+
},
84+
{
85+
"packager": "esp32",
86+
"name": "mklittlefs",
87+
"version": "3.0.0-gnu12-dc7f933"
88+
},
89+
{
90+
"packager": "arduino",
91+
"name": "dfu-util",
92+
"version": "0.11.0-arduino5"
93+
}
94+
]
95+
},
1296
{
1397
"name": "esp32_bluepad32",
1498
"architecture": "esp32",

bluepad32_files/programmers.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
esptool.name=Esptool
2+
esptool.communication=serial
3+
esptool.protocol=serial
4+
esptool.program.protocol=serial
5+
esptool.program.tool=esptool_py
6+
esptool.program.tool.default=esptool_py
7+
esptool.program.extra_params=
8+
esptool.extra_params=

components/bluepad32/CMakeLists.txt

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
include(CMakePrintHelpers)
2+
3+
set(srcs
4+
"bt/uni_bt.c"
5+
"bt/uni_bt_allowlist.c"
6+
"bt/uni_bt_conn.c"
7+
"bt/uni_bt_hci_cmd.c"
8+
"bt/uni_bt_le.c"
9+
"bt/uni_bt_service.c"
10+
"bt/uni_bt_setup.c"
11+
"controller/uni_balance_board.c"
12+
"controller/uni_controller.c"
13+
"controller/uni_gamepad.c"
14+
"controller/uni_keyboard.c"
15+
"controller/uni_mouse.c"
16+
"parser/uni_hid_parser.c"
17+
"parser/uni_hid_parser_8bitdo.c"
18+
"parser/uni_hid_parser_android.c"
19+
"parser/uni_hid_parser_atari.c"
20+
"parser/uni_hid_parser_ds3.c"
21+
"parser/uni_hid_parser_ds4.c"
22+
"parser/uni_hid_parser_ds5.c"
23+
"parser/uni_hid_parser_generic.c"
24+
"parser/uni_hid_parser_icade.c"
25+
"parser/uni_hid_parser_keyboard.c"
26+
"parser/uni_hid_parser_mouse.c"
27+
"parser/uni_hid_parser_nimbus.c"
28+
"parser/uni_hid_parser_ouya.c"
29+
"parser/uni_hid_parser_psmove.c"
30+
"parser/uni_hid_parser_smarttvremote.c"
31+
"parser/uni_hid_parser_steam.c"
32+
"parser/uni_hid_parser_switch.c"
33+
"parser/uni_hid_parser_wii.c"
34+
"parser/uni_hid_parser_xboxone.c"
35+
"platform/uni_platform.c"
36+
"uni_circular_buffer.c"
37+
"uni_hid_device.c"
38+
"uni_init.c"
39+
"uni_joystick.c"
40+
"uni_log.c"
41+
"uni_property.c"
42+
"uni_utils.c"
43+
"uni_version.c"
44+
"uni_virtual_device.c")
45+
46+
if(CONFIG_IDF_TARGET_ESP32 OR PICO_SDK_VERSION_STRING OR BLUEPAD32_TARGET_LINUX)
47+
# Bluetooth Classic files used by:
48+
# - ESP32
49+
# - Pico W
50+
# - Linux (assuming BT dongle supports BR/EDR)
51+
list(APPEND srcs
52+
# BR/EDR code only gets compiled on ESP32
53+
"bt/uni_bt_bredr.c"
54+
"bt/uni_bt_sdp.c")
55+
endif()
56+
57+
if(IDF_TARGET)
58+
# Files that are meant to be compiled for any ESP32 target like
59+
# - ESP32
60+
# - ESP32-S3
61+
# - etc...
62+
# This files call ESP-IDF functions and should be ported/converted
63+
# so that can be called from other targets like Pico W
64+
list(APPEND srcs
65+
"arch/uni_console_esp32.c"
66+
"arch/uni_system_esp32.c"
67+
"arch/uni_log_esp32.c"
68+
"arch/uni_property_esp32.c"
69+
"arch/uni_uart_esp32.c"
70+
"uni_gpio.c"
71+
"uni_mouse_quadrature.c")
72+
elseif(PICO_SDK_VERSION_STRING)
73+
list(APPEND srcs
74+
"arch/uni_console_pico.c"
75+
"arch/uni_system_pico.c"
76+
"arch/uni_log_pico.c"
77+
"arch/uni_property_pico.c"
78+
"arch/uni_uart_pico.c")
79+
elseif(BLUEPAD32_TARGET_LINUX)
80+
list(APPEND srcs
81+
"arch/uni_console_linux.c"
82+
"arch/uni_system_linux.c"
83+
"arch/uni_log_linux.c"
84+
"arch/uni_property_linux.c"
85+
"arch/uni_uart_linux.c")
86+
else()
87+
message(FATAL_ERROR "Define target")
88+
endif()
89+
90+
if(CONFIG_IDF_TARGET_ESP32)
91+
# Files that are only meant to be compiled on ESP32 (original)
92+
list(APPEND srcs
93+
# Place here files that are ESP32-only
94+
"platform/uni_platform_mightymiggy.c"
95+
"platform/uni_platform_nina.c"
96+
"platform/uni_platform_unijoysticle.c"
97+
"platform/uni_platform_unijoysticle_2.c"
98+
"platform/uni_platform_unijoysticle_2plus.c"
99+
"platform/uni_platform_unijoysticle_800xl.c"
100+
"platform/uni_platform_unijoysticle_a500.c"
101+
"platform/uni_platform_unijoysticle_c64.c"
102+
"platform/uni_platform_unijoysticle_msx.c"
103+
"platform/uni_platform_unijoysticle_singleport.c")
104+
endif()
105+
106+
#
107+
# Disabled since it depends on having `compile_gatt.py` somewhere to generate it.
108+
# More difficult to distribute bluepad32 as a "component".
109+
# BLE Service
110+
# to generate .h from .gatt files
111+
#if (NOT DEFINED BLUEPAD32_ROOT)
112+
# set (BLUEPAD32_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
113+
# message(WARNING "BLUEPAD32_ROOT not set. Setting to ${BLUEPAD32_ROOT}")
114+
#endif()
115+
#
116+
#if (NOT DEFINED BTSTACK_ROOT)
117+
# set (BTSTACK_ROOT ${BLUEPAD32_ROOT}/external/btstack)
118+
# message(WARNING "BTSTACK_ROOT not set. Setting to ${BTSTACK_ROOT}")
119+
#endif()
120+
#
121+
#if(NOT CMAKE_BUILD_EARLY_EXPANSION)
122+
# # CMAKE_BUILD_EARLY_EXPANSION needed in ESP32 to prevent command is not scriptable.
123+
# # See: https://esp32.com/viewtopic.php?t=29402
124+
# find_package (Python REQUIRED COMPONENTS Interpreter)
125+
# include_directories(${CMAKE_CURRENT_BINARY_DIR})
126+
#
127+
# set(GATT_FILES
128+
# "uni_bt_service.gatt")
129+
#
130+
# cmake_print_variables(GATT_FILES)
131+
# cmake_print_variables(BLUEPAD32_ROOT)
132+
# cmake_print_variables(BTSTACK_ROOT)
133+
# foreach(GATT_FILE ${GATT_FILES})
134+
# message("Generating GATT DB file")
135+
# cmake_print_variables(GATT_FILE)
136+
# add_custom_command(
137+
# OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${GATT_FILE}.h
138+
# DEPENDS bt/${GATT_FILE}
139+
# COMMAND ${Python_EXECUTABLE}
140+
# ARGS ${BTSTACK_ROOT}/tool/compile_gatt.py ${CMAKE_CURRENT_SOURCE_DIR}/bt/${GATT_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${GATT_FILE}.h
141+
# )
142+
# list(APPEND srcs ${CMAKE_CURRENT_BINARY_DIR}/${GATT_FILE}.h)
143+
# endforeach(GATT_FILE)
144+
#endif()
145+
146+
# Valid for ESP-IDF only
147+
if(IDF_TARGET)
148+
set(requires "nvs_flash" "btstack" "app_update" "esp_timer")
149+
150+
if("${IDF_VERSION_MAJOR}" GREATER_EQUAL 5)
151+
# When using ESP-IDF v5+, use "normal" components
152+
list(APPEND requires "cmd_nvs" "cmd_system")
153+
else()
154+
# Use legacy components when compiled using ESP-IDF v4.4
155+
list(APPEND requires "cmd_nvs_4.4" "cmd_system_4.4")
156+
endif()
157+
158+
idf_component_register(SRCS "${srcs}"
159+
INCLUDE_DIRS "include"
160+
REQUIRES ${requires})
161+
elseif(PICO_SDK_VERSION_STRING OR BLUEPAD32_TARGET_LINUX)
162+
# Valid for Pico W and Linux
163+
add_library(bluepad32 ${srcs})
164+
target_include_directories(bluepad32 PUBLIC ./include)
165+
else()
166+
message(FATAL_ERROR "Define target")
167+
endif()
168+
169+
if(IDF_TARGET)
170+
# ESP-IDF
171+
# Nothing
172+
elseif(PICO_SDK_VERSION_STRING)
173+
target_link_libraries(bluepad32
174+
pico_stdlib
175+
pico_cyw43_arch_none
176+
pico_btstack_ble
177+
pico_btstack_classic
178+
pico_btstack_cyw43
179+
)
180+
elseif(BLUEPAD32_TARGET_LINUX)
181+
# Valid for Linux
182+
# TODO: Add dependencies here
183+
else()
184+
message(FATAL_ERROR "Define target")
185+
endif()

0 commit comments

Comments
 (0)