upd wplugins

This commit is contained in:
r3df0xx 2022-05-18 19:17:59 +03:00
parent 4760c1c5ce
commit fec7523311
27 changed files with 1594 additions and 29 deletions

View File

@ -47,6 +47,7 @@ extern int32_t vibro_test_app(void* p);
extern int32_t bt_hid_app(void* p);
extern int32_t battery_test_app(void* p);
extern int32_t text_box_test_app(void* p);
extern int32_t file_browser_app(void* p);
// Plugins
extern int32_t music_player_app(void* p);
@ -530,6 +531,14 @@ const FlipperApplication FLIPPER_DEBUG_APPS[] = {
.flags = FlipperApplicationFlagDefault},
#endif
#ifdef APP_FILE_BROWSER_TEST
{.app = file_browser_app,
.name = "File Browser test",
.stack_size = 2048,
.icon = &A_BadUsb_14,
.flags = FlipperApplicationFlagDefault},
#endif
#ifdef APP_BATTERY_TEST
{.app = battery_test_app,
.name = "Battery Test",

View File

@ -70,6 +70,7 @@ APP_USB_MOUSE = 1
APP_BAD_USB = 1
APP_U2F = 1
APP_UART_ECHO = 1
APP_FILE_BROWSER_TEST = 1
endif
@ -228,6 +229,11 @@ CFLAGS += -DAPP_KEYPAD_TEST
SRV_GUI = 1
endif
APP_FILE_BROWSER_TEST ?= 0
ifeq ($(APP_FILE_BROWSER_TEST), 1)
CFLAGS += -DAPP_FILE_BROWSER_TEST
SRV_GUI = 1
endif
APP_ACCESSOR ?= 0
ifeq ($(APP_ACCESSOR), 1)

View File

@ -91,11 +91,19 @@ static void bt_battery_level_changed_callback(const void* _event, void* context)
furi_assert(context);
Bt* bt = context;
BtMessage message = {};
const PowerEvent* event = _event;
if(event->type == PowerEventTypeBatteryLevelChanged) {
BtMessage message = {
.type = BtMessageTypeUpdateBatteryLevel,
.data.battery_level = event->data.battery_level};
message.type = BtMessageTypeUpdateBatteryLevel;
message.data.battery_level = event->data.battery_level;
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
} else if(event->type == PowerEventTypeStartCharging || event->type == PowerEventTypeFullyCharged) {
message.type = BtMessageTypeUpdatePowerState;
message.data.battery_is_charging = true;
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
} else if(event->type == PowerEventTypeStopCharging) {
message.type = BtMessageTypeUpdatePowerState;
message.data.battery_is_charging = false;
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
}
}
@ -368,6 +376,8 @@ int32_t bt_srv() {
} else if(message.type == BtMessageTypeUpdateBatteryLevel) {
// Update battery level
furi_hal_bt_update_battery_level(message.data.battery_level);
} else if(message.type == BtMessageTypeUpdatePowerState) {
furi_hal_bt_update_power_state(message.data.battery_is_charging);
} else if(message.type == BtMessageTypePinCodeShow) {
// Display PIN code
bt_pin_code_show(bt, message.data.pin_code);

View File

@ -21,6 +21,7 @@
typedef enum {
BtMessageTypeUpdateStatus,
BtMessageTypeUpdateBatteryLevel,
BtMessageTypeUpdatePowerState,
BtMessageTypePinCodeShow,
BtMessageTypeKeysStorageUpdated,
BtMessageTypeSetProfile,
@ -31,6 +32,7 @@ typedef union {
uint32_t pin_code;
uint8_t battery_level;
BtProfile profile;
bool battery_is_charging;
} BtMessageData;
typedef struct {

View File

@ -0,0 +1,99 @@
#include "assets_icons.h"
#include "file_browser_app_i.h"
#include "gui/modules/file_browser.h"
#include "m-string.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <lib/toolbox/path.h>
static bool file_browser_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
FileBrowserApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}
static bool file_browser_app_back_event_callback(void* context) {
furi_assert(context);
FileBrowserApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static void file_browser_app_tick_event_callback(void* context) {
furi_assert(context);
FileBrowserApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
FileBrowserApp* file_browser_app_alloc(char* arg) {
UNUSED(arg);
FileBrowserApp* app = malloc(sizeof(FileBrowserApp));
app->gui = furi_record_open("gui");
app->dialogs = furi_record_open("dialogs");
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
app->scene_manager = scene_manager_alloc(&file_browser_scene_handlers, app);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, file_browser_app_tick_event_callback, 500);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, file_browser_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, file_browser_app_back_event_callback);
app->widget = widget_alloc();
string_init(app->file_path);
app->file_browser = file_browser_alloc(&(app->file_path));
file_browser_configure(app->file_browser, "*", true, &I_badusb_10px, true);
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewStart, widget_get_view(app->widget));
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewResult, widget_get_view(app->widget));
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewBrowser, file_browser_get_view(app->file_browser));
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
scene_manager_next_scene(app->scene_manager, FileBrowserSceneStart);
return app;
}
void file_browser_app_free(FileBrowserApp* app) {
furi_assert(app);
// Views
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewStart);
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewResult);
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewBrowser);
widget_free(app->widget);
file_browser_free(app->file_browser);
// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);
// Close records
furi_record_close("gui");
furi_record_close("notification");
furi_record_close("dialogs");
string_clear(app->file_path);
free(app);
}
int32_t file_browser_app(void* p) {
FileBrowserApp* file_browser_app = file_browser_app_alloc((char*)p);
view_dispatcher_run(file_browser_app->view_dispatcher);
file_browser_app_free(file_browser_app);
return 0;
}

View File

@ -0,0 +1,32 @@
#pragma once
#include "scenes/file_browser_scene.h"
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <gui/modules/file_browser.h>
#include <dialogs/dialogs.h>
#include <notification/notification_messages.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
typedef struct FileBrowserApp FileBrowserApp;
struct FileBrowserApp {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
DialogsApp* dialogs;
Widget* widget;
FileBrowser* file_browser;
string_t file_path;
};
typedef enum {
FileBrowserAppViewStart,
FileBrowserAppViewBrowser,
FileBrowserAppViewResult,
} FileBrowserAppView;

View File

@ -0,0 +1,30 @@
#include "file_browser_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const file_browser_scene_on_enter_handlers[])(void*) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const file_browser_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const file_browser_scene_on_exit_handlers[])(void* context) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers file_browser_scene_handlers = {
.on_enter_handlers = file_browser_scene_on_enter_handlers,
.on_event_handlers = file_browser_scene_on_event_handlers,
.on_exit_handlers = file_browser_scene_on_exit_handlers,
.scene_num = FileBrowserSceneNum,
};

View File

@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) FileBrowserScene##id,
typedef enum {
#include "file_browser_scene_config.h"
FileBrowserSceneNum,
} FileBrowserScene;
#undef ADD_SCENE
extern const SceneManagerHandlers file_browser_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "file_browser_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "file_browser_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "file_browser_scene_config.h"
#undef ADD_SCENE

