mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 13:28:36 -07:00
Add Battery Reporting to FindMy
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "findmy_i.h"
|
#include "findmy_i.h"
|
||||||
|
#include <furi_hal_power.h>
|
||||||
|
|
||||||
static bool findmy_custom_event_callback(void* context, uint32_t event) {
|
static bool findmy_custom_event_callback(void* context, uint32_t event) {
|
||||||
furi_assert(context);
|
furi_assert(context);
|
||||||
@@ -148,6 +149,7 @@ void findmy_toggle_beacon(FindMy* app) {
|
|||||||
furi_check(furi_hal_bt_extra_beacon_start());
|
furi_check(furi_hal_bt_extra_beacon_start());
|
||||||
}
|
}
|
||||||
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
||||||
|
findmy_update_battery(app, app->state.battery_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_set_tag_type(FindMy* app, FindMyType type) {
|
void findmy_set_tag_type(FindMy* app, FindMyType type) {
|
||||||
@@ -158,6 +160,26 @@ void findmy_set_tag_type(FindMy* app, FindMyType type) {
|
|||||||
FURI_LOG_I("TagType2", "Tag Type: %d", type);
|
FURI_LOG_I("TagType2", "Tag Type: %d", type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void findmy_update_battery(FindMy* app, uint8_t battery_level) {
|
||||||
|
uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
|
||||||
|
uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
|
||||||
|
uint16_t battery_percent = (battery_remaining * 100) / battery_capacity;
|
||||||
|
|
||||||
|
if(battery_percent > 80) {
|
||||||
|
battery_level = BATTERY_FULL;
|
||||||
|
} else if(battery_percent > 50) {
|
||||||
|
battery_level = BATTERY_MEDIUM;
|
||||||
|
} else if(battery_percent > 20) {
|
||||||
|
battery_level = BATTERY_LOW;
|
||||||
|
} else {
|
||||||
|
battery_level = BATTERY_CRITICAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
app->state.battery_level = battery_level;
|
||||||
|
findmy_state_sync_config(&app->state);
|
||||||
|
findmy_state_save(&app->state);
|
||||||
|
}
|
||||||
|
|
||||||
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) {
|
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) {
|
||||||
uint8_t tmp;
|
uint8_t tmp;
|
||||||
for(size_t i = 0; i < GAP_MAC_ADDR_SIZE / 2; i++) {
|
for(size_t i = 0; i < GAP_MAC_ADDR_SIZE / 2; i++) {
|
||||||
|
|||||||
@@ -24,6 +24,11 @@
|
|||||||
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]);
|
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define BATTERY_FULL 0x00
|
||||||
|
#define BATTERY_MEDIUM 0x50
|
||||||
|
#define BATTERY_LOW 0xA0
|
||||||
|
#define BATTERY_CRITICAL 0xF0
|
||||||
|
|
||||||
struct FindMy {
|
struct FindMy {
|
||||||
Gui* gui;
|
Gui* gui;
|
||||||
Storage* storage;
|
Storage* storage;
|
||||||
@@ -54,3 +59,4 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value);
|
|||||||
void findmy_toggle_show_mac(FindMy* app, bool show_mac);
|
void findmy_toggle_show_mac(FindMy* app, bool show_mac);
|
||||||
void findmy_set_tag_type(FindMy* app, FindMyType type);
|
void findmy_set_tag_type(FindMy* app, FindMyType type);
|
||||||
void findmy_toggle_beacon(FindMy* app);
|
void findmy_toggle_beacon(FindMy* app);
|
||||||
|
void findmy_update_battery(FindMy* app, uint8_t battery_level);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "findmy_state.h"
|
#include "findmy_state.h"
|
||||||
|
|
||||||
|
#include "findmy_i.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <furi_hal_bt.h>
|
#include <furi_hal_bt.h>
|
||||||
@@ -42,6 +43,13 @@ bool findmy_state_load(FindMyState* out_state) {
|
|||||||
}
|
}
|
||||||
state.tag_type = tmp;
|
state.tag_type = tmp;
|
||||||
|
|
||||||
|
if(!flipper_format_read_uint32(file, "battery_level", &tmp, 1)) {
|
||||||
|
tmp = 0x00; // Default battery level set to Full
|
||||||
|
flipper_format_rewind(file);
|
||||||
|
}
|
||||||
|
FURI_LOG_I("findmy_load", "Saved Battery: %ld", tmp);
|
||||||
|
state.battery_level = tmp;
|
||||||
|
|
||||||
if(!flipper_format_read_hex(file, "mac", state.mac, sizeof(state.mac))) break;
|
if(!flipper_format_read_hex(file, "mac", state.mac, sizeof(state.mac))) break;
|
||||||
|
|
||||||
if(!flipper_format_read_hex(
|
if(!flipper_format_read_hex(
|
||||||
@@ -74,12 +82,12 @@ bool findmy_state_load(FindMyState* out_state) {
|
|||||||
*data++ = 0x00; // ...
|
*data++ = 0x00; // ...
|
||||||
*data++ = 0x12; // Type (FindMy)
|
*data++ = 0x12; // Type (FindMy)
|
||||||
*data++ = 0x19; // Length
|
*data++ = 0x19; // Length
|
||||||
*data++ = 0x00; // Status
|
*data++ = 0x00; // Battery Status set to Full
|
||||||
// Placeholder Empty Public Key without the MAC address
|
// Placeholder Empty Public Key without the MAC address
|
||||||
for(size_t i = 0; i < 22; ++i) {
|
for(size_t i = 0; i < 22; ++i) {
|
||||||
*data++ = 0x00;
|
*data++ = 0x00;
|
||||||
}
|
}
|
||||||
*data++ = 0x00; // First 2 bits are the version, the rest is the battery level
|
*data++ = 0x00; // First 2 bits are the version
|
||||||
*data++ = 0x00; // Hint (0x00)
|
*data++ = 0x00; // Hint (0x00)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,12 +110,11 @@ void findmy_state_apply(FindMyState* state) {
|
|||||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||||
}
|
}
|
||||||
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
|
furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
|
||||||
|
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||||
|
|
||||||
furi_check(
|
furi_check(
|
||||||
furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
|
furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
|
||||||
|
|
||||||
if(state->beacon_active) {
|
if(state->beacon_active) {
|
||||||
furi_check(furi_hal_bt_extra_beacon_start());
|
furi_check(furi_hal_bt_extra_beacon_start());
|
||||||
}
|
}
|
||||||
@@ -118,6 +125,32 @@ void findmy_state_sync_config(FindMyState* state) {
|
|||||||
state->config.max_adv_interval_ms = (state->broadcast_interval * 1000) + 150;
|
state->config.max_adv_interval_ms = (state->broadcast_interval * 1000) + 150;
|
||||||
state->config.adv_power_level = GapAdvPowerLevel_0dBm + state->transmit_power;
|
state->config.adv_power_level = GapAdvPowerLevel_0dBm + state->transmit_power;
|
||||||
memcpy(state->config.address, state->mac, sizeof(state->config.address));
|
memcpy(state->config.address, state->mac, sizeof(state->config.address));
|
||||||
|
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type) {
|
||||||
|
// Update the battery level in the payload
|
||||||
|
FURI_LOG_I("update_bat", "Before update: %d", battery_level);
|
||||||
|
if(type == FindMyTypeApple) {
|
||||||
|
switch(battery_level) {
|
||||||
|
case BATTERY_FULL:
|
||||||
|
data[6] = BATTERY_FULL;
|
||||||
|
break;
|
||||||
|
case BATTERY_MEDIUM:
|
||||||
|
data[6] = BATTERY_MEDIUM;
|
||||||
|
break;
|
||||||
|
case BATTERY_LOW:
|
||||||
|
data[6] = BATTERY_LOW;
|
||||||
|
break;
|
||||||
|
case BATTERY_CRITICAL:
|
||||||
|
data[6] = BATTERY_CRITICAL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
FURI_LOG_E("update_bat", "Invalid battery level: %d", battery_level);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FURI_LOG_I("update_bat", "After update: %02X", data[6]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_state_save(FindMyState* state) {
|
void findmy_state_save(FindMyState* state) {
|
||||||
@@ -141,10 +174,13 @@ void findmy_state_save(FindMyState* state) {
|
|||||||
tmp = state->tag_type;
|
tmp = state->tag_type;
|
||||||
if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break;
|
if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break;
|
||||||
|
|
||||||
|
tmp = state->battery_level;
|
||||||
|
if(!flipper_format_write_uint32(file, "battery_level", &tmp, 1)) break;
|
||||||
|
|
||||||
if(!flipper_format_write_bool(file, "show_mac", &state->show_mac, 1)) break;
|
if(!flipper_format_write_bool(file, "show_mac", &state->show_mac, 1)) break;
|
||||||
|
|
||||||
if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break;
|
if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break;
|
||||||
|
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||||
if(!flipper_format_write_hex(
|
if(!flipper_format_write_hex(
|
||||||
file, "data", state->data, findmy_state_data_size(state->tag_type)))
|
file, "data", state->data, findmy_state_data_size(state->tag_type)))
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ typedef enum {
|
|||||||
FindMyTypeTile,
|
FindMyTypeTile,
|
||||||
} FindMyType;
|
} FindMyType;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool beacon_active;
|
bool beacon_active;
|
||||||
uint8_t broadcast_interval;
|
uint8_t broadcast_interval;
|
||||||
@@ -24,6 +25,8 @@ typedef struct {
|
|||||||
|
|
||||||
// Generated from the other state values
|
// Generated from the other state values
|
||||||
GapExtraBeaconConfig config;
|
GapExtraBeaconConfig config;
|
||||||
|
|
||||||
|
uint8_t battery_level;
|
||||||
} FindMyState;
|
} FindMyState;
|
||||||
|
|
||||||
bool findmy_state_load(FindMyState* out_state);
|
bool findmy_state_load(FindMyState* out_state);
|
||||||
@@ -34,4 +37,6 @@ void findmy_state_sync_config(FindMyState* state);
|
|||||||
|
|
||||||
void findmy_state_save(FindMyState* state);
|
void findmy_state_save(FindMyState* state);
|
||||||
|
|
||||||
|
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type);
|
||||||
|
|
||||||
uint8_t findmy_state_data_size(FindMyType type);
|
uint8_t findmy_state_data_size(FindMyType type);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ void findmy_scene_main_on_enter(void* context) {
|
|||||||
FindMy* app = context;
|
FindMy* app = context;
|
||||||
|
|
||||||
findmy_main_set_callback(app->findmy_main, findmy_scene_main_callback, app);
|
findmy_main_set_callback(app->findmy_main, findmy_scene_main_callback, app);
|
||||||
|
findmy_update_battery(app, app->state.battery_level);
|
||||||
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewMain);
|
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewMain);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +26,7 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) {
|
|||||||
break;
|
break;
|
||||||
case FindMyMainEventBackground:
|
case FindMyMainEventBackground:
|
||||||
app->state.beacon_active = true;
|
app->state.beacon_active = true;
|
||||||
|
findmy_update_battery(app, app->state.battery_level);
|
||||||
findmy_state_save(&app->state);
|
findmy_state_save(&app->state);
|
||||||
if(!furi_hal_bt_extra_beacon_is_active()) {
|
if(!furi_hal_bt_extra_beacon_is_active()) {
|
||||||
furi_check(furi_hal_bt_extra_beacon_start());
|
furi_check(furi_hal_bt_extra_beacon_start());
|
||||||
|
|||||||
Reference in New Issue
Block a user