Skip to content

Commit

Permalink
TX power level can now be set
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
lemmingDev authored Jan 31, 2025
1 parent 02c7ece commit c48ca5d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 27 deletions.
15 changes: 15 additions & 0 deletions BleGamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<BleGamepad *>(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);
Expand Down
2 changes: 2 additions & 0 deletions BleGamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion BleGamepadConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ BleGamepadConfiguration::BleGamepadConfiguration() : _controllerType(CONTROLLER_
_firmwareRevision("0.7.0"),
_hardwareRevision("1.0.0"),
_enableOutputReport(false),
_outputReportLength(64)
_outputReportLength(64),
_powerLevel(9)
{
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
3 changes: 3 additions & 0 deletions BleGamepadConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class BleGamepadConfiguration
char *_hardwareRevision;
bool _enableOutputReport;
uint16_t _outputReportLength;
int8_t _powerLevel;

public:
BleGamepadConfiguration();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include <BleGamepad.h>

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())
{

}
}
}
1 change: 1 addition & 0 deletions examples/GetPeerInfo/GetPeerInfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ enterPairingMode KEYWORD2
getPeerInfo KEYWORD2
getDeviceName KEYWORD2
getDeviceManufacturer KEYWORD2
getTXPowerLevel KEYWORD2
setTXPowerLevel KEYWORD2

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

0 comments on commit c48ca5d

Please sign in to comment.