View File

@ -0,0 +1,45 @@
#include "../file_browser_app_i.h"
#include "furi/check.h"
#include "furi/log.h"
#include "furi_hal.h"
#include "m-string.h"
#define DEFAULT_PATH "/"
#define EXTENSION "*"
bool file_browser_scene_browser_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
FileBrowserApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_next_scene(app->scene_manager, FileBrowserSceneResult);
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}
static void file_browser_callback(void* context, bool state) {
FileBrowserApp* app = context;
furi_assert(app);
view_dispatcher_send_custom_event(app->view_dispatcher, SceneManagerEventTypeCustom);
UNUSED(state);
}
void file_browser_scene_browser_on_enter(void* context) {
FileBrowserApp* app = context;
file_browser_set_callback(app->file_browser, file_browser_callback, app);
file_browser_start(app->file_browser, app->file_path);
view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewBrowser);
}
void file_browser_scene_browser_on_exit(void* context) {
FileBrowserApp* app = context;
file_browser_stop(app->file_browser);
}

View File

@ -0,0 +1,3 @@
ADD_SCENE(file_browser, start, Start)
ADD_SCENE(file_browser, browser, Browser)
ADD_SCENE(file_browser, result, Result)

View File

@ -0,0 +1,36 @@
#include "../file_browser_app_i.h"
#include "furi_hal.h"
#include "m-string.h"
void file_browser_scene_result_ok_callback(InputType type, void* context) {
furi_assert(context);
FileBrowserApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, type);
}
bool file_browser_scene_result_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
//FileBrowserApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}
void file_browser_scene_result_on_enter(void* context) {
FileBrowserApp* app = context;
widget_add_string_multiline_element(
app->widget, 64, 10, AlignCenter, AlignTop, FontSecondary, string_get_cstr(app->file_path));
view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewResult);
}
void file_browser_scene_result_on_exit(void* context) {
UNUSED(context);
FileBrowserApp* app = context;
widget_reset(app->widget);
}

View File

@ -0,0 +1,44 @@
#include "../file_browser_app_i.h"
#include "furi_hal.h"
#include "gui/modules/widget_elements/widget_element_i.h"
static void
file_browser_scene_start_ok_callback(GuiButtonType result, InputType type, void* context) {
UNUSED(result);
furi_assert(context);
FileBrowserApp* app = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(app->view_dispatcher, type);
}
}
bool file_browser_scene_start_on_event(void* context, SceneManagerEvent event) {
FileBrowserApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
string_set_str(app->file_path, "/any/badusb/demo_windows.txt");
scene_manager_next_scene(app->scene_manager, FileBrowserSceneBrowser);
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}
void file_browser_scene_start_on_enter(void* context) {
FileBrowserApp* app = context;
widget_add_string_multiline_element(
app->widget, 64, 20, AlignCenter, AlignTop, FontSecondary, "Press OK to start");
widget_add_button_element(
app->widget, GuiButtonTypeCenter, "Ok", file_browser_scene_start_ok_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewStart);
}
void file_browser_scene_start_on_exit(void* context) {
UNUSED(context);
FileBrowserApp* app = context;
widget_reset(app->widget);
}

View File

