Skip to content

Commit 2692642

Browse files
committed
Working on BT/BLE API
1 parent 6e82eee commit 2692642

17 files changed

Lines changed: 474 additions & 203 deletions

File tree

frontend/src/components/blockly/colors.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export const colorText = 80;
55
export const colorLists = 100;
66
export const colorIO = 120;
77
export const colorLego = 140;
8-
export const colorFastLED = 160;
9-
export const colorIMU = 180;
10-
export const colorUI = 200;
11-
export const colorDebug = 220;
8+
export const colorGamepad = 160;
9+
export const colorFastLED = 180;
10+
export const colorIMU = 200;
11+
export const colorUI = 220;
12+
export const colorDebug = 240;

frontend/src/components/blockly/component.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {definition as ui_show_value} from './ui_show_value.js'
3131
import {definition as mh_debug_free_heap} from './mh_debug_free_heap.js'
3232
import {definition as lego_get_mode_dataset} from './lego_get_mode_dataset.js'
3333
import {definition as lego_select_mode} from './lego_select_mode.js'
34+
import {definition as gamepad_buttonpressed} from './mh_gamepad_buttonpressed.js'
35+
import {definition as gamepad_value} from './mh_gamepad_value.js'
36+
import {definition as gamepad_connected} from './mh_gamepad_connected.js'
3437

