mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 21:48:35 -07:00
FindMy Flipper Battery Status Refactor, Unify State Sync
This commit is contained in:
@@ -103,17 +103,8 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
app->state.broadcast_interval = value;
|
app->state.broadcast_interval = value;
|
||||||
findmy_state_sync_config(&app->state);
|
|
||||||
findmy_state_save(&app->state);
|
|
||||||
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
|
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
|
||||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
findmy_state_save_and_apply(app, &app->state);
|
||||||
// Always check if beacon is active before changing config
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
|
||||||
}
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
|
|
||||||
if(app->state.beacon_active) {
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_start());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_change_transmit_power(FindMy* app, uint8_t value) {
|
void findmy_change_transmit_power(FindMy* app, uint8_t value) {
|
||||||
@@ -121,49 +112,32 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
app->state.transmit_power = value;
|
app->state.transmit_power = value;
|
||||||
findmy_state_sync_config(&app->state);
|
findmy_state_save_and_apply(app, &app->state);
|
||||||
findmy_state_save(&app->state);
|
|
||||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
|
||||||
}
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
|
|
||||||
if(app->state.beacon_active) {
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_start());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_toggle_show_mac(FindMy* app, bool show_mac) {
|
void findmy_toggle_show_mac(FindMy* app, bool show_mac) {
|
||||||
app->state.show_mac = show_mac;
|
app->state.show_mac = show_mac;
|
||||||
findmy_state_sync_config(&app->state);
|
|
||||||
findmy_state_save(&app->state);
|
|
||||||
findmy_main_toggle_mac(app->findmy_main, app->state.show_mac);
|
findmy_main_toggle_mac(app->findmy_main, app->state.show_mac);
|
||||||
|
findmy_state_save_and_apply(app, &app->state);
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_toggle_beacon(FindMy* app) {
|
void findmy_toggle_beacon(FindMy* app) {
|
||||||
app->state.beacon_active = !app->state.beacon_active;
|
app->state.beacon_active = !app->state.beacon_active;
|
||||||
findmy_state_save(&app->state);
|
findmy_state_save_and_apply(app, &app->state);
|
||||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
|
||||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
|
||||||
}
|
|
||||||
if(app->state.beacon_active) {
|
|
||||||
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) {
|
||||||
app->state.tag_type = type;
|
app->state.tag_type = type;
|
||||||
findmy_state_sync_config(&app->state);
|
findmy_state_save_and_apply(app, &app->state);
|
||||||
findmy_state_save(&app->state);
|
|
||||||
findmy_main_update_type(app->findmy_main, type);
|
findmy_main_update_type(app->findmy_main, type);
|
||||||
FURI_LOG_I("TagType2", "Tag Type: %d", type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_update_battery(FindMy* app, uint8_t battery_level) {
|
void findmy_state_save_and_apply(FindMy* app, FindMyState* state) {
|
||||||
uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
|
uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
|
||||||
uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
|
uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
|
||||||
uint16_t battery_percent = (battery_remaining * 100) / battery_capacity;
|
uint16_t battery_percent = (battery_remaining * 100) / battery_capacity;
|
||||||
|
uint8_t battery_level;
|
||||||
|
|
||||||
if(battery_percent > 80) {
|
if(battery_percent > 80) {
|
||||||
battery_level = BATTERY_FULL;
|
battery_level = BATTERY_FULL;
|
||||||
@@ -174,10 +148,19 @@ void findmy_update_battery(FindMy* app, uint8_t battery_level) {
|
|||||||
} else {
|
} else {
|
||||||
battery_level = BATTERY_CRITICAL;
|
battery_level = BATTERY_CRITICAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
app->state.battery_level = battery_level;
|
app->state.battery_level = battery_level;
|
||||||
findmy_state_sync_config(&app->state);
|
|
||||||
findmy_state_save(&app->state);
|
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||||
|
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||||
|
}
|
||||||
|
furi_check(
|
||||||
|
furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
|
||||||
|
findmy_state_sync_config(state);
|
||||||
|
findmy_state_save(state);
|
||||||
|
furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
|
||||||
|
if(state->beacon_active) {
|
||||||
|
furi_check(furi_hal_bt_extra_beacon_start());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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]) {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ struct FindMy {
|
|||||||
|
|
||||||
uint8_t mac_buf[EXTRA_BEACON_MAC_ADDR_SIZE];
|
uint8_t mac_buf[EXTRA_BEACON_MAC_ADDR_SIZE];
|
||||||
uint8_t packet_buf[EXTRA_BEACON_MAX_DATA_SIZE];
|
uint8_t packet_buf[EXTRA_BEACON_MAX_DATA_SIZE];
|
||||||
|
uint8_t battery_level;
|
||||||
|
|
||||||
FindMyState state;
|
FindMyState state;
|
||||||
};
|
};
|
||||||
@@ -54,4 +55,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);
|
void findmy_state_save_and_apply(FindMy* app, FindMyState* state);
|
||||||
|
|||||||
@@ -105,13 +105,13 @@ bool findmy_state_load(FindMyState* out_state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void findmy_state_apply(FindMyState* state) {
|
void findmy_state_apply(FindMyState* state) {
|
||||||
// Stop any running beacon
|
// This function applies initial state to the beacon (loaded values)
|
||||||
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);
|
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) {
|
||||||
@@ -129,27 +129,25 @@ void findmy_state_sync_config(FindMyState* state) {
|
|||||||
|
|
||||||
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type) {
|
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type) {
|
||||||
// Update the battery level in the payload
|
// Update the battery level in the payload
|
||||||
FURI_LOG_I("update_bat", "Before update: %d", battery_level);
|
|
||||||
if(type == FindMyTypeApple) {
|
if(type == FindMyTypeApple) {
|
||||||
switch(battery_level) {
|
switch(battery_level) {
|
||||||
case BATTERY_FULL:
|
case BATTERY_FULL:
|
||||||
data[6] = BATTERY_FULL;
|
data[6] = BATTERY_FULL;
|
||||||
break;
|
break;
|
||||||
case BATTERY_MEDIUM:
|
case BATTERY_MEDIUM:
|
||||||
data[6] = BATTERY_MEDIUM;
|
data[6] = BATTERY_MEDIUM;
|
||||||
break;
|
break;
|
||||||
case BATTERY_LOW:
|
case BATTERY_LOW:
|
||||||
data[6] = BATTERY_LOW;
|
data[6] = BATTERY_LOW;
|
||||||
break;
|
break;
|
||||||
case BATTERY_CRITICAL:
|
case BATTERY_CRITICAL:
|
||||||
data[6] = BATTERY_CRITICAL;
|
data[6] = BATTERY_CRITICAL;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
FURI_LOG_E("update_bat", "Invalid battery level: %d", battery_level);
|
FURI_LOG_E("update_bat", "Invalid battery level: %d", battery_level);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FURI_LOG_I("update_bat", "After update: %02X", data[6]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void findmy_state_save(FindMyState* state) {
|
void findmy_state_save(FindMyState* state) {
|
||||||
@@ -184,7 +182,6 @@ void findmy_state_save(FindMyState* state) {
|
|||||||
file, "data", state->data, findmy_state_data_size(state->tag_type)))
|
file, "data", state->data, findmy_state_data_size(state->tag_type)))
|
||||||
break;
|
break;
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
flipper_format_free(file);
|
flipper_format_free(file);
|
||||||
furi_record_close(RECORD_STORAGE);
|
furi_record_close(RECORD_STORAGE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ typedef enum {
|
|||||||
FindMyTypeTile,
|
FindMyTypeTile,
|
||||||
} FindMyType;
|
} FindMyType;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool beacon_active;
|
bool beacon_active;
|
||||||
uint8_t broadcast_interval;
|
uint8_t broadcast_interval;
|
||||||
|
|||||||
@@ -29,7 +29,9 @@ void findmy_scene_config_import_result_on_enter(void* context) {
|
|||||||
popup_set_timeout(popup, 1500);
|
popup_set_timeout(popup, 1500);
|
||||||
popup_set_context(popup, app);
|
popup_set_context(popup, app);
|
||||||
popup_set_callback(popup, findmy_scene_config_import_result_callback);
|
popup_set_callback(popup, findmy_scene_config_import_result_callback);
|
||||||
|
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
||||||
|
findmy_main_update_mac(app->findmy_main, app->state.mac);
|
||||||
|
findmy_main_update_type(app->findmy_main, app->state.tag_type);
|
||||||
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewPopup);
|
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewPopup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ 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,7 +25,6 @@ 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