unleashed-firmware/applications/menu/menu.c

273 lines
7.6 KiB
C
Raw Normal View History

#include "menu.h"
#include <stdio.h>
#include <stdbool.h>
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
#include <furi.h>
#include <gui/gui.h>
#include <gui/elements.h>
#include "menu_event.h"
#include "menu_item.h"
#include <assets_icons.h>
struct Menu {
MenuEvent* event;
2020-10-15 15:56:47 +00:00
// GUI
Gui* gui;
ViewPort* view_port;
Icon* icon;
2020-10-15 15:57:21 +00:00
// State
MenuItem* root;
MenuItem* settings;
MenuItem* current;
};
void menu_view_port_callback(Canvas* canvas, void* context);
2020-10-17 08:48:29 +00:00
ValueMutex* menu_init() {
Menu* menu = furi_alloc(sizeof(Menu));
// Event dispatcher
menu->event = menu_event_alloc();
2020-10-17 08:48:29 +00:00
ValueMutex* menu_mutex = furi_alloc(sizeof(ValueMutex));
if(menu_mutex == NULL || !init_mutex(menu_mutex, menu, sizeof(Menu))) {
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311) * FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started. * Furi record, stdglue: check mutex allocation * remove unused test * Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state. * Interrupt manager: explicitly init list. * Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options. * BLE: call debug uart callback on transmission complete * FreeRTOS: add configUSE_NEWLIB_REENTRANT * API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib. * F4: update stack protection CFLAGS to match used compiller * F4: disable compiller stack protection because of incompatibility with current compiller * Makefile: return openocd logs to gdb * BLE: fixed pin, moar power, ble trace info. * Prevent sleep when connection is active * Makefile: return serial port to upload rule, add workaround for mac os * Furi: prevent usage of stack for cmsis functions. * F4: add missing includes, add debugger breakpoints * Applications: per app stack size. * Furi: honor kernel state in stdglue * FreeRTOS: remove unused hooks * Cleanup and format sources Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
printf("[menu_task] cannot create menu mutex\r\n");
furi_check(0);
2020-10-17 08:48:29 +00:00
}
// OpenGui record
menu->gui = furi_record_open("gui");
// Allocate and configure view_port
menu->view_port = view_port_alloc();
// Open GUI and register fullscreen view_port
gui_add_view_port(menu->gui, menu->view_port, GuiLayerFullscreen);
view_port_enabled_set(menu->view_port, false);
view_port_draw_callback_set(menu->view_port, menu_view_port_callback, menu_mutex);
view_port_input_callback_set(menu->view_port, menu_event_input_callback, menu->event);
2020-10-17 08:48:29 +00:00
return menu_mutex;
}
void menu_build_main(Menu* menu) {
furi_assert(menu);
// Root point
menu->root = menu_item_alloc_menu(NULL, NULL);
}
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_draw_primary(Menu* menu, Canvas* canvas) {
}
void menu_draw_secondary(Menu* menu, Canvas* canvas) {
}
void menu_view_port_callback(Canvas* canvas, void* context) {
furi_assert(canvas);
furi_assert(context);
2020-10-17 10:41:50 +00:00
2020-10-17 08:48:29 +00:00
Menu* menu = acquire_mutex((ValueMutex*)context, 100); // wait 10 ms to get mutex
if(menu == NULL) return; // redraw fail
furi_assert(menu->current);
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
size_t position = menu_item_get_position(menu->current);
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
size_t items_count = MenuItemArray_size(*items);
if(items_count) {
MenuItem* item;
size_t shift_position;
// First line
canvas_set_font(canvas, FontSecondary);
shift_position = (0 + position + items_count - 1) % (MenuItemArray_size(*items));
item = *MenuItemArray_get(*items, shift_position);
canvas_draw_icon(canvas, 4, 3, menu_item_get_icon(item));
canvas_draw_str(canvas, 22, 14, menu_item_get_label(item));
// Second line main
canvas_set_font(canvas, FontPrimary);
shift_position = (1 + position + items_count - 1) % (MenuItemArray_size(*items));
item = *MenuItemArray_get(*items, shift_position);
canvas_draw_icon(canvas, 4, 25, menu_item_get_icon(item));
canvas_draw_str(canvas, 22, 36, menu_item_get_label(item));
// Third line
canvas_set_font(canvas, FontSecondary);
shift_position = (2 + position + items_count - 1) % (MenuItemArray_size(*items));
item = *MenuItemArray_get(*items, shift_position);
canvas_draw_icon(canvas, 4, 47, menu_item_get_icon(item));
canvas_draw_str(canvas, 22, 58, menu_item_get_label(item));
// Frame and scrollbar
// elements_frame(canvas, 0, 0, 128 - 5, 21);
elements_frame(canvas, 0, 21, 128 - 5, 21);
// elements_frame(canvas, 0, 42, 128 - 5, 21);
elements_scrollbar(canvas, position, items_count);
} else {
canvas_draw_str(canvas, 2, 32, "Empty");
elements_scrollbar(canvas, 0, 0);
}
2020-10-17 08:48:29 +00:00
release_mutex((ValueMutex*)context, menu);
}
void menu_set_icon(Menu* menu, Icon* icon) {
assert(menu);
if(menu->icon) {
icon_stop_animation(menu->icon);
}
menu->icon = icon;
if(menu->icon) {
icon_start_animation(menu->icon);
}
}
void menu_update(Menu* menu) {
furi_assert(menu);
if(menu->current) {
size_t position = menu_item_get_position(menu->current);
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
size_t items_count = MenuItemArray_size(*items);
if(items_count) {
MenuItem* item = *MenuItemArray_get(*items, position);
menu_set_icon(menu, menu_item_get_icon(item));
}
}
menu_event_activity_notify(menu->event);
view_port_update(menu->view_port);
}
void menu_up(Menu* menu) {
furi_assert(menu);
size_t position = menu_item_get_position(menu->current);
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
if(position == 0) position = MenuItemArray_size(*items);
position--;
menu_item_set_position(menu->current, position);
menu_update(menu);
}
void menu_down(Menu* menu) {
furi_assert(menu);
size_t position = menu_item_get_position(menu->current);
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
position++;
position = position % MenuItemArray_size(*items);
menu_item_set_position(menu->current, position);
menu_update(menu);
}
void menu_ok(Menu* menu) {
furi_assert(menu);
if(!menu->current) {
view_port_enabled_set(menu->view_port, true);
menu->current = menu->root;
menu_item_set_position(menu->current, 0);
menu_update(menu);
return;
}
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
if(!items || MenuItemArray_size(*items) == 0) {
return;
}
size_t position = menu_item_get_position(menu->current);
MenuItem* item = *MenuItemArray_get(*items, position);
MenuItemType type = menu_item_get_type(item);
if(type == MenuItemTypeMenu) {
menu->current = item;
menu_item_set_position(menu->current, 0);
menu_update(menu);
} else if(type == MenuItemTypeFunction) {
menu_item_function_call(item);
gui_send_view_port_back(menu->gui, menu->view_port);
}
}
void menu_back(Menu* menu) {
furi_assert(menu);
MenuItem* parent = menu_item_get_parent(menu->current);
if(parent) {
menu->current = parent;
menu_update(menu);
} else {
menu_exit(menu);
}
}
void menu_exit(Menu* menu) {
furi_assert(menu);
view_port_enabled_set(menu->view_port, false);
menu->current = NULL;
menu_update(menu);
}
int32_t menu_task(void* p) {
2020-10-17 08:48:29 +00:00
ValueMutex* menu_mutex = menu_init();
2020-10-17 08:48:29 +00:00
MenuEvent* menu_event = NULL;
{
Menu* menu = acquire_mutex_block(menu_mutex);
furi_check(menu);
2020-10-17 08:48:29 +00:00
menu_build_main(menu);
// immutable thread-safe object
menu_event = menu->event;
release_mutex(menu_mutex, menu);
}
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
furi_record_create("menu", menu_mutex);
2020-10-15 07:38:20 +00:00
while(1) {
2020-10-17 08:48:29 +00:00
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?
}
2020-10-17 08:48:29 +00:00
release_mutex(menu_mutex, menu);
}
return 0;
}