Skip to content

Commit c48ca5d

Browse files
authored
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)
1 parent 02c7ece commit c48ca5d

7 files changed

+70
-27
lines changed

BleGamepad.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ std::string softwareRevision;
4545
std::string serialNumber;
4646
std::string firmwareRevision;
4747
std::string hardwareRevision;
48+
int8_t powerLevel;
4849

4950
bool enableOutputReport = false;
5051
uint16_t outputReportLength = 64;
@@ -97,6 +98,8 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
9798

9899
enableOutputReport = configuration.getEnableOutputReport();
99100
outputReportLength = configuration.getOutputReportLength();
101+
102+
powerLevel = configuration.getTXPowerLevel();
100103

101104
#ifndef PNPVersionField
102105
uint8_t high = highByte(vid);
@@ -1575,12 +1578,24 @@ String BleGamepad::getDeviceManufacturer()
15751578
return this->deviceManufacturer.c_str();
15761579
}
15771580

1581+
int8_t BleGamepad::getTXPowerLevel()
1582+
{
1583+
return NimBLEDevice::getPower();
1584+
}
1585+
1586+
void BleGamepad::setTXPowerLevel(int8_t level)
1587+
{
1588+
powerLevel = level;
1589+
NimBLEDevice::setPower(powerLevel); // The only valid values are: -12, -9, -6, -3, 0, 3, 6 and 9
1590+
}
1591+
15781592