@ -0,0 +1,532 @@
#include "file_browser.h"
#include "assets_icons.h"
#include "cmsis_os2.h"
#include "file_browser_worker.h"
#include "furi/check.h"
#include "furi/common_defines.h"
#include "furi/log.h"
#include "furi_hal_resources.h"
#include "m-string.h"
#include <m-array.h>
#include <gui/elements.h>
#include <furi.h>
#define LIST_ITEMS 5u
#define MAX_LEN_PX 110
#define FRAME_HEIGHT 12
#define Y_OFFSET 3
#define ITEM_LIST_LEN_MAX 100
typedef enum {
BrowserItemTypeLoading,
BrowserItemTypeBack,
BrowserItemTypeFolder,
BrowserItemTypeFile,
} BrowserItemType;
typedef struct {
string_t path;
BrowserItemType type;
} BrowserItem_t;
static void BrowserItem_t_init(BrowserItem_t* obj) {
obj->type = BrowserItemTypeLoading;
string_init(obj->path);
}
static void BrowserItem_t_init_set(BrowserItem_t* obj, const BrowserItem_t* src) {
obj->type = src->type;
string_init_set(obj->path, src->path);
}
static void BrowserItem_t_set(BrowserItem_t* obj, const BrowserItem_t* src) {
obj->type = src->type;
string_set(obj->path, src->path);
}
static void BrowserItem_t_clear(BrowserItem_t* obj) {
string_clear(obj->path);
}
ARRAY_DEF(
items_array,
BrowserItem_t,
(INIT(API_2(BrowserItem_t_init)),
SET(API_6(BrowserItem_t_set)),
INIT_SET(API_6(BrowserItem_t_init_set)),
CLEAR(API_2(BrowserItem_t_clear))))
struct FileBrowser {
View* view;
BrowserWorker* worker;
char* ext_filter;
bool skip_assets;
FileBrowserCallback callback;
void* context;
string_t* result_path;
};
typedef struct {
items_array_t items;
bool is_root;
bool folder_loading;
bool list_loading;
uint32_t item_cnt;
int32_t item_idx;
int32_t array_offset;
int32_t list_offset;
const Icon* file_icon;
bool hide_ext;
} FileBrowserModel;
static const Icon* BrowserItemIcons[] = {
[BrowserItemTypeLoading] = &I_loading_10px,
[BrowserItemTypeBack] = &I_back_10px,
[BrowserItemTypeFolder] = &I_dir_10px,
[BrowserItemTypeFile] = &I_unknown_10px,
};
static void file_browser_view_draw_callback(Canvas* canvas, void* _model);
static bool file_browser_view_input_callback(InputEvent* event, void* context);
static void
browser_folder_open_cb(void* context, uint32_t item_cnt, int32_t file_idx, bool is_root);
static void browser_list_load_cb(void* context, uint32_t list_load_offset);
static void browser_list_item_cb(void* context, string_t item_path, bool is_folder, bool is_last);
static void browser_long_load_cb(void* context);
FileBrowser* file_browser_alloc(string_t* result_path) {
furi_assert(result_path);
FileBrowser* browser = malloc(sizeof(FileBrowser));
browser->view = view_alloc();
view_allocate_model(browser->view, ViewModelTypeLocking, sizeof(FileBrowserModel));
view_set_context(browser->view, browser);
view_set_draw_callback(browser->view, file_browser_view_draw_callback);
view_set_input_callback(browser->view, file_browser_view_input_callback);
browser->result_path = result_path;
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_init(model->items);
return false;
});
return browser;
}
void file_browser_free(FileBrowser* browser) {
furi_assert(browser);
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_clear(model->items);
return false;
});
view_free(browser->view);
free(browser);
}
View* file_browser_get_view(FileBrowser* browser) {
furi_assert(browser);
return browser->view;
}
void file_browser_configure(
FileBrowser* browser,
char* extension,
bool skip_assets,
const Icon* file_icon,
bool hide_ext) {
furi_assert(browser);
browser->ext_filter = extension;
browser->skip_assets = skip_assets;
with_view_model(
browser->view, (FileBrowserModel * model) {
model->file_icon = file_icon;
model->hide_ext = hide_ext;
return false;
});
}
void file_browser_start(FileBrowser* browser, string_t path) {
furi_assert(browser);
browser->worker = file_browser_worker_alloc(path, browser->ext_filter, browser->skip_assets);
file_browser_worker_set_callback_context(browser->worker, browser);
file_browser_worker_set_folder_callback(browser->worker, browser_folder_open_cb);
file_browser_worker_set_list_callback(browser->worker, browser_list_load_cb);
file_browser_worker_set_item_callback(browser->worker, browser_list_item_cb);
file_browser_worker_set_long_load_callback(browser->worker, browser_long_load_cb);
}
void file_browser_stop(FileBrowser* browser) {
furi_assert(browser);
file_browser_worker_free(browser->worker);
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_reset(model->items);
model->item_cnt = 0;
model->item_idx = 0;
model->array_offset = 0;
model->list_offset = 0;
return false;
});
}
void file_browser_set_callback(FileBrowser* browser, FileBrowserCallback callback, void* context) {
browser->context = context;
browser->callback = callback;
}
static bool browser_is_item_in_array(FileBrowserModel* model, uint32_t idx) {
size_t array_size = items_array_size(model->items);
if((idx >= (uint32_t)model->array_offset + array_size) ||
(idx < (uint32_t)model->array_offset)) {
return false;
}
return true;
}
static bool browser_is_list_load_required(FileBrowserModel* model) {
size_t array_size = items_array_size(model->items);
uint32_t item_cnt = (model->is_root) ? model->item_cnt : model->item_cnt - 1;
if((model->list_loading) || (array_size >= item_cnt)) {
return false;
}
if((model->array_offset > 0) &&
(model->item_idx < (model->array_offset + ITEM_LIST_LEN_MAX / 4))) {
return true;
}
if(((model->array_offset + array_size) < item_cnt) &&
(model->item_idx > (int32_t)(model->array_offset + array_size - ITEM_LIST_LEN_MAX / 4))) {
return true;
}
return false;
}
static void browser_update_offset(FileBrowser* browser) {
furi_assert(browser);
with_view_model(
browser->view, (FileBrowserModel * model) {
uint16_t bounds = model->item_cnt > (LIST_ITEMS - 1) ? 2 : model->item_cnt;
if((model->item_cnt > (LIST_ITEMS - 1)) &&
(model->item_idx >= ((int32_t)model->item_cnt - 1))) {
model->list_offset = model->item_idx - (LIST_ITEMS - 1);
} else if(model->list_offset < model->item_idx - bounds) {
model->list_offset = CLAMP(
model->item_idx - (int32_t)(LIST_ITEMS - 2),
(int32_t)model->item_cnt - bounds,
0);
} else if(model->list_offset > model->item_idx - bounds) {
model->list_offset =
CLAMP(model->item_idx - 1, (int32_t)model->item_cnt - bounds, 0);
}
return false;
});
}
static void
browser_folder_open_cb(void* context, uint32_t item_cnt, int32_t file_idx, bool is_root) {
furi_assert(context);
FileBrowser* browser = (FileBrowser*)context;
int32_t load_offset = 0;
with_view_model(
browser->view, (FileBrowserModel * model) {
if(is_root) {
model->item_cnt = item_cnt;
model->item_idx = (file_idx > 0) ? file_idx : 0;
load_offset =
CLAMP(model->item_idx - ITEM_LIST_LEN_MAX / 2, (int32_t)model->item_cnt, 0);
} else {
model->item_cnt = item_cnt + 1;
model->item_idx = file_idx + 1;
load_offset = CLAMP(
model->item_idx - ITEM_LIST_LEN_MAX / 2 - 1, (int32_t)model->item_cnt - 1, 0);
}
model->array_offset = 0;
model->list_offset = 0;
model->is_root = is_root;
model->list_loading = true;
model->folder_loading = false;
return true;
});
browser_update_offset(browser);
file_browser_worker_load(browser->worker, load_offset, ITEM_LIST_LEN_MAX);
}
static void browser_list_load_cb(void* context, uint32_t list_load_offset) {
furi_assert(context);
FileBrowser* browser = (FileBrowser*)context;
BrowserItem_t back_item;
BrowserItem_t_init(&back_item);
back_item.type = BrowserItemTypeBack;
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_reset(model->items);
model->array_offset = list_load_offset;
if(!model->is_root) {
if(list_load_offset == 0) {
items_array_push_back(model->items, back_item);
} else {
model->array_offset += 1;
}
}
return false;
});
BrowserItem_t_clear(&back_item);
}
static void browser_list_item_cb(void* context, string_t item_path, bool is_folder, bool is_last) {
furi_assert(context);
FileBrowser* browser = (FileBrowser*)context;
BrowserItem_t item;
if(!is_last) {
BrowserItem_t_init(&item);
string_set(item.path, item_path);
item.type = (is_folder) ? BrowserItemTypeFolder : BrowserItemTypeFile;
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_push_back(model->items, item);
return false;
});
BrowserItem_t_clear(&item);
} else {
with_view_model(
browser->view, (FileBrowserModel * model) {
model->list_loading = false;
return true;
});
}
}
static void browser_long_load_cb(void* context) {
furi_assert(context);
FileBrowser* browser = (FileBrowser*)context;
with_view_model(
browser->view, (FileBrowserModel * model) {
model->folder_loading = true;
return true;
});
}
static void browser_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_box(
canvas, 0, Y_OFFSET + idx * FRAME_HEIGHT, (scrollbar ? 122 : 127), FRAME_HEIGHT);
canvas_set_color(canvas, ColorWhite);
canvas_draw_dot(canvas, 0, Y_OFFSET + idx * FRAME_HEIGHT);
canvas_draw_dot(canvas, 1, Y_OFFSET + idx * FRAME_HEIGHT);
canvas_draw_dot(canvas, 0, (Y_OFFSET + idx * FRAME_HEIGHT) + 1);
canvas_draw_dot(canvas, 0, (Y_OFFSET + idx * FRAME_HEIGHT) + (FRAME_HEIGHT - 1));
canvas_draw_dot(canvas, scrollbar ? 121 : 126, Y_OFFSET + idx * FRAME_HEIGHT);
canvas_draw_dot(
canvas, scrollbar ? 121 : 126, (Y_OFFSET + idx * FRAME_HEIGHT) + (FRAME_HEIGHT - 1));
}
static void browser_draw_loading(Canvas* canvas, FileBrowserModel* model) {
uint8_t width = 49;
uint8_t height = 47;
uint8_t x = 128 / 2 - width / 2;
uint8_t y = 64 / 2 - height / 2;
UNUSED(model);
elements_bold_rounded_frame(canvas, x, y, width, height);
canvas_set_font(canvas, FontSecondary);
elements_multiline_text(canvas, x + 7, y + 13, "Loading...");
canvas_draw_icon(canvas, x + 13, y + 19, &A_Loading_24);
}
static void browser_draw_list(Canvas* canvas, FileBrowserModel* model) {
uint32_t array_size = items_array_size(model->items);
bool show_scrollbar = model->item_cnt > LIST_ITEMS;
string_t filename;
string_init(filename);
for(uint32_t i = 0; i < MIN(model->item_cnt, LIST_ITEMS); i++) {
int32_t idx = CLAMP((uint32_t)(i + model->list_offset), model->item_cnt, 0u);
BrowserItemType item_type = BrowserItemTypeLoading;
if(browser_is_item_in_array(model, idx)) {
BrowserItem_t* item = items_array_get(
model->items, CLAMP(idx - model->array_offset, (int32_t)(array_size - 1), 0));
item_type = item->type;
file_browser_worker_get_filename(
item->path, filename, (model->hide_ext) && (item_type == BrowserItemTypeFile));
} else {
string_set_str(filename, "---");
}
if(item_type == BrowserItemTypeBack) {
string_set_str(filename, ". .");
}
elements_string_fit_width(
canvas, filename, (show_scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX));
if(model->item_idx == idx) {
browser_draw_frame(canvas, i, show_scrollbar);
} else {
canvas_set_color(canvas, ColorBlack);
}
if((item_type == BrowserItemTypeFile) && (model->file_icon)) {
canvas_draw_icon(canvas, 2, Y_OFFSET + 1 + i * FRAME_HEIGHT, model->file_icon);
} else if(BrowserItemIcons[item_type] != NULL) {
canvas_draw_icon(
canvas, 2, Y_OFFSET + 1 + i * FRAME_HEIGHT, BrowserItemIcons[item_type]);
}
canvas_draw_str(canvas, 15, Y_OFFSET + 9 + i * FRAME_HEIGHT, string_get_cstr(filename));
}
if(show_scrollbar) {
elements_scrollbar_pos(
canvas,
126,
Y_OFFSET,
canvas_height(canvas) - Y_OFFSET,
model->item_idx,
model->item_cnt);
}
string_clear(filename);
}
static void file_browser_view_draw_callback(Canvas* canvas, void* _model) {
FileBrowserModel* model = _model;
if(model->folder_loading) {
browser_draw_loading(canvas, model);
} else {
browser_draw_list(canvas, model);
}
}
static bool file_browser_view_input_callback(InputEvent* event, void* context) {
FileBrowser* browser = context;
furi_assert(browser);
bool consumed = false;
bool is_loading = false;
with_view_model(
browser->view, (FileBrowserModel * model) {
is_loading = model->folder_loading;
return false;
});
if(is_loading) {
return false;
} else if(event->key == InputKeyUp || event->key == InputKeyDown) {
if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
with_view_model(
browser->view, (FileBrowserModel * model) {
if(event->key == InputKeyUp) {
model->item_idx =
((model->item_idx - 1) + model->item_cnt) % model->item_cnt;
if(browser_is_list_load_required(model)) {
model->list_loading = true;
int32_t load_offset = CLAMP(
model->item_idx - ITEM_LIST_LEN_MAX / 4 * 3,
(int32_t)model->item_cnt - ITEM_LIST_LEN_MAX,
0);
file_browser_worker_load(
browser->worker, load_offset, ITEM_LIST_LEN_MAX);
}
} else if(event->key == InputKeyDown) {
model->item_idx = (model->item_idx + 1) % model->item_cnt;
if(browser_is_list_load_required(model)) {
model->list_loading = true;
int32_t load_offset = CLAMP(
model->item_idx - ITEM_LIST_LEN_MAX / 4 * 1,
(int32_t)model->item_cnt - ITEM_LIST_LEN_MAX,
0);
file_browser_worker_load(
browser->worker, load_offset, ITEM_LIST_LEN_MAX);
}
}
return true;
});
browser_update_offset(browser);
consumed = true;
}
} else if(event->key == InputKeyOk) {
if(event->type == InputTypeShort) {
BrowserItem_t* selected_item = NULL;
int32_t select_index = 0;
with_view_model(
browser->view, (FileBrowserModel * model) {
if(browser_is_item_in_array(model, model->item_idx)) {
selected_item =
items_array_get(model->items, model->item_idx - model->array_offset);
select_index = model->item_idx;
if((!model->is_root) && (select_index > 0)) {
select_index -= 1;
}
}
return false;
});
if(selected_item) {
if(selected_item->type == BrowserItemTypeBack) {
file_browser_worker_folder_exit(browser->worker);
} else if(selected_item->type == BrowserItemTypeFolder) {
file_browser_worker_folder_enter(
browser->worker, selected_item->path, select_index);
} else if(selected_item->type == BrowserItemTypeFile) {
string_set(*(browser->result_path), selected_item->path);
if(browser->callback) {
browser->callback(browser->context, true);
}
}
}
consumed = true;
}
} else if(event->key == InputKeyLeft) {
if(event->type == InputTypeShort) {
bool is_root = false;
with_view_model(
browser->view, (FileBrowserModel * model) {
is_root = model->is_root;
return false;
});
if(!is_root) {
file_browser_worker_folder_exit(browser->worker);
}
consumed = true;
}
}
return consumed;
}

