Skip to content

Commit

Permalink
[WIP] Init PlayerLED feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeNX committed Jan 31, 2021
1 parent e22923f commit 8096d8e
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 9 deletions.
1 change: 1 addition & 0 deletions BleConnectionStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BleConnectionStatus : public BLEServerCallbacks
void onConnect(BLEServer* pServer);
void onDisconnect(BLEServer* pServer);
BLECharacteristic* inputGamepad;
BLECharacteristic* outputGamepad;
};

#endif // CONFIG_BT_ENABLED
Expand Down
32 changes: 30 additions & 2 deletions BleGamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#include "BLE2902.h"
#include "BLEHIDDevice.h"
#include "HIDTypes.h"
#include "HIDKeyboardTypes.h"
//#include "HIDKeyboardTypes.h"
#include <driver/adc.h>
#include "sdkconfig.h"

#include "BleConnectionStatus.h"
#include "GamepadOutputCallbacks.h"
#include "BleGamepad.h"

#if defined(CONFIG_ARDUHAL_ESP_LOG)
Expand All @@ -35,6 +36,18 @@ static const uint8_t _hidReportDescriptor[] = {
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x40, // REPORT_COUNT (64)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;64 button bits

// ------------------------------------------------- Player 1 to 4 leds
REPORT_COUNT(1), 0x04, // REPORT_COUNT (4) ; 4 bits (Player 1, Player 2, Player 3, Player 4)
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
USAGE_PAGE(1), 0x08, // USAGE_PAGE (LEDs)
USAGE_MINIMUM(1), 0x61, // USAGE_MINIMUM (0x61) ; Player 1
USAGE_MAXIMUM(1), 0x64, // USAGE_MAXIMUM (0x64) ; Player 4
HIDOUTPUT(1), 0x02, // OUTPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) ; 4 bits (Padding)
REPORT_SIZE(1), 0x04, // REPORT_SIZE (4)
HIDOUTPUT(1), 0x01, // OUTPUT (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)

// ------------------------------------------------- X/Y position, Z/rZ position
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
COLLECTION(1), 0x00, // COLLECTION (Physical)
Expand All @@ -47,6 +60,7 @@ static const uint8_t _hidReportDescriptor[] = {
REPORT_SIZE(1), 0x10, // REPORT_SIZE (16)
REPORT_COUNT(1), 0x04, // REPORT_COUNT (4)
HIDINPUT(1), 0x02, // INPUT (Data,Var,Abs)

// ------------------------------------------------- Triggers
USAGE(1), 0x33, // USAGE (rX) Left Trigger
USAGE(1), 0x34, // USAGE (rY) Right Trigger
Expand All @@ -55,6 +69,7 @@ static const uint8_t _hidReportDescriptor[] = {
REPORT_SIZE(1), 0x10, // REPORT_SIZE (16)
REPORT_COUNT(1), 0x02, // REPORT_COUNT (2)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;4 bytes (X,Y,Z,rZ)

// ------------------------------------------------- Sliders
USAGE(1), 0x36, // USAGE (Slider) Slider 1
USAGE(1), 0x36, // USAGE (Slider) Slider 2
Expand All @@ -64,7 +79,8 @@ static const uint8_t _hidReportDescriptor[] = {
REPORT_COUNT(1), 0x02, // REPORT_COUNT (2)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;20 bytes (slider 1 and slider 2)
END_COLLECTION(0), // END_COLLECTION
// ------------------------------------------------- Hats

// ------------------------------------------------- Hats
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x39, // Usage (Hat Switch) Hat 4
USAGE(1), 0x39, // Usage (Hat Switch) Hat 3
Expand Down Expand Up @@ -360,6 +376,11 @@ void BleGamepad::setAutoReport(bool autoReport)
_autoReport = autoReport;
}

void BleGamepad::setLedChangeCallBack(void (*func)(PlayerLeds*))
{
gamepadOutputCallBack->func = func;
}

bool BleGamepad::isPressed(uint64_t b)
{
if ((b & _buttons) > 0)
Expand Down Expand Up @@ -388,7 +409,13 @@ void BleGamepad::taskServer(void* pvParameter)

BleGamepadInstance->hid = new BLEHIDDevice(pServer);
BleGamepadInstance->inputGamepad = BleGamepadInstance->hid->inputReport(0); // <-- input REPORTID from report map
BleGamepadInstance->outputGamepad = BleGamepadInstance->hid->outputReport(0);
BleGamepadInstance->connectionStatus->inputGamepad = BleGamepadInstance->inputGamepad;
BleGamepadInstance->connectionStatus->outputGamepad = BleGamepadInstance->outputGamepad;

BleGamepadInstance->gamepadOutputCallBack = new GamepadOutputCallbacks();
BleGamepadInstance->outputGamepad->setCallbacks(BleGamepadInstance->gamepadOutputCallBack);
// BleGamepadInstance->outputGamepad->setCallbacks(new GamepadOutputCallbacks());

BleGamepadInstance->hid->manufacturer()->setValue(BleGamepadInstance->deviceManufacturer);

Expand All @@ -407,6 +434,7 @@ void BleGamepad::taskServer(void* pvParameter)
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->setAppearance(HID_GAMEPAD);
pAdvertising->addServiceUUID(BleGamepadInstance->hid->hidService()->getUUID());
//pAdvertising->setScanResponse(false);
pAdvertising->start();
BleGamepadInstance->hid->setBatteryLevel(BleGamepadInstance->batteryLevel);

Expand Down
4 changes: 4 additions & 0 deletions BleGamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)

#include "GamepadOutputCallbacks.h"
#include "BleConnectionStatus.h"
#include "BLEHIDDevice.h"
#include "BLECharacteristic.h"
Expand Down Expand Up @@ -113,9 +114,11 @@ class BleGamepad {
int16_t _hat4;
bool _autoReport;

GamepadOutputCallbacks* gamepadOutputCallBack;
BleConnectionStatus* connectionStatus;
BLEHIDDevice* hid;
BLECharacteristic* inputGamepad;
BLECharacteristic* outputGamepad;
void buttons(uint64_t b);
void rawAction(uint8_t msg[], char msgSize);
static void taskServer(void* pvParameter);
Expand Down Expand Up @@ -148,6 +151,7 @@ class BleGamepad {
void setSlider2(uint16_t slider2 = 0);
void setAutoReport(bool autoReport = true);
void sendReport();
void setLedChangeCallBack(void (*func)(PlayerLeds*));
bool isPressed(uint64_t b = BUTTON_1); // check BUTTON_1 by default
bool isConnected(void);
void setBatteryLevel(uint8_t level);
Expand Down
29 changes: 29 additions & 0 deletions GamepadOutputCallbacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "GamepadOutputCallbacks.h"

#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "BLEDevice";
#endif

//ESP_LOGI(LOG_TAG, "Callbacks file included");

GamepadOutputCallbacks::GamepadOutputCallbacks(void) {
ESP_LOGI(LOG_TAG, "Callbacks initialised");
}

void GamepadOutputCallbacks::onWrite(BLECharacteristic* me) {
// uint8_t* value = (uint8_t*)(me->getValue().c_str());
PlayerLeds* gpled = (PlayerLeds*)(me->getValue().c_str());
ESP_LOGI(LOG_TAG, "leds: %d", *gpled);
ESP_LOGI(LOG_TAG, "Callbacks written to");

//Serial.print("LED state: ");
//Serial.print((int) *value);
//Serial.println();

// if(func!=NULL)
func(gpled);
}
29 changes: 29 additions & 0 deletions GamepadOutputCallbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef ESP32_BLE_GAMEPAD_OUTPUT_CALLBACKS_H
#define ESP32_BLE_GAMEPAD_OUTPUT_CALLBACKS_H
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)

#include <BLEServer.h>
#include "BLE2902.h"
#include "BLECharacteristic.h"

// key report back
typedef struct{
uint8_t bmPlayerLED1 : 1;
uint8_t bmPlayerLED2 : 1;
uint8_t bmPlayerLED3 : 1;
uint8_t bmPlayerLED4 : 1;
uint8_t bmReserved : 4;
} PlayerLeds;
using callBackFunc = void (*)(PlayerLeds*);

class GamepadOutputCallbacks : public BLECharacteristicCallbacks
{
public:
callBackFunc func = [](PlayerLeds*){ };
GamepadOutputCallbacks(void);
void onWrite(BLECharacteristic* me);
};

#endif // CONFIG_BT_ENABLED
#endif // ESP32_BLE_GAMEPAD_OUTPUT_CALLBACKS_H
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Bluetooth LE Gamepad library for the ESP32

This library allows you to make the ESP32 act as a Bluetooth Gamepad and control what it does. E.g. move axes and press buttons

Adding Player LED Support Feedback and teting info.

## Features

- [x] Button press (64 buttons)
Expand Down Expand Up @@ -83,10 +85,3 @@ By default the battery level will be set to 100%, the device name will be `ESP32
Credits to [T-vK](https://github.com/T-vK) as this library is based on his ESP32-BLE-Mouse library (https://github.com/T-vK/ESP32-BLE-Mouse) that he provided.

Credits to [chegewara](https://github.com/chegewara) as the ESP32-BLE-Mouse library is based on [this piece of code](https://github.com/nkolban/esp32-snippets/issues/230#issuecomment-473135679) that he provided.

## Notes
Use [this](http://www.planetpointy.co.uk/joystick-test-application/) Windows test app to test/see all of the buttons

You might also be interested in:
- [ESP32-BLE-Mouse](https://github.com/T-vK/ESP32-BLE-Mouse)
- [ESP32-BLE-Keyboard](https://github.com/T-vK/ESP32-BLE-Keyboard)
70 changes: 70 additions & 0 deletions examples/PlayerLEDTest/PlayerLEDTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* A simple sketch that maps a single pin on the ESP32 to a single button on the controller
*/

#include <BleGamepad.h> // https://github.com/lemmingDev/ESP32-BLE-Gamepad

BleGamepad bleGamepad;

int previousButton1State = HIGH;

int PlayerLED1 = 0;
int PlayerLED2 = 0;
int PlayerLED3 = 0;
int PlayerLED4 = 0;

//
// Register your callback function to be notified of LED changes
// This is called from the bluetooth stack, so don't do system calls
// from within this function.
//
void GamePadLedCb(PlayerLeds *gpls)
{
// digitalWrite(2,gpls->bmPlayerLED1);
// digitalWrite(2,gpls->bmPlayerLED2);
// ...
PlayerLED1 = gpls->bmPlayerLED1;
PlayerLED2 = gpls->bmPlayerLED2;
PlayerLED3 = gpls->bmPlayerLED3;
PlayerLED4 = gpls->bmPlayerLED4;

}

void setup()
{
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
Serial.println("Starting BLE work!");
bleGamepad.begin();
delay(1000); //must have delay for the BLE finish inital
bleGamepad.setLedChangeCallBack(GamePadLedCb);

}

void loop()
{
if(bleGamepad.isConnected())
{

Serial.println(PlayerLED1);

int currentButton1State = digitalRead(2);

if (currentButton1State != previousButton1State)
{
if(currentButton1State == LOW)
{
bleGamepad.press(BUTTON_1);
}
else
{
bleGamepad.release(BUTTON_1);
}
}
previousButton1State = currentButton1State;
}

Serial.println("Waiting 5 seconds...");
delay(5000);

}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ setSlider KEYWORD2
setSliders KEYWORD2
setAutoReport KEYWORD2
sendReport KEYWORD2
setLedChangeCallBack KEYWORD2

#######################################
# Constants
Expand Down

0 comments on commit 8096d8e

Please sign in to comment.