Skip to content

Commit

Permalink
Fix butthurt and battery (flipperdevices#850)
Browse files Browse the repository at this point in the history
* Fix butthurt and battery
  • Loading branch information
albkharisov authored Nov 29, 2021
1 parent cf591ef commit d86125c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 0 additions & 1 deletion applications/dolphin/dolphin.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ static void dolphin_check_butthurt(DolphinState* state) {
float diff_time = difftime(state->data.timestamp, dolphin_state_timestamp());

if((fabs(diff_time)) > DOLPHIN_TIMEGATE) {
FURI_LOG_I("DolphinState", "Increasing butthurt");
dolphin_state_butthurted(state);
}
}
Expand Down
30 changes: 22 additions & 8 deletions applications/dolphin/helpers/dolphin_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define DOLPHIN_LVL_THRESHOLD 20.0f
#define LEVEL2_THRESHOLD 20
#define LEVEL3_THRESHOLD 100
#define BUTTHURT_MAX 14
#define BUTTHURT_MIN 0

DolphinState* dolphin_state_alloc() {
return furi_alloc(sizeof(DolphinState));
Expand Down Expand Up @@ -44,20 +46,27 @@ bool dolphin_state_save(DolphinState* dolphin_state) {
}

bool dolphin_state_load(DolphinState* dolphin_state) {
bool loaded = saved_struct_load(
bool success = saved_struct_load(
DOLPHIN_STATE_PATH,
&dolphin_state->data,
sizeof(DolphinStoreData),
DOLPHIN_STATE_HEADER_MAGIC,
DOLPHIN_STATE_HEADER_VERSION);

if(!loaded) {
if(success) {
if((dolphin_state->data.butthurt > BUTTHURT_MAX) ||
(dolphin_state->data.butthurt < BUTTHURT_MIN)) {
success = false;
}
}

if(!success) {
FURI_LOG_W(TAG, "Reset dolphin-state");
memset(dolphin_state, 0, sizeof(*dolphin_state));
dolphin_state->dirty = true;
}

return loaded;
return success;
}

uint64_t dolphin_state_timestamp() {
Expand Down Expand Up @@ -124,8 +133,10 @@ bool dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
dolphin_state->data.icounter += MIN(xp_to_levelup, deed_weight->icounter);
}

uint32_t new_butthurt =
CLAMP(((int32_t)dolphin_state->data.butthurt) + deed_weight->butthurt, 14, 0);
uint32_t new_butthurt = CLAMP(
((int32_t)dolphin_state->data.butthurt) + deed_weight->butthurt,
BUTTHURT_MAX,
BUTTHURT_MIN);

if(!!dolphin_state->data.butthurt != !!new_butthurt) {
mood_changed = true;
Expand All @@ -138,9 +149,12 @@ bool dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
}

void dolphin_state_butthurted(DolphinState* dolphin_state) {
dolphin_state->data.butthurt++;
dolphin_state->data.timestamp = dolphin_state_timestamp();
dolphin_state->dirty = true;
if(dolphin_state->data.butthurt < BUTTHURT_MAX) {
dolphin_state->data.butthurt++;
FURI_LOG_I("DolphinState", "Increasing butthurt");
dolphin_state->data.timestamp = dolphin_state_timestamp();
dolphin_state->dirty = true;
}
}

void dolphin_state_increase_level(DolphinState* dolphin_state) {
Expand Down
3 changes: 2 additions & 1 deletion applications/power/power_service/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gui/view.h>

#define POWER_OFF_TIMEOUT 90
#define POWER_BATTERY_WELL_LEVEL 99
#define POWER_BATTERY_WELL_LEVEL 70

bool power_is_battery_well(PowerInfo* info) {
return info->health > POWER_BATTERY_WELL_LEVEL;
Expand Down Expand Up @@ -172,6 +172,7 @@ static void power_check_battery_level_change(Power* power) {
int32_t power_srv(void* p) {
(void)p;
Power* power = power_alloc();
power_update_info(power);
furi_record_create("power", power);

while(1) {
Expand Down

0 comments on commit d86125c

Please sign in to comment.