3538
import {colorLogic,
3639
colorMath,
@@ -251,7 +254,13 @@ const customBlocks = {
251254

252255
"mh_digitalread": mh_digitalread,
253256

254-
"mh_set_motor_speed" : mh_set_motor_speed,
257+
"mh_set_motor_speed": mh_set_motor_speed,
258+
259+
"mh_gamepad_buttonpressed": gamepad_buttonpressed,
260+
261+
"mh_gamepad_value": gamepad_value,
262+
263+
"mh_gamepad_connected": gamepad_connected,
255264

256265
"lego_get_mode_dataset": lego_get_mode_dataset,
257266

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {colorGamepad} from './colors.js'
2+
3+
// clang-format off
4+
export const definition = {
5+
category : 'Gamepad',
6+
colour : colorGamepad,
7+
blockdefinition : {
8+
"type" : "mh_gamepad_buttonpressed",
9+
"message0" : "Checks if %2 is pressed on %1",
10+
"args0" : [
11+
{
12+
"type" : "field_dropdown",
13+
"name" : "GAMEPAD",
14+
"options" : [
15+
[ "GAMEPAD1", "GAMEPAD1" ],
16+
]
17+
},
18+
{
19+
"type" : "field_dropdown",
20+
"name" : "BUTTON",
21+
"options" : [
22+
[ "GAMEPAD_BUTTON_1", "GAMEPAD_BUTTON_1" ],
23+
]
24+
}
25+
],
26+
"output": null,
27+
"colour" : colorGamepad,
28+
"tooltip" : "Checks if a specific button is pressed on a connected gamepad",
29+
"helpUrl" : ""
30+
},
31+
generator : (block, generator) => {
32+
const gamepad = block.getFieldValue('GAMEPAD');
33+
const button = block.getFieldValue('BUTTON');
34+
35+
const command = "gamepad.buttonpressed(" + gamepad + "," + button + ")";
36+
37+
return [ command, 0 ];
38+
}
39+
};
40+
// clang-format on
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {colorGamepad} from './colors.js'
2+
3+
// clang-format off
4+
export const definition = {
5+
category : 'Gamepad',
6+
colour : colorGamepad,
7+
blockdefinition : {
8+
"type" : "mh_gamepad_connected",
9+
"message0" : "Test if %1 is connected",
10+
"args0" : [
11+
{
12+
"type" : "field_dropdown",
13+
"name" : "GAMEPAD",
14+
"options" : [
15+
[ "GAMEPAD1", "GAMEPAD1" ],
16+
]
17+
},
18+
],
19+
"output": null,
20+
"colour" : colorGamepad,
21+
"tooltip" : "Tests if the Gamepad is connected",
22+
"helpUrl" : ""
23+
},
24+
generator : (block, generator) => {
25+
const gamepad = block.getFieldValue('GAMEPAD');
26+
27+
const command = "gamepad.connected(" + gamepad + ")";
28+
29+
return [ command, 0 ];
30+
}
31+
};
32+
// clang-format on
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {colorGamepad} from './colors.js'
2+
3+
// clang-format off
4+
export const definition = {
5+
category : 'Gamepad',
6+
colour : colorGamepad,
7+
blockdefinition : {
8+
"type" : "mh_gamepad_value",
9+
"message0" : "Get %2 from %1",
10+
"args0" : [
11+
{
12+
"type" : "field_dropdown",
13+
"name" : "GAMEPAD",
14+
"options" : [
15+
[ "GAMEPAD1", "GAMEPAD1" ],
16+
]
17+
},
18+
{
19+
"type" : "field_dropdown",
20+
"name" : "VALUE",
21+
"options" : [
22+
["GAMEPAD_LEFT_X", "GAMEPAD_LEFT_X"],
23+
["GAMEPAD_LEFT_Y", "GAMEPAD_LEFT_Y"],
24+
["GAMEPAD_RIGHT_X", "GAMEPAD_RIGHT_X"],
25+
[ "GAMEPAD_RIGHT_Y", "GAMEPAD_RIGHT_Y" ],
26+
]
27+
}
28+
],
29+
"output": null,
30+
"colour" : colorGamepad,
31+
"tooltip" : "Gets a value from a connected Gamepad",
32+
"helpUrl" : ""
33+
},
34+
generator : (block, generator) => {
35+
const gamepad = block.getFieldValue('GAMEPAD');
36+
const value = block.getFieldValue('VALUE');
37+
38+
const command = "gamepad.value(" + gamepad + "," + value + ")";
39+
40+
return [ command, 0 ];
41+
}
42+
};
43+
// clang-format on

lib/btremote/include/btremote.h

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "esp_gatt_common_api.h"
1111
#include "esp_gatts_api.h"
1212
#include "esp_hidh_api.h"
13+
#include "inputdevices.h"
1314
#include "megahub.h"
1415

1516
#include <ArduinoJson.h>
@@ -111,25 +112,6 @@ struct HIDDevice {
111112
uint32_t handle;
112113
};
113114

114-
// Gamepad State - Standard HID gamepad layout
115-
struct GamepadState {
116-
esp_bd_addr_t address;
117-
bool connected;
118-
uint32_t timestamp;
119-
uint16_t vendorId;
120-
uint16_t productId;
121-
122-
uint16_t buttons;
123-
124-
uint8_t dpad;
125-
126-
// Analog sticks (16-bit signed, range: -32768 to 32767, center=0)
127-
int16_t leftStickX;
128-
int16_t leftStickY;
129-
int16_t rightStickX;
130-
int16_t rightStickY;
131-
};
132-
133115
// HID Event Queue Item for async processing
134116
struct HIDEventItem {
135117
esp_hidh_cb_event_t event;
@@ -143,6 +125,7 @@ class BTRemote {
143125
uint16_t gatts_if_;
144126
uint16_t app_id_;
145127

128+
InputDevices *inputDevices_;
146129
SerialLoggingOutput *loggingOutput_;
147130
Configuration *configuration_;
148131
Megahub *hub_;
@@ -176,8 +159,6 @@ class BTRemote {
176159
// HID Host
177160
std::map<std::string, HIDDevice> hidDevices_;
178161
SemaphoreHandle_t hidDevicesMutex_;
179-
std::map<std::string, GamepadState> gamepadStates_;
180-
SemaphoreHandle_t gamepadStatesMutex_;
181162

182163
// HID event processing queue and task
183164
QueueHandle_t hidEventQueue_;
@@ -249,7 +230,7 @@ class BTRemote {
249230
void stringToBdAddr(const std::string &str, esp_bd_addr_t address);
250231

251232
public:
252-
BTRemote(FS *fs, Megahub *hub, SerialLoggingOutput *loggingOutput, Configuration *configuragtion);
233+
BTRemote(FS *fs, InputDevices *inputDevices, Megahub *hub, SerialLoggingOutput *loggingOutput, Configuration *configuragtion);
253234

254235
void begin(const char *deviceName);
255236

@@ -283,10 +264,6 @@ class BTRemote {
283264
bool hidConnect(const char *macAddress);
284265
bool hidDisconnect(const char *macAddress);
285266
std::vector<HIDDevice> getConnectedHIDDevices();
286-
287-
// Gamepad API
288-
bool getGamepadState(const char *macAddress, GamepadState &state);
289-
std::vector<GamepadState> getAllGamepadStates();
290267
};
291268

292269
#endif // BTREMOTE_H

0 commit comments

Comments
 (0)