View File

@ -0,0 +1,39 @@
/**
* @file file_browser.h
* GUI: FileBrowser view module API
*/
#pragma once
#include "m-string.h"
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FileBrowser FileBrowser;
typedef void (*FileBrowserCallback)(void* context, bool state);
FileBrowser* file_browser_alloc(string_t* result_path);
void file_browser_free(FileBrowser* browser);
View* file_browser_get_view(FileBrowser* browser);
void file_browser_configure(
FileBrowser* browser,
char* extension,
bool skip_assets,
const Icon* file_icon,
bool hide_ext);
void file_browser_start(FileBrowser* browser, string_t path);
void file_browser_stop(FileBrowser* browser);
void file_browser_set_callback(FileBrowser* browser, FileBrowserCallback callback, void* context);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,420 @@
#include "file_browser_worker.h"
#include "furi/check.h"
#include "furi/common_defines.h"
#include "m-string.h"
#include "storage/filesystem_api_defines.h"
#include <m-array.h>
#include <stdbool.h>
#include <storage/storage.h>
#include <furi.h>
#include <stddef.h>
#define TAG "BrowserWorker"
#define ASSETS_DIR "assets"
#define BROWSER_ROOT "/any"
#define FILE_NAME_LEN_MAX 256
#define LONG_LOAD_THRESHOLD 100
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtLoad = (1 << 1),
WorkerEvtFolderEnter = (1 << 2),
WorkerEvtFolderExit = (1 << 3),
} WorkerEvtFlags;
#define WORKER_FLAGS_ALL \
(WorkerEvtStop | WorkerEvtLoad | WorkerEvtFolderEnter | WorkerEvtFolderExit)
ARRAY_DEF(idx_last_array, int32_t)
struct BrowserWorker {
FuriThread* thread;
string_t filter_extension;
string_t path_next;
int32_t item_sel_idx;
uint32_t load_offset;
uint32_t load_count;
bool skip_assets;
idx_last_array_t idx_last;
void* cb_ctx;
BrowserWorkerFolderOpenCallback folder_cb;
BrowserWorkerListLoadCallback list_load_cb;
BrowserWorkerListItemCallback list_item_cb;
BrowserWorkerLongLoadCallback long_load_cb;
};
static bool browser_path_is_file(string_t path) {
bool state = false;
FileInfo file_info;
Storage* storage = furi_record_open("storage");
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
if((file_info.flags & FSF_DIRECTORY) == 0) {
state = true;
}
}
furi_record_close("storage");
return state;
}
static bool browser_path_trim(string_t path) {
bool is_root = false;
size_t filename_start = string_search_rchar(path, '/');
string_left(path, filename_start);
if((string_empty_p(path)) || (filename_start == STRING_FAILURE)) {
string_set_str(path, BROWSER_ROOT);
is_root = true;
}
return is_root;
}
static bool browser_filter_by_name(BrowserWorker* browser, string_t name, bool is_folder) {
if(is_folder) {
// Skip assets folders (if enabled)
if(browser->skip_assets) {
return ((string_cmp_str(name, ASSETS_DIR) == 0) ? (false) : (true));
} else {
return true;
}
} else {
// Filter files by extension
if((string_empty_p(browser->filter_extension)) ||
(string_cmp_str(browser->filter_extension, "*") == 0)) {
return true;
}
if(string_end_with_string_p(name, browser->filter_extension)) {
return true;
}
}
return false;
}
static bool browser_folder_check_and_switch(string_t path) {
FileInfo file_info;
Storage* storage = furi_record_open("storage");
bool is_root = false;
while(1) {
// Check if folder is existing and navigate back if not
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
if(file_info.flags & FSF_DIRECTORY) {
break;
}
}
if(is_root) {
break;
}
is_root = browser_path_trim(path);
}
furi_record_close("storage");
return is_root;
}
static bool browser_folder_init(
BrowserWorker* browser,
string_t path,
string_t filename,
uint32_t* item_cnt,
int32_t* file_idx) {
bool state = false;
FileInfo file_info;
uint32_t total_files_cnt = 0;
Storage* storage = furi_record_open("storage");
File* directory = storage_file_alloc(storage);
char name_temp[FILE_NAME_LEN_MAX];
string_t name_str;
string_init(name_str);
*item_cnt = 0;
*file_idx = -1;
if(storage_dir_open(directory, string_get_cstr(path))) {
state = true;
while(1) {
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
break;
}
if((storage_file_get_error(directory) == FSE_OK) && (name_temp[0] != '\0')) {
total_files_cnt++;
string_set_str(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
if(!string_empty_p(filename)) {
if(string_cmp(name_str, filename) == 0) {
*file_idx = *item_cnt;
}
}
(*item_cnt)++;
}
if(total_files_cnt == LONG_LOAD_THRESHOLD) {
if(browser->long_load_cb) {
browser->long_load_cb(browser->cb_ctx);
}
}
}
}
}
string_clear(name_str);
storage_dir_close(directory);
storage_file_free(directory);
furi_record_close("storage");
return state;
}
static bool
browser_folder_load(BrowserWorker* browser, string_t path, uint32_t offset, uint32_t count) {
FileInfo file_info;
Storage* storage = furi_record_open("storage");
File* directory = storage_file_alloc(storage);
char name_temp[FILE_NAME_LEN_MAX];
string_t name_str;
string_init(name_str);
uint32_t items_cnt = 0;
do {
if(!storage_dir_open(directory, string_get_cstr(path))) {
break;
}
items_cnt = 0;
while(items_cnt < offset) {
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
break;
}
if(storage_file_get_error(directory) == FSE_OK) {
string_set_str(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
items_cnt++;
}
} else {
break;
}
}
if(items_cnt != offset) {
break;
}
if(browser->list_load_cb) {
browser->list_load_cb(browser->cb_ctx, offset);
}
items_cnt = 0;
while(items_cnt < count) {
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
break;
}
if(storage_file_get_error(directory) == FSE_OK) {
string_set_str(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
string_printf(name_str, "%s/%s", string_get_cstr(path), name_temp);
if(browser->list_item_cb) {
browser->list_item_cb(
browser->cb_ctx, name_str, (file_info.flags & FSF_DIRECTORY), false);
}
items_cnt++;
}
} else {
break;
}
}
if(browser->list_item_cb) {
browser->list_item_cb(browser->cb_ctx, NULL, false, true);
}
} while(0);
string_clear(name_str);
storage_dir_close(directory);
storage_file_free(directory);
furi_record_close("storage");
return (items_cnt == count);
}
static int32_t browser_worker(void* context) {
BrowserWorker* browser = (BrowserWorker*)context;
furi_assert(browser);
FURI_LOG_D(TAG, "Start");
uint32_t items_cnt = 0;
string_t path;
string_init_set_str(path, BROWSER_ROOT);
browser->item_sel_idx = -1;
// If start path is a path to the file - try finding index of this file in a folder
string_t filename;
string_init(filename);
if(browser_path_is_file(browser->path_next)) {
file_browser_worker_get_filename(browser->path_next, filename, false);
}
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtFolderEnter);
while(1) {
uint32_t flags = osThreadFlagsWait(WORKER_FLAGS_ALL, osFlagsWaitAny, osWaitForever);
furi_assert((flags & osFlagsError) == 0);
if(flags & WorkerEvtFolderEnter) {
string_set(path, browser->path_next);
bool is_root = browser_folder_check_and_switch(path);
// Push previous selected item index to history array
idx_last_array_push_back(browser->idx_last, browser->item_sel_idx);
int32_t file_idx = 0;
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
FURI_LOG_D(
TAG,
"Enter folder: %s items: %u idx: %d",
string_get_cstr(path),
items_cnt,
file_idx);
if(browser->folder_cb) {
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
}
string_reset(filename);
}
if(flags & WorkerEvtFolderExit) {
browser_path_trim(path);
bool is_root = browser_folder_check_and_switch(path);
int32_t file_idx = 0;
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
if(idx_last_array_size(browser->idx_last) > 0) {
// Pop previous selected item index from history array
idx_last_array_pop_back(&file_idx, browser->idx_last);
}
FURI_LOG_D(
TAG, "Exit to: %s items: %u idx: %d", string_get_cstr(path), items_cnt, file_idx);
if(browser->folder_cb) {
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
}
}
if(flags & WorkerEvtLoad) {
FURI_LOG_D(TAG, "Load offset: %u cnt: %u", browser->load_offset, browser->load_count);
browser_folder_load(browser, path, browser->load_offset, browser->load_count);
}
if(flags & WorkerEvtStop) {
break;
}
}
string_clear(filename);
string_clear(path);
FURI_LOG_D(TAG, "End");
return 0;
}
void file_browser_worker_get_filename(string_t path, string_t name, bool trim_ext) {
size_t filename_start = string_search_rchar(path, '/');
if(filename_start > 0) {
filename_start++;
string_set_n(name, path, filename_start, string_size(path) - filename_start);
}
if(trim_ext) {
size_t dot = string_search_rchar(name, '.');
if(dot > 0) {
string_left(name, dot);
}
}
}
BrowserWorker* file_browser_worker_alloc(string_t path, char* filter_ext, bool skip_assets) {
BrowserWorker* browser = malloc(sizeof(BrowserWorker));
idx_last_array_init(browser->idx_last);
string_init_set_str(browser->filter_extension, filter_ext);
browser->skip_assets = skip_assets;
string_init_set(browser->path_next, path);
browser->thread = furi_thread_alloc();
furi_thread_set_name(browser->thread, "BrowserWorker");
furi_thread_set_stack_size(browser->thread, 2048);
furi_thread_set_context(browser->thread, browser);
furi_thread_set_callback(browser->thread, browser_worker);
furi_thread_start(browser->thread);
return browser;
}
void file_browser_worker_free(BrowserWorker* browser) {
furi_assert(browser);
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtStop);
furi_thread_join(browser->thread);
furi_thread_free(browser->thread);
string_clear(browser->filter_extension);
string_clear(browser->path_next);
idx_last_array_clear(browser->idx_last);
free(browser);
}
void file_browser_worker_set_callback_context(BrowserWorker* browser, void* context) {
furi_assert(browser);
browser->cb_ctx = context;
}
void file_browser_worker_set_folder_callback(
BrowserWorker* browser,
BrowserWorkerFolderOpenCallback cb) {
furi_assert(browser);
browser->folder_cb = cb;
}
void file_browser_worker_set_list_callback(
BrowserWorker* browser,
BrowserWorkerListLoadCallback cb) {
furi_assert(browser);
browser->list_load_cb = cb;
}
void file_browser_worker_set_item_callback(
BrowserWorker* browser,
BrowserWorkerListItemCallback cb) {
furi_assert(browser);
browser->list_item_cb = cb;
}
void file_browser_worker_set_long_load_callback(
BrowserWorker* browser,
BrowserWorkerLongLoadCallback cb) {
furi_assert(browser);
browser->long_load_cb = cb;
}
void file_browser_worker_folder_enter(BrowserWorker* browser, string_t path, int32_t item_idx) {
furi_assert(browser);
string_set(browser->path_next, path);
browser->item_sel_idx = item_idx;
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtFolderEnter);
}
void file_browser_worker_folder_exit(BrowserWorker* browser) {
furi_assert(browser);
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtFolderExit);
}
void file_browser_worker_load(BrowserWorker* browser, uint32_t offset, uint32_t count) {
furi_assert(browser);
browser->load_offset = offset;
browser->load_count = count;
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtLoad);
}

