Display and UI implementation (#169)

* Menu app. Lib: add mlib submodule.

* Menu: new startup lib dependency definition

* Menu: hierarchy in menu. Cube: fix heap1/4 inconsistency, stack protection.

* GUI: rendering pipeline initial version.

* GUI: layered widget composing, FURI record. Menu: FURI record, api.

* GUI: input dispatching. Menu: switch to input from GUI.

* GUI, MENU: code style cleanup, fix type conversion warnings.

* GUI, Menu: syntax check.

* Makefile: check and reinit submodules, format.

* Menu: lock on event processing. Makefile: proper submodule initialization.

* Menu: fix stack corruption by queue.

* GUI: refactor.

* Makefile: format rule fix, st-util pid.

* GUI, Menu, FURI: format with clang-format.

* GUI, MENU: locks in critical sections, fix stack corruption, ready signaling.

* Makefile: clang format rule cleanup.

* GUI,MENU: migrate to new API.

* Applications: PRODUCTION_HW variable, skip drivers build on local target.

* refactor production build

* add git to dockerfile

* GUI: uncomment lock block

Co-authored-by: Aleksandr Kutuzov <aku@plooks.com>
This commit is contained in:
coreglitch 2020-10-14 16:21:55 +06:00 committed by GitHub
parent 1b85fa69af
commit b2a12d091a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 1122 additions and 28 deletions

View File

@ -5,8 +5,17 @@ CFLAGS += -I$(APP_DIR)
APP_RELEASE ?= 0
ifeq ($(APP_RELEASE), 1)
APP_DISPLAY = 1
APP_INPUT = 1
APP_DISPLAY = 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)
endif
APP_TEST ?= 0
@ -68,6 +77,12 @@ APP_DISPLAY = 1
endif
# device drivers
APP_GUI ?= 0
ifeq ($(APP_GUI), 1)
APP_DISPLAY = 1
CFLAGS += -DAPP_GUI
C_SOURCES += $(wildcard $(APP_DIR)/gui/*.c)
endif
ifeq ($(APP_DISPLAY), 1)
CFLAGS += -DAPP_DISPLAY
@ -78,4 +93,4 @@ APP_INPUT ?= 0
ifeq ($(APP_INPUT), 1)
CFLAGS += -DAPP_INPUT
C_SOURCES += $(APP_DIR)/input/input.c
endif
endif

79
applications/gui/canvas.c Normal file
View File

@ -0,0 +1,79 @@
#include "canvas.h"
#include "canvas_i.h"
#include <assert.h>
#include <flipper.h>
#include <u8g2.h>
struct Canvas {
FuriRecordSubscriber* fb_record;
u8g2_t* fb;
uint8_t offset_x;
uint8_t offset_y;
uint8_t width;
uint8_t height;
};
Canvas* canvas_alloc() {
Canvas* canvas = furi_alloc(sizeof(Canvas));
canvas->fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
assert(canvas->fb_record);
return canvas;
}
void canvas_free(Canvas* canvas) {
assert(canvas);
free(canvas);
}
void canvas_commit(Canvas* canvas) {
assert(canvas);
if(canvas->fb) {
furi_commit(canvas->fb_record);
canvas->fb = NULL;
}
}
void canvas_frame_set(
Canvas* canvas,
uint8_t offset_x,
uint8_t offset_y,
uint8_t width,
uint8_t height) {
assert(canvas);
canvas->offset_x = offset_x;
canvas->offset_y = offset_y;
canvas->width = width;
canvas->height = height;
}
u8g2_t* canvas_fb(Canvas* canvas) {
if(!canvas->fb) {
canvas->fb = furi_take(canvas->fb_record);
assert(canvas->fb);
}
return canvas->fb;
}
void canvas_clear(Canvas* canvas) {
u8g2_t* fb = canvas_fb(canvas);
u8g2_ClearBuffer(fb);
}
void canvas_color_set(Canvas* canvas, uint8_t color) {
u8g2_t* fb = canvas_fb(canvas);
u8g2_SetDrawColor(fb, 1);
}
void canvas_font_set(Canvas* canvas, font_t font) {
u8g2_t* fb = canvas_fb(canvas);
u8g2_SetFontMode(fb, 1);
u8g2_SetFont(fb, font);
}
void canvas_str_draw(Canvas* canvas, uint8_t x, uint8_t y, const char* str) {
x += canvas->offset_x;
y += canvas->offset_y;
u8g2_t* fb = canvas_fb(canvas);
u8g2_DrawStr(fb, x, y, str);
}

24
applications/gui/canvas.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
#include <u8g2.h>
#define COLOR_WHITE 0x00
#define COLOR_BLACK 0x01
#define CANVAS_FONT_PRIMARY u8g2_font_Born2bSportyV2_tr
#define CANVAS_FONT_SECONDARY u8g2_font_HelvetiPixel_tr
typedef struct Canvas Canvas;
typedef const uint8_t* font_t;
uint8_t canvas_width(Canvas* canvas);
uint8_t canvas_height(Canvas* canvas);
void canvas_clear(Canvas* canvas);
void canvas_color_set(Canvas* canvas, uint8_t color);
void canvas_font_set(Canvas* canvas, font_t font);
void canvas_str_draw(Canvas* canvas, uint8_t x, uint8_t y, const char* str);

View File

@ -0,0 +1,14 @@
#pragma once
Canvas* canvas_alloc();
void canvas_free(Canvas* canvas);
void canvas_commit(Canvas* canvas);
void canvas_frame_set(
Canvas* canvas,
uint8_t offset_x,
uint8_t offset_y,
uint8_t width,
uint8_t height);

176
applications/gui/gui.c Normal file
View File

@ -0,0 +1,176 @@
#include "gui.h"
#include "gui_i.h"
#include <flipper.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 {
GUIEvent* event;
Canvas* canvas;
WidgetArray_t widgets_status_bar;
WidgetArray_t widgets;
WidgetArray_t widgets_fs;
WidgetArray_t widgets_dialog;
};
void gui_widget_status_bar_add(GUI* gui, Widget* widget) {
assert(gui);
assert(widget);
gui_event_lock(gui->event);
WidgetArray_push_back(gui->widgets_status_bar, widget);
widget_gui_set(widget, gui);
gui_event_unlock(gui->event);
gui_update(gui);
}
void gui_widget_add(GUI* gui, Widget* widget) {
assert(gui);
assert(widget);
gui_event_lock(gui->event);
WidgetArray_push_back(gui->widgets, widget);
widget_gui_set(widget, gui);
gui_event_unlock(gui->event);
gui_update(gui);
}
void gui_widget_fs_add(GUI* gui, Widget* widget) {
assert(gui);
assert(widget);
gui_event_lock(gui->event);
WidgetArray_push_back(gui->widgets_fs, widget);
widget_gui_set(widget, gui);
gui_event_unlock(gui->event);
gui_update(gui);
}
void gui_widget_dialog_add(GUI* gui, Widget* widget) {
assert(gui);
assert(widget);
gui_event_lock(gui->event);
WidgetArray_push_back(gui->widgets_dialog, 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, 0, 0, 128, 64);
Widget* widget = gui_widget_find_enabled(gui->widgets_fs);
if(widget) {
widget_draw(widget, gui->canvas);
return true;
} else {
return false;
}
}
void gui_redraw_status_bar(GUI* gui) {
canvas_frame_set(gui->canvas, 0, 0, 128, 64);
Widget* widget = gui_widget_find_enabled(gui->widgets_status_bar);
if(widget) widget_draw(widget, gui->canvas);
}
void gui_redraw_normal(GUI* gui) {
canvas_frame_set(gui->canvas, 0, 9, 128, 55);
Widget* widget = gui_widget_find_enabled(gui->widgets);
if(widget) widget_draw(widget, gui->canvas);
}
void gui_redraw_dialogs(GUI* gui) {
canvas_frame_set(gui->canvas, 10, 20, 118, 44);
Widget* widget = gui_widget_find_enabled(gui->widgets_dialog);
if(widget) widget_draw(widget, gui->canvas);
}
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);
}
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
gui->canvas = canvas_alloc();
return gui;
}
void gui_task(void* p) {
GUI* gui = gui_alloc();
// Create FURI record
if(!furi_create_deprecated("gui", gui, sizeof(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);
}
}
}

12
applications/gui/gui.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
typedef struct Widget Widget;
typedef struct GUI GUI;
void gui_widget_status_bar_add(GUI* gui, Widget* widget);
void gui_widget_add(GUI* gui, Widget* widget);
void gui_widget_fs_add(GUI* gui, Widget* widget);
void gui_widget_dialog_add(GUI* gui, Widget* widget);

View 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, 0);
}
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);
while(osMessageQueueGet(gui_event->mqueue, &message, NULL, osWaitForever) != osOK) {
};
gui_event_lock(gui_event);
return message;
}

View 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);

3
applications/gui/gui_i.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void gui_update(GUI* gui);

79
applications/gui/widget.c Normal file
View File

@ -0,0 +1,79 @@
#include "widget.h"
#include "widget_i.h"
#include <cmsis_os.h>
#include <flipper.h>
#include "gui.h"
#include "gui_i.h"
struct Widget {
void* 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, Canvas* canvas) {
assert(widget);
assert(canvas);
assert(widget->gui);
if(widget->draw_callback) widget->draw_callback(canvas, 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);
}

22
applications/gui/widget.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <input/input.h>
typedef struct GUI GUI;
typedef struct Canvas Canvas;
typedef struct Widget Widget;
typedef void (*WidgetDrawCallback)(Canvas* canvas, 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);

View File

@ -0,0 +1,7 @@
#pragma once
void widget_gui_set(Widget* widget, GUI* gui);
void widget_draw(Widget* widget, Canvas* canvas);
void widget_input(Widget* widget, InputEvent* event);

214
applications/menu/menu.c Normal file
View File

@ -0,0 +1,214 @@
#include "menu.h"
#include <cmsis_os.h>
#include <stdio.h>
#include <stdbool.h>
#include <flipper.h>
#include <gui/gui.h>
#include <gui/canvas.h>
#include <gui/widget.h>
#include "menu_event.h"
#include "menu_item.h"
struct Menu {
MenuEvent* event;
// GUI
FuriRecordSubscriber* gui_record;
Widget* widget;
// State
MenuItem* root;
MenuItem* settings;
MenuItem* current;
uint32_t position;
};
void menu_widget_callback(Canvas* canvas, void* context);
Menu* menu_alloc() {
Menu* menu = furi_alloc(sizeof(Menu));
// Event dispatcher
menu->event = menu_event_alloc();
// Allocate and configure widget
menu->widget = widget_alloc();
widget_draw_callback_set(menu->widget, menu_widget_callback, menu);
widget_input_callback_set(menu->widget, menu_event_input_callback, menu->event);
// Open GUI and register fullscreen widget
menu->gui_record = furi_open_deprecated("gui", false, false, NULL, NULL, NULL);
assert(menu->gui_record);
return menu;
}
void menu_build_main(Menu* menu) {
assert(menu);
// Root point
menu->root = menu_item_alloc_menu(NULL, NULL);
menu_item_add(menu, menu_item_alloc_function("Sub 1 gHz", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("125 kHz RFID", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("Infrared", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("I-Button", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("USB", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("Bluetooth", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("GPIO / HW", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("NFC", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("U2F", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("Tamagotchi", NULL, NULL));
menu_item_add(menu, menu_item_alloc_function("Plugins", NULL, NULL));
menu->settings = menu_item_alloc_menu("Setting", NULL);
menu_item_subitem_add(menu->settings, menu_item_alloc_function("one", NULL, NULL));
menu_item_subitem_add(menu->settings, menu_item_alloc_function("two", NULL, NULL));
menu_item_subitem_add(menu->settings, menu_item_alloc_function("three", 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(Canvas* canvas, void* context) {
assert(canvas);
assert(context);
Menu* menu = context;
menu_event_lock(menu->event);
if(!menu->current) {
canvas_clear(canvas);
canvas_color_set(canvas, COLOR_BLACK);
canvas_font_set(canvas, CANVAS_FONT_PRIMARY);
canvas_str_draw(canvas, 2, 32, "Idle Screen");
} else {
MenuItemArray_t* items = menu_item_get_subitems(menu->current);
canvas_clear(canvas);
canvas_color_set(canvas, COLOR_BLACK);
canvas_font_set(canvas, CANVAS_FONT_SECONDARY);
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_str_draw(canvas, 2, 12 * (i + 1), menu_item_get_label(item));
}
}
menu_event_unlock(menu->event);
}
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) {
MenuItemCallback function = menu_item_get_function(item);
if(function) function();
}
}
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) {
Menu* menu = menu_alloc();
menu_build_main(menu);
// Register widget
GUI* gui = furi_take(menu->gui_record);
assert(gui);
gui_widget_fs_add(gui, menu->widget);
furi_commit(menu->gui_record);
if(!furi_create_deprecated("menu", menu, sizeof(menu))) {
printf("[menu_task] cannot create the menu record\n");
furiac_exit(NULL);
}
furiac_ready();
while(1) {
MenuMessage m = menu_event_next(menu->event);
if(!menu->current && m.type != MenuMessageTypeOk) {
continue;
} 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?
}
}
}

17
applications/menu/menu.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
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);

View File

@ -0,0 +1,96 @@
#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;
osMutexId_t lock_mutex;
};
void MenuEventimeout_callback(void* arg) {
MenuEvent* menu_event = arg;
MenuMessage message;
message.type = MenuMessageTypeIdle;
osMessageQueuePut(menu_event->mqueue, &message, 0, 0);
}
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);
menu_event->lock_mutex = osMutexNew(NULL);
assert(menu_event->lock_mutex);
menu_event_lock(menu_event);
return menu_event;
}
void menu_event_free(MenuEvent* menu_event) {
assert(menu_event);
menu_event_unlock(menu_event);
assert(osMessageQueueDelete(menu_event->mqueue) == osOK);
free(menu_event);
}
void menu_event_lock(MenuEvent* menu_event) {
assert(osMutexAcquire(menu_event->lock_mutex, osWaitForever) == osOK);
}
void menu_event_unlock(MenuEvent* menu_event) {
assert(osMutexRelease(menu_event->lock_mutex) == osOK);
}
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;
menu_event_unlock(menu_event);
while(osMessageQueueGet(menu_event->mqueue, &message, NULL, osWaitForever) != osOK) {
};
menu_event_lock(menu_event);
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, 0);
}

View File

@ -0,0 +1,36 @@
#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_lock(MenuEvent* menu_event);
void menu_event_unlock(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);

View File

@ -0,0 +1,89 @@
#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;
};
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 function) {
MenuItem* menu_item = menu_item_alloc();
menu_item->type = MenuItemTypeFunction;
menu_item->label = label;
menu_item->icon = icon;
menu_item->data = function;
return menu_item;
}
void menu_item_release(MenuItem* menu_item) {
if(menu_item->type == MenuItemTypeMenu) free(menu_item->data);
free(menu_item);
}
MenuItem* menu_item_get_parent(MenuItem* menu_item) {
return menu_item->parent;
}
void menu_item_subitem_add(MenuItem* menu_item, MenuItem* sub_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) {
return menu_item->type;
}
void menu_item_set_label(MenuItem* menu_item, const char* label) {
menu_item->label = label;
}
const char* menu_item_get_label(MenuItem* menu_item) {
return menu_item->label;
}
void menu_item_set_icon(MenuItem* menu_item, void* icon) {
menu_item->icon = icon;
}
void* menu_item_get_icon(MenuItem* menu_item) {
return menu_item->icon;
}
MenuItemArray_t* menu_item_get_subitems(MenuItem* menu_item) {
assert(menu_item->type == MenuItemTypeMenu);
return menu_item->data;
}
MenuItemCallback menu_item_get_function(MenuItem* menu_item) {
assert(menu_item->type == MenuItemTypeFunction);
return menu_item->data;
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <stdint.h>
#include <m-array.h>
typedef enum {
MenuItemTypeMenu = 0x00,
MenuItemTypeFunction = 0x01,
} MenuItemType;
typedef struct MenuItem MenuItem;
typedef void (*MenuItemCallback)();
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 function);
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);
MenuItemCallback menu_item_get_function(MenuItem* menu_item);

View File

@ -25,6 +25,7 @@ void coreglitch_demo_0(void* p);
void u8g2_qrcode(void* p);
void fatfs_list(void* p);
void gui_task(void* p);
const FlipperStartupApp FLIPPER_STARTUP[] = {
#ifdef APP_DISPLAY
@ -35,6 +36,14 @@ const FlipperStartupApp FLIPPER_STARTUP[] = {
{.app = input_task, .name = "input_task", .libs = {0}},
#endif
#ifdef APP_GUI
{.app = gui_task, .name = "gui_task", .libs = {1, FURI_LIB{"display_u8g2"}}},
#endif
#ifdef APP_MENU
{.app = menu_task, .name = "menu_task", .libs = {1, FURI_LIB{"gui_task"}}},
#endif
// {.app = coreglitch_demo_0, .name = "coreglitch_demo_0", .libs = ""},
#ifdef APP_TEST

View File

@ -1,15 +1,26 @@
#pragma once
#include "cmsis_os.h"
#ifdef HAVE_FREERTOS
#include <semphr.h>
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#define MAX_TASK_RECORDS 8
#define MAX_RECORD_SUBSCRIBERS 8
inline static void* furi_alloc(size_t size) {
void* p = malloc(size);
assert(p);
return memset(p, 0, size);
}
/// application is just a function
typedef void (*FlipperApplication)(void*);

View File

@ -21,6 +21,7 @@ RUN apt update && \
libstdc++-arm-none-eabi-newlib \
libclang-10-dev \
clang-format-10 \
git \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --profile=minimal --target thumbv7em-none-eabi thumbv7em-none-eabihf && \
@ -31,11 +32,11 @@ RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --profile=minimal --target thum
RUN apt update && \
apt install -y --no-install-recommends \
gcc build-essential cmake libusb-1.0 libusb-1.0-0-dev libgtk-3-dev pandoc \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN wget https://github.com/stlink-org/stlink/archive/v1.5.1.zip
RUN unzip v1.5.1.zip
RUN cd stlink-1.5.1 && make clean && make release
RUN cd stlink-1.5.1/build/Release && make install && ldconfig
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
wget https://github.com/stlink-org/stlink/archive/v1.5.1.zip && \
unzip v1.5.1.zip && \
cd stlink-1.5.1 && make clean && make release && \
cd build/Release && make install && ldconfig
COPY entrypoint.sh syntax_check.sh /

View File

@ -51,6 +51,10 @@
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
/* USER CODE BEGIN 0 */
extern void configureTimerForRunTimeStats(void);
extern unsigned long getRunTimeCounterValue(void);
/* USER CODE END 0 */
#endif
#define configENABLE_FPU 0
#define configENABLE_MPU 0
@ -64,15 +68,18 @@
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES ( 56 )
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
#define configTOTAL_HEAP_SIZE ((size_t)8192)
#define configTOTAL_HEAP_SIZE ((size_t)40960)
#define configMAX_TASK_NAME_LEN ( 16 )
#define configGENERATE_RUN_TIME_STATS 1
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 1
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
#define configRECORD_STACK_HIGH_ADDRESS 1
/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
/* Defaults to size_t for backward compatibility, but can be changed
if lengths will always be less than the number of bytes in a size_t. */
@ -152,6 +159,12 @@ standard names. */
#define xPortSysTickHandler SysTick_Handler
/* USER CODE BEGIN 2 */
/* Definitions needed when configGENERATE_RUN_TIME_STATS is on */
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS configureTimerForRunTimeStats
#define portGET_RUN_TIME_COUNTER_VALUE getRunTimeCounterValue
/* USER CODE END 2 */
/* USER CODE BEGIN Defines */
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
/* USER CODE END Defines */

View File

@ -26,7 +26,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -75,7 +75,23 @@ extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);
void vApplicationIdleHook(void);
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName);
/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{
}
__weak unsigned long getRunTimeCounterValue(void)
{
return 0;
}
/* USER CODE END 1 */
/* USER CODE BEGIN 2 */
__weak void vApplicationIdleHook( void )
@ -92,6 +108,16 @@ __weak void vApplicationIdleHook( void )
}
/* USER CODE END 2 */
/* USER CODE BEGIN 4 */
__weak void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
called if a stack overflow is detected. */
exit(255);
}
/* USER CODE END 4 */
/**
* @brief FreeRTOS initialization
* @param None

View File

@ -86,7 +86,9 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
if ((*(volatile uint32_t *)CoreDebug_BASE) & (1 << 0)) {
__asm("bkpt 1");
}
/* USER CODE END HardFault_IRQn 0 */
while (1)
{

View File

@ -97,6 +97,7 @@ PB14.GPIOParameters=GPIO_Speed,PinState,GPIO_Label,GPIO_ModeDefaultOutputPP
NVIC.EXTI2_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true
RCC.PLLPoutputFreq_Value=18285714.285714287
RCC.APB1TimFreq_Value=64000000
FREERTOS.configGENERATE_RUN_TIME_STATS=1
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
RCC.LPUART1Freq_Value=64000000
USB_OTG_FS.IPParameters=VirtualMode
@ -123,6 +124,7 @@ PC13.Locked=true
ADC1.OffsetNumber-0\#ChannelRegularConversion=ADC_OFFSET_NONE
PC13.Signal=GPXTI13
RCC.SWPMI1Freq_Value=64000000
FREERTOS.configCHECK_FOR_STACK_OVERFLOW=1
PB8.GPIO_PuPd=GPIO_PULLDOWN
PC6.Signal=GPIO_Output
PC2.Signal=GPXTI2
@ -182,8 +184,8 @@ SPI1.Mode=SPI_MODE_MASTER
Mcu.Pin39=PA15 (JTDI)
PB3\ (JTDO-TRACESWO).Mode=TX_Only_Simplex_Unidirect_Master
RCC.RNGFreq_Value=48000000
VP_ADC1_TempSens_Input.Signal=ADC1_TempSens_Input
PC2.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI
VP_ADC1_TempSens_Input.Signal=ADC1_TempSens_Input
Mcu.Pin30=PC8
PA1.GPIO_Label=BUTTON_DOWN
Mcu.Pin33=PA9
@ -310,7 +312,7 @@ PB9.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING
Mcu.Pin7=PC2
Mcu.Pin8=PC3
Mcu.Pin9=PA0
FREERTOS.IPParameters=Tasks01,configTOTAL_HEAP_SIZE,HEAP_NUMBER,configUSE_TIMERS,configUSE_IDLE_HOOK,FootprintOK
FREERTOS.IPParameters=Tasks01,configTOTAL_HEAP_SIZE,HEAP_NUMBER,configUSE_TIMERS,configUSE_IDLE_HOOK,FootprintOK,configCHECK_FOR_STACK_OVERFLOW,configRECORD_STACK_HIGH_ADDRESS,configGENERATE_RUN_TIME_STATS
RCC.AHBFreq_Value=64000000
Mcu.Pin0=PC13
SPI3.DataSize=SPI_DATASIZE_8BIT
@ -342,7 +344,7 @@ PB7.Signal=GPIO_Input
PB8.Locked=true
PB6.GPIOParameters=GPIO_Speed,GPIO_Label
PB0.Locked=true
FREERTOS.configTOTAL_HEAP_SIZE=8192
FREERTOS.configTOTAL_HEAP_SIZE=40960
VP_COMP1_VS_VREFINT12.Mode=VREFINT_12
ProjectManager.ProjectName=cube
PB1.PinState=GPIO_PIN_SET
@ -405,6 +407,7 @@ PA9.Mode=Asynchronous
PB4\ (NJTRST).GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI
NVIC.TIM8_CC_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true
PB14.PinState=GPIO_PIN_SET
FREERTOS.configRECORD_STACK_HIGH_ADDRESS=1
ProjectManager.TargetToolchain=Makefile
PB10.GPIO_Label=DISPLAY_RST
PB7.GPIOParameters=GPIO_Label

View File

@ -57,7 +57,7 @@ C_SOURCES += \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/tasks.c \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/timers.c \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_1.c \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c \
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c \
$(CUBE_DIR)/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c \
$(CUBE_DIR)/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c \

View File

@ -1,5 +1,6 @@
LIB_DIR = $(PROJECT_ROOT)/lib
# TODO: some places use lib/header.h includes, is it ok?
CFLAGS += -I$(LIB_DIR)
# Mlib containers

View File

@ -14,12 +14,19 @@ DEPS = $(OBJECTS:.o=.d)
$(shell mkdir -p $(OBJ_DIR))
BUILD_FLAGS_SHELL=\
echo -n "$(CFLAGS)" > $(OBJ_DIR)/BUILD_FLAGS.tmp; \
diff $(OBJ_DIR)/BUILD_FLAGS $(OBJ_DIR)/BUILD_FLAGS.tmp > /dev/null \
echo "$(CFLAGS)" > $(OBJ_DIR)/BUILD_FLAGS.tmp; \
diff $(OBJ_DIR)/BUILD_FLAGS $(OBJ_DIR)/BUILD_FLAGS.tmp 2>/dev/null \
&& ( echo "CFLAGS ok"; rm $(OBJ_DIR)/BUILD_FLAGS.tmp) \
|| ( echo "CFLAGS has been changed"; mv $(OBJ_DIR)/BUILD_FLAGS.tmp $(OBJ_DIR)/BUILD_FLAGS )
$(info $(shell $(BUILD_FLAGS_SHELL)))
CHECK_AND_REINIT_SUBMODULES_SHELL=\
if git submodule status | egrep -q '^[-]|^[+]' ; then \
echo "INFO: Need to reinitialize git submodules"; \
git submodule update --init; \
fi
$(info $(shell $(CHECK_AND_REINIT_SUBMODULES_SHELL)))
all: $(OBJ_DIR)/$(PROJECT).elf $(OBJ_DIR)/$(PROJECT).hex $(OBJ_DIR)/$(PROJECT).bin
$(OBJ_DIR)/$(PROJECT).elf: $(OBJECTS)
@ -60,25 +67,18 @@ flash: $(OBJ_DIR)/flash
upload: $(OBJ_DIR)/upload
debug: flash
set -m; st-util -n --semihosting & echo $$! > st-util.PID
arm-none-eabi-gdb \
set -m; st-util -n --semihosting & echo $$! > $(OBJ_DIR)/st-util.PID
arm-none-eabi-gdb-py \
-ex "target extended-remote 127.0.0.1:4242" \
-ex "set confirm off" \
$(OBJ_DIR)/$(PROJECT).elf; \
kill `cat st-util.PID`; \
rm st-util.PID
kill `cat $(OBJ_DIR)/st-util.PID`; \
rm $(OBJ_DIR)/st-util.PID
clean:
@echo "\tCLEAN\t"
@$(RM) $(OBJ_DIR)/*
.PHONY: check-and-reinit-submodules
check-and-reinit-submodules:
@if git submodule status | egrep -q '^[-]|^[+]' ; then \
echo "INFO: Need to reinitialize git submodules"; \
git submodule update --init; \
fi
z: clean
$(MAKE) all
@ -88,4 +88,11 @@ zz: clean
zzz: clean
$(MAKE) debug
FORMAT_SOURCES := $(shell find ../applications -iname "*.h" -o -iname "*.c" -o -iname "*.cpp")
FORMAT_SOURCES += $(shell find ../core -iname "*.h" -o -iname "*.c" -o -iname "*.cpp")
format:
@echo "Formatting sources with clang-format"
@clang-format -style=file -i $(FORMAT_SOURCES)
-include $(DEPS)