From c48ca5df8fe10118640073932a5235ec6e7d434a Mon Sep 17 00:00:00 2001 From: lemmingDev Date: Fri, 31 Jan 2025 16:26:25 +0900 Subject: [PATCH] TX power level can now be set TX power level can now be set either initially using the configuration system eg. bleGamepadConfig.setTXPowerLevel(txPowerLevel); Defaults to 9 if not set Range: -12 to 9 dBm and the only valid values are: -12, -9, -6, -3, 0, 3, 6 and 9 TX power can also be set at any time using bleGamepad.setTXPower(int8_t) You can also get the current TX power level by using bleGamepad.getTXPower(int8_t) --- BleGamepad.cpp | 15 ++++ BleGamepad.h | 2 + BleGamepadConfiguration.cpp | 6 +- BleGamepadConfiguration.h | 3 + .../CharacteristicsConfiguration.ino | 68 ++++++++++++------- examples/GetPeerInfo/GetPeerInfo.ino | 1 + keywords.txt | 2 + 7 files changed, 70 insertions(+), 27 deletions(-) diff --git a/BleGamepad.cpp b/BleGamepad.cpp index 23c4700..baa6b89 100644 --- a/BleGamepad.cpp +++ b/BleGamepad.cpp @@ -45,6 +45,7 @@ std::string softwareRevision; std::string serialNumber; std::string firmwareRevision; std::string hardwareRevision; +int8_t powerLevel; bool enableOutputReport = false; uint16_t outputReportLength = 64; @@ -97,6 +98,8 @@ void BleGamepad::begin(BleGamepadConfiguration *config) enableOutputReport = configuration.getEnableOutputReport(); outputReportLength = configuration.getOutputReportLength(); + + powerLevel = configuration.getTXPowerLevel(); #ifndef PNPVersionField uint8_t high = highByte(vid); @@ -1575,12 +1578,24 @@ String BleGamepad::getDeviceManufacturer() return this->deviceManufacturer.c_str(); } +int8_t BleGamepad::getTXPowerLevel() +{ + return NimBLEDevice::getPower(); +} + +void BleGamepad::setTXPowerLevel(int8_t level) +{ + powerLevel = level; + NimBLEDevice::setPower(powerLevel); // The only valid values are: -12, -9, -6, -3, 0, 3, 6 and 9 +} + void BleGamepad::taskServer(void *pvParameter) { BleGamepad *BleGamepadInstance = (BleGamepad *)pvParameter; // static_cast(pvParameter); NimBLEDevice::init(BleGamepadInstance->deviceName); + NimBLEDevice::setPower(powerLevel); // Set transmit power for advertising (Range: -127 to +9 dBm) NimBLEServer *pServer = NimBLEDevice::createServer(); pServer->setCallbacks(BleGamepadInstance->connectionStatus); pServer->advertiseOnDisconnect(true); diff --git a/BleGamepad.h b/BleGamepad.h index 6199fb4..ba3b272 100644 --- a/BleGamepad.h +++ b/BleGamepad.h @@ -109,6 +109,8 @@ class BleGamepad bool isConnected(void); void resetButtons(); void setBatteryLevel(uint8_t level); + void setTXPowerLevel(int8_t level = 9); + int8_t getTXPowerLevel(); uint8_t batteryLevel; std::string deviceManufacturer; std::string deviceName; diff --git a/BleGamepadConfiguration.cpp b/BleGamepadConfiguration.cpp index 32a8194..bddcc32 100644 --- a/BleGamepadConfiguration.cpp +++ b/BleGamepadConfiguration.cpp @@ -21,7 +21,8 @@ BleGamepadConfiguration::BleGamepadConfiguration() : _controllerType(CONTROLLER_ _firmwareRevision("0.7.0"), _hardwareRevision("1.0.0"), _enableOutputReport(false), - _outputReportLength(64) + _outputReportLength(64), + _powerLevel(9) { } @@ -123,6 +124,8 @@ char *BleGamepadConfiguration::getFirmwareRevision(){ return _firmwareRevision; char *BleGamepadConfiguration::getHardwareRevision(){ return _hardwareRevision; } bool BleGamepadConfiguration::getEnableOutputReport(){ return _enableOutputReport; } uint16_t BleGamepadConfiguration::getOutputReportLength(){ return _outputReportLength; } +int8_t BleGamepadConfiguration::getTXPowerLevel(){ return _powerLevel; } // Returns the power level that was set as the server started + void BleGamepadConfiguration::setWhichSpecialButtons(bool start, bool select, bool menu, bool home, bool back, bool volumeInc, bool volumeDec, bool volumeMute) { _whichSpecialButtons[START_BUTTON] = start; @@ -196,3 +199,4 @@ void BleGamepadConfiguration::setFirmwareRevision(char *value) { _firmwareRevisi void BleGamepadConfiguration::setHardwareRevision(char *value) { _hardwareRevision = value; } void BleGamepadConfiguration::setEnableOutputReport(bool value) { _enableOutputReport = value; } void BleGamepadConfiguration::setOutputReportLength(uint16_t value) { _outputReportLength = value; } +void BleGamepadConfiguration::setTXPowerLevel(int8_t value) { _powerLevel = value; } diff --git a/BleGamepadConfiguration.h b/BleGamepadConfiguration.h index 97929cf..bf97836 100644 --- a/BleGamepadConfiguration.h +++ b/BleGamepadConfiguration.h @@ -224,6 +224,7 @@ class BleGamepadConfiguration char *_hardwareRevision; bool _enableOutputReport; uint16_t _outputReportLength; + int8_t _powerLevel; public: BleGamepadConfiguration(); @@ -276,6 +277,7 @@ class BleGamepadConfiguration char *getHardwareRevision(); bool getEnableOutputReport(); uint16_t getOutputReportLength(); + int8_t getTXPowerLevel(); void setControllerType(uint8_t controllerType); void setAutoReport(bool value); @@ -320,6 +322,7 @@ class BleGamepadConfiguration void setHardwareRevision(char *value); void setEnableOutputReport(bool value); void setOutputReportLength(uint16_t value); + void setTXPowerLevel(int8_t value); }; #endif diff --git a/examples/CharacteristicsConfiguration/CharacteristicsConfiguration.ino b/examples/CharacteristicsConfiguration/CharacteristicsConfiguration.ino index 0feec63..9687168 100644 --- a/examples/CharacteristicsConfiguration/CharacteristicsConfiguration.ino +++ b/examples/CharacteristicsConfiguration/CharacteristicsConfiguration.ino @@ -1,41 +1,57 @@ /* - * Sets BLE characteristic options - * Use BLE Scanner etc on Android to see them - */ + Sets BLE characteristic options + Use BLE Scanner etc on Android to see them + + Also shows how to set transmit power during initial configuration, + or at any stage whilst running by using bleGamepad.setTXPowerLevel(int8_t) + + The only valid values are: -12, -9, -6, -3, 0, 3, 6 and 9 + Values correlate to dbm + + You can get the currently set TX power level by calling bleGamepad.setTXPowerLevel() + +*/ #include #include +int8_t txPowerLevel = 3; + BleGamepad bleGamepad("Custom Contoller Name", "lemmingDev", 100); // Set custom device name, manufacturer and initial battery level BleGamepadConfiguration bleGamepadConfig; // Create a BleGamepadConfiguration object to store all of the options - + 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.setVid(0xe502); - bleGamepadConfig.setPid(0xabcd); - - bleGamepadConfig.setModelNumber("1.0"); - bleGamepadConfig.setSoftwareRevision("Software Rev 1"); - bleGamepadConfig.setSerialNumber("9876543210"); - bleGamepadConfig.setFirmwareRevision("2.0"); - bleGamepadConfig.setHardwareRevision("1.7"); - - // Some non-Windows operating systems and web based gamepad testers don't like min axis set below 0, so 0 is set by default - //bleGamepadConfig.setAxesMin(0x8001); // -32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal - 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 - - bleGamepad.begin(&bleGamepadConfig); // Begin gamepad with configuration options + 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.setVid(0xe502); + bleGamepadConfig.setPid(0xabcd); + bleGamepadConfig.setTXPowerLevel(txPowerLevel); // Defaults to 9 if not set. (Range: -12 to 9 dBm) + + bleGamepadConfig.setModelNumber("1.0"); + bleGamepadConfig.setSoftwareRevision("Software Rev 1"); + bleGamepadConfig.setSerialNumber("9876543210"); + bleGamepadConfig.setFirmwareRevision("2.0"); + bleGamepadConfig.setHardwareRevision("1.7"); + + // Some non-Windows operating systems and web based gamepad testers don't like min axis set below 0, so 0 is set by default + //bleGamepadConfig.setAxesMin(0x8001); // -32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal + 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 + + bleGamepad.begin(&bleGamepadConfig); // Begin gamepad with configuration options + + // Change power level to 6 + bleGamepad.setTXPowerLevel(6); + } void loop() { - if (bleGamepad.isConnected()) - { + if (bleGamepad.isConnected()) + { - } + } } diff --git a/examples/GetPeerInfo/GetPeerInfo.ino b/examples/GetPeerInfo/GetPeerInfo.ino index e86fd60..cafd365 100644 --- a/examples/GetPeerInfo/GetPeerInfo.ino +++ b/examples/GetPeerInfo/GetPeerInfo.ino @@ -69,6 +69,7 @@ void loop() Serial.println(bleGamepad.configuration.getVid(), HEX); Serial.println(bleGamepad.configuration.getPid(), HEX); Serial.println(bleGamepad.configuration.getGuidVersion()); + Serial.println(bleGamepad.configuration.getTXPowerLevel()); Serial.println(); delay(1000); } diff --git a/keywords.txt b/keywords.txt index 807d3a5..2ba7544 100644 --- a/keywords.txt +++ b/keywords.txt @@ -158,6 +158,8 @@ enterPairingMode KEYWORD2 getPeerInfo KEYWORD2 getDeviceName KEYWORD2 getDeviceManufacturer KEYWORD2 +getTXPowerLevel KEYWORD2 +setTXPowerLevel KEYWORD2 ####################################### # Constants