Skip to content

Commit ad6e680

Browse files
authored
Add optional Nordic UART Service
Examples still to come
1 parent f3c97cc commit ad6e680

File tree

9 files changed

+335
-9
lines changed

9 files changed

+335
-9
lines changed

BleGamepad.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static const char *LOG_TAG = "BLEGamepad";
4040
#define POWER_STATE_CHARGING 3 // 0b11
4141
#define POWER_STATE_CRITICAL 3 // 0b11
4242

43-
BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : _buttons(),
43+
BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel, bool delayAdvertising) : _buttons(),
4444
_specialButtons(0),
4545
_x(0),
4646
_y(0),
@@ -71,19 +71,23 @@ BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, u
7171
_powerLevel(0),
7272
hid(0),
7373
pCharacteristic_Power_State(0),
74-
configuration()
74+
configuration(),
75+
pServer(nullptr),
76+
nus(nullptr)
7577
{
7678
this->resetButtons();
7779
this->deviceName = deviceName;
7880
this->deviceManufacturer = deviceManufacturer;
7981
this->batteryLevel = batteryLevel;
82+
this->delayAdvertising = delayAdvertising;
8083
this->connectionStatus = new BleConnectionStatus();
8184

8285
hidReportDescriptorSize = 0;
8386
hidReportSize = 0;
8487
numOfButtonBytes = 0;
8588
enableOutputReport = false;
8689
outputReportLength = 64;
90+
nusInitialized = false;
8791
}
8892

8993
void BleGamepad::resetButtons()
@@ -1933,6 +1937,42 @@ void BleGamepad::setPowerLevel(uint8_t powerLevel)
19331937
setPowerStateAll(_batteryPowerInformation, _dischargingState, _chargingState, _powerLevel);
19341938
}
19351939

1940+
void BleGamepad::beginNUS()
1941+
{
1942+
if (!this->nusInitialized)
1943+
{
1944+
// Extrememly important to make sure that the pointer to server is actually valid
1945+
while(!NimBLEDevice::isInitialized ()){} // Wait until the server is initialized
1946+
while(NimBLEDevice::getServer() == nullptr){} // Ensure pointer to server is actually valid
1947+
1948+
// Now server is nkown to be valid, initialise nus to new BleNUS instance
1949+
nus = new BleNUS(NimBLEDevice::getServer()); // Pass the existing BLE server
1950+
nus->begin();
1951+
nusInitialized = true;
1952+
}
1953+
}
1954+
1955+
BleNUS* BleGamepad::getNUS()
1956+
{
1957+
return nus; // Return a pointer instead of a reference
1958+
}
1959+
1960+
void BleGamepad::sendDataOverNUS(const uint8_t* data, size_t length)
1961+
{
1962+
if (nus)
1963+
{
1964+
nus->sendData(data, length);
1965+
}
1966+
}
1967+
1968+
void BleGamepad::setNUSDataReceivedCallback(void (*callback)(const uint8_t* data, size_t length))
1969+
{
1970+
if (nus)
1971+
{
1972+
nus->setDataReceivedCallback(callback);
1973+
}
1974+
}
1975+
19361976
void BleGamepad::taskServer(void *pvParameter)
19371977
{
19381978
BleGamepad *BleGamepadInstance = (BleGamepad *)pvParameter; // static_cast<BleGamepad *>(pvParameter);
@@ -2025,9 +2065,18 @@ void BleGamepad::taskServer(void *pvParameter)
20252065
pAdvertising->setAppearance(HID_GAMEPAD);
20262066
pAdvertising->setName(BleGamepadInstance->deviceName);
20272067
pAdvertising->addServiceUUID(BleGamepadInstance->hid->getHidService()->getUUID());
2028-
pAdvertising->start();
2068+
2069+
if(BleGamepadInstance->delayAdvertising)
2070+
{
2071+
NIMBLE_LOGD(LOG_TAG, "Main NimBLE server advertising delayed (until Nordic UART Service added)");
2072+
}
2073+
else
2074+
{
2075+
NIMBLE_LOGD(LOG_TAG, "Main NimBLE server advertising started!");
2076+
pAdvertising->start();
2077+
}
2078+
20292079
BleGamepadInstance->hid->setBatteryLevel(BleGamepadInstance->batteryLevel);
20302080

2031-
NIMBLE_LOGD(LOG_TAG, "Advertising started!");
20322081
vTaskDelay(portMAX_DELAY); // delay(portMAX_DELAY);
20332082
}

BleGamepad.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "NimBLECharacteristic.h"
1212
#include "BleGamepadConfiguration.h"
1313
#include "BleOutputReceiver.h"
14+
#include "BleNUS.h"
1415

