Skip to content

Commit 305e194

Browse files
Add setBattery to BLE HID devices (#1246)
1 parent 719ab01 commit 305e194

File tree

13 files changed

+30
-3
lines changed

13 files changed

+30
-3
lines changed

libraries/JoystickBLE/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sliderRight KEYWORD2
2727
hat KEYWORD2
2828
useManualSend KEYWORD2
2929
send_now KEYWORD2
30+
setBattery KEYWORD2
3031

3132
#######################################
3233
# Constants (LITERAL1)

libraries/JoystickBLE/src/JoystickBLE.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ void JoystickBLE_::end(void) {
8686
PicoBluetoothBLEHID.end();
8787
}
8888

89+
void JoystickBLE_::setBattery(int lvl) {
90+
PicoBluetoothBLEHID.setBattery(lvl);
91+
}
92+
8993
void JoystickBLE_::button(uint8_t button, bool val) {
9094
//I've no idea why, but without a second dword, it is not possible.
9195
//maybe something with the alignment when using bit set/clear?!?

libraries/JoystickBLE/src/JoystickBLE.h

+2
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,7 @@ class JoystickBLE_ {
6464
// and are mapped internally to int8_t
6565
//if use8bit(true) is called, -127 to 127 values are used.
6666
void use8bit(bool mode);
67+
68+
void setBattery(int lvl);
6769
};
6870
extern JoystickBLE_ JoystickBLE;

libraries/KeyboardBLE/examples/BLEKeyboardPassword/BLEKeyboardPassword.ino

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void loop() {
1515
if (BOOTSEL) {
1616
Serial.println("Typing password for you...shhhh....");
1717
KeyboardBLE.print("ThisPasswordIsWeakLikeABaby");
18+
KeyboardBLE.setBattery(random(0, 101)); // Set between 0...100%
1819
while (BOOTSEL);
1920
}
2021
}

libraries/KeyboardBLE/keywords.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ write KEYWORD2
1717
press KEYWORD2
1818
release KEYWORD2
1919
releaseAll KEYWORD2
20+
setBattery KEYWORD2
2021

2122
#######################################
2223
# Constants (LITERAL1)
2324
#######################################
24-

libraries/KeyboardBLE/src/KeyboardBLE.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ void KeyboardBLE_::end(void) {
4545
PicoBluetoothBLEHID.end();
4646
}
4747

48+
void KeyboardBLE_::setBattery(int lvl) {
49+
PicoBluetoothBLEHID.setBattery(lvl);
50+
}
51+
4852
void KeyboardBLE_::sendReport(KeyReport* keys) {
4953
hid_keyboard_report_t data;
5054
data.modifier = keys->modifiers;

libraries/KeyboardBLE/src/KeyboardBLE.h

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class KeyboardBLE_ : public Print {
135135
size_t press(uint8_t k);
136136
size_t release(uint8_t k);
137137
void releaseAll(void);
138+
void setBattery(int lvl);
138139
};
139140
extern KeyboardBLE_ KeyboardBLE;
140141

libraries/MouseBLE/examples/BLECircle/BLECircle.ino

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void loop() {
2626
oy = ay;
2727
delay(10);
2828
}
29+
MouseBLE.setBattery(random(0, 101)); // Set between 0...100%
2930
while (BOOTSEL) {
3031
delay(1);
3132
}

libraries/MouseBLE/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ move KEYWORD2
1818
press KEYWORD2
1919
release KEYWORD2
2020
isPressed KEYWORD2
21+
setBattery KEYWORD2
2122

2223
#######################################
2324
# Constants (LITERAL1)

libraries/MouseBLE/src/MouseBLE.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ void MouseBLE_::end(void) {
5454
PicoBluetoothBLEHID.end();
5555
}
5656

57+
void MouseBLE_::setBattery(int lvl) {
58+
PicoBluetoothBLEHID.setBattery(lvl);
59+
}
60+
5761
void MouseBLE_::click(uint8_t b) {
5862
_buttons = b;
5963
move(0, 0, 0);

libraries/MouseBLE/src/MouseBLE.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MouseBLE_ {
4646
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
4747
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
4848
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
49+
void setBattery(int lvl);
4950
};
5051
extern MouseBLE_ MouseBLE;
5152

libraries/PicoBluetoothBLEHID/keywords.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ connected KEYWORD2
2020
send KEYWORD2
2121
lockBluetooth KEYWORD2
2222
unlockBluetooth KEYWORD2
23-
getCID KEYWORD2
23+
setBattery KEYWORD2
2424

2525
#######################################
2626
# Constants (LITERAL1)
2727
#######################################
28-

libraries/PicoBluetoothBLEHID/src/PicoBluetoothBLEHID.h

+8
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ class PicoBluetoothBLEHID_ {
219219
return connected();
220220
}
221221

222+
bool setBattery(int level) {
223+
if (!_running || (level < 0) || (level > 100)) {
224+
return false;
225+
}
226+
battery_service_server_set_battery_value(level);
227+
return true;
228+
}
229+
222230
static void lockBluetooth() {
223231
async_context_acquire_lock_blocking(cyw43_arch_async_context());
224232
}

0 commit comments

Comments
 (0)