merge
This commit is contained in:
120
applications/app-loader/app-loader.c
Normal file
120
applications/app-loader/app-loader.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "flipper_v2.h"
|
||||
#include <gui/gui.h>
|
||||
#include "menu/menu.h"
|
||||
|
||||
typedef struct {
|
||||
FuriApp* handler;
|
||||
Widget* widget;
|
||||
FlipperStartupApp* current_app;
|
||||
} AppLoaderState;
|
||||
|
||||
typedef struct {
|
||||
AppLoaderState* state;
|
||||
FlipperStartupApp* app;
|
||||
} AppLoaderContext;
|
||||
|
||||
void render_callback(CanvasApi* canvas, void* _ctx) {
|
||||
AppLoaderState* ctx = (AppLoaderState*)_ctx;
|
||||
|
||||
canvas->clear(canvas);
|
||||
canvas->set_color(canvas, ColorBlack);
|
||||
canvas->set_font(canvas, FontPrimary);
|
||||
canvas->draw_str(canvas, 2, 32, ctx->current_app->name);
|
||||
|
||||
canvas->set_font(canvas, FontSecondary);
|
||||
canvas->draw_str(canvas, 2, 44, "press back to exit");
|
||||
}
|
||||
|
||||
void input_callback(InputEvent* input_event, void* _ctx) {
|
||||
AppLoaderState* ctx = (AppLoaderState*)_ctx;
|
||||
|
||||
if(input_event->state && input_event->input == InputBack) {
|
||||
furiac_kill(ctx->handler);
|
||||
widget_enabled_set(ctx->widget, false);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_menu(void* _ctx) {
|
||||
AppLoaderContext* ctx = (AppLoaderContext*)_ctx;
|
||||
|
||||
widget_enabled_set(ctx->state->widget, true);
|
||||
|
||||
// TODO how to call this?
|
||||
// furiac_wait_libs(&FLIPPER_STARTUP[i].libs);
|
||||
|
||||
ctx->state->current_app = ctx->app;
|
||||
ctx->state->handler = furiac_start(ctx->app->app, ctx->app->name, NULL);
|
||||
}
|
||||
|
||||
void application_blink(void* p);
|
||||
void application_uart_write(void* p);
|
||||
void application_input_dump(void* p);
|
||||
|
||||
const FlipperStartupApp FLIPPER_APPS[] = {
|
||||
{.app = application_blink, .name = "blink", .libs = {0}},
|
||||
{.app = application_uart_write, .name = "uart write", .libs = {0}},
|
||||
{.app = application_input_dump, .name = "input dump", .libs = {1, FURI_LIB{"input_task"}}},
|
||||
};
|
||||
|
||||
void app_loader(void* p) {
|
||||
osThreadId_t self_id = osThreadGetId();
|
||||
assert(self_id);
|
||||
|
||||
AppLoaderState state;
|
||||
state.handler = NULL;
|
||||
|
||||
state.widget = widget_alloc();
|
||||
assert(state.widget);
|
||||
widget_enabled_set(state.widget, false);
|
||||
widget_draw_callback_set(state.widget, render_callback, &state);
|
||||
widget_input_callback_set(state.widget, input_callback, &state);
|
||||
|
||||
ValueMutex* menu_mutex = furi_open("menu");
|
||||
if(menu_mutex == NULL) {
|
||||
printf("menu is not available\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
// Open GUI and register widget
|
||||
GuiApi* gui = furi_open("gui");
|
||||
if(gui == NULL) {
|
||||
printf("gui is not available\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
gui->add_widget(gui, state.widget, WidgetLayerFullscreen);
|
||||
|
||||
{
|
||||
Menu* menu = acquire_mutex_block(menu_mutex);
|
||||
|
||||
// FURI startup
|
||||
const size_t flipper_app_count = sizeof(FLIPPER_APPS) / sizeof(FLIPPER_APPS[0]);
|
||||
|
||||
for(size_t i = 0; i < flipper_app_count; i++) {
|
||||
AppLoaderContext* ctx = furi_alloc(sizeof(AppLoaderContext));
|
||||
ctx->state = &state;
|
||||
ctx->app = &FLIPPER_APPS[i];
|
||||
|
||||
menu_item_add(
|
||||
menu, menu_item_alloc_function(FLIPPER_APPS[i].name, NULL, handle_menu, ctx));
|
||||
}
|
||||
|
||||
/*
|
||||
menu_item_add(menu, menu_item_alloc_function("Sub 1 gHz", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("125 kHz RFID", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("Infrared", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("I-Button", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("USB", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("Bluetooth", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("GPIO / HW", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("U2F", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("Tamagotchi", NULL, NULL, NULL));
|
||||
menu_item_add(menu, menu_item_alloc_function("Plugins", NULL, NULL, NULL));
|
||||
*/
|
||||
|
||||
release_mutex(menu_mutex, menu);
|
||||
}
|
||||
|
||||
printf("[app loader] start\n");
|
||||
|
||||
osThreadSuspend(self_id);
|
||||
}
|
||||
@@ -5,8 +5,22 @@ CFLAGS += -I$(APP_DIR)
|
||||
|
||||
APP_RELEASE ?= 0
|
||||
ifeq ($(APP_RELEASE), 1)
|
||||
APP_DISPLAY = 1
|
||||
APP_INPUT = 1
|
||||
APP_GUI = 1
|
||||
APP_INPUT = 1
|
||||
APP_MENU = 1
|
||||
endif
|
||||
|
||||
APP_MENU ?= 0
|
||||
ifeq ($(APP_MENU), 1)
|
||||
APP_INPUT = 1
|
||||
APP_GUI = 1
|
||||
CFLAGS += -DAPP_MENU
|
||||
C_SOURCES += $(wildcard $(APP_DIR)/menu/*.c)
|
||||
C_SOURCES += $(wildcard $(APP_DIR)/app-loader/*.c)
|
||||
|
||||
APP_EXAMPLE_BLINK = 1
|
||||
APP_EXAMPLE_UART_WRITE = 1
|
||||
APP_EXAMPLE_INPUT_DUMP = 1
|
||||
endif
|
||||
|
||||
APP_TEST ?= 0
|
||||
@@ -17,6 +31,8 @@ C_SOURCES += $(APP_DIR)/tests/furi_record_test.c
|
||||
C_SOURCES += $(APP_DIR)/tests/test_index.c
|
||||
C_SOURCES += $(APP_DIR)/tests/minunit_test.c
|
||||
C_SOURCES += $(APP_DIR)/tests/furi_valuemutex_test.c
|
||||
C_SOURCES += $(APP_DIR)/tests/furi_pubsub_test.c
|
||||
C_SOURCES += $(APP_DIR)/tests/furi_memmgr_test.c
|
||||
endif
|
||||
|
||||
APP_EXAMPLE_BLINK ?= 0
|
||||
@@ -77,6 +93,12 @@ APP_DISPLAY = 1
|
||||
endif
|
||||
|
||||
# device drivers
|
||||
APP_GUI ?= 0
|
||||
ifeq ($(APP_GUI), 1)
|
||||
CFLAGS += -DAPP_GUI
|
||||
C_SOURCES += $(wildcard $(APP_DIR)/gui/*.c)
|
||||
C_SOURCES += $(wildcard $(APP_DIR)/backlight-control/*.c)
|
||||
endif
|
||||
|
||||
ifeq ($(APP_DISPLAY), 1)
|
||||
CFLAGS += -DAPP_DISPLAY
|
||||
@@ -87,4 +109,4 @@ APP_INPUT ?= 0
|
||||
ifeq ($(APP_INPUT), 1)
|
||||
CFLAGS += -DAPP_INPUT
|
||||
C_SOURCES += $(APP_DIR)/input/input.c
|
||||
endif
|
||||
endif
|
||||
|
||||
30
applications/backlight-control/backlight-control.c
Normal file
30
applications/backlight-control/backlight-control.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "flipper.h"
|
||||
|
||||
static void event_cb(const void* value, size_t size, void* ctx) {
|
||||
xSemaphoreGive((SemaphoreHandle_t*)ctx);
|
||||
}
|
||||
|
||||
const uint32_t BACKLIGHT_TIME = 10000;
|
||||
|
||||
void backlight_control(void* p) {
|
||||
// TODO use FURI
|
||||
HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_SET);
|
||||
|
||||
StaticSemaphore_t event_descriptor;
|
||||
SemaphoreHandle_t update = xSemaphoreCreateCountingStatic(255, 0, &event_descriptor);
|
||||
|
||||
// open record
|
||||
furi_open_deprecated("input_events", false, false, event_cb, NULL, (void*)update);
|
||||
|
||||
// we ready to work
|
||||
furiac_ready();
|
||||
|
||||
while(1) {
|
||||
// wait for event
|
||||
if(xSemaphoreTake(update, BACKLIGHT_TIME) == pdTRUE) {
|
||||
HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_SET);
|
||||
} else {
|
||||
HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
}
|
||||
}
|
||||
119
applications/gui/canvas.c
Normal file
119
applications/gui/canvas.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "canvas.h"
|
||||
#include "canvas_i.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <flipper.h>
|
||||
|
||||
typedef struct {
|
||||
CanvasApi api;
|
||||
|
||||
u8g2_t fb;
|
||||
uint8_t offset_x;
|
||||
uint8_t offset_y;
|
||||
uint8_t width;
|
||||
uint8_t height;
|
||||
} Canvas;
|
||||
|
||||
uint8_t canvas_width(CanvasApi* api);
|
||||
uint8_t canvas_height(CanvasApi* api);
|
||||
void canvas_clear(CanvasApi* api);
|
||||
void canvas_color_set(CanvasApi* api, uint8_t color);
|
||||
void canvas_font_set(CanvasApi* api, Font font);
|
||||
void canvas_str_draw(CanvasApi* api, uint8_t x, uint8_t y, const char* str);
|
||||
|
||||
uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
|
||||
uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
|
||||
|
||||
CanvasApi* canvas_api_init() {
|
||||
Canvas* canvas = furi_alloc(sizeof(Canvas));
|
||||
|
||||
u8g2_Setup_st7565_erc12864_alt_f(
|
||||
&canvas->fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
|
||||
|
||||
// send init sequence to the display, display is in sleep mode after this
|
||||
u8g2_InitDisplay(&canvas->fb);
|
||||
u8g2_SetContrast(&canvas->fb, 36);
|
||||
|
||||
u8g2_SetPowerSave(&canvas->fb, 0); // wake up display
|
||||
u8g2_SendBuffer(&canvas->fb);
|
||||
|
||||
canvas->api.width = canvas_width;
|
||||
canvas->api.height = canvas_height;
|
||||
canvas->api.clear = canvas_clear;
|
||||
canvas->api.set_color = canvas_color_set;
|
||||
canvas->api.set_font = canvas_font_set;
|
||||
canvas->api.draw_str = canvas_str_draw;
|
||||
|
||||
return (CanvasApi*)canvas;
|
||||
}
|
||||
|
||||
void canvas_api_free(CanvasApi* api) {
|
||||
assert(api);
|
||||
free(api);
|
||||
}
|
||||
|
||||
void canvas_commit(CanvasApi* api) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
u8g2_SetPowerSave(&canvas->fb, 0); // wake up display
|
||||
u8g2_SendBuffer(&canvas->fb);
|
||||
}
|
||||
|
||||
void canvas_frame_set(
|
||||
CanvasApi* api,
|
||||
uint8_t offset_x,
|
||||
uint8_t offset_y,
|
||||
uint8_t width,
|
||||
uint8_t height) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
canvas->offset_x = offset_x;
|
||||
canvas->offset_y = offset_y;
|
||||
canvas->width = width;
|
||||
canvas->height = height;
|
||||
}
|
||||
|
||||
uint8_t canvas_width(CanvasApi* api) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
return canvas->width;
|
||||
}
|
||||
|
||||
uint8_t canvas_height(CanvasApi* api) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
return canvas->height;
|
||||
}
|
||||
|
||||
void canvas_clear(CanvasApi* api) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
u8g2_ClearBuffer(&canvas->fb);
|
||||
}
|
||||
|
||||
void canvas_color_set(CanvasApi* api, Color color) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
u8g2_SetDrawColor(&canvas->fb, color);
|
||||
}
|
||||
|
||||
void canvas_font_set(CanvasApi* api, Font font) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
u8g2_SetFontMode(&canvas->fb, 1);
|
||||
if(font == FontPrimary) {
|
||||
u8g2_SetFont(&canvas->fb, u8g2_font_Born2bSportyV2_tr);
|
||||
} else if(font == FontSecondary) {
|
||||
u8g2_SetFont(&canvas->fb, u8g2_font_HelvetiPixel_tr);
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
void canvas_str_draw(CanvasApi* api, uint8_t x, uint8_t y, const char* str) {
|
||||
assert(api);
|
||||
Canvas* canvas = (Canvas*)api;
|
||||
x += canvas->offset_x;
|
||||
y += canvas->offset_y;
|
||||
u8g2_DrawStr(&canvas->fb, x, y, str);
|
||||
}
|
||||
27
applications/gui/canvas.h
Normal file
27
applications/gui/canvas.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <u8g2.h>
|
||||
|
||||
typedef enum {
|
||||
ColorWhite = 0x00,
|
||||
ColorBlack = 0x01,
|
||||
} Color;
|
||||
|
||||
typedef enum {
|
||||
FontPrimary = 0x00,
|
||||
FontSecondary = 0x01,
|
||||
} Font;
|
||||
|
||||
typedef struct CanvasApi CanvasApi;
|
||||
struct CanvasApi {
|
||||
uint8_t (*width)(CanvasApi* canvas);
|
||||
uint8_t (*height)(CanvasApi* canvas);
|
||||
|
||||
void (*clear)(CanvasApi* canvas);
|
||||
|
||||
void (*set_color)(CanvasApi* canvas, Color color);
|
||||
void (*set_font)(CanvasApi* canvas, Font font);
|
||||
|
||||
void (*draw_str)(CanvasApi* canvas, uint8_t x, uint8_t y, const char* str);
|
||||
};
|
||||
14
applications/gui/canvas_i.h
Normal file
14
applications/gui/canvas_i.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
CanvasApi* canvas_api_init();
|
||||
|
||||
void canvas_api_free(CanvasApi* api);
|
||||
|
||||
void canvas_commit(CanvasApi* api);
|
||||
|
||||
void canvas_frame_set(
|
||||
CanvasApi* api,
|
||||
uint8_t offset_x,
|
||||
uint8_t offset_y,
|
||||
uint8_t width,
|
||||
uint8_t height);
|
||||
172
applications/gui/gui.c
Normal file
172
applications/gui/gui.c
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "gui.h"
|
||||
#include "gui_i.h"
|
||||
|
||||
#include <flipper.h>
|
||||
#include <flipper_v2.h>
|
||||
#include <stdio.h>
|
||||
#include <m-array.h>
|
||||
|
||||
#include "gui_event.h"
|
||||
#include "canvas.h"
|
||||
#include "canvas_i.h"
|
||||
#include "widget.h"
|
||||
#include "widget_i.h"
|
||||
|
||||
ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST);
|
||||
|
||||
struct Gui {
|
||||
GuiApi api;
|
||||
GuiEvent* event;
|
||||
CanvasApi* canvas_api;
|
||||
WidgetArray_t widgets_status_bar;
|
||||
WidgetArray_t widgets;
|
||||
WidgetArray_t widgets_fs;
|
||||
WidgetArray_t widgets_dialog;
|
||||
};
|
||||
|
||||
void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) {
|
||||
assert(gui_api);
|
||||
assert(widget);
|
||||
Gui* gui = (Gui*)gui_api;
|
||||
|
||||
// TODO add mutex on widget array
|
||||
WidgetArray_t* widget_array = NULL;
|
||||
|
||||
switch(layer) {
|
||||
case WidgetLayerStatusBar:
|
||||
widget_array = &gui->widgets_status_bar;
|
||||
break;
|
||||
case WidgetLayerMain:
|
||||
widget_array = &gui->widgets;
|
||||
break;
|
||||
case WidgetLayerFullscreen:
|
||||
widget_array = &gui->widgets_fs;
|
||||
break;
|
||||
case WidgetLayerDialog:
|
||||
widget_array = &gui->widgets_dialog;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
assert(widget_array);
|
||||
|
||||
gui_event_lock(gui->event);
|
||||
WidgetArray_push_back(*widget_array, widget);
|
||||
widget_gui_set(widget, gui);
|
||||
gui_event_unlock(gui->event);
|
||||
|
||||
gui_update(gui);
|
||||
}
|
||||
|
||||
void gui_update(Gui* gui) {
|
||||
assert(gui);
|
||||
GuiMessage message;
|
||||
message.type = GuiMessageTypeRedraw;
|
||||
gui_event_messsage_send(gui->event, &message);
|
||||
}
|
||||
|
||||
Widget* gui_widget_find_enabled(WidgetArray_t array) {
|
||||
size_t widgets_count = WidgetArray_size(array);
|
||||
for(size_t i = 0; i < widgets_count; i++) {
|
||||
Widget* widget = *WidgetArray_get(array, widgets_count - i - 1);
|
||||
if(widget_is_enabled(widget)) {
|
||||
return widget;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool gui_redraw_fs(Gui* gui) {
|
||||
canvas_frame_set(gui->canvas_api, 0, 0, 128, 64);
|
||||
Widget* widget = gui_widget_find_enabled(gui->widgets_fs);
|
||||
if(widget) {
|
||||
widget_draw(widget, gui->canvas_api);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void gui_redraw_status_bar(Gui* gui) {
|
||||
canvas_frame_set(gui->canvas_api, 0, 0, 128, 64);
|
||||
Widget* widget = gui_widget_find_enabled(gui->widgets_status_bar);
|
||||
if(widget) widget_draw(widget, gui->canvas_api);
|
||||
}
|
||||
|
||||
void gui_redraw_normal(Gui* gui) {
|
||||
canvas_frame_set(gui->canvas_api, 0, 9, 128, 55);
|
||||
Widget* widget = gui_widget_find_enabled(gui->widgets);
|
||||
if(widget) widget_draw(widget, gui->canvas_api);
|
||||
}
|
||||
|
||||
void gui_redraw_dialogs(Gui* gui) {
|
||||
canvas_frame_set(gui->canvas_api, 10, 20, 118, 44);
|
||||
Widget* widget = gui_widget_find_enabled(gui->widgets_dialog);
|
||||
if(widget) widget_draw(widget, gui->canvas_api);
|
||||
}
|
||||
|
||||
void gui_redraw(Gui* gui) {
|
||||
assert(gui);
|
||||
|
||||
if(!gui_redraw_fs(gui)) {
|
||||
gui_redraw_status_bar(gui);
|
||||
gui_redraw_normal(gui);
|
||||
}
|
||||
gui_redraw_dialogs(gui);
|
||||
|
||||
canvas_commit(gui->canvas_api);
|
||||
}
|
||||
|
||||
void gui_input(Gui* gui, InputEvent* input_event) {
|
||||
assert(gui);
|
||||
|
||||
Widget* widget = gui_widget_find_enabled(gui->widgets_dialog);
|
||||
if(!widget) widget = gui_widget_find_enabled(gui->widgets_fs);
|
||||
if(!widget) widget = gui_widget_find_enabled(gui->widgets);
|
||||
|
||||
if(widget) {
|
||||
widget_input(widget, input_event);
|
||||
}
|
||||
}
|
||||
|
||||
Gui* gui_alloc() {
|
||||
Gui* gui = furi_alloc(sizeof(Gui));
|
||||
|
||||
// Initialize widget arrays
|
||||
WidgetArray_init(gui->widgets_status_bar);
|
||||
WidgetArray_init(gui->widgets);
|
||||
WidgetArray_init(gui->widgets_fs);
|
||||
WidgetArray_init(gui->widgets_dialog);
|
||||
|
||||
// Event dispatcher
|
||||
gui->event = gui_event_alloc();
|
||||
|
||||
// Drawing canvas api
|
||||
gui->canvas_api = canvas_api_init();
|
||||
|
||||
gui->api.add_widget = gui_add_widget;
|
||||
|
||||
return gui;
|
||||
}
|
||||
|
||||
void gui_task(void* p) {
|
||||
Gui* gui = gui_alloc();
|
||||
// Create FURI record
|
||||
if(!furi_create("gui", gui)) {
|
||||
printf("[gui_task] cannot create the gui record\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
furiac_ready();
|
||||
|
||||
// Forever dispatch
|
||||
while(1) {
|
||||
GuiMessage message = gui_event_message_next(gui->event);
|
||||
if(message.type == GuiMessageTypeRedraw) {
|
||||
gui_redraw(gui);
|
||||
} else if(message.type == GuiMessageTypeInput) {
|
||||
gui_input(gui, &message.input);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
applications/gui/gui.h
Normal file
18
applications/gui/gui.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include "canvas.h"
|
||||
|
||||
typedef enum {
|
||||
WidgetLayerStatusBar,
|
||||
WidgetLayerMain,
|
||||
WidgetLayerFullscreen,
|
||||
WidgetLayerDialog
|
||||
} WidgetLayer;
|
||||
|
||||
typedef struct Widget Widget;
|
||||
|
||||
typedef struct GuiApi GuiApi;
|
||||
struct GuiApi {
|
||||
void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer);
|
||||
};
|
||||
73
applications/gui/gui_event.c
Normal file
73
applications/gui/gui_event.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "gui_event.h"
|
||||
|
||||
#include <flipper.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define GUI_EVENT_MQUEUE_SIZE 8
|
||||
|
||||
struct GuiEvent {
|
||||
FuriRecordSubscriber* input_event_record;
|
||||
osMessageQueueId_t mqueue;
|
||||
osMutexId_t lock_mutex;
|
||||
};
|
||||
|
||||
void gui_event_input_events_callback(const void* value, size_t size, void* ctx) {
|
||||
assert(ctx);
|
||||
GuiEvent* gui_event = ctx;
|
||||
|
||||
GuiMessage message;
|
||||
message.type = GuiMessageTypeInput;
|
||||
message.input = *(InputEvent*)value;
|
||||
|
||||
osMessageQueuePut(gui_event->mqueue, &message, 0, osWaitForever);
|
||||
}
|
||||
|
||||
GuiEvent* gui_event_alloc() {
|
||||
GuiEvent* gui_event = furi_alloc(sizeof(GuiEvent));
|
||||
// Allocate message que
|
||||
gui_event->mqueue = osMessageQueueNew(GUI_EVENT_MQUEUE_SIZE, sizeof(GuiMessage), NULL);
|
||||
assert(gui_event->mqueue);
|
||||
|
||||
// Input
|
||||
gui_event->input_event_record = furi_open_deprecated(
|
||||
"input_events", false, false, gui_event_input_events_callback, NULL, gui_event);
|
||||
assert(gui_event->input_event_record != NULL);
|
||||
// Lock mutex
|
||||
gui_event->lock_mutex = osMutexNew(NULL);
|
||||
assert(gui_event->lock_mutex);
|
||||
gui_event_lock(gui_event);
|
||||
|
||||
return gui_event;
|
||||
}
|
||||
|
||||
void gui_event_free(GuiEvent* gui_event) {
|
||||
assert(gui_event);
|
||||
gui_event_unlock(gui_event);
|
||||
assert(osMessageQueueDelete(gui_event->mqueue) == osOK);
|
||||
free(gui_event);
|
||||
}
|
||||
|
||||
void gui_event_lock(GuiEvent* gui_event) {
|
||||
assert(gui_event);
|
||||
assert(osMutexAcquire(gui_event->lock_mutex, osWaitForever) == osOK);
|
||||
}
|
||||
|
||||
void gui_event_unlock(GuiEvent* gui_event) {
|
||||
assert(gui_event);
|
||||
assert(osMutexRelease(gui_event->lock_mutex) == osOK);
|
||||
}
|
||||
|
||||
void gui_event_messsage_send(GuiEvent* gui_event, GuiMessage* message) {
|
||||
assert(gui_event);
|
||||
assert(message);
|
||||
osMessageQueuePut(gui_event->mqueue, message, 0, 0);
|
||||
}
|
||||
|
||||
GuiMessage gui_event_message_next(GuiEvent* gui_event) {
|
||||
assert(gui_event);
|
||||
GuiMessage message;
|
||||
gui_event_unlock(gui_event);
|
||||
assert(osMessageQueueGet(gui_event->mqueue, &message, NULL, osWaitForever) == osOK);
|
||||
gui_event_lock(gui_event);
|
||||
return message;
|
||||
}
|
||||
29
applications/gui/gui_event.h
Normal file
29
applications/gui/gui_event.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <input/input.h>
|
||||
|
||||
typedef enum {
|
||||
GuiMessageTypeRedraw = 0x00,
|
||||
GuiMessageTypeInput = 0x01,
|
||||
} GuiMessageType;
|
||||
|
||||
typedef struct {
|
||||
GuiMessageType type;
|
||||
InputEvent input;
|
||||
void* data;
|
||||
} GuiMessage;
|
||||
|
||||
typedef struct GuiEvent GuiEvent;
|
||||
|
||||
GuiEvent* gui_event_alloc();
|
||||
|
||||
void gui_event_free(GuiEvent* gui_event);
|
||||
|
||||
void gui_event_lock(GuiEvent* gui_event);
|
||||
|
||||
void gui_event_unlock(GuiEvent* gui_event);
|
||||
|
||||
void gui_event_messsage_send(GuiEvent* gui_event, GuiMessage* message);
|
||||
|
||||
GuiMessage gui_event_message_next(GuiEvent* gui_event);
|
||||
5
applications/gui/gui_i.h
Normal file
5
applications/gui/gui_i.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct Gui Gui;
|
||||
|
||||
void gui_update(Gui* gui);
|
||||
114
applications/gui/u8g2_periphery.c
Normal file
114
applications/gui/u8g2_periphery.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "u8g2/u8g2.h"
|
||||
#include "flipper.h"
|
||||
|
||||
extern SPI_HandleTypeDef hspi1;
|
||||
|
||||
// TODO: fix log
|
||||
#ifdef DEBUG
|
||||
#undef DEBUG
|
||||
#endif
|
||||
|
||||
uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
|
||||
switch(msg) {
|
||||
//Initialize SPI peripheral
|
||||
case U8X8_MSG_GPIO_AND_DELAY_INIT:
|
||||
/* HAL initialization contains all what we need so we can skip this part. */
|
||||
break;
|
||||
|
||||
//Function which implements a delay, arg_int contains the amount of ms
|
||||
case U8X8_MSG_DELAY_MILLI:
|
||||
osDelay(arg_int);
|
||||
break;
|
||||
|
||||
//Function which delays 10us
|
||||
case U8X8_MSG_DELAY_10MICRO:
|
||||
delay_us(10);
|
||||
break;
|
||||
|
||||
//Function which delays 100ns
|
||||
case U8X8_MSG_DELAY_100NANO:
|
||||
asm("nop");
|
||||
break;
|
||||
|
||||
// Function to define the logic level of the RESET line
|
||||
case U8X8_MSG_GPIO_RESET:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] rst %d\n", arg_int);
|
||||
#endif
|
||||
|
||||
// TODO change it to FuriRecord pin
|
||||
HAL_GPIO_WritePin(
|
||||
DISPLAY_RST_GPIO_Port, DISPLAY_RST_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
break;
|
||||
|
||||
default:
|
||||
#ifdef DEBUG
|
||||
fufuprintf(log, "[u8g2] unknown io %d\n", msg);
|
||||
#endif
|
||||
|
||||
return 0; //A message was received which is not implemented, return 0 to indicate an error
|
||||
}
|
||||
|
||||
return 1; // command processed successfully.
|
||||
}
|
||||
|
||||
uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
|
||||
switch(msg) {
|
||||
case U8X8_MSG_BYTE_SEND:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] send %d bytes %02X\n", arg_int, ((uint8_t*)arg_ptr)[0]);
|
||||
#endif
|
||||
|
||||
// TODO change it to FuriRecord SPI
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)arg_ptr, arg_int, 10000);
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_SET_DC:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] dc %d\n", arg_int);
|
||||
#endif
|
||||
|
||||
// TODO change it to FuriRecord pin
|
||||
HAL_GPIO_WritePin(
|
||||
DISPLAY_DI_GPIO_Port, DISPLAY_DI_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_INIT:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] init\n");
|
||||
#endif
|
||||
|
||||
// TODO change it to FuriRecord pin
|
||||
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_START_TRANSFER:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] start\n");
|
||||
#endif
|
||||
|
||||
// TODO change it to FuriRecord pin
|
||||
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
|
||||
asm("nop");
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_END_TRANSFER:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] end\n");
|
||||
#endif
|
||||
|
||||
asm("nop");
|
||||
// TODO change it to FuriRecord pin
|
||||
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
|
||||
break;
|
||||
|
||||
default:
|
||||
#ifdef DEBUG
|
||||
fuprintf(log, "[u8g2] unknown xfer %d\n", msg);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
83
applications/gui/widget.c
Normal file
83
applications/gui/widget.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "widget.h"
|
||||
#include "widget_i.h"
|
||||
|
||||
#include <cmsis_os.h>
|
||||
#include <flipper.h>
|
||||
|
||||
#include "gui.h"
|
||||
#include "gui_i.h"
|
||||
|
||||
// TODO add mutex to widget ops
|
||||
|
||||
struct Widget {
|
||||
Gui* gui;
|
||||
bool is_enabled;
|
||||
WidgetDrawCallback draw_callback;
|
||||
void* draw_callback_context;
|
||||
WidgetInputCallback input_callback;
|
||||
void* input_callback_context;
|
||||
};
|
||||
|
||||
Widget* widget_alloc(WidgetDrawCallback callback, void* callback_context) {
|
||||
Widget* widget = furi_alloc(sizeof(Widget));
|
||||
widget->is_enabled = true;
|
||||
return widget;
|
||||
}
|
||||
|
||||
void widget_free(Widget* widget) {
|
||||
assert(widget);
|
||||
assert(widget->gui == NULL);
|
||||
free(widget);
|
||||
}
|
||||
|
||||
void widget_enabled_set(Widget* widget, bool enabled) {
|
||||
assert(widget);
|
||||
widget->is_enabled = enabled;
|
||||
widget_update(widget);
|
||||
}
|
||||
|
||||
bool widget_is_enabled(Widget* widget) {
|
||||
assert(widget);
|
||||
return widget->is_enabled;
|
||||
}
|
||||
|
||||
void widget_draw_callback_set(Widget* widget, WidgetDrawCallback callback, void* context) {
|
||||
assert(widget);
|
||||
widget->draw_callback = callback;
|
||||
widget->draw_callback_context = context;
|
||||
}
|
||||
|
||||
void widget_input_callback_set(Widget* widget, WidgetInputCallback callback, void* context) {
|
||||
assert(widget);
|
||||
widget->input_callback = callback;
|
||||
widget->input_callback_context = context;
|
||||
}
|
||||
|
||||
void widget_update(Widget* widget) {
|
||||
assert(widget);
|
||||
if(widget->gui) gui_update(widget->gui);
|
||||
}
|
||||
|
||||
void widget_gui_set(Widget* widget, Gui* gui) {
|
||||
assert(widget);
|
||||
assert(gui);
|
||||
widget->gui = gui;
|
||||
}
|
||||
|
||||
void widget_draw(Widget* widget, CanvasApi* canvas_api) {
|
||||
assert(widget);
|
||||
assert(canvas_api);
|
||||
assert(widget->gui);
|
||||
|
||||
if(widget->draw_callback) {
|
||||
widget->draw_callback(canvas_api, widget->draw_callback_context);
|
||||
}
|
||||
}
|
||||
|
||||
void widget_input(Widget* widget, InputEvent* event) {
|
||||
assert(widget);
|
||||
assert(event);
|
||||
assert(widget->gui);
|
||||
|
||||
if(widget->input_callback) widget->input_callback(event, widget->input_callback_context);
|
||||
}
|
||||
21
applications/gui/widget.h
Normal file
21
applications/gui/widget.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <input/input.h>
|
||||
#include "canvas.h"
|
||||
|
||||
typedef struct Widget Widget;
|
||||
|
||||
typedef void (*WidgetDrawCallback)(CanvasApi* api, void* context);
|
||||
typedef void (*WidgetInputCallback)(InputEvent* event, void* context);
|
||||
|
||||
Widget* widget_alloc();
|
||||
void widget_free(Widget* widget);
|
||||
|
||||
void widget_enabled_set(Widget* widget, bool enabled);
|
||||
bool widget_is_enabled(Widget* widget);
|
||||
|
||||
void widget_draw_callback_set(Widget* widget, WidgetDrawCallback callback, void* context);
|
||||
void widget_input_callback_set(Widget* widget, WidgetInputCallback callback, void* context);
|
||||
|
||||
// emit update signal
|
||||
void widget_update(Widget* widget);
|
||||
9
applications/gui/widget_i.h
Normal file
9
applications/gui/widget_i.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "gui_i.h"
|
||||
|
||||
void widget_gui_set(Widget* widget, Gui* gui);
|
||||
|
||||
void widget_draw(Widget* widget, CanvasApi* canvas_api);
|
||||
|
||||
void widget_input(Widget* widget, InputEvent* event);
|
||||
217
applications/menu/menu.c
Normal file
217
applications/menu/menu.c
Normal file
@@ -0,0 +1,217 @@
|
||||
#include "menu.h"
|
||||
#include <cmsis_os.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <flipper_v2.h>
|
||||
#include <gui/gui.h>
|
||||
|
||||
#include "menu_event.h"
|
||||
#include "menu_item.h"
|
||||
|
||||
struct Menu {
|
||||
MenuEvent* event;
|
||||
|
||||
// GUI
|
||||
Widget* widget;
|
||||
|
||||
// State
|
||||
MenuItem* root;
|
||||
MenuItem* settings;
|
||||
MenuItem* current;
|
||||
uint32_t position;
|
||||
};
|
||||
|
||||
void menu_widget_callback(CanvasApi* canvas, void* context);
|
||||
|
||||
ValueMutex* menu_init() {
|
||||
Menu* menu = furi_alloc(sizeof(Menu));
|
||||
|
||||
// Event dispatcher
|
||||
menu->event = menu_event_alloc();
|
||||
|
||||
ValueMutex* menu_mutex = furi_alloc(sizeof(ValueMutex));
|
||||
if(menu_mutex == NULL || !init_mutex(menu_mutex, menu, sizeof(Menu))) {
|
||||
printf("[menu_task] cannot create menu mutex\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
// Allocate and configure widget
|
||||
menu->widget = widget_alloc();
|
||||
|
||||
// Open GUI and register fullscreen widget
|
||||
GuiApi* gui = furi_open("gui");
|
||||
assert(gui);
|
||||
gui->add_widget(gui, menu->widget, WidgetLayerFullscreen);
|
||||
|
||||
widget_draw_callback_set(menu->widget, menu_widget_callback, menu_mutex);
|
||||
widget_input_callback_set(menu->widget, menu_event_input_callback, menu->event);
|
||||
|
||||
return menu_mutex;
|
||||
}
|
||||
|
||||
void menu_build_main(Menu* menu) {
|
||||
assert(menu);
|
||||
// Root point
|
||||
menu->root = menu_item_alloc_menu(NULL, NULL);
|
||||
|
||||
menu->settings = menu_item_alloc_menu("Setting", NULL);
|
||||
menu_item_subitem_add(menu->settings, menu_item_alloc_function("one", NULL, NULL, NULL));
|
||||
menu_item_subitem_add(menu->settings, menu_item_alloc_function("two", NULL, NULL, NULL));
|
||||
menu_item_subitem_add(menu->settings, menu_item_alloc_function("three", NULL, NULL, NULL));
|
||||
|
||||
menu_item_add(menu, menu->settings);
|
||||
}
|
||||
|
||||
void menu_item_add(Menu* menu, MenuItem* item) {
|
||||
menu_item_subitem_add(menu->root, item);
|
||||
}
|
||||
|
||||
void menu_settings_item_add(Menu* menu, MenuItem* item) {
|
||||
menu_item_subitem_add(menu->settings, item);
|
||||
}
|
||||
|
||||
void menu_widget_callback(CanvasApi* canvas, void* context) {
|
||||
assert(canvas);
|
||||
assert(context);
|
||||
|
||||
Menu* menu = acquire_mutex((ValueMutex*)context, 100); // wait 10 ms to get mutex
|
||||
if(menu == NULL) return; // redraw fail
|
||||
|
||||
if(!menu->current) {
|
||||
canvas->clear(canvas);
|
||||
canvas->set_color(canvas, ColorBlack);
|
||||
canvas->set_font(canvas, FontPrimary);
|
||||
canvas->draw_str(canvas, 2, 32, "Idle Screen");
|
||||
} else {
|
||||
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
|
||||
canvas->clear(canvas);
|
||||
canvas->set_color(canvas, ColorBlack);
|
||||
canvas->set_font(canvas, FontSecondary);
|
||||
for(size_t i = 0; i < 5; i++) {
|
||||
size_t shift_position = i + menu->position + MenuItemArray_size(*items) - 2;
|
||||
shift_position = shift_position % (MenuItemArray_size(*items));
|
||||
MenuItem* item = *MenuItemArray_get(*items, shift_position);
|
||||
canvas->draw_str(canvas, 2, 12 * (i + 1), menu_item_get_label(item));
|
||||
}
|
||||
}
|
||||
|
||||
release_mutex((ValueMutex*)context, menu);
|
||||
}
|
||||
|
||||
void menu_update(Menu* menu) {
|
||||
assert(menu);
|
||||
|
||||
menu_event_activity_notify(menu->event);
|
||||
widget_update(menu->widget);
|
||||
}
|
||||
|
||||
void menu_up(Menu* menu) {
|
||||
assert(menu);
|
||||
|
||||
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
|
||||
if(menu->position == 0) menu->position = MenuItemArray_size(*items);
|
||||
menu->position--;
|
||||
menu_update(menu);
|
||||
}
|
||||
|
||||
void menu_down(Menu* menu) {
|
||||
assert(menu);
|
||||
|
||||
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
|
||||
menu->position++;
|
||||
menu->position = menu->position % MenuItemArray_size(*items);
|
||||
menu_update(menu);
|
||||
}
|
||||
|
||||
void menu_ok(Menu* menu) {
|
||||
assert(menu);
|
||||
|
||||
if(!menu->current) {
|
||||
menu->current = menu->root;
|
||||
menu_update(menu);
|
||||
return;
|
||||
}
|
||||
|
||||
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
|
||||
MenuItem* item = *MenuItemArray_get(*items, menu->position);
|
||||
MenuItemType type = menu_item_get_type(item);
|
||||
|
||||
if(type == MenuItemTypeMenu) {
|
||||
menu->current = item;
|
||||
menu->position = 0;
|
||||
menu_update(menu);
|
||||
} else if(type == MenuItemTypeFunction) {
|
||||
menu_item_function_call(item);
|
||||
}
|
||||
}
|
||||
|
||||
void menu_back(Menu* menu) {
|
||||
assert(menu);
|
||||
MenuItem* parent = menu_item_get_parent(menu->current);
|
||||
if(parent) {
|
||||
menu->current = parent;
|
||||
menu->position = 0;
|
||||
menu_update(menu);
|
||||
} else {
|
||||
menu_exit(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void menu_exit(Menu* menu) {
|
||||
assert(menu);
|
||||
menu->position = 0;
|
||||
menu->current = NULL;
|
||||
menu_update(menu);
|
||||
}
|
||||
|
||||
void menu_task(void* p) {
|
||||
ValueMutex* menu_mutex = menu_init();
|
||||
|
||||
MenuEvent* menu_event = NULL;
|
||||
{
|
||||
Menu* menu = acquire_mutex_block(menu_mutex);
|
||||
assert(menu);
|
||||
|
||||
menu_build_main(menu);
|
||||
|
||||
// immutable thread-safe object
|
||||
menu_event = menu->event;
|
||||
|
||||
release_mutex(menu_mutex, menu);
|
||||
}
|
||||
|
||||
if(!furi_create("menu", menu_mutex)) {
|
||||
printf("[menu_task] cannot create the menu record\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
furiac_ready();
|
||||
|
||||
while(1) {
|
||||
MenuMessage m = menu_event_next(menu_event);
|
||||
|
||||
Menu* menu = acquire_mutex_block(menu_mutex);
|
||||
|
||||
if(!menu->current && m.type != MenuMessageTypeOk) {
|
||||
} else if(m.type == MenuMessageTypeUp) {
|
||||
menu_up(menu);
|
||||
} else if(m.type == MenuMessageTypeDown) {
|
||||
menu_down(menu);
|
||||
} else if(m.type == MenuMessageTypeOk) {
|
||||
menu_ok(menu);
|
||||
} else if(m.type == MenuMessageTypeLeft) {
|
||||
menu_back(menu);
|
||||
} else if(m.type == MenuMessageTypeRight) {
|
||||
menu_ok(menu);
|
||||
} else if(m.type == MenuMessageTypeBack) {
|
||||
menu_back(menu);
|
||||
} else if(m.type == MenuMessageTypeIdle) {
|
||||
menu_exit(menu);
|
||||
} else {
|
||||
// TODO: fail somehow?
|
||||
}
|
||||
|
||||
release_mutex(menu_mutex, menu);
|
||||
}
|
||||
}
|
||||
19
applications/menu/menu.h
Normal file
19
applications/menu/menu.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "menu/menu_item.h"
|
||||
|
||||
typedef struct Menu Menu;
|
||||
typedef struct MenuItem MenuItem;
|
||||
|
||||
// Add menu item to root menu
|
||||
void menu_item_add(Menu* menu, MenuItem* item);
|
||||
|
||||
// Add menu item to settings menu
|
||||
void menu_settings_item_add(Menu* menu, MenuItem* item);
|
||||
|
||||
// Menu controls
|
||||
void menu_up(Menu* menu);
|
||||
void menu_down(Menu* menu);
|
||||
void menu_ok(Menu* menu);
|
||||
void menu_back(Menu* menu);
|
||||
void menu_exit(Menu* menu);
|
||||
79
applications/menu/menu_event.c
Normal file
79
applications/menu/menu_event.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "menu_event.h"
|
||||
|
||||
#include <cmsis_os.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <flipper.h>
|
||||
|
||||
#define MENU_MESSAGE_MQUEUE_SIZE 8
|
||||
|
||||
struct MenuEvent {
|
||||
osMessageQueueId_t mqueue;
|
||||
osTimerId_t timeout_timer;
|
||||
};
|
||||
|
||||
void MenuEventimeout_callback(void* arg) {
|
||||
MenuEvent* menu_event = arg;
|
||||
MenuMessage message;
|
||||
message.type = MenuMessageTypeIdle;
|
||||
osMessageQueuePut(menu_event->mqueue, &message, 0, osWaitForever);
|
||||
}
|
||||
|
||||
MenuEvent* menu_event_alloc() {
|
||||
MenuEvent* menu_event = furi_alloc(sizeof(MenuEvent));
|
||||
|
||||
menu_event->mqueue = osMessageQueueNew(MENU_MESSAGE_MQUEUE_SIZE, sizeof(MenuMessage), NULL);
|
||||
assert(menu_event->mqueue);
|
||||
|
||||
menu_event->timeout_timer =
|
||||
osTimerNew(MenuEventimeout_callback, osTimerOnce, menu_event, NULL);
|
||||
assert(menu_event->timeout_timer);
|
||||
|
||||
return menu_event;
|
||||
}
|
||||
|
||||
void menu_event_free(MenuEvent* menu_event) {
|
||||
assert(menu_event);
|
||||
assert(osMessageQueueDelete(menu_event->mqueue) == osOK);
|
||||
free(menu_event);
|
||||
}
|
||||
|
||||
void menu_event_activity_notify(MenuEvent* menu_event) {
|
||||
assert(menu_event);
|
||||
osTimerStart(menu_event->timeout_timer, 60000U); // 1m timeout, return to main
|
||||
}
|
||||
|
||||
MenuMessage menu_event_next(MenuEvent* menu_event) {
|
||||
assert(menu_event);
|
||||
MenuMessage message;
|
||||
while(osMessageQueueGet(menu_event->mqueue, &message, NULL, osWaitForever) != osOK) {
|
||||
};
|
||||
return message;
|
||||
}
|
||||
|
||||
void menu_event_input_callback(InputEvent* input_event, void* context) {
|
||||
MenuEvent* menu_event = context;
|
||||
MenuMessage message;
|
||||
|
||||
if(!input_event->state) return;
|
||||
|
||||
if(input_event->input == InputUp) {
|
||||
message.type = MenuMessageTypeUp;
|
||||
} else if(input_event->input == InputDown) {
|
||||
message.type = MenuMessageTypeDown;
|
||||
} else if(input_event->input == InputRight) {
|
||||
message.type = MenuMessageTypeRight;
|
||||
} else if(input_event->input == InputLeft) {
|
||||
message.type = MenuMessageTypeLeft;
|
||||
} else if(input_event->input == InputOk) {
|
||||
message.type = MenuMessageTypeOk;
|
||||
} else if(input_event->input == InputBack) {
|
||||
message.type = MenuMessageTypeBack;
|
||||
} else {
|
||||
message.type = MenuMessageTypeUnknown;
|
||||
}
|
||||
|
||||
osMessageQueuePut(menu_event->mqueue, &message, 0, osWaitForever);
|
||||
}
|
||||
32
applications/menu/menu_event.h
Normal file
32
applications/menu/menu_event.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <input/input.h>
|
||||
|
||||
typedef enum {
|
||||
MenuMessageTypeUp = 0x00,
|
||||
MenuMessageTypeDown = 0x01,
|
||||
MenuMessageTypeLeft = 0x02,
|
||||
MenuMessageTypeRight = 0x03,
|
||||
MenuMessageTypeOk = 0x04,
|
||||
MenuMessageTypeBack = 0x05,
|
||||
MenuMessageTypeIdle = 0x06,
|
||||
MenuMessageTypeUnknown = 0xFF,
|
||||
} MenuMessageType;
|
||||
|
||||
typedef struct {
|
||||
MenuMessageType type;
|
||||
void* data;
|
||||
} MenuMessage;
|
||||
|
||||
typedef struct MenuEvent MenuEvent;
|
||||
|
||||
MenuEvent* menu_event_alloc();
|
||||
|
||||
void menu_event_free(MenuEvent* menu_event);
|
||||
|
||||
void menu_event_activity_notify(MenuEvent* menu_event);
|
||||
|
||||
MenuMessage menu_event_next(MenuEvent* menu_event);
|
||||
|
||||
void menu_event_input_callback(InputEvent* input_event, void* context);
|
||||
107
applications/menu/menu_item.c
Normal file
107
applications/menu/menu_item.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "menu_item.h"
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <flipper.h>
|
||||
|
||||
struct MenuItem {
|
||||
MenuItemType type;
|
||||
const char* label;
|
||||
void* icon;
|
||||
MenuItem* parent;
|
||||
void* data;
|
||||
MenuItemCallback callback;
|
||||
void* callback_context;
|
||||
};
|
||||
|
||||
MenuItem* menu_item_alloc() {
|
||||
MenuItem* menu_item = furi_alloc(sizeof(MenuItem));
|
||||
return menu_item;
|
||||
}
|
||||
|
||||
MenuItem* menu_item_alloc_menu(const char* label, void* icon) {
|
||||
MenuItem* menu_item = menu_item_alloc();
|
||||
|
||||
menu_item->type = MenuItemTypeMenu;
|
||||
menu_item->label = label;
|
||||
menu_item->icon = icon;
|
||||
|
||||
MenuItemArray_t* items = furi_alloc(sizeof(MenuItemArray_t));
|
||||
MenuItemArray_init(*items);
|
||||
menu_item->data = items;
|
||||
|
||||
return menu_item;
|
||||
}
|
||||
|
||||
MenuItem*
|
||||
menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context) {
|
||||
MenuItem* menu_item = menu_item_alloc();
|
||||
|
||||
menu_item->type = MenuItemTypeFunction;
|
||||
menu_item->label = label;
|
||||
menu_item->icon = icon;
|
||||
menu_item->callback = callback;
|
||||
menu_item->callback_context = context;
|
||||
|
||||
return menu_item;
|
||||
}
|
||||
|
||||
void menu_item_release(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
if(menu_item->type == MenuItemTypeMenu) {
|
||||
//TODO: iterate and release
|
||||
free(menu_item->data);
|
||||
}
|
||||
free(menu_item);
|
||||
}
|
||||
|
||||
MenuItem* menu_item_get_parent(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
return menu_item->parent;
|
||||
}
|
||||
|
||||
void menu_item_subitem_add(MenuItem* menu_item, MenuItem* sub_item) {
|
||||
assert(menu_item);
|
||||
assert(menu_item->type == MenuItemTypeMenu);
|
||||
MenuItemArray_t* items = menu_item->data;
|
||||
sub_item->parent = menu_item;
|
||||
MenuItemArray_push_back(*items, sub_item);
|
||||
}
|
||||
|
||||
uint8_t menu_item_get_type(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
return menu_item->type;
|
||||
}
|
||||
|
||||
void menu_item_set_label(MenuItem* menu_item, const char* label) {
|
||||
assert(menu_item);
|
||||
menu_item->label = label;
|
||||
}
|
||||
|
||||
const char* menu_item_get_label(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
return menu_item->label;
|
||||
}
|
||||
|
||||
void menu_item_set_icon(MenuItem* menu_item, void* icon) {
|
||||
assert(menu_item);
|
||||
menu_item->icon = icon;
|
||||
}
|
||||
|
||||
void* menu_item_get_icon(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
return menu_item->icon;
|
||||
}
|
||||
|
||||
MenuItemArray_t* menu_item_get_subitems(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
assert(menu_item->type == MenuItemTypeMenu);
|
||||
return menu_item->data;
|
||||
}
|
||||
|
||||
void menu_item_function_call(MenuItem* menu_item) {
|
||||
assert(menu_item);
|
||||
assert(menu_item->type == MenuItemTypeFunction);
|
||||
|
||||
if(menu_item->callback) menu_item->callback(menu_item->callback_context);
|
||||
}
|
||||
37
applications/menu/menu_item.h
Normal file
37
applications/menu/menu_item.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <m-array.h>
|
||||
|
||||
typedef enum {
|
||||
MenuItemTypeMenu = 0x00,
|
||||
MenuItemTypeFunction = 0x01,
|
||||
} MenuItemType;
|
||||
|
||||
typedef struct MenuItem MenuItem;
|
||||
typedef void (*MenuItemCallback)(void* context);
|
||||
|
||||
ARRAY_DEF(MenuItemArray, MenuItem*, M_PTR_OPLIST);
|
||||
|
||||
MenuItem* menu_item_alloc_menu(const char* label, void* icon);
|
||||
|
||||
MenuItem*
|
||||
menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context);
|
||||
|
||||
void menu_item_release(MenuItem* menu_item);
|
||||
|
||||
MenuItem* menu_item_get_parent(MenuItem* menu_item);
|
||||
|
||||
void menu_item_subitem_add(MenuItem* menu_item, MenuItem* sub_item);
|
||||
|
||||
MenuItemType menu_item_get_type(MenuItem* menu_item);
|
||||
|
||||
void menu_item_set_label(MenuItem* menu_item, const char* label);
|
||||
const char* menu_item_get_label(MenuItem* menu_item);
|
||||
|
||||
void menu_item_set_icon(MenuItem* menu_item, void* icon);
|
||||
void* menu_item_get_icon(MenuItem* menu_item);
|
||||
|
||||
MenuItemArray_t* menu_item_get_subitems(MenuItem* menu_item);
|
||||
|
||||
void menu_item_function_call(MenuItem* menu_item);
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include "flipper.h"
|
||||
|
||||
#define FURI_LIB (const char*[])
|
||||
|
||||
#ifdef APP_TEST
|
||||
void flipper_test_app(void* p);
|
||||
#endif
|
||||
@@ -26,6 +24,9 @@ void cc1101_workaround(void* p);
|
||||
|
||||
void u8g2_qrcode(void* p);
|
||||
void fatfs_list(void* p);
|
||||
void gui_task(void* p);
|
||||
void backlight_control(void* p);
|
||||
void app_loader(void* p);
|
||||
|
||||
const FlipperStartupApp FLIPPER_STARTUP[] = {
|
||||
#ifdef APP_DISPLAY
|
||||
@@ -36,29 +37,27 @@ const FlipperStartupApp FLIPPER_STARTUP[] = {
|
||||
{.app = input_task, .name = "input_task", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_GUI
|
||||
{.app = backlight_control, .name = "backlight_control", .libs = {1, FURI_LIB{"input_task"}}},
|
||||
{.app = gui_task, .name = "gui_task", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_MENU
|
||||
{.app = menu_task, .name = "menu_task", .libs = {1, FURI_LIB{"gui_task"}}},
|
||||
{.app = app_loader, .name = "app_loader", .libs = {1, FURI_LIB{"menu_task"}}},
|
||||
#endif
|
||||
|
||||
// {.app = coreglitch_demo_0, .name = "coreglitch_demo_0", .libs = ""},
|
||||
|
||||
#ifdef APP_TEST
|
||||
{.app = flipper_test_app, .name = "test app", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_EXAMPLE_BLINK
|
||||
{.app = application_blink, .name = "blink", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_EXAMPLE_UART_WRITE
|
||||
{.app = application_uart_write, .name = "uart write", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_EXAMPLE_IPC
|
||||
{.app = application_ipc_display, .name = "ipc display", .libs = {0}},
|
||||
{.app = application_ipc_widget, .name = "ipc widget", .libs = {0}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_EXAMPLE_INPUT_DUMP
|
||||
{.app = application_input_dump, .name = "input dump", .libs = {1, FURI_LIB{"input_task"}}},
|
||||
#endif
|
||||
|
||||
#ifdef APP_EXAMPLE_QRCODE
|
||||
{.app = u8g2_qrcode, .name = "u8g2_qrcode", .libs = {1, FURI_LIB{"display_u8g2"}}},
|
||||
#endif
|
||||
|
||||
99
applications/tests/furi_memmgr_test.c
Normal file
99
applications/tests/furi_memmgr_test.c
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "minunit.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
// this test is not accurate, but gives a basic understanding
|
||||
// that memory management is working fine
|
||||
|
||||
// do not include memmgr.h here
|
||||
// we also test that we are linking against stdlib
|
||||
extern size_t memmgr_get_free_heap(void);
|
||||
extern size_t memmgr_get_minimum_free_heap(void);
|
||||
|
||||
// current heap managment realization consume:
|
||||
// X bytes after allocate and 0 bytes after allocate and free,
|
||||
// where X = sizeof(void*) + sizeof(size_t), look to BlockLink_t
|
||||
const size_t heap_overhead_max_size = sizeof(void*) + sizeof(size_t);
|
||||
|
||||
bool heap_equal(size_t heap_size, size_t heap_size_old) {
|
||||
// heap borders with overhead
|
||||
const size_t heap_low = heap_size_old - heap_overhead_max_size;
|
||||
const size_t heap_high = heap_size_old + heap_overhead_max_size;
|
||||
|
||||
// not extact, so we must test it against bigger numbers than "overhead size"
|
||||
const bool result = ((heap_size >= heap_low) && (heap_size <= heap_high));
|
||||
|
||||
// debug allocation info
|
||||
if(!result) {
|
||||
printf("\n(hl: %zu) <= (p: %zu) <= (hh: %zu)\n", heap_low, heap_size, heap_high);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void test_furi_memmgr() {
|
||||
size_t heap_size = 0;
|
||||
size_t heap_size_old = 0;
|
||||
const int alloc_size = 128;
|
||||
|
||||
void* ptr = NULL;
|
||||
void* original_ptr = NULL;
|
||||
|
||||
// do not include furi memmgr.h case
|
||||
#ifdef FURI_MEMMGR_GUARD
|
||||
mu_fail("do not link against furi memmgr.h");
|
||||
#endif
|
||||
|
||||
// allocate memory case
|
||||
heap_size_old = memmgr_get_free_heap();
|
||||
ptr = malloc(alloc_size);
|
||||
heap_size = memmgr_get_free_heap();
|
||||
mu_assert_pointers_not_eq(ptr, NULL);
|
||||
mu_assert(heap_equal(heap_size, heap_size_old - alloc_size), "allocate failed");
|
||||
|
||||
// free memory case
|
||||
heap_size_old = memmgr_get_free_heap();
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
heap_size = memmgr_get_free_heap();
|
||||
mu_assert(heap_equal(heap_size, heap_size_old + alloc_size), "free failed");
|
||||
|
||||
// reallocate memory case
|
||||
|
||||
// get filled array with some data
|
||||
original_ptr = malloc(alloc_size);
|
||||
mu_assert_pointers_not_eq(original_ptr, NULL);
|
||||
for(int i = 0; i < alloc_size; i++) {
|
||||
*(unsigned char*)(original_ptr + i) = i;
|
||||
}
|
||||
|
||||
// malloc array and copy data
|
||||
ptr = malloc(alloc_size);
|
||||
mu_assert_pointers_not_eq(ptr, NULL);
|
||||
memcpy(ptr, original_ptr, alloc_size);
|
||||
|
||||
// reallocate array
|
||||
heap_size_old = memmgr_get_free_heap();
|
||||
ptr = realloc(ptr, alloc_size * 2);
|
||||
heap_size = memmgr_get_free_heap();
|
||||
mu_assert(heap_equal(heap_size, heap_size_old - alloc_size), "reallocate failed");
|
||||
mu_assert_int_eq(memcmp(original_ptr, ptr, alloc_size), 0);
|
||||
free(original_ptr);
|
||||
free(ptr);
|
||||
|
||||
// allocate and zero-initialize array (calloc)
|
||||
original_ptr = malloc(alloc_size);
|
||||
mu_assert_pointers_not_eq(original_ptr, NULL);
|
||||
|
||||
for(int i = 0; i < alloc_size; i++) {
|
||||
*(unsigned char*)(original_ptr + i) = 0;
|
||||
}
|
||||
heap_size_old = memmgr_get_free_heap();
|
||||
ptr = calloc(1, alloc_size);
|
||||
heap_size = memmgr_get_free_heap();
|
||||
mu_assert(heap_equal(heap_size, heap_size_old - alloc_size), "callocate failed");
|
||||
mu_assert_int_eq(memcmp(original_ptr, ptr, alloc_size), 0);
|
||||
|
||||
free(original_ptr);
|
||||
free(ptr);
|
||||
}
|
||||
56
applications/tests/furi_pubsub_test.c
Normal file
56
applications/tests/furi_pubsub_test.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "flipper_v2.h"
|
||||
#include "log.h"
|
||||
|
||||
#include "minunit.h"
|
||||
|
||||
const uint32_t context_value = 0xdeadbeef;
|
||||
const uint32_t notify_value_0 = 0x12345678;
|
||||
const uint32_t notify_value_1 = 0x11223344;
|
||||
|
||||
uint32_t pubsub_value = 0;
|
||||
uint32_t pubsub_context_value = 0;
|
||||
|
||||
void test_pubsub_handler(void* arg, void* ctx) {
|
||||
pubsub_value = *(uint32_t*)arg;
|
||||
pubsub_context_value = *(uint32_t*)ctx;
|
||||
}
|
||||
|
||||
void test_furi_pubsub() {
|
||||
bool result;
|
||||
PubSub test_pubsub;
|
||||
PubSubItem* test_pubsub_item;
|
||||
|
||||
// init pubsub case
|
||||
result = init_pubsub(&test_pubsub);
|
||||
mu_assert(result, "init pubsub failed");
|
||||
|
||||
// subscribe pubsub case
|
||||
test_pubsub_item = subscribe_pubsub(&test_pubsub, test_pubsub_handler, (void*)&context_value);
|
||||
mu_assert_pointers_not_eq(test_pubsub_item, NULL);
|
||||
|
||||
/// notify pubsub case
|
||||
result = notify_pubsub(&test_pubsub, (void*)¬ify_value_0);
|
||||
mu_assert(result, "notify pubsub failed");
|
||||
mu_assert_int_eq(pubsub_value, notify_value_0);
|
||||
mu_assert_int_eq(pubsub_context_value, context_value);
|
||||
|
||||
// unsubscribe pubsub case
|
||||
result = unsubscribe_pubsub(test_pubsub_item);
|
||||
mu_assert(result, "unsubscribe pubsub failed");
|
||||
|
||||
result = unsubscribe_pubsub(test_pubsub_item);
|
||||
mu_assert(!result, "unsubscribe pubsub not failed");
|
||||
|
||||
/// notify unsubscribed pubsub case
|
||||
result = notify_pubsub(&test_pubsub, (void*)¬ify_value_1);
|
||||
mu_assert(result, "notify pubsub failed");
|
||||
mu_assert_int_not_eq(pubsub_value, notify_value_1);
|
||||
|
||||
// delete pubsub case
|
||||
result = delete_pubsub(&test_pubsub);
|
||||
mu_assert(result, "unsubscribe pubsub failed");
|
||||
|
||||
// TODO test case that the pubsub_delete will remove pubsub from heap
|
||||
}
|
||||
@@ -14,197 +14,3 @@ void test_furi_create_open() {
|
||||
void* record = furi_open("test/holding");
|
||||
mu_assert_pointers_eq(record, &test_data);
|
||||
}
|
||||
|
||||
/*
|
||||
TEST: non-existent data
|
||||
1. Try to open non-existent record
|
||||
2. Check for NULL handler
|
||||
3. Try to write/read, get error
|
||||
|
||||
TODO: implement this test
|
||||
*/
|
||||
bool test_furi_nonexistent_data() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
TEST: mute algorithm
|
||||
1. Create "parent" application:
|
||||
1. Create pipe record
|
||||
2. Open watch handler: no_mute=false, solo=false, subscribe to data.
|
||||
|
||||
2. Open handler A: no_mute=false, solo=false, NULL subscriber. Subscribe to state.
|
||||
Try to write data to A and check subscriber.
|
||||
|
||||
3. Open handler B: no_mute=true, solo=true, NULL subscriber.
|
||||
Check A state cb get FlipperRecordStateMute.
|
||||
Try to write data to A and check that subscriber get no data. (muted)
|
||||
Try to write data to B and check that subscriber get data.
|
||||
|
||||
TODO: test 3 not pass beacuse state callback not implemented
|
||||
|
||||
4. Open hadler C: no_mute=false, solo=true, NULL subscriber.
|
||||
Try to write data to A and check that subscriber get no data. (muted)
|
||||
Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
|
||||
Try to write data to C and check that subscriber get data.
|
||||
|
||||
5. Open handler D: no_mute=false, solo=false, NULL subscriber.
|
||||
Try to write data to A and check that subscriber get no data. (muted)
|
||||
Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
|
||||
Try to write data to C and check that subscriber get data. (not muted because D open without solo)
|
||||
Try to write data to D and check that subscriber get data.
|
||||
|
||||
6. Close C, close B.
|
||||
Check A state cb get FlipperRecordStateUnmute
|
||||
Try to write data to A and check that subscriber get data. (unmuted)
|
||||
Try to write data to D and check that subscriber get data.
|
||||
|
||||
TODO: test 6 not pass beacuse cleanup is not implemented
|
||||
TODO: test 6 not pass because mute algorithm is unfinished.
|
||||
|
||||
7. Exit "parent application"
|
||||
Check A state cb get FlipperRecordStateDeleted
|
||||
|
||||
TODO: test 7 not pass beacuse cleanup is not implemented
|
||||
*/
|
||||
|
||||
static uint8_t mute_last_value = 0;
|
||||
static FlipperRecordState mute_last_state = 255;
|
||||
|
||||
void mute_record_cb(const void* value, size_t size, void* ctx) {
|
||||
// hold value to static var
|
||||
mute_last_value = *((uint8_t*)value);
|
||||
}
|
||||
|
||||
void mute_record_state_cb(FlipperRecordState state, void* ctx) {
|
||||
mute_last_state = state;
|
||||
}
|
||||
|
||||
void furi_mute_parent_app(void* p) {
|
||||
// 1. Create pipe record
|
||||
if(!furi_create_deprecated("test/mute", NULL, 0)) {
|
||||
printf("cannot create record\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
// 2. Open watch handler: solo=false, no_mute=false, subscribe to data
|
||||
FuriRecordSubscriber* watch_handler =
|
||||
furi_open_deprecated("test/mute", false, false, mute_record_cb, NULL, NULL);
|
||||
if(watch_handler == NULL) {
|
||||
printf("cannot open watch handler\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
while(1) {
|
||||
// TODO we don't have thread sleep
|
||||
delay(100000);
|
||||
}
|
||||
}
|
||||
|
||||
bool test_furi_mute_algorithm() {
|
||||
// 1. Create "parent" application:
|
||||
FuriApp* parent_app = furiac_start(furi_mute_parent_app, "parent app", NULL);
|
||||
|
||||
delay(2); // wait creating record
|
||||
|
||||
// 2. Open handler A: solo=false, no_mute=false, NULL subscriber. Subscribe to state.
|
||||
FuriRecordSubscriber* handler_a =
|
||||
furi_open_deprecated("test/mute", false, false, NULL, mute_record_state_cb, NULL);
|
||||
if(handler_a == NULL) {
|
||||
printf("cannot open handler A\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t test_counter = 1;
|
||||
|
||||
// Try to write data to A and check subscriber
|
||||
if(!furi_write(handler_a, &test_counter, sizeof(uint8_t))) {
|
||||
printf("write to A failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(mute_last_value != test_counter) {
|
||||
printf("value A mismatch: %d vs %d\n", mute_last_value, test_counter);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. Open handler B: solo=true, no_mute=true, NULL subscriber.
|
||||
FuriRecordSubscriber* handler_b =
|
||||
furi_open_deprecated("test/mute", true, true, NULL, NULL, NULL);
|
||||
if(handler_b == NULL) {
|
||||
printf("cannot open handler B\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check A state cb get FlipperRecordStateMute.
|
||||
if(mute_last_state != FlipperRecordStateMute) {
|
||||
printf("A state is not FlipperRecordStateMute: %d\n", mute_last_state);
|
||||
return false;
|
||||
}
|
||||
|
||||
test_counter = 2;
|
||||
|
||||
// Try to write data to A and check that subscriber get no data. (muted)
|
||||
if(furi_write(handler_a, &test_counter, sizeof(uint8_t))) {
|
||||
printf("A not muted\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(mute_last_value == test_counter) {
|
||||
printf("value A must be muted\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
test_counter = 3;
|
||||
|
||||
// Try to write data to B and check that subscriber get data.
|
||||
if(!furi_write(handler_b, &test_counter, sizeof(uint8_t))) {
|
||||
printf("write to B failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(mute_last_value != test_counter) {
|
||||
printf("value B mismatch: %d vs %d\n", mute_last_value, test_counter);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. Open hadler C: solo=true, no_mute=false, NULL subscriber.
|
||||
FuriRecordSubscriber* handler_c =
|
||||
furi_open_deprecated("test/mute", true, false, NULL, NULL, NULL);
|
||||
if(handler_c == NULL) {
|
||||
printf("cannot open handler C\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Try to write data to A and check that subscriber get no data. (muted)
|
||||
// TODO: Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
|
||||
// TODO: Try to write data to C and check that subscriber get data.
|
||||
|
||||
// 5. Open handler D: solo=false, no_mute=false, NULL subscriber.
|
||||
FuriRecordSubscriber* handler_d =
|
||||
furi_open_deprecated("test/mute", false, false, NULL, NULL, NULL);
|
||||
if(handler_d == NULL) {
|
||||
printf("cannot open handler D\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Try to write data to A and check that subscriber get no data. (muted)
|
||||
// TODO: Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
|
||||
// TODO: Try to write data to C and check that subscriber get data. (not muted because D open without solo)
|
||||
// TODO: Try to write data to D and check that subscriber get data.
|
||||
|
||||
// 6. Close C, close B.
|
||||
// TODO: Check A state cb get FlipperRecordStateUnmute
|
||||
// TODO: Try to write data to A and check that subscriber get data. (unmuted)
|
||||
// TODO: Try to write data to D and check that subscriber get data.
|
||||
|
||||
// 7. Exit "parent application"
|
||||
if(!furiac_kill(parent_app)) {
|
||||
printf("kill parent_app fail\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Check A state cb get FlipperRecordStateDeleted
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -7,13 +7,13 @@
|
||||
bool test_furi_ac_create_kill();
|
||||
bool test_furi_ac_switch_exit();
|
||||
|
||||
bool test_furi_nonexistent_data();
|
||||
bool test_furi_mute_algorithm();
|
||||
|
||||
// v2 tests
|
||||
void test_furi_create_open();
|
||||
void test_furi_valuemutex();
|
||||
void test_furi_concurrent_access();
|
||||
void test_furi_pubsub();
|
||||
|
||||
void test_furi_memmgr();
|
||||
|
||||
static int foo = 0;
|
||||
|
||||
@@ -37,10 +37,6 @@ MU_TEST(mu_test_furi_ac_switch_exit) {
|
||||
mu_assert_int_eq(test_furi_ac_switch_exit(), true);
|
||||
}
|
||||
|
||||
MU_TEST(mu_test_furi_nonexistent_data) {
|
||||
mu_assert_int_eq(test_furi_nonexistent_data(), true);
|
||||
}
|
||||
|
||||
// v2 tests
|
||||
MU_TEST(mu_test_furi_create_open) {
|
||||
test_furi_create_open();
|
||||
@@ -54,6 +50,16 @@ MU_TEST(mu_test_furi_concurrent_access) {
|
||||
test_furi_concurrent_access();
|
||||
}
|
||||
|
||||
MU_TEST(mu_test_furi_pubsub) {
|
||||
test_furi_pubsub();
|
||||
}
|
||||
|
||||
MU_TEST(mu_test_furi_memmgr) {
|
||||
// this test is not accurate, but gives a basic understanding
|
||||
// that memory management is working fine
|
||||
test_furi_memmgr();
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(test_suite) {
|
||||
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
|
||||
|
||||
@@ -61,12 +67,13 @@ MU_TEST_SUITE(test_suite) {
|
||||
MU_RUN_TEST(mu_test_furi_ac_create_kill);
|
||||
MU_RUN_TEST(mu_test_furi_ac_switch_exit);
|
||||
|
||||
MU_RUN_TEST(mu_test_furi_nonexistent_data);
|
||||
|
||||
// v2 tests
|
||||
MU_RUN_TEST(mu_test_furi_create_open);
|
||||
MU_RUN_TEST(mu_test_furi_valuemutex);
|
||||
MU_RUN_TEST(mu_test_furi_concurrent_access);
|
||||
MU_RUN_TEST(mu_test_furi_pubsub);
|
||||
|
||||
MU_RUN_TEST(mu_test_furi_memmgr);
|
||||
}
|
||||
|
||||
int run_minunit() {
|
||||
|
||||
Reference in New Issue
Block a user