-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Output Report function and provided examples.
- Loading branch information
1 parent
11ee6e9
commit cdf05b3
Showing
7 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "BleOutputReceiver.h" | ||
|
||
BleOutputReceiver::BleOutputReceiver(uint16_t outputReportLength) | ||
{ | ||
this->outputReportLength = outputReportLength; | ||
outputBuffer = new uint8_t[outputReportLength]; | ||
} | ||
|
||
BleOutputReceiver::~BleOutputReceiver() | ||
{ | ||
// Release memory | ||
if (outputBuffer) | ||
{ | ||
delete[] outputBuffer; | ||
} | ||
} | ||
|
||
void BleOutputReceiver::onWrite(NimBLECharacteristic *pCharacteristic) | ||
{ | ||
// Retrieve data sent from the host | ||
std::string value = pCharacteristic->getValue(); | ||
|
||
// Store the received data in the buffer | ||
for (int i = 0; i < std::min(value.length(), (size_t)outputReportLength); i++) | ||
{ | ||
outputBuffer[i] = (uint8_t)value[i]; | ||
} | ||
|
||
// Testing | ||
// Serial.println("Received data from host:"); | ||
// for (size_t i = 0; i < value.length(); i++) { | ||
// Serial.print((uint8_t)value[i], HEX); | ||
// Serial.print(" "); | ||
// } | ||
// Serial.println(); | ||
|
||
outputFlag = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef BLE_OUTPUT_RECEIVER_H | ||
#define BLE_OUTPUT_RECEIVER_H | ||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
|
||
#include "nimconfig.h" | ||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) | ||
|
||
#include <NimBLEServer.h> | ||
#include "NimBLECharacteristic.h" | ||
|
||
class BleOutputReceiver : public NimBLECharacteristicCallbacks | ||
{ | ||
public: | ||
BleOutputReceiver(uint16_t outputReportLength); | ||
~BleOutputReceiver(); | ||
void onWrite(NimBLECharacteristic *pCharacteristic) override; | ||
bool outputFlag = false; | ||
uint16_t outputReportLength; | ||
uint8_t *outputBuffer; | ||
}; | ||
|
||
#endif // CONFIG_BT_NIMBLE_ROLE_PERIPHERAL | ||
#endif // CONFIG_BT_ENABLED | ||
#endif // BLE_OUTPUT_RECEIVER_H |
50 changes: 50 additions & 0 deletions
50
examples/TestReceivingOutputReport/TestReceivingOutputReport.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Test receiving Output Report | ||
*/ | ||
#include <BleGamepad.h> | ||
|
||
#define numOfButtons 16 | ||
|
||
BleGamepad bleGamepad; | ||
BleGamepadConfiguration bleGamepadConfig; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println("Starting BLE work!"); | ||
|
||
bleGamepadConfig.setAutoReport(false); | ||
bleGamepadConfig.setControllerType(CONTROLLER_TYPE_GAMEPAD); // CONTROLLER_TYPE_JOYSTICK, CONTROLLER_TYPE_GAMEPAD (DEFAULT), CONTROLLER_TYPE_MULTI_AXIS | ||
|
||
bleGamepadConfig.setEnableOutputReport(true); // (Necessary) Enable Output Report. Default is false. | ||
bleGamepadConfig.setOutputReportLength(128); // (Optional) Set Report Length 128(Bytes). The default value is 64 bytes. | ||
// Do not set the OutputReportLength too large, otherwise it will be truncated. For example, if the hexadecimal value of 10000 in decimal is 0x2710, it will be truncated to 0x710. | ||
|
||
bleGamepadConfig.setHidReportId(0x05); // (Optional) Set ReportID to 0x05. | ||
//When you send data from the upper computer to ESP32, you must send the ReportID in the first byte! The default ReportID is 3. | ||
|
||
bleGamepadConfig.setButtonCount(numOfButtons); | ||
|
||
bleGamepadConfig.setAxesMin(0x0000); // 0 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal | ||
bleGamepadConfig.setAxesMax(0x7FFF); // 32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal | ||
|
||
// Try NOT to modify VID, otherwise it may cause the host to be unable to send output reports to the device. | ||
bleGamepadConfig.setVid(0x3412); | ||
// You can freely set the PID | ||
bleGamepadConfig.setPid(0x0100); | ||
bleGamepad.begin(&bleGamepadConfig); | ||
|
||
// changing bleGamepadConfig after the begin function has no effect, unless you call the begin function again | ||
} | ||
|
||
void loop() { | ||
if (bleGamepad.isConnected()) { | ||
if (bleGamepad.isOutputReceived()) { | ||
uint8_t* buffer = bleGamepad.getOutputBuffer(); | ||
Serial.print("Receive: "); | ||
for (int i = 0; i < 64; i++) { | ||
Serial.printf("0x%X ",buffer[i]); // Print data from buffer | ||
} | ||
Serial.println(""); | ||
} | ||
} | ||
} |