forked from DarkFlippers/unleashed-firmware
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubghz_last_settings.c
More file actions
105 lines (85 loc) · 3.36 KB
/
subghz_last_settings.c
File metadata and controls
105 lines (85 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "subghz_last_settings.h"
#include <furi.h>
#include <furi_hal.h>
#include <lib/flipper_format/flipper_format.h>
#define TAG "SubGhzLastSettings"
#define SUBGHZ_LAST_SETTING_FILE_TYPE "Flipper SubGhz Last Setting File"
#define SUBGHZ_LAST_SETTING_FILE_VERSION 1
#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH("subghz/assets/.subghz.settings")
// 1 = "AM650"
// "AM270", "AM650", "FM238", "FM476",
#define SUBGHZ_LAST_SETTING_DEFAULT_PRESET 1
#define SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY 433920000
SubGhzLastSettings* subghz_last_settings_alloc(void) {
SubGhzLastSettings* instance = malloc(sizeof(SubGhzLastSettings));
return instance;
}
void subghz_last_settings_free(SubGhzLastSettings* instance) {
furi_assert(instance);
free(instance);
}
void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count) {
furi_assert(instance);
#if FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_load");
#endif
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
uint32_t temp_frequency = 0;
int32_t temp_preset = 0;
if(FSE_OK == storage_sd_status(storage) && SUBGHZ_LAST_SETTINGS_PATH &&
flipper_format_file_open_existing(fff_data_file, SUBGHZ_LAST_SETTINGS_PATH)) {
flipper_format_read_int32(fff_data_file, "Preset", (int32_t*)&temp_preset, 1);
flipper_format_read_uint32(fff_data_file, "Frequency", (uint32_t*)&temp_frequency, 1);
} else {
FURI_LOG_E(TAG, "Error open file %s", SUBGHZ_LAST_SETTINGS_PATH);
}
if(temp_frequency == 0 || !furi_hal_subghz_is_tx_allowed(temp_frequency)) {
FURI_LOG_W(TAG, "Last used frequency not found or can't be used!");
instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY;
instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
} else {
instance->frequency = temp_frequency;
if(temp_preset > (int32_t)preset_count - 1 || temp_preset < 0) {
FURI_LOG_W(TAG, "Last used preset no found");
instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
} else {
instance->preset = temp_preset;
}
}
flipper_format_free(fff_data_file);
furi_record_close(RECORD_STORAGE);
}
bool subghz_last_settings_save(SubGhzLastSettings* instance) {
furi_assert(instance);
#if FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_save");
#endif
bool saved = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
do {
if(FSE_OK != storage_sd_status(storage)) {
break;
}
// Open file
if(!flipper_format_file_open_always(file, SUBGHZ_LAST_SETTINGS_PATH)) break;
// Write header
if(!flipper_format_write_header_cstr(
file, SUBGHZ_LAST_SETTING_FILE_TYPE, SUBGHZ_LAST_SETTING_FILE_VERSION))
break;
if(!flipper_format_insert_or_update_int32(file, "Preset", &instance->preset, 1)) {
break;
}
if(!flipper_format_insert_or_update_uint32(file, "Frequency", &instance->frequency, 1)) {
break;
}
saved = true;
} while(0);
if(!saved) {
FURI_LOG_E(TAG, "Error save file %s", SUBGHZ_LAST_SETTINGS_PATH);
}
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
return saved;
}