View File

@ -0,0 +1,57 @@
#pragma once
#include "m-string.h"
#include <gui/view.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct BrowserWorker BrowserWorker;
typedef void (*BrowserWorkerFolderOpenCallback)(
void* context,
uint32_t item_cnt,
int32_t file_idx,
bool is_root);
typedef void (*BrowserWorkerListLoadCallback)(void* context, uint32_t list_load_offset);
typedef void (*BrowserWorkerListItemCallback)(
void* context,
string_t item_path,
bool is_folder,
bool is_last);
typedef void (*BrowserWorkerLongLoadCallback)(void* context);
void file_browser_worker_get_filename(string_t path, string_t name, bool trim_ext);
BrowserWorker* file_browser_worker_alloc(string_t path, char* filter_ext, bool skip_assets);
void file_browser_worker_free(BrowserWorker* browser);
void file_browser_worker_set_callback_context(BrowserWorker* browser, void* context);
void file_browser_worker_set_folder_callback(
BrowserWorker* browser,
BrowserWorkerFolderOpenCallback cb);
void file_browser_worker_set_list_callback(
BrowserWorker* browser,
BrowserWorkerListLoadCallback cb);
void file_browser_worker_set_item_callback(
BrowserWorker* browser,
BrowserWorkerListItemCallback cb);
void file_browser_worker_set_long_load_callback(
BrowserWorker* browser,
BrowserWorkerLongLoadCallback cb);
void file_browser_worker_folder_enter(BrowserWorker* browser, string_t path, int32_t item_idx);
void file_browser_worker_folder_exit(BrowserWorker* browser);
void file_browser_worker_load(BrowserWorker* browser, uint32_t offset, uint32_t count);
#ifdef __cplusplus
}
#endif

