forked from lemmingDev/ESP32-BLE-Gamepad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
9 deletions.
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
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); | ||
} |
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,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 |
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,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); | ||
|
||
} |
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