2020-12-18 20:15:29 +00:00
|
|
|
#include "dolphin_state.h"
|
2021-11-24 16:21:12 +00:00
|
|
|
#include <stdint.h>
|
[FL-1191][FL-1524] Filesystem rework (#568)
* FS-Api: removed datetime manipulation functions and most of the file flags
* Filesystem: common proxy api
* Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance.
* Storage: sd mount and sd file open
* Storage: sd file close
* Storage: temporary test app
* Storage: free filedata on close
* Storage: sd file read and write
* Storage: added internal storage (LittleFS)
* Storage: renamed internal commands
* Storage: seek, tell, truncate, size, sync, eof
* Storage: error descriptions
* Storage: directory management api (open, close, read, rewind)
* Storage: common management api (stat, fs_stat, remove, rename, mkdir)
* Dolphin app and Notifications app now use raw storage.
* Storage: storage statuses renamed. Implemented sd card icon.
* Storage: added raw sd-card api.
* Storage settings: work started
* Assets: use new icons approach
* Storage settings: working storage settings
* Storage: completely redesigned api, no longer sticking out FS_Api
* Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file
* Storage: cli info and format commands
* Storage-cli: file list
* Storage: a simpler and more reliable api
* FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service.
* Storage-cli: accommodate to the new cli api.
* Storage: filesystem api is separated into internal and common api.
* Cli: added the ability to print the list of free heap blocks
* Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags.
* Storage settings: added the ability to benchmark the SD card.
* Gui module file select: uses new storage api
* Apps: removed deprecated sd_card_test application
* Args lib: support for enquoted arguments
* Dialogs: a new gui app for simple non-asynchronous apps
* Dialogs: view holder for easy single view work
* File worker: use new storage api
* IButton and lfrrfid apps: save keys to any storage
* Apps: fix ibutton and lfrfid stack, remove sd_card_test.
* SD filesystem: app removed
* File worker: fixed api pointer type
* Subghz: loading assets using the new storage api
* NFC: use the new storage api
* Dialogs: the better api for the message element
* Archive: use new storage api
* Irda: changed assest path, changed app path
* FileWorker: removed unused file_buf_cnt
* Storage: copying and renaming files now works between storages
* Storage cli: read, copy, remove, rename commands
* Archive: removed commented code
* Storage cli: write command
* Applications: add SRV_STORAGE and SRV_DIALOGS
* Internal-storage: removed
* Storage: improved api
* Storage app: changed api pointer from StorageApp to Storage
* Storage: better file_id handling
* Storage: more consistent errors
* Loader: support for NULL icons
* Storage: do nothing with the lfs file or directory if it is not open
* Storage: fix typo
* Storage: minor float usage cleanup, rename some symbols.
* Storage: compact doxygen comments.
Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
|
|
|
#include <storage/storage.h>
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
|
|
|
#include <furi.h>
|
2021-03-25 17:48:58 +00:00
|
|
|
#include <math.h>
|
2021-11-03 17:22:49 +00:00
|
|
|
#include <toolbox/saved_struct.h>
|
2020-12-18 20:15:29 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "DolphinState"
|
2021-11-04 17:26:41 +00:00
|
|
|
#define DOLPHIN_STATE_PATH "/int/dolphin.state"
|
|
|
|
#define DOLPHIN_STATE_HEADER_MAGIC 0xD0
|
|
|
|
#define DOLPHIN_STATE_HEADER_VERSION 0x01
|
2021-05-11 08:29:44 +00:00
|
|
|
#define DOLPHIN_LVL_THRESHOLD 20.0f
|
2021-11-24 16:21:12 +00:00
|
|
|
#define LEVEL2_THRESHOLD 20
|
|
|
|
#define LEVEL3_THRESHOLD 100
|
2021-05-11 08:29:44 +00:00
|
|
|
|
2020-12-18 20:15:29 +00:00
|
|
|
DolphinState* dolphin_state_alloc() {
|
2021-11-03 17:22:49 +00:00
|
|
|
return furi_alloc(sizeof(DolphinState));
|
2020-12-18 20:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 18:06:25 +00:00
|
|
|
void dolphin_state_free(DolphinState* dolphin_state) {
|
2020-12-18 20:15:29 +00:00
|
|
|
free(dolphin_state);
|
|
|
|
}
|
|
|
|
|
2021-01-08 04:42:48 +00:00
|
|
|
bool dolphin_state_save(DolphinState* dolphin_state) {
|
2021-11-04 17:26:41 +00:00
|
|
|
if(!dolphin_state->dirty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = saved_struct_save(
|
|
|
|
DOLPHIN_STATE_PATH,
|
2021-11-03 17:22:49 +00:00
|
|
|
&dolphin_state->data,
|
|
|
|
sizeof(DolphinStoreData),
|
2021-11-04 17:26:41 +00:00
|
|
|
DOLPHIN_STATE_HEADER_MAGIC,
|
|
|
|
DOLPHIN_STATE_HEADER_VERSION);
|
|
|
|
|
|
|
|
if(result) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "State saved");
|
2021-11-04 17:26:41 +00:00
|
|
|
dolphin_state->dirty = false;
|
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Failed to save state");
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2020-12-18 20:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 04:42:48 +00:00
|
|
|
bool dolphin_state_load(DolphinState* dolphin_state) {
|
2021-11-03 17:22:49 +00:00
|
|
|
bool loaded = saved_struct_load(
|
2021-11-04 17:26:41 +00:00
|
|
|
DOLPHIN_STATE_PATH,
|
2021-11-03 17:22:49 +00:00
|
|
|
&dolphin_state->data,
|
|
|
|
sizeof(DolphinStoreData),
|
2021-11-04 17:26:41 +00:00
|
|
|
DOLPHIN_STATE_HEADER_MAGIC,
|
|
|
|
DOLPHIN_STATE_HEADER_VERSION);
|
|
|
|
|
2021-11-03 17:22:49 +00:00
|
|
|
if(!loaded) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_W(TAG, "Reset dolphin-state");
|
2021-11-03 17:22:49 +00:00
|
|
|
memset(dolphin_state, 0, sizeof(*dolphin_state));
|
2021-11-04 17:26:41 +00:00
|
|
|
dolphin_state->dirty = true;
|
2021-01-08 04:42:48 +00:00
|
|
|
}
|
[FL-1191][FL-1524] Filesystem rework (#568)
* FS-Api: removed datetime manipulation functions and most of the file flags
* Filesystem: common proxy api
* Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance.
* Storage: sd mount and sd file open
* Storage: sd file close
* Storage: temporary test app
* Storage: free filedata on close
* Storage: sd file read and write
* Storage: added internal storage (LittleFS)
* Storage: renamed internal commands
* Storage: seek, tell, truncate, size, sync, eof
* Storage: error descriptions
* Storage: directory management api (open, close, read, rewind)
* Storage: common management api (stat, fs_stat, remove, rename, mkdir)
* Dolphin app and Notifications app now use raw storage.
* Storage: storage statuses renamed. Implemented sd card icon.
* Storage: added raw sd-card api.
* Storage settings: work started
* Assets: use new icons approach
* Storage settings: working storage settings
* Storage: completely redesigned api, no longer sticking out FS_Api
* Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file
* Storage: cli info and format commands
* Storage-cli: file list
* Storage: a simpler and more reliable api
* FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service.
* Storage-cli: accommodate to the new cli api.
* Storage: filesystem api is separated into internal and common api.
* Cli: added the ability to print the list of free heap blocks
* Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags.
* Storage settings: added the ability to benchmark the SD card.
* Gui module file select: uses new storage api
* Apps: removed deprecated sd_card_test application
* Args lib: support for enquoted arguments
* Dialogs: a new gui app for simple non-asynchronous apps
* Dialogs: view holder for easy single view work
* File worker: use new storage api
* IButton and lfrrfid apps: save keys to any storage
* Apps: fix ibutton and lfrfid stack, remove sd_card_test.
* SD filesystem: app removed
* File worker: fixed api pointer type
* Subghz: loading assets using the new storage api
* NFC: use the new storage api
* Dialogs: the better api for the message element
* Archive: use new storage api
* Irda: changed assest path, changed app path
* FileWorker: removed unused file_buf_cnt
* Storage: copying and renaming files now works between storages
* Storage cli: read, copy, remove, rename commands
* Archive: removed commented code
* Storage cli: write command
* Applications: add SRV_STORAGE and SRV_DIALOGS
* Internal-storage: removed
* Storage: improved api
* Storage app: changed api pointer from StorageApp to Storage
* Storage: better file_id handling
* Storage: more consistent errors
* Loader: support for NULL icons
* Storage: do nothing with the lfs file or directory if it is not open
* Storage: fix typo
* Storage: minor float usage cleanup, rename some symbols.
* Storage: compact doxygen comments.
Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
return loaded;
|
2020-12-18 20:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:32:37 +00:00
|
|
|
uint64_t dolphin_state_timestamp() {
|
|
|
|
RTC_TimeTypeDef time;
|
|
|
|
RTC_DateTypeDef date;
|
|
|
|
struct tm current;
|
|
|
|
|
|
|
|
HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
|
|
|
|
HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
|
|
|
|
|
|
|
|
current.tm_year = date.Year + 100;
|
|
|
|
current.tm_mday = date.Date;
|
|
|
|
current.tm_mon = date.Month - 1;
|
|
|
|
|
|
|
|
current.tm_hour = time.Hours;
|
|
|
|
current.tm_min = time.Minutes;
|
|
|
|
current.tm_sec = time.Seconds;
|
|
|
|
|
|
|
|
return mktime(¤t);
|
|
|
|
}
|
|
|
|
|
2021-11-24 16:21:12 +00:00
|
|
|
bool dolphin_state_is_levelup(uint32_t icounter) {
|
|
|
|
return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t dolphin_get_level(uint32_t icounter) {
|
|
|
|
if(icounter <= LEVEL2_THRESHOLD) {
|
|
|
|
return 1;
|
|
|
|
} else if(icounter <= LEVEL3_THRESHOLD) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
|
|
|
|
uint32_t threshold = 0;
|
|
|
|
if(icounter <= LEVEL2_THRESHOLD) {
|
|
|
|
threshold = LEVEL2_THRESHOLD;
|
|
|
|
} else if(icounter <= LEVEL3_THRESHOLD) {
|
|
|
|
threshold = LEVEL3_THRESHOLD;
|
|
|
|
} else {
|
|
|
|
threshold = (uint32_t)-1;
|
|
|
|
}
|
|
|
|
return threshold - icounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
|
2020-12-18 20:15:29 +00:00
|
|
|
const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
|
2021-01-08 04:42:48 +00:00
|
|
|
int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
|
2021-11-24 16:21:12 +00:00
|
|
|
bool level_up = false;
|
|
|
|
bool mood_changed = false;
|
|
|
|
|
|
|
|
if(icounter <= 0) {
|
|
|
|
icounter = 0;
|
|
|
|
if(dolphin_state->data.icounter == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 20:15:29 +00:00
|
|
|
|
2021-11-24 16:21:12 +00:00
|
|
|
uint8_t xp_to_levelup = dolphin_state_xp_to_levelup(dolphin_state->data.icounter);
|
|
|
|
if(xp_to_levelup) {
|
|
|
|
level_up = true;
|
|
|
|
dolphin_state->data.icounter += MIN(xp_to_levelup, deed_weight->icounter);
|
2020-12-18 20:15:29 +00:00
|
|
|
}
|
2021-09-30 19:50:58 +00:00
|
|
|
|
2021-11-24 16:21:12 +00:00
|
|
|
uint32_t new_butthurt =
|
|
|
|
CLAMP(((int32_t)dolphin_state->data.butthurt) + deed_weight->butthurt, 14, 0);
|
|
|
|
|
|
|
|
if(!!dolphin_state->data.butthurt != !!new_butthurt) {
|
|
|
|
mood_changed = true;
|
|
|
|
}
|
|
|
|
dolphin_state->data.butthurt = new_butthurt;
|
|
|
|
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
2021-09-30 19:50:58 +00:00
|
|
|
dolphin_state->dirty = true;
|
2021-11-24 16:21:12 +00:00
|
|
|
|
|
|
|
return level_up || mood_changed;
|
2020-12-18 20:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:32:37 +00:00
|
|
|
void dolphin_state_butthurted(DolphinState* dolphin_state) {
|
|
|
|
dolphin_state->data.butthurt++;
|
|
|
|
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
|
|
|
dolphin_state->dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-11-24 16:21:12 +00:00
|
|
|
void dolphin_state_increase_level(DolphinState* dolphin_state) {
|
|
|
|
++dolphin_state->data.icounter;
|
|
|
|
dolphin_state->dirty = true;
|
2021-11-03 17:22:49 +00:00
|
|
|
}
|