View File

@ -1,10 +1,12 @@
#include "subghz_frequency_analyzer_worker.h"
#include <lib/drivers/cc1101_regs.h>
#include <lib/drivers/cc1101.h>
#include <furi.h>
#include "../subghz_i.h"
#define SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD -90.0f
static const uint8_t subghz_preset_ook_58khz[][2] = {
{CC1101_FIFOTHR, 0x47}, // The only important bit is ADC_RETENTION, FIFO Tx=33 Rx=32
{CC1101_MDMCFG4, 0xF5}, // Rx BW filter is 58.035714kHz
@ -37,6 +39,16 @@ struct SubGhzFrequencyAnalyzerWorker {
void* context;
};
static void subghz_frequency_analyzer_worker_load_registers(const uint8_t data[][2]) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
uint32_t i = 0;
while(data[i][0]) {
cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, data[i][0], data[i][1]);
i++;
}
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
}
// running average with adaptive coefficient
static uint32_t subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
SubGhzFrequencyAnalyzerWorker* instance,
@ -65,27 +77,55 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
float rssi;
uint32_t frequency;
uint32_t frequency_start;
CC1101Status status;
//Start CC1101
furi_hal_subghz_reset();
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
furi_hal_subghz_set_frequency(433920000);
furi_hal_subghz_flush_rx();
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
cc1101_flush_rx(&furi_hal_spi_bus_handle_subghz);
cc1101_flush_tx(&furi_hal_spi_bus_handle_subghz);
cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW);
cc1101_write_reg(
&furi_hal_spi_bus_handle_subghz,
CC1101_AGCCTRL2,
0b0000111); // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAIN_TARGET 42 dB
cc1101_write_reg(
&furi_hal_spi_bus_handle_subghz,
CC1101_AGCCTRL1,
0b00000000); // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
cc1101_write_reg(
&furi_hal_spi_bus_handle_subghz,
CC1101_AGCCTRL0,
0b00000001); // 00 - No hysteresis, medium asymmetric dead zone, medium gain ; 00 - 8 samples agc; 00 - Normal AGC, 01 - 8dB boundary
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
furi_hal_subghz_rx();
while(instance->worker_running) {
osDelay(10);
frequency_rssi.rssi = -127.0f;
furi_hal_subghz_idle();
furi_hal_subghz_load_registers(subghz_preset_ook_650khz);
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_650khz);
for(size_t i = 0; i < subghz_setting_get_frequency_count(instance->setting); i++) {
if(furi_hal_subghz_is_frequency_valid(
subghz_setting_get_frequency(instance->setting, i))) {
furi_hal_subghz_idle();
frequency = furi_hal_subghz_set_frequency(
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
frequency = cc1101_set_frequency(
&furi_hal_spi_bus_handle_subghz,
subghz_setting_get_frequency(instance->setting, i));
furi_hal_subghz_rx();
cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
do {
status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
} while(status.STATE != CC1101StateIDLE);
cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
osDelay(3);
rssi = furi_hal_subghz_get_rssi();
if(frequency_rssi.rssi < rssi) {
@ -95,19 +135,28 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
}
}
if(frequency_rssi.rssi > -90.0) {
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
// -0.5 ... 433.92 ... +0.5
frequency_start = frequency_rssi.frequency - 250000;
//step 10KHz
frequency_start = frequency_rssi.frequency - 500000;
frequency_rssi.rssi = -127.0;
furi_hal_subghz_idle();
furi_hal_subghz_load_registers(subghz_preset_ook_58khz);
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_58khz);
//step 10KHz
for(uint32_t i = frequency_start; i < frequency_start + 500000; i += 10000) {
if(furi_hal_subghz_is_frequency_valid(i)) {
furi_hal_subghz_idle();
frequency = furi_hal_subghz_set_frequency(i);
furi_hal_subghz_rx();
osDelay(3);
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
frequency = cc1101_set_frequency(&furi_hal_spi_bus_handle_subghz, i);
cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
do {
status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
} while(status.STATE != CC1101StateIDLE);
cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
osDelay(5);
rssi = furi_hal_subghz_get_rssi();
if(frequency_rssi.rssi < rssi) {
frequency_rssi.rssi = rssi;
@ -117,7 +166,7 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
}
}
if(frequency_rssi.rssi > -90.0) {
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
instance->count_repet = 20;
if(instance->filVal) {
frequency_rssi.frequency =

View File

@ -40,6 +40,9 @@ const uint8_t* const _I_125_10px[] = {_I_125_10px_0};
const uint8_t _I_Nfc_10px_0[] = {0x00,0x80,0x00,0x00,0x01,0x22,0x02,0x43,0x02,0x45,0x02,0x49,0x02,0x31,0x02,0x22,0x02,0x00,0x01,0x80,0x00,};
const uint8_t* const _I_Nfc_10px[] = {_I_Nfc_10px_0};
const uint8_t _I_back_10px_0[] = {0x00,0x00,0x00,0x10,0x00,0x38,0x00,0x7C,0x00,0xFE,0x00,0x38,0x00,0x38,0x00,0xF8,0x01,0xF8,0x01,0x00,0x00,};
const uint8_t* const _I_back_10px[] = {_I_back_10px_0};
const uint8_t _I_badusb_10px_0[] = {0x01,0x00,0x11,0x00,0x00,0x0f,0xe2,0x01,0xfc,0x80,0xdd,0x20,0x32,0x48,0x08,0x14,0x40,0x23,0xa8,0x08,0xa0,};
const uint8_t* const _I_badusb_10px[] = {_I_badusb_10px_0};
@ -55,6 +58,9 @@ const uint8_t* const _I_ibutt_10px[] = {_I_ibutt_10px_0};
const uint8_t _I_ir_10px_0[] = {0x00,0xFC,0x00,0x02,0x01,0x79,0x02,0x84,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x58,0x00,0x78,0x00,0xFF,0x03,};
const uint8_t* const _I_ir_10px[] = {_I_ir_10px_0};
const uint8_t _I_loading_10px_0[] = {0x00,0xFE,0x00,0x82,0x00,0xBA,0x00,0x54,0x00,0x28,0x00,0x28,0x00,0x44,0x00,0x92,0x00,0xBA,0x00,0xFE,0x00,};
const uint8_t* const _I_loading_10px[] = {_I_loading_10px_0};
const uint8_t _I_sub1_10px_0[] = {0x01,0x00,0x12,0x00,0x81,0x40,0x69,0x30,0x2c,0x2c,0x0b,0x6a,0x01,0x28,0x0c,0x0a,0x65,0x01,0x98,0x40,0x00,0x26,};
const uint8_t* const _I_sub1_10px[] = {_I_sub1_10px_0};
@ -730,11 +736,13 @@ const Icon A_Levelup1_128x64 = {.width=128,.height=64,.frame_count=11,.frame_rat
const Icon A_Levelup2_128x64 = {.width=128,.height=64,.frame_count=11,.frame_rate=2,.frames=_A_Levelup2_128x64};
const Icon I_125_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_125_10px};
const Icon I_Nfc_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_Nfc_10px};
const Icon I_back_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_back_10px};
const Icon I_badusb_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_badusb_10px};
const Icon I_ble_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_ble_10px};
const Icon I_dir_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_dir_10px};
const Icon I_ibutt_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_ibutt_10px};
const Icon I_ir_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_ir_10px};
const Icon I_loading_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_loading_10px};
const Icon I_sub1_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_sub1_10px};
const Icon I_u2f_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_u2f_10px};
const Icon I_unknown_10px = {.width=10,.height=10,.frame_count=1,.frame_rate=0,.frames=_I_unknown_10px};