15791593
void BleGamepad::taskServer(void *pvParameter)
15801594
{
15811595
BleGamepad *BleGamepadInstance = (BleGamepad *)pvParameter; // static_cast<BleGamepad *>(pvParameter);
15821596

15831597
NimBLEDevice::init(BleGamepadInstance->deviceName);
1598+
NimBLEDevice::setPower(powerLevel); // Set transmit power for advertising (Range: -127 to +9 dBm)
15841599
NimBLEServer *pServer = NimBLEDevice::createServer();
15851600
pServer->setCallbacks(BleGamepadInstance->connectionStatus);
15861601
pServer->advertiseOnDisconnect(true);

BleGamepad.h

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class BleGamepad
109109
bool isConnected(void);
110110
void resetButtons();
111111
void setBatteryLevel(uint8_t level);
112+
void setTXPowerLevel(int8_t level = 9);
113+
int8_t getTXPowerLevel();
112114
uint8_t batteryLevel;
113115
std::string deviceManufacturer;
114116
std::string deviceName;

BleGamepadConfiguration.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ BleGamepadConfiguration::BleGamepadConfiguration() : _controllerType(CONTROLLER_
2121
_firmwareRevision("0.7.0"),
2222
_hardwareRevision("1.0.0"),
2323
_enableOutputReport(false),
24-
_outputReportLength(64)
24+
_outputReportLength(64),
25+
_powerLevel(9)
2526
{
2627
}
2728

@@ -123,6 +124,8 @@ char *BleGamepadConfiguration::getFirmwareRevision(){ return _firmwareRevision;
123124
char *BleGamepadConfiguration::getHardwareRevision(){ return _hardwareRevision; }
124125
bool BleGamepadConfiguration::getEnableOutputReport(){ return _enableOutputReport; }
125126
uint16_t BleGamepadConfiguration::getOutputReportLength(){ return _outputReportLength; }
127+
int8_t BleGamepadConfiguration::getTXPowerLevel(){ return _powerLevel; } // Returns the power level that was set as the server started
128+
126129
void BleGamepadConfiguration::setWhichSpecialButtons(bool start, bool select, bool menu, bool home, bool back, bool volumeInc, bool volumeDec, bool volumeMute)
127130
{
128131
_whichSpecialButtons[START_BUTTON] = start;
@@ -196,3 +199,4 @@ void BleGamepadConfiguration::setFirmwareRevision(char *value) { _firmwareRevisi
196199
void BleGamepadConfiguration::setHardwareRevision(char *value) { _hardwareRevision = value; }
197200
void BleGamepadConfiguration::setEnableOutputReport(bool value) { _enableOutputReport = value; }
198201
void BleGamepadConfiguration::setOutputReportLength(uint16_t value) { _outputReportLength = value; }
202+
void BleGamepadConfiguration::setTXPowerLevel(int8_t value) { _powerLevel = value; }

BleGamepadConfiguration.h

+3
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class BleGamepadConfiguration
224224
char *_hardwareRevision;
225225
bool _enableOutputReport;
226226
uint16_t _outputReportLength;
227+
int8_t _powerLevel;
227228

228229
public:
229230
BleGamepadConfiguration();
@@ -276,6 +277,7 @@ class BleGamepadConfiguration
276277
char *getHardwareRevision();
277278
bool getEnableOutputReport();
278279
uint16_t getOutputReportLength();
280+
int8_t getTXPowerLevel();
279281

280282
void setControllerType(uint8_t controllerType);
281283
void setAutoReport(bool value);
@@ -320,6 +322,7 @@ class BleGamepadConfiguration
320322
void setHardwareRevision(char *value);
321323
void setEnableOutputReport(bool value);
322324
void setOutputReportLength(uint16_t value);
325+
void setTXPowerLevel(int8_t value);
323326
};
324327

325328
#endif
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
/*
2-
* Sets BLE characteristic options
3-
* Use BLE Scanner etc on Android to see them
4-
*/
2+
Sets BLE characteristic options
3+
Use BLE Scanner etc on Android to see them
4+
5+
Also shows how to set transmit power during initial configuration,
6+
or at any stage whilst running by using bleGamepad.setTXPowerLevel(int8_t)
7+
8+
The only valid values are: -12, -9, -6, -3, 0, 3, 6 and 9
9+
Values correlate to dbm
10+
11+
You can get the currently set TX power level by calling bleGamepad.setTXPowerLevel()
12+
13+
*/
514

615
#include <Arduino.h>
716
#include <BleGamepad.h>
817

18+
int8_t txPowerLevel = 3;
19+
920
BleGamepad bleGamepad("Custom Contoller Name", "lemmingDev", 100); // Set custom device name, manufacturer and initial battery level
1021
BleGamepadConfiguration bleGamepadConfig; // Create a BleGamepadConfiguration object to store all of the options
11-
22+
1223
void setup()
1324
{
14-
Serial.begin(115200);
15-
Serial.println("Starting BLE work!");
16-
bleGamepadConfig.setAutoReport(false);
17-
bleGamepadConfig.setControllerType(CONTROLLER_TYPE_GAMEPAD); // CONTROLLER_TYPE_JOYSTICK, CONTROLLER_TYPE_GAMEPAD (DEFAULT), CONTROLLER_TYPE_MULTI_AXIS
18-
bleGamepadConfig.setVid(0xe502);
19-
bleGamepadConfig.setPid(0xabcd);
20-
21-
bleGamepadConfig.setModelNumber("1.0");
22-
bleGamepadConfig.setSoftwareRevision("Software Rev 1");
23-
bleGamepadConfig.setSerialNumber("9876543210");
24-
bleGamepadConfig.setFirmwareRevision("2.0");
25-
bleGamepadConfig.setHardwareRevision("1.7");
26-
27-
// Some non-Windows operating systems and web based gamepad testers don't like min axis set below 0, so 0 is set by default
28-
//bleGamepadConfig.setAxesMin(0x8001); // -32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
29-
bleGamepadConfig.setAxesMin(0x0000); // 0 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
30-
bleGamepadConfig.setAxesMax(0x7FFF); // 32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
31-
32-
bleGamepad.begin(&bleGamepadConfig); // Begin gamepad with configuration options
25+
Serial.begin(115200);
26+
Serial.println("Starting BLE work!");
27+
bleGamepadConfig.setAutoReport(false);
28+
bleGamepadConfig.setControllerType(CONTROLLER_TYPE_GAMEPAD); // CONTROLLER_TYPE_JOYSTICK, CONTROLLER_TYPE_GAMEPAD (DEFAULT), CONTROLLER_TYPE_MULTI_AXIS
29+
bleGamepadConfig.setVid(0xe502);
30+
bleGamepadConfig.setPid(0xabcd);
31+
bleGamepadConfig.setTXPowerLevel(txPowerLevel); // Defaults to 9 if not set. (Range: -12 to 9 dBm)
32+
33+
bleGamepadConfig.setModelNumber("1.0");
34+
bleGamepadConfig.setSoftwareRevision("Software Rev 1");
35+
bleGamepadConfig.setSerialNumber("9876543210");
36+
bleGamepadConfig.setFirmwareRevision("2.0");
37+
bleGamepadConfig.setHardwareRevision("1.7");
38+
39+
// Some non-Windows operating systems and web based gamepad testers don't like min axis set below 0, so 0 is set by default
40+
//bleGamepadConfig.setAxesMin(0x8001); // -32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
41+
bleGamepadConfig.setAxesMin(0x0000); // 0 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
42+
bleGamepadConfig.setAxesMax(0x7FFF); // 32767 --> int16_t - 16 bit signed integer - Can be in decimal or hexadecimal
43+
44+
bleGamepad.begin(&bleGamepadConfig); // Begin gamepad with configuration options
45+
46+
// Change power level to 6
47+
bleGamepad.setTXPowerLevel(6);
48+
3349
}
3450

3551
void loop()
3652
{
37-
if (bleGamepad.isConnected())
38-
{
53+
if (bleGamepad.isConnected())
54+
{
3955

40-
}
56+
}
4157
}

examples/GetPeerInfo/GetPeerInfo.ino

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void loop()
6969
Serial.println(bleGamepad.configuration.getVid(), HEX);
7070
Serial.println(bleGamepad.configuration.getPid(), HEX);
7171
Serial.println(bleGamepad.configuration.getGuidVersion());
72+
Serial.println(bleGamepad.configuration.getTXPowerLevel());
7273
Serial.println();
7374
delay(1000);
7475
}

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ enterPairingMode KEYWORD2
158158
getPeerInfo KEYWORD2
159159
getDeviceName KEYWORD2
160160
getDeviceManufacturer KEYWORD2
161+
getTXPowerLevel KEYWORD2
162+
setTXPowerLevel KEYWORD2
161163

162164
#######################################
163165
# Constants

0 commit comments

Comments
 (0)