mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 16:18:35 -07:00
BLE Spam refactor again to use scene manager
This commit is contained in:
50
applications/external/ble_spam/ble_spam.c
vendored
50
applications/external/ble_spam/ble_spam.c
vendored
@@ -1,7 +1,7 @@
|
||||
#include "ble_spam.h"
|
||||
#include <gui/gui.h>
|
||||
#include <furi_hal_bt.h>
|
||||
#include <gui/elements.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
#include "protocols/_registry.h"
|
||||
|
||||
@@ -114,6 +114,7 @@ static Attack attacks[] = {
|
||||
uint16_t delays[] = {20, 50, 100, 200};
|
||||
|
||||
typedef struct {
|
||||
Ctx ctx;
|
||||
bool resume;
|
||||
bool advertising;
|
||||
uint8_t delay;
|
||||
@@ -164,10 +165,6 @@ static void toggle_adv(State* state) {
|
||||
}
|
||||
}
|
||||
|
||||
enum {
|
||||
ViewMain,
|
||||
};
|
||||
|
||||
#define PAGE_MIN (-3)
|
||||
#define PAGE_MAX ATTACKS_COUNT
|
||||
enum {
|
||||
@@ -363,9 +360,9 @@ static bool input_callback(InputEvent* input, void* ctx) {
|
||||
return consumed;
|
||||
}
|
||||
|
||||
static uint32_t exit_callback(void* ctx) {
|
||||
UNUSED(ctx);
|
||||
return VIEW_NONE;
|
||||
static bool back_event_callback(void* _ctx) {
|
||||
Ctx* ctx = _ctx;
|
||||
return scene_manager_handle_back_event(ctx->scene_manager);
|
||||
}
|
||||
|
||||
int32_t ble_spam(void* p) {
|
||||
@@ -377,27 +374,30 @@ int32_t ble_spam(void* p) {
|
||||
furi_thread_set_stack_size(state->thread, 4096);
|
||||
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
ViewDispatcher* view_dispatcher = view_dispatcher_alloc();
|
||||
view_dispatcher_enable_queue(view_dispatcher);
|
||||
view_dispatcher_set_event_callback_context(view_dispatcher, state);
|
||||
state->ctx.view_dispatcher = view_dispatcher_alloc();
|
||||
view_dispatcher_enable_queue(state->ctx.view_dispatcher);
|
||||
view_dispatcher_set_event_callback_context(state->ctx.view_dispatcher, &state->ctx);
|
||||
view_dispatcher_set_navigation_event_callback(state->ctx.view_dispatcher, back_event_callback);
|
||||
state->ctx.scene_manager = scene_manager_alloc(&scene_handlers, &state->ctx);
|
||||
|
||||
View* view = view_alloc();
|
||||
view_allocate_model(view, ViewModelTypeLockFree, sizeof(State*));
|
||||
View* view_main = view_alloc();
|
||||
view_allocate_model(view_main, ViewModelTypeLockFree, sizeof(State*));
|
||||
with_view_model(
|
||||
view, State * *model, { *model = state; }, false);
|
||||
view_set_context(view, view);
|
||||
view_set_draw_callback(view, draw_callback);
|
||||
view_set_input_callback(view, input_callback);
|
||||
view_set_previous_callback(view, exit_callback);
|
||||
view_dispatcher_add_view(view_dispatcher, ViewMain, view);
|
||||
view_main, State * *model, { *model = state; }, false);
|
||||
view_set_context(view_main, view_main);
|
||||
view_set_draw_callback(view_main, draw_callback);
|
||||
view_set_input_callback(view_main, input_callback);
|
||||
view_dispatcher_add_view(state->ctx.view_dispatcher, ViewMain, view_main);
|
||||
|
||||
view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen);
|
||||
view_dispatcher_switch_to_view(view_dispatcher, ViewMain);
|
||||
view_dispatcher_run(view_dispatcher);
|
||||
view_dispatcher_attach_to_gui(state->ctx.view_dispatcher, gui, ViewDispatcherTypeFullscreen);
|
||||
scene_manager_next_scene(state->ctx.scene_manager, SceneMain);
|
||||
view_dispatcher_run(state->ctx.view_dispatcher);
|
||||
|
||||
view_dispatcher_remove_view(view_dispatcher, ViewMain);
|
||||
view_free(view);
|
||||
view_dispatcher_free(view_dispatcher);
|
||||
view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewMain);
|
||||
view_free(view_main);
|
||||
|
||||
scene_manager_free(state->ctx.scene_manager);
|
||||
view_dispatcher_free(state->ctx.view_dispatcher);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
furi_thread_free(state->thread);
|
||||
|
||||
14
applications/external/ble_spam/ble_spam.h
vendored
Normal file
14
applications/external/ble_spam/ble_spam.h
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
#include "scenes/_setup.h"
|
||||
|
||||
enum {
|
||||
ViewMain,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
ViewDispatcher* view_dispatcher;
|
||||
SceneManager* scene_manager;
|
||||
} Ctx;
|
||||
0
applications/external/ble_spam/protocols/_scenes.h
vendored
Normal file
0
applications/external/ble_spam/protocols/_scenes.h
vendored
Normal file
2
applications/external/ble_spam/scenes/_scenes.h
vendored
Normal file
2
applications/external/ble_spam/scenes/_scenes.h
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
ADD_SCENE(main, Main)
|
||||
#include "../protocols/_scenes.h"
|
||||
30
applications/external/ble_spam/scenes/_setup.c
vendored
Normal file
30
applications/external/ble_spam/scenes/_setup.c
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "_setup.h"
|
||||
|
||||
// Generate scene on_enter handlers array
|
||||
#define ADD_SCENE(name, id) scene_##name##_on_enter,
|
||||
void (*const scene_on_enter_handlers[])(void*) = {
|
||||
#include "_scenes.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers array
|
||||
#define ADD_SCENE(name, id) scene_##name##_on_event,
|
||||
bool (*const scene_on_event_handlers[])(void*, SceneManagerEvent) = {
|
||||
#include "_scenes.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers array
|
||||
#define ADD_SCENE(name, id) scene_##name##_on_exit,
|
||||
void (*const scene_on_exit_handlers[])(void*) = {
|
||||
#include "_scenes.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Initialize scene handlers configuration structure
|
||||
const SceneManagerHandlers scene_handlers = {
|
||||
.on_enter_handlers = scene_on_enter_handlers,
|
||||
.on_event_handlers = scene_on_event_handlers,
|
||||
.on_exit_handlers = scene_on_exit_handlers,
|
||||
.scene_num = SceneCount,
|
||||
};
|
||||
28
applications/external/ble_spam/scenes/_setup.h
vendored
Normal file
28
applications/external/ble_spam/scenes/_setup.h
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/scene_manager.h>
|
||||
|
||||
// Generate scene id and total number
|
||||
#define ADD_SCENE(name, id) Scene##id,
|
||||
typedef enum {
|
||||
#include "_scenes.h"
|
||||
SceneCount,
|
||||
} Scene;
|
||||
#undef ADD_SCENE
|
||||
|
||||
extern const SceneManagerHandlers scene_handlers;
|
||||
|
||||
// Generate scene on_enter handlers declaration
|
||||
#define ADD_SCENE(name, id) void scene_##name##_on_enter(void*);
|
||||
#include "_scenes.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers declaration
|
||||
#define ADD_SCENE(name, id) bool scene_##name##_on_event(void*, SceneManagerEvent);
|
||||
#include "_scenes.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers declaration
|
||||
#define ADD_SCENE(name, id) void scene_##name##_on_exit(void*);
|
||||
#include "_scenes.h"
|
||||
#undef ADD_SCENE
|
||||
16
applications/external/ble_spam/scenes/main.c
vendored
Normal file
16
applications/external/ble_spam/scenes/main.c
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "../ble_spam.h"
|
||||
|
||||
void scene_main_on_enter(void* _ctx) {
|
||||
Ctx* ctx = _ctx;
|
||||
view_dispatcher_switch_to_view(ctx->view_dispatcher, ViewMain);
|
||||
}
|
||||
|
||||
bool scene_main_on_event(void* _ctx, SceneManagerEvent event) {
|
||||
UNUSED(_ctx);
|
||||
UNUSED(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
void scene_main_on_exit(void* _ctx) {
|
||||
UNUSED(_ctx);
|
||||
}
|
||||
Reference in New Issue
Block a user