Skip to content

Commit 5fa2183

Browse files
committed
Working on BT/BLE API
1 parent 1735e56 commit 5fa2183

8 files changed

Lines changed: 775 additions & 195 deletions

File tree

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Megahub
22

3-
[![PlatformIO CI](https://github.com/mirkosertic/Megahub/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/mirkosertic/Megahub/actions/workflows/build.yml)
3+
![PlatformIO CI](https://github.com/mirkosertic/Megahub/actions/workflows/build.yml/badge.svg?branch=main)
44

55
## Introduction
66

7-
Megahub is an ESP32-based alternative hub designed for LEGO© enthusiasts and robotics builders. It combines the flexibility of the ESP32 platform with native support for LEGO© WeDo/Powered Up! devices, offering both Lua scripting and Blockly visual programming for easy control and automation.
7+
Megahub is an ESP32-based alternative hub designed for LEGO© enthusiasts and robotics builders. It combines the flexibility of the ESP32 platform with native support for LEGO© WeDo/Powered Up! devices, offering both Lua scripting and Blockly visual programming for easy control and automation.
88

9-
This project provides a powerful, open-source alternative to proprietary solutions, giving makers complete control over their robotic creations while maintaining compatibility with popular LEGO© sensors and motors.
9+
This project provides a powerful, open-source alternative to proprietary solutions, giving makers complete control over their robotic creations while maintaining compatibility with popular LEGO© sensors and motors.
1010
It comes with the hardware schematics, production files and the Megahub firmware as a PlatformIO project as well.
1111

12-
** Build and use it at your own Risk!! **
12+
\*\* Build and use it at your own Risk!! \*\*
1313

1414
## Key Features
1515

@@ -19,7 +19,7 @@ It comes with the hardware schematics, production files and the Megahub firmware
1919
- **IDE**: Configure and program Megahub through the intuitive [Megahub IDE](https://mirkosertic.github.io/Megahub/)
2020
- **Visual Programming Interface**: Blockly visual programming for beginners
2121
- **SD-Card**: Built-in SD-card reader to store multiple projects (autostart possible)
22-
- **4 UART Ports**: Connect up to 4 LEGO© WeDo/Powered Up! devices simultaneously (motors, sensors, etc.)
22+
- **4 UART Ports**: Connect up to 4 LEGO© WeDo/Powered Up! devices simultaneously (motors, sensors, etc.)
2323
- **Built-in IMU**: Integrated MPU6050 6-axis accelerometer and gyroscope for motion sensing
2424
- **FastLED/NeoPixel Support**: Built-in support for addressable RGB LED strips
2525
- **Flexible GPIO**: Additional GPIO pins available for custom sensors, buttons, and peripherals
@@ -36,17 +36,17 @@ It comes with the hardware schematics, production files and the Megahub firmware
3636

3737
## How It Differs from Existing Solutions
3838

39-
### vs. LEGO© Powered Up!
39+
### vs. LEGO© Powered Up!
4040

4141
- **Open Source**: Complete control over firmware and functionality
4242
- **Expandable**: Access to additional GPIO pins for custom hardware
4343
- **Programming Flexibility**: Choose between Lua scripting or visual programming
4444
- **Built-in Features**: Integrated IMU and LED controller without additional hubs
4545
- **Cost-effective**: Based on affordable ESP32 hardware
4646

47-
### vs. fischertechnik© RX Controller
47+
### vs. fischertechnik© RX Controller
4848

49-
- **LEGO Compatibility**: Native support for LEGO© WeDo 2.0 protocol
49+
- **LEGO Compatibility**: Native support for LEGO© WeDo 2.0 protocol
5050
- **Web-based Programming**: No proprietary software required
5151
- **Cost-effective**: Based on affordable ESP32 hardware
5252
- **Visual Programming**: Blockly support makes it accessible for all skill levels
@@ -63,5 +63,4 @@ It comes with the hardware schematics, production files and the Megahub firmware
6363

6464
## Getting Started
6565

66-
Documentation coming soon...
67-
66+
Documentation coming soon...

lib/btremote/include/btremote.h

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
#define BTREMOTE_H
33

44
#include "configuration.h"
5-
#include "megahub.h"
6-
7-
#include <ArduinoJson.h>
8-
#include <map>
9-
#include <vector>
10-
115
#include "esp_bt.h"
6+
#include "esp_bt_defs.h"
127
#include "esp_bt_main.h"
138
#include "esp_gap_ble_api.h"
9+
#include "esp_gap_bt_api.h"
1410
#include "esp_gatt_common_api.h"
1511
#include "esp_gatts_api.h"
16-
#include "esp_gap_bt_api.h"
17-
#include "esp_bt_defs.h"
1812
#include "esp_hidh_api.h"
13+
#include "megahub.h"
14+
15+
#include <ArduinoJson.h>
16+
#include <map>
17+
#include <vector>
1918

2019
enum class ProtocolMessageType : uint8_t {
2120
REQUEST = 0x01,
@@ -112,38 +111,23 @@ struct HIDDevice {
112111
uint32_t handle;
113112
};
114113

115-
// HID Report Data for Gamepad Processing
116-
struct HIDReportData {
117-
uint8_t reportId;
118-
uint8_t reportType;
119-
uint16_t length;
120-
uint8_t data[64]; // Max HID report size
121-
uint32_t timestamp;
122-
};
123-
124114
// Gamepad State - Standard HID gamepad layout
125115
struct GamepadState {
126116
esp_bd_addr_t address;
127117
bool connected;
128118
uint32_t timestamp;
119+
uint16_t vendorId;
120+
uint16_t productId;
129121

130-
// Buttons (bit field): 0=A, 1=B, 2=X, 3=Y, 4=L1, 5=R1, 6=L2, 7=R2,
131-
// 8=Select, 9=Start, 10=L3, 11=R3, 12-15=Reserved
132122
uint16_t buttons;
133123

134-
// D-Pad (hat switch): 0=up, 1=up-right, 2=right, 3=down-right,
135-
// 4=down, 5=down-left, 6=left, 7=up-left, 8=center
136124
uint8_t dpad;
137125

138-
// Analog sticks (range: -127 to 127, center=0)
139-
int8_t leftStickX;
140-
int8_t leftStickY;
141-
int8_t rightStickX;
142-
int8_t rightStickY;
143-
144-
// Triggers (range: 0 to 255)
145-
uint8_t leftTrigger;
146-
uint8_t rightTrigger;
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;
147131
};
148132

149133
// HID Event Queue Item for async processing
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <ArduinoJson.h>
5+
#include <vector>
6+
7+
/**
8+
* @brief Parser for HID Report Descriptors
9+
*
10+
* Parses binary HID report descriptors according to the USB HID specification
11+
* and converts them to human-readable JSON format for debugging purposes.
12+
*/
13+
class HIDReportParser {
14+
public:
15+
/**
16+
* @brief Parse a HID report descriptor and output as JSON to Serial
17+
*
18+
* @param descriptor Pointer to the descriptor data
19+
* @param length Length of the descriptor in bytes
20+
*/
21+
static void parseAndPrintJSON(const uint8_t *descriptor, size_t length);
22+
23+
private:
24+
// HID Item Type (bits 2-3 of prefix byte)
25+
enum ItemType {
26+
ITEM_TYPE_MAIN = 0,
27+
ITEM_TYPE_GLOBAL = 1,
28+
ITEM_TYPE_LOCAL = 2,
29+
ITEM_TYPE_RESERVED = 3
30+
};
31+
32+
// Main Item Tags
33+
enum MainItemTag {
34+
MAIN_INPUT = 0x8,
35+
MAIN_OUTPUT = 0x9,
36+
MAIN_COLLECTION = 0xA,
37+
MAIN_FEATURE = 0xB,
38+
MAIN_END_COLLECTION = 0xC
39+
};
40+
41+
// Global Item Tags
42+
enum GlobalItemTag {
43+
GLOBAL_USAGE_PAGE = 0x0,
44+
GLOBAL_LOGICAL_MIN = 0x1,
45+
GLOBAL_LOGICAL_MAX = 0x2,
46+
GLOBAL_PHYSICAL_MIN = 0x3,
47+
GLOBAL_PHYSICAL_MAX = 0x4,
48+
GLOBAL_UNIT_EXPONENT = 0x5,
49+
GLOBAL_UNIT = 0x6,
50+
GLOBAL_REPORT_SIZE = 0x7,
51+
GLOBAL_REPORT_ID = 0x8,
52+
GLOBAL_REPORT_COUNT = 0x9,
53+
GLOBAL_PUSH = 0xA,
54+
GLOBAL_POP = 0xB
55+
};
56+
57+
// Local Item Tags
58+
enum LocalItemTag {
59+
LOCAL_USAGE = 0x0,
60+
LOCAL_USAGE_MIN = 0x1,
61+
LOCAL_USAGE_MAX = 0x2,
62+
LOCAL_DESIGNATOR_INDEX = 0x3,
63+
LOCAL_DESIGNATOR_MIN = 0x4,
64+
LOCAL_DESIGNATOR_MAX = 0x5,
65+
LOCAL_STRING_INDEX = 0x7,
66+
LOCAL_STRING_MIN = 0x8,
67+
LOCAL_STRING_MAX = 0x9,
68+
LOCAL_DELIMITER = 0xA
69+
};
70+
71+
// Collection types
72+
enum CollectionType {
73+
COLLECTION_PHYSICAL = 0x00,
74+
COLLECTION_APPLICATION = 0x01,
75+
COLLECTION_LOGICAL = 0x02,
76+
COLLECTION_REPORT = 0x03,
77+
COLLECTION_NAMED_ARRAY = 0x04,
78+
COLLECTION_USAGE_SWITCH = 0x05,
79+
COLLECTION_USAGE_MODIFIER = 0x06
80+
};
81+
82+
/**
83+
* @brief Parse the descriptor and build a JSON document
84+
*/
85+
static JsonDocument parseDescriptor(const uint8_t *descriptor, size_t length);
86+
87+
/**
88+
* @brief Get the name of a usage page
89+
*/
90+
static const char* getUsagePageName(uint16_t usagePage);
91+
92+
/**
93+
* @brief Get the name of a usage for a given usage page
94+
*/
95+
static const char* getUsageName(uint16_t usagePage, uint16_t usage);
96+
97+
/**
98+
* @brief Get the name of a collection type
99+
*/
100+
static const char* getCollectionTypeName(uint8_t type);
101+
102+
/**
103+
* @brief Decode input/output/feature flags
104+
*/
105+
static void decodeIOFlags(JsonObject &obj, uint32_t flags);
106+
107+
/**
108+
* @brief Extract signed or unsigned value from item data
109+
*/
110+
static int32_t extractValue(const uint8_t *data, uint8_t size, bool isSigned = false);
111+
};

0 commit comments

Comments
 (0)