Skip to content

Commit 32b7e3d

Browse files
Rick Hewesfpistm
Rick Hewes
authored andcommitted
Add Battery Status callback
1 parent 34b54c9 commit 32b7e3d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/STM32LoRaWAN.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ void STM32LoRaWAN::MacProcessNotify()
125125
instance->maintain_needed_callback();
126126
}
127127

128+
uint8_t STM32LoRaWAN::GetBatteryLevel()
129+
{
130+
// Called by the stack from an ISR when there is a DevStatusReq
131+
uint8_t battery_level = BAT_LEVEL_NO_MEASURE;
132+
if (instance->battery_level_callback) {
133+
battery_level = instance->battery_level_callback();
134+
}
135+
136+
return battery_level;
137+
}
138+
128139
void STM32LoRaWAN::maintain()
129140
{
130141
if (mac_process_pending) {

src/STM32LoRaWAN.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,18 @@ class STM32LoRaWAN : public Stream {
790790
*/
791791
void setMaintainNeededCallback(std::function<void(void)> callback) { this->maintain_needed_callback = callback; }
792792

793+
/**
794+
* Registers a callback that is called whenever there is a DevStatusReq.
795+
*
796+
* BAT_LEVEL_EXT_SRC - The end-device is connected to an external power source
797+
* BAT_LEVEL_EMPTY - The battery is empty
798+
* 1..254 - The battery level, 1 being at minimum and 254 being at maximum
799+
* BAT_LEVEL_FULL - The battery is full
800+
* BAT_LEVEL_NO_MEASURE - The end-device was not able to measure the battery level
801+
*
802+
* \NotInMKRWAN
803+
*/
804+
void setBatteryLevelCallback(std::function<uint8_t(void)> callback) { this->battery_level_callback = callback; }
793805
/// @}
794806

795807
/**
@@ -957,6 +969,7 @@ class STM32LoRaWAN : public Stream {
957969
static void MacMlmeConfirm(MlmeConfirm_t *MlmeConfirm);
958970
static void MacMlmeIndication(MlmeIndication_t *MlmeIndication, LoRaMacRxStatus_t *RxStatus);
959971
static void MacProcessNotify();
972+
static uint8_t GetBatteryLevel();
960973

961974
static STM32LoRaWAN *instance;
962975

@@ -968,7 +981,7 @@ class STM32LoRaWAN : public Stream {
968981
};
969982

970983
LoRaMacCallback_t LoRaMacCallbacks = {
971-
.GetBatteryLevel = nullptr,
984+
.GetBatteryLevel = GetBatteryLevel,
972985
.GetTemperatureLevel = nullptr,
973986
.GetUniqueId = nullptr, // Not needed, we just explicitly set the deveui in begin()
974987
.GetDevAddress = nullptr, // Not needed, user explicitly configures devaddr
@@ -1046,6 +1059,7 @@ class STM32LoRaWAN : public Stream {
10461059
uint32_t fcnt_down = 0;
10471060

10481061
std::function<void(void)> maintain_needed_callback;
1062+
std::function<uint8_t(void)> battery_level_callback;
10491063

10501064
bool mac_process_pending = false;
10511065

0 commit comments

Comments
 (0)