1516
class BleGamepad
1617
{
@@ -52,13 +53,17 @@ class BleGamepad
5253
uint8_t _dischargingState;
5354
uint8_t _chargingState;
5455
uint8_t _powerLevel;
55-
56+
bool nusInitialized;
57+
5658
//BleGamepadConfiguration configuration;
5759

5860
BleConnectionStatus *connectionStatus;
5961

6062
BleOutputReceiver *outputReceiver;
6163

64+
NimBLEServer *pServer;
65+
BleNUS* nus;
66+
6267
NimBLEHIDDevice *hid;
6368
NimBLECharacteristic *inputGamepad;
6469
NimBLECharacteristic *outputGamepad;
@@ -73,7 +78,7 @@ class BleGamepad
7378
public:
7479
BleGamepadConfiguration configuration;
7580

76-
BleGamepad(std::string deviceName = "ESP32 BLE Gamepad", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100);
81+
BleGamepad(std::string deviceName = "ESP32 BLE Gamepad", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100, bool delayAdvertising = false);
7782
void begin(BleGamepadConfiguration *config = new BleGamepadConfiguration());
7883
void end(void);
7984
void setAxes(int16_t x = 0, int16_t y = 0, int16_t z = 0, int16_t rX = 0, int16_t rY = 0, int16_t rZ = 0, int16_t slider1 = 0, int16_t slider2 = 0);
@@ -139,6 +144,7 @@ class BleGamepad
139144
void setTXPowerLevel(int8_t level = 9);
140145
int8_t getTXPowerLevel();
141146
uint8_t batteryLevel;
147+
bool delayAdvertising;
142148
bool isOutputReceived();
143149
uint8_t* getOutputBuffer();
144150
bool deleteBond(bool resetBoard = false);
@@ -152,7 +158,10 @@ class BleGamepad
152158
void setGyroscope(int16_t gX = 0, int16_t gY = 0, int16_t gZ = 0);
153159
void setAccelerometer(int16_t aX = 0, int16_t aY = 0, int16_t aZ = 0);
154160
void setMotionControls(int16_t gX = 0, int16_t gY = 0, int16_t gZ = 0, int16_t aX = 0, int16_t aY = 0, int16_t aZ = 0);
155-
161+
void beginNUS();
162+
void sendDataOverNUS(const uint8_t* data, size_t length);
163+
void setNUSDataReceivedCallback(void (*callback)(const uint8_t* data, size_t length));
164+
BleNUS* getNUS();
156165

157166
protected:
158167
virtual void onStarted(NimBLEServer *pServer) {};

BleGamepadConfiguration.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ BleGamepadConfiguration::BleGamepadConfiguration() : _controllerType(CONTROLLER_
2222
_modelNumber("1.0.0"),
2323
_softwareRevision("1.0.0"),
2424
_serialNumber("0123456789"),
25-
_firmwareRevision("0.7.3"),
25+
_firmwareRevision("0.7.4"),
2626
_hardwareRevision("1.0.0"),
2727
_enableOutputReport(false),
28+
_enableNordicUARTService(false),
2829
_outputReportLength(64),
2930
_transmitPowerLevel(9)
3031
{
@@ -131,6 +132,7 @@ char *BleGamepadConfiguration::getSerialNumber(){ return _serialNumber; }
131132
char *BleGamepadConfiguration::getFirmwareRevision(){ return _firmwareRevision; }
132133
char *BleGamepadConfiguration::getHardwareRevision(){ return _hardwareRevision; }
133134
bool BleGamepadConfiguration::getEnableOutputReport(){ return _enableOutputReport; }
135+
bool BleGamepadConfiguration::getEnableNordicUARTService(){ return _enableNordicUARTService; }
134136
uint16_t BleGamepadConfiguration::getOutputReportLength(){ return _outputReportLength; }
135137
int8_t BleGamepadConfiguration::getTXPowerLevel(){ return _transmitPowerLevel; } // Returns the power level that was set as the server started
136138

@@ -210,5 +212,6 @@ void BleGamepadConfiguration::setSerialNumber(char *value) { _serialNumber = val
210212
void BleGamepadConfiguration::setFirmwareRevision(char *value) { _firmwareRevision = value; }
211213
void BleGamepadConfiguration::setHardwareRevision(char *value) { _hardwareRevision = value; }
212214
void BleGamepadConfiguration::setEnableOutputReport(bool value) { _enableOutputReport = value; }
215+
void BleGamepadConfiguration::setEnableNordicUARTService(bool value) { _enableNordicUARTService = value; }
213216
void BleGamepadConfiguration::setOutputReportLength(uint16_t value) { _outputReportLength = value; }
214217
void BleGamepadConfiguration::setTXPowerLevel(int8_t value) { _transmitPowerLevel = value; }

BleGamepadConfiguration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class BleGamepadConfiguration
238238
char *_firmwareRevision;
239239
char *_hardwareRevision;
240240
bool _enableOutputReport;
241+
bool _enableNordicUARTService;
241242
uint16_t _outputReportLength;
242243
int8_t _transmitPowerLevel;
243244

@@ -296,6 +297,7 @@ class BleGamepadConfiguration
296297
char *getFirmwareRevision();
297298
char *getHardwareRevision();
298299
bool getEnableOutputReport();
300+
bool getEnableNordicUARTService();
299301
uint16_t getOutputReportLength();
300302
int8_t getTXPowerLevel();
301303

@@ -345,6 +347,7 @@ class BleGamepadConfiguration
345347
void setFirmwareRevision(char *value);
346348
void setHardwareRevision(char *value);
347349
void setEnableOutputReport(bool value);
350+
void setEnableNordicUARTService(bool value);
348351
void setOutputReportLength(uint16_t value);
349352
void setTXPowerLevel(int8_t value);
350353
};

0 commit comments

Comments
 (0)