View File

@ -7,11 +7,13 @@ extern const Icon A_Levelup1_128x64;
extern const Icon A_Levelup2_128x64;
extern const Icon I_125_10px;
extern const Icon I_Nfc_10px;
extern const Icon I_back_10px;
extern const Icon I_badusb_10px;
extern const Icon I_ble_10px;
extern const Icon I_dir_10px;
extern const Icon I_ibutt_10px;
extern const Icon I_ir_10px;
extern const Icon I_loading_10px;
extern const Icon I_sub1_10px;
extern const Icon I_u2f_10px;
extern const Icon I_unknown_10px;

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B

View File

@ -127,7 +127,7 @@
* Maximum number of simultaneous connections that the device will support.
* Valid values are from 1 to 8
*/
#define CFG_BLE_NUM_LINK 2
#define CFG_BLE_NUM_LINK 1
/**
* Maximum number of Services that can be stored in the GATT database.

View File

@ -8,13 +8,44 @@
typedef struct {
uint16_t svc_handle;
uint16_t char_level_handle;
uint16_t battery_level_char_handle;
uint16_t power_state_char_handle;
} BatterySvc;
enum {
// Common states
BatterySvcPowerStateUnknown = 0b00,
BatterySvcPowerStateUnsupported = 0b01,
// Level states
BatterySvcPowerStateGoodLevel = 0b10,
BatterySvcPowerStateCriticallyLowLevel = 0b11,
// Charging states
BatterySvcPowerStateNotCharging = 0b10,
BatterySvcPowerStateCharging = 0b11,
// Discharging states
BatterySvcPowerStateNotDischarging = 0b10,
BatterySvcPowerStateDischarging = 0b11,
// Battery states
BatterySvcPowerStateBatteryNotPresent = 0b10,
BatterySvcPowerStateBatteryPresent = 0b11,
};
typedef struct {
uint8_t present : 2;
uint8_t discharging : 2;
uint8_t charging : 2;
uint8_t level : 2;
} BattrySvcPowerState;
_Static_assert(sizeof(BattrySvcPowerState) == 1, "Incorrect structure size");
static BatterySvc* battery_svc = NULL;
#define BATTERY_POWER_STATE (0x2A1A)
static const uint16_t service_uuid = BATTERY_SERVICE_UUID;
static const uint16_t char_battery_level_uuid = BATTERY_LEVEL_CHAR_UUID;
static const uint16_t battery_level_char_uuid = BATTERY_LEVEL_CHAR_UUID;
static const uint16_t power_state_char_uuid = BATTERY_POWER_STATE;
void battery_svc_start() {
battery_svc = malloc(sizeof(BatterySvc));
@ -22,7 +53,7 @@ void battery_svc_start() {
// Add Battery service
status = aci_gatt_add_service(
UUID_TYPE_16, (Service_UUID_t*)&service_uuid, PRIMARY_SERVICE, 4, &battery_svc->svc_handle);
UUID_TYPE_16, (Service_UUID_t*)&service_uuid, PRIMARY_SERVICE, 8, &battery_svc->svc_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add Battery service: %d", status);
}
@ -30,14 +61,29 @@ void battery_svc_start() {
status = aci_gatt_add_char(
battery_svc->svc_handle,
UUID_TYPE_16,
(Char_UUID_t*)&char_battery_level_uuid,
(Char_UUID_t*)&battery_level_char_uuid,
1,
CHAR_PROP_READ | CHAR_PROP_NOTIFY,
ATTR_PERMISSION_AUTHEN_READ,
GATT_DONT_NOTIFY_EVENTS,
10,
CHAR_VALUE_LEN_CONSTANT,
&battery_svc->char_level_handle);
&battery_svc->battery_level_char_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add Battery level characteristic: %d", status);
}
// Add Power state characteristic
status = aci_gatt_add_char(
battery_svc->svc_handle,
UUID_TYPE_16,
(Char_UUID_t*)&power_state_char_uuid,
1,
CHAR_PROP_READ | CHAR_PROP_NOTIFY,
ATTR_PERMISSION_AUTHEN_READ,
GATT_DONT_NOTIFY_EVENTS,
10,
CHAR_VALUE_LEN_CONSTANT,
&battery_svc->power_state_char_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add Battery level characteristic: %d", status);
}
@ -47,7 +93,13 @@ void battery_svc_stop() {
tBleStatus status;
if(battery_svc) {
// Delete Battery level characteristic
status = aci_gatt_del_char(battery_svc->svc_handle, battery_svc->char_level_handle);
status =
aci_gatt_del_char(battery_svc->svc_handle, battery_svc->battery_level_char_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to delete Battery level characteristic: %d", status);
}
// Delete Power state characteristic
status = aci_gatt_del_char(battery_svc->svc_handle, battery_svc->power_state_char_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to delete Battery level characteristic: %d", status);
}
@ -73,7 +125,37 @@ bool battery_svc_update_level(uint8_t battery_charge) {
// Update battery level characteristic
FURI_LOG_D(TAG, "Updating battery level characteristic");
tBleStatus result = aci_gatt_update_char_value(
battery_svc->svc_handle, battery_svc->char_level_handle, 0, 1, &battery_charge);
battery_svc->svc_handle, battery_svc->battery_level_char_handle, 0, 1, &battery_charge);
if(result) {
FURI_LOG_E(TAG, "Failed updating RX characteristic: %d", result);
}
return result != BLE_STATUS_SUCCESS;
}
bool battery_svc_update_power_state(bool is_charging) {
// Check if service was started
if(battery_svc == NULL) {
return false;
}
// Update power state characteristic
BattrySvcPowerState power_state = {
.level = BatterySvcPowerStateUnsupported,
.present = BatterySvcPowerStateBatteryPresent,
};
if(is_charging) {
power_state.charging = BatterySvcPowerStateCharging;
power_state.discharging = BatterySvcPowerStateNotDischarging;
} else {
power_state.charging = BatterySvcPowerStateNotCharging;
power_state.discharging = BatterySvcPowerStateDischarging;
}
FURI_LOG_D(TAG, "Updating power state characteristic");
tBleStatus result = aci_gatt_update_char_value(
battery_svc->svc_handle,
battery_svc->power_state_char_handle,
0,
1,
(uint8_t*)&power_state);
if(result) {
FURI_LOG_E(TAG, "Failed updating RX characteristic: %d", result);
}

View File

@ -15,6 +15,8 @@ bool battery_svc_is_started();
bool battery_svc_update_level(uint8_t battery_level);
bool battery_svc_update_power_state(bool is_charging);
#ifdef __cplusplus
}
#endif

View File

@ -54,6 +54,23 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
}
serial_svc->bytes_ready_to_receive -= MIN(
serial_svc->bytes_ready_to_receive, attribute_modified->Attr_Data_Length);
// Update flow control characteristic without notification
uint32_t buff_size_reversed =
REVERSE_BYTES_U32(serial_svc->bytes_ready_to_receive);
tBleStatus result = aci_gatt_update_char_value_ext(
0,
serial_svc->svc_handle,
serial_svc->flow_ctrl_char_handle,
0,
sizeof(uint32_t),
0,
sizeof(uint32_t),
(uint8_t*)&buff_size_reversed);
if(result) {
FURI_LOG_E(TAG, "Failed to update flow control char: %02X", result);
}
SerialServiceEvent event = {
.event = SerialServiceEventTypeDataReceived,
.data = {

View File

@ -284,6 +284,12 @@ void furi_hal_bt_update_battery_level(uint8_t battery_level) {
}
}
void furi_hal_bt_update_power_state(bool battery_is_charging) {
if(battery_svc_is_started()) {
battery_svc_update_power_state(battery_is_charging);
}
}
void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size) {
ble_app_get_key_storage_buff(key_buff_addr, key_buff_size);
}

View File

@ -91,6 +91,12 @@ bool furi_hal_bt_change_app(FuriHalBtProfile profile, GapEventCallback event_cb,
*/
void furi_hal_bt_update_battery_level(uint8_t battery_level);
/** Update battery power state
*
* @param battery_is_charging battery charging state
*/
void furi_hal_bt_update_power_state(bool battery_is_charging);
/** Checks if BLE state is active
*
* @return true if device is connected or advertising, false otherwise