From 1b85fa69af93371124252133df66fc4f1ee67112 Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Tue, 13 Oct 2020 21:55:04 +1000 Subject: [PATCH 01/26] Build firmware for target without bootloader (#173) * fixed inline functions for modern C standart * Build firmware for mcu without bootloader --- .../targets/f2/STM32L476RGTx_FLASH_NO_BOOT.ld | 204 ++++++++++++++++++ firmware/targets/f2/Src/system_stm32l4xx.c | 21 +- firmware/targets/f2/target.mk | 14 ++ 3 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 firmware/targets/f2/STM32L476RGTx_FLASH_NO_BOOT.ld diff --git a/firmware/targets/f2/STM32L476RGTx_FLASH_NO_BOOT.ld b/firmware/targets/f2/STM32L476RGTx_FLASH_NO_BOOT.ld new file mode 100644 index 00000000..ad48c14f --- /dev/null +++ b/firmware/targets/f2/STM32L476RGTx_FLASH_NO_BOOT.ld @@ -0,0 +1,204 @@ +/* +****************************************************************************** +** + +** File : LinkerScript.ld +** +** Author : Auto-generated by System Workbench for STM32 +** +** Abstract : Linker script for STM32L476RGTx series +** 1024Kbytes FLASH and 128Kbytes RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** Distribution: The file is distributed “as is,” without any warranty +** of any kind. +** +***************************************************************************** +** @attention +** +**

© COPYRIGHT(c) 2019 STMicroelectronics

+** +** Redistribution and use in source and binary forms, with or without modification, +** are permitted provided that the following conditions are met: +** 1. Redistributions of source code must retain the above copyright notice, +** this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright notice, +** this list of conditions and the following disclaimer in the documentation +** and/or other materials provided with the distribution. +** 3. Neither the name of STMicroelectronics nor the names of its contributors +** may be used to endorse or promote products derived from this software +** without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* Highest address of the user mode stack */ +_estack = 0x20018000; /* end of RAM */ +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0x200; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ + +/* Specify the memory areas */ +MEMORY +{ +RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K +RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K +FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(8); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(8); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(8); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(8); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + /* Constant data goes into FLASH */ + .rodata : + { + . = ALIGN(8); + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + . = ALIGN(8); + } >FLASH + + .ARM.extab : + { + . = ALIGN(8); + *(.ARM.extab* .gnu.linkonce.armextab.*) + . = ALIGN(8); + } >FLASH + .ARM : { + . = ALIGN(8); + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + . = ALIGN(8); + } >FLASH + + .preinit_array : + { + . = ALIGN(8); + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + . = ALIGN(8); + } >FLASH + + .init_array : + { + . = ALIGN(8); + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + . = ALIGN(8); + } >FLASH + .fini_array : + { + . = ALIGN(8); + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + . = ALIGN(8); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = LOADADDR(.data); + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : + { + . = ALIGN(8); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(8); + _edata = .; /* define a global symbol at data end */ + } >RAM AT> FLASH + + + /* Uninitialized data section */ + . = ALIGN(4); + .bss : + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + } >RAM + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack : + { + . = ALIGN(8); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(8); + } >RAM + + + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + + .ARM.attributes 0 : { *(.ARM.attributes) } +} + + diff --git a/firmware/targets/f2/Src/system_stm32l4xx.c b/firmware/targets/f2/Src/system_stm32l4xx.c index 6406ce5a..abe29aa3 100644 --- a/firmware/targets/f2/Src/system_stm32l4xx.c +++ b/firmware/targets/f2/Src/system_stm32l4xx.c @@ -123,25 +123,32 @@ /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ + +#ifdef NO_BOOTLOADER +#define VECT_TAB_OFFSET 0x0000 /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#else #define VECT_TAB_OFFSET 0x8000 /*!< Vector Table base offset field. This value must be a multiple of 0x200. */ -/******************************************************************************/ -/** +#endif + + /******************************************************************************/ + /** * @} */ -/** @addtogroup STM32L4xx_System_Private_Macros + /** @addtogroup STM32L4xx_System_Private_Macros * @{ */ -/** + /** * @} */ -/** @addtogroup STM32L4xx_System_Private_Variables + /** @addtogroup STM32L4xx_System_Private_Variables * @{ */ - /* The SystemCoreClock variable is updated in three ways: + /* The SystemCoreClock variable is updated in three ways: 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetHCLKFreq() 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency @@ -149,7 +156,7 @@ is no need to call the 2 first functions listed above, since SystemCoreClock variable is updated automatically. */ - uint32_t SystemCoreClock = 4000000U; + uint32_t SystemCoreClock = 4000000U; const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; diff --git a/firmware/targets/f2/target.mk b/firmware/targets/f2/target.mk index 3558d8a1..c30c0710 100644 --- a/firmware/targets/f2/target.mk +++ b/firmware/targets/f2/target.mk @@ -5,6 +5,15 @@ FW_ADDRESS = 0x08008000 OS_OFFSET = 0x00008000 FLASH_ADDRESS = 0x08008000 +NO_BOOTLOADER ?= 0 +ifeq ($(NO_BOOTLOADER), 1) +BOOT_ADDRESS = 0x08000000 +FW_ADDRESS = 0x08000000 +OS_OFFSET = 0x00000000 +FLASH_ADDRESS = 0x08000000 +CFLAGS += -DNO_BOOTLOADER +endif + BOOT_CFLAGS = -DBOOT_ADDRESS=$(BOOT_ADDRESS) -DFW_ADDRESS=$(FW_ADDRESS) -DOS_OFFSET=$(OS_OFFSET) MCU_FLAGS = -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard @@ -65,7 +74,12 @@ CFLAGS += \ -DHAVE_FREERTOS \ -DBUTON_INVERT=false \ -DDEBUG_UART=huart1 + +ifeq ($(NO_BOOTLOADER), 1) +LDFLAGS += -T$(TARGET_DIR)/STM32L476RGTx_FLASH_NO_BOOT.ld +else LDFLAGS += -T$(TARGET_DIR)/STM32L476RGTx_FLASH.ld +endif CFLAGS += \ -I$(TARGET_DIR)/Inc \ From b2a12d091a74758ae1a4eb3d69887c7144546b0a Mon Sep 17 00:00:00 2001 From: coreglitch Date: Wed, 14 Oct 2020 16:21:55 +0600 Subject: [PATCH 02/26] 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 --- applications/applications.mk | 21 ++- applications/gui/canvas.c | 79 +++++++++ applications/gui/canvas.h | 24 +++ applications/gui/canvas_i.h | 14 ++ applications/gui/gui.c | 176 +++++++++++++++++++ applications/gui/gui.h | 12 ++ applications/gui/gui_event.c | 73 ++++++++ applications/gui/gui_event.h | 29 +++ applications/gui/gui_i.h | 3 + applications/gui/widget.c | 79 +++++++++ applications/gui/widget.h | 22 +++ applications/gui/widget_i.h | 7 + applications/menu/menu.c | 214 +++++++++++++++++++++++ applications/menu/menu.h | 17 ++ applications/menu/menu_event.c | 96 ++++++++++ applications/menu/menu_event.h | 36 ++++ applications/menu/menu_item.c | 89 ++++++++++ applications/menu/menu_item.h | 36 ++++ applications/startup.h | 9 + core/furi-deprecated.h | 11 ++ docker/Dockerfile | 11 +- firmware/targets/f2/Inc/FreeRTOSConfig.h | 15 +- firmware/targets/f2/Src/freertos.c | 28 ++- firmware/targets/f2/Src/stm32l4xx_it.c | 4 +- firmware/targets/f2/cube.ioc | 9 +- firmware/targets/f2/target.mk | 2 +- lib/lib.mk | 1 + make/rules.mk | 33 ++-- 28 files changed, 1122 insertions(+), 28 deletions(-) create mode 100644 applications/gui/canvas.c create mode 100644 applications/gui/canvas.h create mode 100644 applications/gui/canvas_i.h create mode 100644 applications/gui/gui.c create mode 100644 applications/gui/gui.h create mode 100644 applications/gui/gui_event.c create mode 100644 applications/gui/gui_event.h create mode 100644 applications/gui/gui_i.h create mode 100644 applications/gui/widget.c create mode 100644 applications/gui/widget.h create mode 100644 applications/gui/widget_i.h create mode 100644 applications/menu/menu.c create mode 100644 applications/menu/menu.h create mode 100644 applications/menu/menu_event.c create mode 100644 applications/menu/menu_event.h create mode 100644 applications/menu/menu_item.c create mode 100644 applications/menu/menu_item.h diff --git a/applications/applications.mk b/applications/applications.mk index 71db1d39..906f6690 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -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 \ No newline at end of file +endif diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c new file mode 100644 index 00000000..6f137284 --- /dev/null +++ b/applications/gui/canvas.c @@ -0,0 +1,79 @@ +#include "canvas.h" +#include "canvas_i.h" + +#include +#include +#include + +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); +} diff --git a/applications/gui/canvas.h b/applications/gui/canvas.h new file mode 100644 index 00000000..3b0f547e --- /dev/null +++ b/applications/gui/canvas.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +#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); diff --git a/applications/gui/canvas_i.h b/applications/gui/canvas_i.h new file mode 100644 index 00000000..c24b2102 --- /dev/null +++ b/applications/gui/canvas_i.h @@ -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); diff --git a/applications/gui/gui.c b/applications/gui/gui.c new file mode 100644 index 00000000..ecc314d7 --- /dev/null +++ b/applications/gui/gui.c @@ -0,0 +1,176 @@ +#include "gui.h" +#include "gui_i.h" + +#include +#include +#include + +#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); + } + } +} diff --git a/applications/gui/gui.h b/applications/gui/gui.h new file mode 100644 index 00000000..a95b802c --- /dev/null +++ b/applications/gui/gui.h @@ -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); diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c new file mode 100644 index 00000000..1d7caf64 --- /dev/null +++ b/applications/gui/gui_event.c @@ -0,0 +1,73 @@ +#include "gui_event.h" + +#include +#include + +#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; +} diff --git a/applications/gui/gui_event.h b/applications/gui/gui_event.h new file mode 100644 index 00000000..c3a47e45 --- /dev/null +++ b/applications/gui/gui_event.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +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); diff --git a/applications/gui/gui_i.h b/applications/gui/gui_i.h new file mode 100644 index 00000000..e12970fc --- /dev/null +++ b/applications/gui/gui_i.h @@ -0,0 +1,3 @@ +#pragma once + +void gui_update(GUI* gui); diff --git a/applications/gui/widget.c b/applications/gui/widget.c new file mode 100644 index 00000000..bff2a4b9 --- /dev/null +++ b/applications/gui/widget.c @@ -0,0 +1,79 @@ +#include "widget.h" +#include "widget_i.h" + +#include +#include + +#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); +} diff --git a/applications/gui/widget.h b/applications/gui/widget.h new file mode 100644 index 00000000..b29ade45 --- /dev/null +++ b/applications/gui/widget.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +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); diff --git a/applications/gui/widget_i.h b/applications/gui/widget_i.h new file mode 100644 index 00000000..5c1af22d --- /dev/null +++ b/applications/gui/widget_i.h @@ -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); diff --git a/applications/menu/menu.c b/applications/menu/menu.c new file mode 100644 index 00000000..f551c6af --- /dev/null +++ b/applications/menu/menu.c @@ -0,0 +1,214 @@ +#include "menu.h" +#include +#include +#include + +#include +#include +#include +#include + +#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? + } + } +} diff --git a/applications/menu/menu.h b/applications/menu/menu.h new file mode 100644 index 00000000..01c68be2 --- /dev/null +++ b/applications/menu/menu.h @@ -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); diff --git a/applications/menu/menu_event.c b/applications/menu/menu_event.c new file mode 100644 index 00000000..82b32f8f --- /dev/null +++ b/applications/menu/menu_event.c @@ -0,0 +1,96 @@ +#include "menu_event.h" + +#include +#include +#include +#include + +#include + +#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); +} diff --git a/applications/menu/menu_event.h b/applications/menu/menu_event.h new file mode 100644 index 00000000..858dc27b --- /dev/null +++ b/applications/menu/menu_event.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +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); diff --git a/applications/menu/menu_item.c b/applications/menu/menu_item.c new file mode 100644 index 00000000..57072bc2 --- /dev/null +++ b/applications/menu/menu_item.c @@ -0,0 +1,89 @@ +#include "menu_item.h" +#include +#include +#include +#include + +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; +} diff --git a/applications/menu/menu_item.h b/applications/menu/menu_item.h new file mode 100644 index 00000000..57b81124 --- /dev/null +++ b/applications/menu/menu_item.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +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); diff --git a/applications/startup.h b/applications/startup.h index d5c00d48..2f96ce08 100644 --- a/applications/startup.h +++ b/applications/startup.h @@ -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 diff --git a/core/furi-deprecated.h b/core/furi-deprecated.h index 981146fa..5f401bbe 100644 --- a/core/furi-deprecated.h +++ b/core/furi-deprecated.h @@ -1,15 +1,26 @@ #pragma once #include "cmsis_os.h" + #ifdef HAVE_FREERTOS #include #endif + #include +#include #include +#include +#include #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*); diff --git a/docker/Dockerfile b/docker/Dockerfile index 7da59dee..e6cb146e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 / diff --git a/firmware/targets/f2/Inc/FreeRTOSConfig.h b/firmware/targets/f2/Inc/FreeRTOSConfig.h index 313a8a5a..7cc5c1aa 100644 --- a/firmware/targets/f2/Inc/FreeRTOSConfig.h +++ b/firmware/targets/f2/Inc/FreeRTOSConfig.h @@ -51,6 +51,10 @@ #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) #include 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 */ diff --git a/firmware/targets/f2/Src/freertos.c b/firmware/targets/f2/Src/freertos.c index 571f95cb..bede73d6 100644 --- a/firmware/targets/f2/Src/freertos.c +++ b/firmware/targets/f2/Src/freertos.c @@ -26,7 +26,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ - +#include /* 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 diff --git a/firmware/targets/f2/Src/stm32l4xx_it.c b/firmware/targets/f2/Src/stm32l4xx_it.c index 6dfe949a..2a94913e 100644 --- a/firmware/targets/f2/Src/stm32l4xx_it.c +++ b/firmware/targets/f2/Src/stm32l4xx_it.c @@ -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) { diff --git a/firmware/targets/f2/cube.ioc b/firmware/targets/f2/cube.ioc index 82ebbba1..dcabbc67 100644 --- a/firmware/targets/f2/cube.ioc +++ b/firmware/targets/f2/cube.ioc @@ -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 diff --git a/firmware/targets/f2/target.mk b/firmware/targets/f2/target.mk index c30c0710..f9a5e109 100644 --- a/firmware/targets/f2/target.mk +++ b/firmware/targets/f2/target.mk @@ -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 \ diff --git a/lib/lib.mk b/lib/lib.mk index 25a8ec02..f4e3359c 100644 --- a/lib/lib.mk +++ b/lib/lib.mk @@ -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 diff --git a/make/rules.mk b/make/rules.mk index 1c70fdff..44ab372b 100644 --- a/make/rules.mk +++ b/make/rules.mk @@ -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) From d33e16dae1bea25b7cc170976663baf1055a84ae Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Wed, 14 Oct 2020 21:11:08 +1000 Subject: [PATCH 03/26] Update .gitignore (#176) Co-authored-by: coreglitch --- .gitignore | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index dc2a376d..05fc0781 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,9 @@ bindings/ .mxproject # Visual Studio Code -.vscode/ \ No newline at end of file +.vscode/ + +# legendary cmake's +build +CMakeLists.txt +firmware/targets/f2/CMakeLists.txt \ No newline at end of file From f2a38cf1740e1f0605a11af5588f72025119333c Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 10:38:20 +0300 Subject: [PATCH 04/26] fix syntax --- applications/examples/fatfs_list.c | 3 ++- applications/examples/u8g2_example.c | 3 ++- applications/examples/u8g2_qrcode.c | 3 ++- applications/gui/gui_event.c | 7 ++++--- applications/menu/menu.c | 2 +- applications/tests/furi_valuemutex_test.c | 1 - applications/tests/minunit_test.c | 1 - core/api-basic/value-expanders.h | 12 +++--------- core/api-hal/api-spi.h | 17 ++++++++++------- 9 files changed, 24 insertions(+), 25 deletions(-) diff --git a/applications/examples/fatfs_list.c b/applications/examples/fatfs_list.c index b15db514..58e6730c 100644 --- a/applications/examples/fatfs_list.c +++ b/applications/examples/fatfs_list.c @@ -49,7 +49,8 @@ void fatfs_list(void* p) { furi_log = get_default_log(); - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); if(fb_record == NULL) { fuprintf(furi_log, "[widget][fatfs_list] cannot create fb record\n"); furiac_exit(NULL); diff --git a/applications/examples/u8g2_example.c b/applications/examples/u8g2_example.c index 02f35ecb..e77012d4 100644 --- a/applications/examples/u8g2_example.c +++ b/applications/examples/u8g2_example.c @@ -5,7 +5,8 @@ void u8g2_example(void* p) { FuriRecordSubscriber* log = get_default_log(); // open record - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); if(fb_record == NULL) { fuprintf(log, "[widget] cannot create fb record\n"); diff --git a/applications/examples/u8g2_qrcode.c b/applications/examples/u8g2_qrcode.c index 630199c1..2799df33 100644 --- a/applications/examples/u8g2_qrcode.c +++ b/applications/examples/u8g2_qrcode.c @@ -14,7 +14,8 @@ void u8g2_qrcode(void* p) { FuriRecordSubscriber* log = get_default_log(); // open record - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); // Allocate a chunk of memory to store the QR code // https://github.com/ricmoo/QRCode diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c index 1d7caf64..a2ee801b 100644 --- a/applications/gui/gui_event.c +++ b/applications/gui/gui_event.c @@ -29,8 +29,8 @@ GUIEvent* gui_event_alloc() { 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); + 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); @@ -58,7 +58,8 @@ void gui_event_unlock(GUIEvent* gui_event) { } void gui_event_messsage_send(GUIEvent* gui_event, GUIMessage* message) { - assert(gui_event); assert(message); + assert(gui_event); + assert(message); osMessageQueuePut(gui_event->mqueue, message, 0, 0); } diff --git a/applications/menu/menu.c b/applications/menu/menu.c index f551c6af..8cafe6a4 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -187,7 +187,7 @@ void menu_task(void* p) { furiac_exit(NULL); } furiac_ready(); - + while(1) { MenuMessage m = menu_event_next(menu->event); diff --git a/applications/tests/furi_valuemutex_test.c b/applications/tests/furi_valuemutex_test.c index 960d1543..6279d188 100644 --- a/applications/tests/furi_valuemutex_test.c +++ b/applications/tests/furi_valuemutex_test.c @@ -39,7 +39,6 @@ void test_furi_valuemutex() { //read mutex blocking case } - /* TEST: concurrent access diff --git a/applications/tests/minunit_test.c b/applications/tests/minunit_test.c index f99b6204..37cc6b77 100644 --- a/applications/tests/minunit_test.c +++ b/applications/tests/minunit_test.c @@ -67,7 +67,6 @@ MU_TEST_SUITE(test_suite) { MU_RUN_TEST(mu_test_furi_create_open); MU_RUN_TEST(mu_test_furi_valuemutex); MU_RUN_TEST(mu_test_furi_concurrent_access); - } int run_minunit() { diff --git a/core/api-basic/value-expanders.h b/core/api-basic/value-expanders.h index cec0e75c..70adf856 100644 --- a/core/api-basic/value-expanders.h +++ b/core/api-basic/value-expanders.h @@ -12,15 +12,10 @@ void COPY_COMPOSE(void* ctx, void* state) { read_mutex((ValueMutex*)ctx, state, 0); } -typedef enum { - UiLayerBelowNotify - UiLayerNotify, - UiLayerAboveNotify -} UiLayer; +typedef enum { UiLayerBelowNotify UiLayerNotify, UiLayerAboveNotify } UiLayer; -ValueComposerHandle* add_compose_layer( - ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer -); +ValueComposerHandle* +add_compose_layer(ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer); bool remove_compose_layer(ValueComposerHandle* handle); @@ -44,7 +39,6 @@ typedef struct { PubSub pubsub; } ValueManager; - /* acquire value, changes it and send notify with current value. */ diff --git a/core/api-hal/api-spi.h b/core/api-hal/api-spi.h index b3bfe674..74559c54 100644 --- a/core/api-hal/api-spi.h +++ b/core/api-hal/api-spi.h @@ -17,13 +17,17 @@ For transmit/receive data use `spi_xfer` function. */ bool spi_xfer( SPI_HandleTypeDef* spi, - uint8_t* tx_data, uint8_t* rx_data, size_t len, - PubSubCallback cb, void* ctx); + uint8_t* tx_data, + uint8_t* rx_data, + size_t len, + PubSubCallback cb, + void* ctx); /* Blocking verison: */ -static inline bool spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) { +static inline bool +spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) { semaphoreInfo s; osSemaphore block = createSemaphoreStatic(s); if(!spi_xfer(spi, tx_data, rx_data, len, RELEASE_SEMAPHORE, (void*)block)) { @@ -52,15 +56,15 @@ typedef struct { ValueMutex* bus; ///< } SpiDevice; -## SPI IRQ device +##SPI IRQ device -/* + /* Many devices (like CC1101 and NFC) present as SPI bus and IRQ line. For work with it there is special entity `SpiIrqDevice`. Use `subscribe_pubsub` for subscribinq to irq events. */ -typedef struct { + typedef struct { ValueMutex* bus; ///< PubSub* irq; } SpiIrqDevice; @@ -75,7 +79,6 @@ typedef struct { ValueMutex* spi; ///< } DisplayBus; - typedef struct { ValueMutex* bus; ///< } DisplayDevice; From 19ef348c80e027f4c9ea304bc96349c195e97fbb Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 11:32:48 +0300 Subject: [PATCH 05/26] [wip] change gui/widget api --- applications/gui/gui.c | 72 ++++++++++++++++++--------------------- applications/gui/gui.h | 20 +++++++---- applications/gui/widget.c | 9 ----- applications/gui/widget.h | 10 +++++- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/applications/gui/gui.c b/applications/gui/gui.c index ecc314d7..6724b97d 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -22,48 +22,32 @@ struct GUI { WidgetArray_t widgets_dialog; }; -void gui_widget_status_bar_add(GUI* gui, Widget* widget) { +void gui_add_widget(GUI* gui, Widget* widget, WidgetLayer layer) { + 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: + } + assert(gui); assert(widget); + assert(widget_array); 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); + WidgetArray_push_back(widget_array, widget); widget_gui_set(widget, gui); gui_event_unlock(gui->event); @@ -148,8 +132,10 @@ GUI* gui_alloc() { 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(); @@ -158,12 +144,20 @@ GUI* gui_alloc() { void gui_task(void* p) { GUI* gui = gui_alloc(); + + GuiApi gui_api = { + .add_widget = gui_add_widget, + .gui = gui, + }; + // Create FURI record - if(!furi_create_deprecated("gui", gui, sizeof(gui))) { + if(!furi_create("gui", &gui_api)) { 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); diff --git a/applications/gui/gui.h b/applications/gui/gui.h index a95b802c..b09e3bff 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -1,12 +1,18 @@ #pragma once +#include "widget.h" + +typedef enum { + WidgetLayerStatusBar, + WidgetLayerMain, + WidgetLayerFullscreen, + WidgetLayerDialog +} WidgetLayer; + 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); +typedef struct { + void (*add_widget)(GUI* gui, Widget* widget, WidgetLayer layer); + GUI* gui; +} GuiApi; diff --git a/applications/gui/widget.c b/applications/gui/widget.c index bff2a4b9..1b6b6345 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -7,15 +7,6 @@ #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; diff --git a/applications/gui/widget.h b/applications/gui/widget.h index b29ade45..340e5310 100644 --- a/applications/gui/widget.h +++ b/applications/gui/widget.h @@ -4,7 +4,15 @@ typedef struct GUI GUI; typedef struct Canvas Canvas; -typedef struct Widget Widget; + +typedef struct { + void* gui; + bool is_enabled; + WidgetDrawCallback draw_callback; + void* draw_callback_context; + WidgetInputCallback input_callback; + void* input_callback_context; +} Widget; typedef void (*WidgetDrawCallback)(Canvas* canvas, void* context); typedef void (*WidgetInputCallback)(InputEvent* event, void* context); From e684869970ce5e4b9d785dd25ccd6620c82f441a Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 18:05:28 +0300 Subject: [PATCH 06/26] refactor canvas --- applications/gui/canvas.c | 116 ++++++++++++++++++++++------------- applications/gui/canvas.h | 37 +++++++---- applications/gui/canvas_i.h | 4 +- applications/gui/gui.c | 49 ++++++++------- applications/gui/gui.h | 7 ++- applications/gui/gui_event.c | 18 +++--- applications/gui/gui_event.h | 14 ++--- applications/gui/gui_i.h | 2 +- applications/gui/widget.c | 21 ++++++- applications/gui/widget.h | 15 +---- applications/gui/widget_i.h | 2 +- applications/menu/menu.c | 3 +- 12 files changed, 169 insertions(+), 119 deletions(-) diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c index 6f137284..2572bd23 100644 --- a/applications/gui/canvas.c +++ b/applications/gui/canvas.c @@ -6,74 +6,104 @@ #include struct Canvas { - FuriRecordSubscriber* fb_record; - u8g2_t* fb; + 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; +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); + +CanvasApi* canvas_api_init() { + CanvasApi* api = furi_alloc(sizeof(CanvasApi)); + + u8g2_t _u8g2; + u8g2_Setup_st7565_erc12864_alt_f( + &api->canvas.fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32); + u8g2_InitDisplay( + &canvas->fb); // send init sequence to the display, display is in sleep mode after this + u8g2_SetContrast(&api->canvas.fb, 36); + + u8g2_SetPowerSave(&api->canvas.fb, 0); // wake up display + u8g2_SendBuffer(&api->canvas.fb); + + api->width = canvas_width; + api->height = canvas_height; + api->clear = canvas_clear; + api->canvas_color_set = canvas_color_set; + api->canvas_font_set = canvas_font_set; + api->draw_str = canvas_str_draw; + + api->fonts = { + .primary = u8g2_font_Born2bSportyV2_tr, + .secondary = u8g2_font_HelvetiPixel_tr, + }; + + return api; } -void canvas_free(Canvas* canvas) { - assert(canvas); - free(canvas); +void canvas_api_free(CanvasApi* api) { + assert(api); + free(api); } -void canvas_commit(Canvas* canvas) { - assert(canvas); - if(canvas->fb) { - furi_commit(canvas->fb_record); - canvas->fb = NULL; - } +void canvas_commit(CanvasApi* api) { + assert(api); + + u8g2_SetPowerSave(&api->canvas.fb, 0); // wake up display + u8g2_SendBuffer(&api->canvas.fb); } void canvas_frame_set( - Canvas* canvas, + CanvasApi* api, 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; + assert(api); + api->canvas.offset_x = offset_x; + api->canvas.offset_y = offset_y; + api->canvas.width = width; + api->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; +uint8_t canvas_width(CanvasApi* api) { + assert(api); + + return api->canvas.width; } -void canvas_clear(Canvas* canvas) { - u8g2_t* fb = canvas_fb(canvas); - u8g2_ClearBuffer(fb); +uint8_t canvas_height(CanvasApi* api) { + assert(api); + + return api->canvas.height; } -void canvas_color_set(Canvas* canvas, uint8_t color) { - u8g2_t* fb = canvas_fb(canvas); - u8g2_SetDrawColor(fb, 1); +void canvas_clear(CanvasApi* api) { + assert(api); + u8g2_ClearBuffer(&api->canvas.fb); } -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_color_set(CanvasApi* api, Color color) { + assert(api); + u8g2_SetDrawColor(&api->canvas.fb, color); } -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); +void canvas_font_set(CanvasApi* api, Font font) { + assert(api); + u8g2_SetFontMode(&api->canvas.fb, 1); + u8g2_SetFont(&api->canvas.fb, font); +} + +void canvas_str_draw(CanvasApi* api, uint8_t x, uint8_t y, const char* str) { + assert(api); + x += api->canvas.offset_x; + y += api->canvas.offset_y; + u8g2_DrawStr(&api->canvas.fb, x, y, str); } diff --git a/applications/gui/canvas.h b/applications/gui/canvas.h index 3b0f547e..d5692bf7 100644 --- a/applications/gui/canvas.h +++ b/applications/gui/canvas.h @@ -1,24 +1,35 @@ #pragma once #include -#include -#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 enum { + ColorWhite = 0x00, + ColorBlack = 0x01, +} Color; typedef struct Canvas Canvas; -typedef const uint8_t* font_t; +typedef const uint8_t* Font; -uint8_t canvas_width(Canvas* canvas); -uint8_t canvas_height(Canvas* canvas); +struct _CanvasApi; -void canvas_clear(Canvas* canvas); +typedef struct _CanvasApi CanvasApi; -void canvas_color_set(Canvas* canvas, uint8_t color); +typedef struct { + Font primary; + Font secondary; +} Fonts; -void canvas_font_set(Canvas* canvas, font_t font); +struct { + Canvas canvas; + Fonts* fonts; + + uint8_t (*width)(CanvasApi* canvas); + uint8_t (*height)(CanvasApi* canvas); -void canvas_str_draw(Canvas* canvas, uint8_t x, uint8_t y, const char* str); + void (*clear)(CanvasApi* canvas); + + void (*canvas_color_set)(CanvasApi* canvas, Color color); + void (*canvas_font_set)(CanvasApi* canvas, Font font); + + void (*draw_str)(CanvasApi* canvas, uint8_t x, uint8_t y, const char* str); +} _CanvasApi; diff --git a/applications/gui/canvas_i.h b/applications/gui/canvas_i.h index c24b2102..b32eb049 100644 --- a/applications/gui/canvas_i.h +++ b/applications/gui/canvas_i.h @@ -1,8 +1,8 @@ #pragma once -Canvas* canvas_alloc(); +CanvasApi* canvas_api_init(); -void canvas_free(Canvas* canvas); +void canvas_api_free(CanvasApi* api); void canvas_commit(Canvas* canvas); diff --git a/applications/gui/gui.c b/applications/gui/gui.c index 6724b97d..75b9611d 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -13,16 +13,16 @@ ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST); -struct GUI { - GUIEvent* event; - Canvas* canvas; +struct Gui { + 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(GUI* gui, Widget* widget, WidgetLayer layer) { +void gui_add_widget(Gui* gui, Widget* widget, WidgetLayer layer) { WidgetArray_t* widget_array = NULL; switch(layer) { @@ -54,10 +54,10 @@ void gui_add_widget(GUI* gui, Widget* widget, WidgetLayer layer) { gui_update(gui); } -void gui_update(GUI* gui) { +void gui_update(Gui* gui) { assert(gui); - GUIMessage message; - message.type = GUIMessageTypeRedraw; + GuiMessage message; + message.type = GuiMessageTypeRedraw; gui_event_messsage_send(gui->event, &message); } @@ -72,7 +72,7 @@ Widget* gui_widget_find_enabled(WidgetArray_t array) { return NULL; } -bool gui_redraw_fs(GUI* gui) { +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) { @@ -83,25 +83,25 @@ bool gui_redraw_fs(GUI* gui) { } } -void gui_redraw_status_bar(GUI* gui) { +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) { +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) { +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) { +void gui_redraw(Gui* gui) { assert(gui); if(!gui_redraw_fs(gui)) { @@ -110,10 +110,11 @@ void gui_redraw(GUI* gui) { } gui_redraw_dialogs(gui); - canvas_commit(gui->canvas); + // canvas_commit(gui->canvas); + // redraw u8g2 } -void gui_input(GUI* gui, InputEvent* input_event) { +void gui_input(Gui* gui, InputEvent* input_event) { assert(gui); Widget* widget = gui_widget_find_enabled(gui->widgets_dialog); @@ -125,8 +126,8 @@ void gui_input(GUI* gui, InputEvent* input_event) { } } -GUI* gui_alloc() { - GUI* gui = furi_alloc(sizeof(GUI)); +Gui* gui_alloc() { + Gui* gui = furi_alloc(sizeof(Gui)); // Initialize widget arrays WidgetArray_init(gui->widgets_status_bar); WidgetArray_init(gui->widgets); @@ -135,15 +136,17 @@ GUI* gui_alloc() { // Event dispatcher gui->event = gui_event_alloc(); - - // Drawing canvas - gui->canvas = canvas_alloc(); + + // Drawing canvas api + + gui->canvas_api = + canvas_api_init(); return gui; } void gui_task(void* p) { - GUI* gui = gui_alloc(); + Gui* gui = gui_alloc(); GuiApi gui_api = { .add_widget = gui_add_widget, @@ -160,10 +163,10 @@ void gui_task(void* p) { // Forever dispatch while(1) { - GUIMessage message = gui_event_message_next(gui->event); - if(message.type == GUIMessageTypeRedraw) { + GuiMessage message = gui_event_message_next(gui->event); + if(message.type == GuiMessageTypeRedraw) { gui_redraw(gui); - } else if(message.type == GUIMessageTypeInput) { + } else if(message.type == GuiMessageTypeInput) { gui_input(gui, &message.input); } } diff --git a/applications/gui/gui.h b/applications/gui/gui.h index b09e3bff..a65ea53d 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -1,6 +1,7 @@ #pragma once #include "widget.h" +#include "canvas.h" typedef enum { WidgetLayerStatusBar, @@ -10,9 +11,9 @@ typedef enum { } WidgetLayer; typedef struct Widget Widget; -typedef struct GUI GUI; +typedef struct Gui Gui; typedef struct { - void (*add_widget)(GUI* gui, Widget* widget, WidgetLayer layer); - GUI* gui; + void (*add_widget)(Gui* gui, Widget* widget, WidgetLayer layer); + Gui* gui; } GuiApi; diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c index a2ee801b..5c04d9a3 100644 --- a/applications/gui/gui_event.c +++ b/applications/gui/gui_event.c @@ -5,7 +5,7 @@ #define GUI_EVENT_MQUEUE_SIZE 8 -struct GUIEvent { +struct GuiEvent { FuriRecordSubscriber* input_event_record; osMessageQueueId_t mqueue; osMutexId_t lock_mutex; @@ -13,7 +13,7 @@ struct GUIEvent { void gui_event_input_events_callback(const void* value, size_t size, void* ctx) { assert(ctx); - GUIEvent* gui_event = ctx; + GuiEvent* gui_event = ctx; GUIMessage message; message.type = GUIMessageTypeInput; @@ -22,8 +22,8 @@ void gui_event_input_events_callback(const void* value, size_t size, void* ctx) osMessageQueuePut(gui_event->mqueue, &message, 0, 0); } -GUIEvent* gui_event_alloc() { - GUIEvent* gui_event = furi_alloc(sizeof(GUIEvent)); +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); @@ -40,30 +40,30 @@ GUIEvent* gui_event_alloc() { return gui_event; } -void gui_event_free(GUIEvent* 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) { +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) { +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) { +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) { +GUIMessage gui_event_message_next(GuiEvent* gui_event) { assert(gui_event); GUIMessage message; gui_event_unlock(gui_event); diff --git a/applications/gui/gui_event.h b/applications/gui/gui_event.h index c3a47e45..730eb8c6 100644 --- a/applications/gui/gui_event.h +++ b/applications/gui/gui_event.h @@ -14,16 +14,16 @@ typedef struct { void* data; } GUIMessage; -typedef struct GUIEvent GUIEvent; +typedef struct GuiEvent GuiEvent; -GUIEvent* gui_event_alloc(); +GuiEvent* gui_event_alloc(); -void gui_event_free(GUIEvent* gui_event); +void gui_event_free(GuiEvent* gui_event); -void gui_event_lock(GUIEvent* gui_event); +void gui_event_lock(GuiEvent* gui_event); -void gui_event_unlock(GUIEvent* gui_event); +void gui_event_unlock(GuiEvent* gui_event); -void gui_event_messsage_send(GUIEvent* gui_event, GUIMessage* message); +void gui_event_messsage_send(GuiEvent* gui_event, GUIMessage* message); -GUIMessage gui_event_message_next(GUIEvent* gui_event); +GUIMessage gui_event_message_next(GuiEvent* gui_event); diff --git a/applications/gui/gui_i.h b/applications/gui/gui_i.h index e12970fc..6a9bf2a0 100644 --- a/applications/gui/gui_i.h +++ b/applications/gui/gui_i.h @@ -1,3 +1,3 @@ #pragma once -void gui_update(GUI* gui); +void gui_update(Gui* gui); diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 1b6b6345..16981262 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -7,6 +7,15 @@ #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; @@ -53,12 +62,18 @@ void widget_gui_set(Widget* widget, GUI* gui) { widget->gui = gui; } -void widget_draw(Widget* widget, Canvas* canvas) { +void widget_draw(Widget* widget, ValueMutex* canvas_api_mutex) { assert(widget); - assert(canvas); + assert(canvas_api_mutex); assert(widget->gui); - if(widget->draw_callback) widget->draw_callback(canvas, widget->draw_callback_context); + if(widget->draw_callback) { + CanvasApi* api = acquire_mutex_block(canvas_api_mutex); // TODO: timeout? + if(api != NULL) { + widget->draw_callback(api, widget->draw_callback_context); + } + release_mutex(canvas_api_mutex, api); + } } void widget_input(Widget* widget, InputEvent* event) { diff --git a/applications/gui/widget.h b/applications/gui/widget.h index 340e5310..83f7d497 100644 --- a/applications/gui/widget.h +++ b/applications/gui/widget.h @@ -1,20 +1,11 @@ #pragma once #include +#include "canvas.h" -typedef struct GUI GUI; -typedef struct Canvas Canvas; +typedef struct Widget Widget; -typedef struct { - void* gui; - bool is_enabled; - WidgetDrawCallback draw_callback; - void* draw_callback_context; - WidgetInputCallback input_callback; - void* input_callback_context; -} Widget; - -typedef void (*WidgetDrawCallback)(Canvas* canvas, void* context); +typedef void (*WidgetDrawCallback)(CanvasApi* api, void* context); typedef void (*WidgetInputCallback)(InputEvent* event, void* context); Widget* widget_alloc(); diff --git a/applications/gui/widget_i.h b/applications/gui/widget_i.h index 5c1af22d..b0fefa44 100644 --- a/applications/gui/widget_i.h +++ b/applications/gui/widget_i.h @@ -1,6 +1,6 @@ #pragma once -void widget_gui_set(Widget* widget, GUI* gui); +void widget_gui_set(Widget* widget, Gui* gui); void widget_draw(Widget* widget, Canvas* canvas); diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 8cafe6a4..fdef8cad 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -6,7 +6,6 @@ #include #include #include -#include #include "menu_event.h" #include "menu_item.h" @@ -37,7 +36,7 @@ Menu* menu_alloc() { 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); + menu->gui_record = furi_open("gui"); assert(menu->gui_record); return menu; From 78e016412e5cda6bc25932c1fbae25d19662737c Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 18:13:18 +0300 Subject: [PATCH 07/26] gui draw refactor --- applications/gui/gui.c | 22 ++++++++++------------ applications/gui/gui_event.c | 12 ++++++------ applications/gui/gui_event.h | 14 +++++++------- applications/gui/widget.c | 12 ++++-------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/applications/gui/gui.c b/applications/gui/gui.c index 75b9611d..c49ced53 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -73,10 +73,10 @@ Widget* gui_widget_find_enabled(WidgetArray_t array) { } bool gui_redraw_fs(Gui* gui) { - canvas_frame_set(gui->canvas, 0, 0, 128, 64); + 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); + widget_draw(widget, gui->canvas_api); return true; } else { return false; @@ -84,21 +84,21 @@ bool gui_redraw_fs(Gui* gui) { } void gui_redraw_status_bar(Gui* gui) { - canvas_frame_set(gui->canvas, 0, 0, 128, 64); + 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); + if(widget) widget_draw(widget, gui->canvas_api); } void gui_redraw_normal(Gui* gui) { - canvas_frame_set(gui->canvas, 0, 9, 128, 55); + 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); + if(widget) widget_draw(widget, gui->canvas_api); } void gui_redraw_dialogs(Gui* gui) { - canvas_frame_set(gui->canvas, 10, 20, 118, 44); + 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); + if(widget) widget_draw(widget, gui->canvas_api); } void gui_redraw(Gui* gui) { @@ -110,8 +110,7 @@ void gui_redraw(Gui* gui) { } gui_redraw_dialogs(gui); - // canvas_commit(gui->canvas); - // redraw u8g2 + canvas_commit(gui->canvas); } void gui_input(Gui* gui, InputEvent* input_event) { @@ -139,8 +138,7 @@ Gui* gui_alloc() { // Drawing canvas api - gui->canvas_api = - canvas_api_init(); + gui->canvas_api = canvas_api_init(); return gui; } diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c index 5c04d9a3..8d0914c2 100644 --- a/applications/gui/gui_event.c +++ b/applications/gui/gui_event.c @@ -15,8 +15,8 @@ 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; + GuiMessage message; + message.type = GuiMessageTypeInput; message.input = *(InputEvent*)value; osMessageQueuePut(gui_event->mqueue, &message, 0, 0); @@ -25,7 +25,7 @@ void gui_event_input_events_callback(const void* value, size_t size, void* ctx) 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); + gui_event->mqueue = osMessageQueueNew(GUI_EVENT_MQUEUE_SIZE, sizeof(GuiMessage), NULL); assert(gui_event->mqueue); // Input @@ -57,15 +57,15 @@ void gui_event_unlock(GuiEvent* gui_event) { assert(osMutexRelease(gui_event->lock_mutex) == osOK); } -void gui_event_messsage_send(GuiEvent* gui_event, GUIMessage* message) { +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) { +GuiMessage gui_event_message_next(GuiEvent* gui_event) { assert(gui_event); - GUIMessage message; + GuiMessage message; gui_event_unlock(gui_event); while(osMessageQueueGet(gui_event->mqueue, &message, NULL, osWaitForever) != osOK) { }; diff --git a/applications/gui/gui_event.h b/applications/gui/gui_event.h index 730eb8c6..89ba4ee2 100644 --- a/applications/gui/gui_event.h +++ b/applications/gui/gui_event.h @@ -4,15 +4,15 @@ #include typedef enum { - GUIMessageTypeRedraw = 0x00, - GUIMessageTypeInput = 0x01, -} GUIMessageType; + GuiMessageTypeRedraw = 0x00, + GuiMessageTypeInput = 0x01, +} GuiMessageType; typedef struct { - GUIMessageType type; + GuiMessageType type; InputEvent input; void* data; -} GUIMessage; +} GuiMessage; typedef struct GuiEvent GuiEvent; @@ -24,6 +24,6 @@ void gui_event_lock(GuiEvent* gui_event); void gui_event_unlock(GuiEvent* gui_event); -void gui_event_messsage_send(GuiEvent* gui_event, GUIMessage* message); +void gui_event_messsage_send(GuiEvent* gui_event, GuiMessage* message); -GUIMessage gui_event_message_next(GuiEvent* gui_event); +GuiMessage gui_event_message_next(GuiEvent* gui_event); diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 16981262..106ac7f1 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -56,23 +56,19 @@ void widget_update(Widget* widget) { if(widget->gui) gui_update(widget->gui); } -void widget_gui_set(Widget* widget, GUI* gui) { +void widget_gui_set(Widget* widget, Gui* gui) { assert(widget); assert(gui); widget->gui = gui; } -void widget_draw(Widget* widget, ValueMutex* canvas_api_mutex) { +void widget_draw(Widget* widget, CanvasApi* canvas_api) { assert(widget); - assert(canvas_api_mutex); + assert(canvas_api); assert(widget->gui); if(widget->draw_callback) { - CanvasApi* api = acquire_mutex_block(canvas_api_mutex); // TODO: timeout? - if(api != NULL) { - widget->draw_callback(api, widget->draw_callback_context); - } - release_mutex(canvas_api_mutex, api); + widget->draw_callback(canvas_api, widget->draw_callback_context); } } From f0f3615c550b55fcb55a5881c901ae7118e7ccc1 Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 18:56:47 +0300 Subject: [PATCH 08/26] compile/run ok, issue with backlight --- applications/applications.mk | 3 +-- applications/gui/canvas.c | 34 +++++++++++++--------------- applications/gui/canvas.h | 27 ++++++++++++++-------- applications/gui/canvas_i.h | 4 ++-- applications/gui/gui.c | 31 +++++++++++++++----------- applications/gui/gui.h | 9 +++++--- applications/gui/widget.c | 4 +++- applications/gui/widget_i.h | 4 +++- applications/menu/menu.c | 43 +++++++++++++++++------------------- applications/startup.h | 2 +- 10 files changed, 87 insertions(+), 74 deletions(-) diff --git a/applications/applications.mk b/applications/applications.mk index 906f6690..d60ed2d5 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -5,7 +5,7 @@ CFLAGS += -I$(APP_DIR) APP_RELEASE ?= 0 ifeq ($(APP_RELEASE), 1) -APP_DISPLAY = 1 +APP_GUI = 1 APP_INPUT = 1 APP_MENU = 1 endif @@ -79,7 +79,6 @@ endif # device drivers APP_GUI ?= 0 ifeq ($(APP_GUI), 1) -APP_DISPLAY = 1 CFLAGS += -DAPP_GUI C_SOURCES += $(wildcard $(APP_DIR)/gui/*.c) endif diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c index 2572bd23..5a8d7307 100644 --- a/applications/gui/canvas.c +++ b/applications/gui/canvas.c @@ -3,15 +3,6 @@ #include #include -#include - -struct Canvas { - u8g2_t fb; - uint8_t offset_x; - uint8_t offset_y; - uint8_t width; - uint8_t height; -}; uint8_t canvas_width(CanvasApi* api); uint8_t canvas_height(CanvasApi* api); @@ -20,14 +11,20 @@ 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() { CanvasApi* api = furi_alloc(sizeof(CanvasApi)); - u8g2_t _u8g2; u8g2_Setup_st7565_erc12864_alt_f( - &api->canvas.fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32); - u8g2_InitDisplay( - &canvas->fb); // send init sequence to the display, display is in sleep mode after this + &api->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(&api->canvas.fb); u8g2_SetContrast(&api->canvas.fb, 36); u8g2_SetPowerSave(&api->canvas.fb, 0); // wake up display @@ -36,14 +33,13 @@ CanvasApi* canvas_api_init() { api->width = canvas_width; api->height = canvas_height; api->clear = canvas_clear; - api->canvas_color_set = canvas_color_set; - api->canvas_font_set = canvas_font_set; + api->set_color = canvas_color_set; + api->set_font = canvas_font_set; api->draw_str = canvas_str_draw; - api->fonts = { - .primary = u8g2_font_Born2bSportyV2_tr, - .secondary = u8g2_font_HelvetiPixel_tr, - }; + api->fonts = furi_alloc(sizeof(Fonts)); + api->fonts->primary = u8g2_font_Born2bSportyV2_tr; + api->fonts->secondary = u8g2_font_HelvetiPixel_tr; return api; } diff --git a/applications/gui/canvas.h b/applications/gui/canvas.h index d5692bf7..3d77e8fe 100644 --- a/applications/gui/canvas.h +++ b/applications/gui/canvas.h @@ -1,25 +1,34 @@ #pragma once #include +#include typedef enum { ColorWhite = 0x00, ColorBlack = 0x01, } Color; -typedef struct Canvas Canvas; typedef const uint8_t* Font; -struct _CanvasApi; - -typedef struct _CanvasApi CanvasApi; - typedef struct { Font primary; Font secondary; } Fonts; -struct { +struct _CanvasApi; + +typedef struct _CanvasApi CanvasApi; + +// Canvas is private but we need its declaration here +typedef struct { + u8g2_t fb; + uint8_t offset_x; + uint8_t offset_y; + uint8_t width; + uint8_t height; +} Canvas; + +struct _CanvasApi { Canvas canvas; Fonts* fonts; @@ -28,8 +37,8 @@ struct { void (*clear)(CanvasApi* canvas); - void (*canvas_color_set)(CanvasApi* canvas, Color color); - void (*canvas_font_set)(CanvasApi* canvas, Font font); + 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); -} _CanvasApi; +}; diff --git a/applications/gui/canvas_i.h b/applications/gui/canvas_i.h index b32eb049..7337db00 100644 --- a/applications/gui/canvas_i.h +++ b/applications/gui/canvas_i.h @@ -4,10 +4,10 @@ CanvasApi* canvas_api_init(); void canvas_api_free(CanvasApi* api); -void canvas_commit(Canvas* canvas); +void canvas_commit(CanvasApi* api); void canvas_frame_set( - Canvas* canvas, + CanvasApi* api, uint8_t offset_x, uint8_t offset_y, uint8_t width, diff --git a/applications/gui/gui.c b/applications/gui/gui.c index c49ced53..e03971eb 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -2,6 +2,7 @@ #include "gui_i.h" #include +#include #include #include @@ -22,36 +23,40 @@ struct Gui { WidgetArray_t widgets_dialog; }; -void gui_add_widget(Gui* gui, Widget* widget, WidgetLayer layer) { +void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) { + assert(gui_api); + assert(gui_api->gui); + + // TODO add mutex on widget array + WidgetArray_t* widget_array = NULL; switch(layer) { case WidgetLayerStatusBar: - widget_array = &gui->widgets_status_bar; + widget_array = &gui_api->gui->widgets_status_bar; break; case WidgetLayerMain: - widget_array = &gui->widgets; + widget_array = &gui_api->gui->widgets; break; case WidgetLayerFullscreen: - widget_array = &gui->widgets_fs; + widget_array = &gui_api->gui->widgets_fs; break; case WidgetLayerDialog: - widget_array = &gui->widgets_dialog; + widget_array = &gui_api->gui->widgets_dialog; break; - default: + default: break; } - assert(gui); assert(widget); 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_event_lock(gui_api->gui->event); + WidgetArray_push_back((struct WidgetArray_s*)widget_array, widget); + widget_gui_set(widget, gui_api->gui); + gui_event_unlock(gui_api->gui->event); - gui_update(gui); + gui_update(gui_api->gui); } void gui_update(Gui* gui) { @@ -110,7 +115,7 @@ void gui_redraw(Gui* gui) { } gui_redraw_dialogs(gui); - canvas_commit(gui->canvas); + canvas_commit(gui->canvas_api); } void gui_input(Gui* gui, InputEvent* input_event) { diff --git a/applications/gui/gui.h b/applications/gui/gui.h index a65ea53d..371c0434 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -13,7 +13,10 @@ typedef enum { typedef struct Widget Widget; typedef struct Gui Gui; -typedef struct { - void (*add_widget)(Gui* gui, Widget* widget, WidgetLayer layer); +struct _GuiApi; +typedef struct _GuiApi GuiApi; + +struct _GuiApi { + void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer); Gui* gui; -} GuiApi; +}; diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 106ac7f1..148abdf6 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -7,6 +7,8 @@ #include "gui.h" #include "gui_i.h" +// TODO add mutex to widget ops + struct Widget { void* gui; bool is_enabled; @@ -14,7 +16,7 @@ struct Widget { 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)); diff --git a/applications/gui/widget_i.h b/applications/gui/widget_i.h index b0fefa44..f6cc2f14 100644 --- a/applications/gui/widget_i.h +++ b/applications/gui/widget_i.h @@ -1,7 +1,9 @@ #pragma once +#include "gui.h" + void widget_gui_set(Widget* widget, Gui* gui); -void widget_draw(Widget* widget, Canvas* canvas); +void widget_draw(Widget* widget, CanvasApi* canvas_api); void widget_input(Widget* widget, InputEvent* event); diff --git a/applications/menu/menu.c b/applications/menu/menu.c index fdef8cad..92737b87 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -4,17 +4,18 @@ #include #include +#include #include -#include #include "menu_event.h" #include "menu_item.h" struct Menu { MenuEvent* event; + // GUI - FuriRecordSubscriber* gui_record; Widget* widget; + // State MenuItem* root; MenuItem* settings; @@ -22,9 +23,9 @@ struct Menu { uint32_t position; }; -void menu_widget_callback(Canvas* canvas, void* context); +void menu_widget_callback(CanvasApi* canvas, void* context); -Menu* menu_alloc() { +Menu* menu_init() { Menu* menu = furi_alloc(sizeof(Menu)); // Event dispatcher @@ -36,8 +37,9 @@ Menu* menu_alloc() { widget_input_callback_set(menu->widget, menu_event_input_callback, menu->event); // Open GUI and register fullscreen widget - menu->gui_record = furi_open("gui"); - assert(menu->gui_record); + GuiApi* gui = furi_open("gui"); + assert(gui); + gui->add_widget(gui, menu->widget, WidgetLayerFullscreen); return menu; } @@ -75,7 +77,7 @@ void menu_settings_item_add(Menu* menu, MenuItem* item) { menu_item_subitem_add(menu->settings, item); } -void menu_widget_callback(Canvas* canvas, void* context) { +void menu_widget_callback(CanvasApi* canvas, void* context) { assert(canvas); assert(context); @@ -84,20 +86,20 @@ void menu_widget_callback(Canvas* canvas, void* 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"); + canvas->clear(canvas); + canvas->set_color(canvas, ColorBlack); + canvas->set_font(canvas, canvas->fonts->primary); + canvas->draw_str(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); + canvas->clear(canvas); + canvas->set_color(canvas, ColorBlack); + canvas->set_font(canvas, canvas->fonts->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)); + canvas->draw_str(canvas, 2, 12 * (i + 1), menu_item_get_label(item)); } } @@ -172,19 +174,14 @@ void menu_exit(Menu* 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); + Menu* menu = menu_init(); + menu_build_main(menu); if(!furi_create_deprecated("menu", menu, sizeof(menu))) { printf("[menu_task] cannot create the menu record\n"); furiac_exit(NULL); } + furiac_ready(); while(1) { diff --git a/applications/startup.h b/applications/startup.h index 2f96ce08..58c8c11d 100644 --- a/applications/startup.h +++ b/applications/startup.h @@ -37,7 +37,7 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { #endif #ifdef APP_GUI - {.app = gui_task, .name = "gui_task", .libs = {1, FURI_LIB{"display_u8g2"}}}, + {.app = gui_task, .name = "gui_task", .libs = {0}}, #endif #ifdef APP_MENU From f9bda68ef84036b5708e102e06cf34e81f72a245 Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 18:57:21 +0300 Subject: [PATCH 09/26] lint --- applications/gui/canvas.c | 5 +---- applications/gui/canvas.h | 10 +++++----- applications/gui/gui.c | 19 ++++++++++--------- applications/gui/widget.c | 2 +- applications/menu/menu.c | 4 ++-- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c index 5a8d7307..6dc7962c 100644 --- a/applications/gui/canvas.c +++ b/applications/gui/canvas.c @@ -18,10 +18,7 @@ CanvasApi* canvas_api_init() { CanvasApi* api = furi_alloc(sizeof(CanvasApi)); u8g2_Setup_st7565_erc12864_alt_f( - &api->canvas.fb, - U8G2_R0, - u8x8_hw_spi_stm32, - u8g2_gpio_and_delay_stm32); + &api->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(&api->canvas.fb); diff --git a/applications/gui/canvas.h b/applications/gui/canvas.h index 3d77e8fe..27dd2efe 100644 --- a/applications/gui/canvas.h +++ b/applications/gui/canvas.h @@ -4,15 +4,15 @@ #include typedef enum { - ColorWhite = 0x00, - ColorBlack = 0x01, + ColorWhite = 0x00, + ColorBlack = 0x01, } Color; typedef const uint8_t* Font; typedef struct { - Font primary; - Font secondary; + Font primary; + Font secondary; } Fonts; struct _CanvasApi; @@ -31,7 +31,7 @@ typedef struct { struct _CanvasApi { Canvas canvas; Fonts* fonts; - + uint8_t (*width)(CanvasApi* canvas); uint8_t (*height)(CanvasApi* canvas); diff --git a/applications/gui/gui.c b/applications/gui/gui.c index e03971eb..6be88315 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -32,20 +32,21 @@ void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) { WidgetArray_t* widget_array = NULL; switch(layer) { - case WidgetLayerStatusBar: - widget_array = &gui_api->gui->widgets_status_bar; + case WidgetLayerStatusBar: + widget_array = &gui_api->gui->widgets_status_bar; break; - case WidgetLayerMain: - widget_array = &gui_api->gui->widgets; + case WidgetLayerMain: + widget_array = &gui_api->gui->widgets; break; - case WidgetLayerFullscreen: - widget_array = &gui_api->gui->widgets_fs; + case WidgetLayerFullscreen: + widget_array = &gui_api->gui->widgets_fs; break; - case WidgetLayerDialog: - widget_array = &gui_api->gui->widgets_dialog; + case WidgetLayerDialog: + widget_array = &gui_api->gui->widgets_dialog; break; - default: break; + default: + break; } assert(widget); diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 148abdf6..3277c24d 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -70,7 +70,7 @@ void widget_draw(Widget* widget, CanvasApi* canvas_api) { assert(widget->gui); if(widget->draw_callback) { - widget->draw_callback(canvas_api, widget->draw_callback_context); + widget->draw_callback(canvas_api, widget->draw_callback_context); } } diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 92737b87..156e7f60 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -15,7 +15,7 @@ struct Menu { // GUI Widget* widget; - + // State MenuItem* root; MenuItem* settings; @@ -175,7 +175,7 @@ void menu_exit(Menu* menu) { void menu_task(void* p) { Menu* menu = menu_init(); - menu_build_main(menu); + menu_build_main(menu); if(!furi_create_deprecated("menu", menu, sizeof(menu))) { printf("[menu_task] cannot create the menu record\n"); From 1e40f873a5e4ccc7362718acbe63c981e5b40c5c Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 19:15:53 +0300 Subject: [PATCH 10/26] backlight control --- applications/applications.mk | 1 + .../backlight-control/backlight-control.c | 30 +++++++++++++++++++ applications/startup.h | 2 ++ 3 files changed, 33 insertions(+) create mode 100644 applications/backlight-control/backlight-control.c diff --git a/applications/applications.mk b/applications/applications.mk index d60ed2d5..e8f61573 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -81,6 +81,7 @@ 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) diff --git a/applications/backlight-control/backlight-control.c b/applications/backlight-control/backlight-control.c new file mode 100644 index 00000000..ef22f3df --- /dev/null +++ b/applications/backlight-control/backlight-control.c @@ -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); + } + } +} \ No newline at end of file diff --git a/applications/startup.h b/applications/startup.h index 58c8c11d..847009dc 100644 --- a/applications/startup.h +++ b/applications/startup.h @@ -26,6 +26,7 @@ void coreglitch_demo_0(void* p); void u8g2_qrcode(void* p); void fatfs_list(void* p); void gui_task(void* p); +void backlight_control(void* p); const FlipperStartupApp FLIPPER_STARTUP[] = { #ifdef APP_DISPLAY @@ -37,6 +38,7 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { #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 From 2806a3ce3d9a16ace33d13b95372de24693b6b8e Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 19:16:11 +0300 Subject: [PATCH 11/26] add forgotten u8g2 periphery --- applications/gui/u8g2_periphery.c | 114 ++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 applications/gui/u8g2_periphery.c diff --git a/applications/gui/u8g2_periphery.c b/applications/gui/u8g2_periphery.c new file mode 100644 index 00000000..db12c506 --- /dev/null +++ b/applications/gui/u8g2_periphery.c @@ -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; +} From 7570ac08a9b30c46670dfdb51f0e386021da98fc Mon Sep 17 00:00:00 2001 From: rusdacent <57439765+rusdacent@users.noreply.github.com> Date: Thu, 15 Oct 2020 19:50:21 +0300 Subject: [PATCH 12/26] Fix codestyle check pipeline (#178) * change continue-on-error to false * add manual trigger * revert manual trigger * test fail checkstyle * restore prev trigger and settings * fix syntax * fix exit code for codestyle * try to break pipeline * restore pipeline Co-authored-by: aanper --- .github/workflows/ci.yml | 2 +- applications/examples/fatfs_list.c | 3 ++- applications/examples/u8g2_example.c | 3 ++- applications/examples/u8g2_qrcode.c | 3 ++- applications/gui/gui_event.c | 7 ++++--- applications/menu/menu.c | 2 +- applications/tests/furi_valuemutex_test.c | 1 - applications/tests/minunit_test.c | 1 - core/api-basic/value-expanders.h | 12 +++--------- core/api-hal/api-spi.h | 17 ++++++++++------- docker/syntax_check.sh | 4 ++-- 11 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd5465a8..acb79084 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,7 @@ jobs: - name: Check syntax uses: ./.github/actions/docker - continue-on-error: true + continue-on-error: false with: run: /syntax_check.sh diff --git a/applications/examples/fatfs_list.c b/applications/examples/fatfs_list.c index b15db514..58e6730c 100644 --- a/applications/examples/fatfs_list.c +++ b/applications/examples/fatfs_list.c @@ -49,7 +49,8 @@ void fatfs_list(void* p) { furi_log = get_default_log(); - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); if(fb_record == NULL) { fuprintf(furi_log, "[widget][fatfs_list] cannot create fb record\n"); furiac_exit(NULL); diff --git a/applications/examples/u8g2_example.c b/applications/examples/u8g2_example.c index 02f35ecb..e77012d4 100644 --- a/applications/examples/u8g2_example.c +++ b/applications/examples/u8g2_example.c @@ -5,7 +5,8 @@ void u8g2_example(void* p) { FuriRecordSubscriber* log = get_default_log(); // open record - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); if(fb_record == NULL) { fuprintf(log, "[widget] cannot create fb record\n"); diff --git a/applications/examples/u8g2_qrcode.c b/applications/examples/u8g2_qrcode.c index 630199c1..2799df33 100644 --- a/applications/examples/u8g2_qrcode.c +++ b/applications/examples/u8g2_qrcode.c @@ -14,7 +14,8 @@ void u8g2_qrcode(void* p) { FuriRecordSubscriber* log = get_default_log(); // open record - FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); + FuriRecordSubscriber* fb_record = + furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); // Allocate a chunk of memory to store the QR code // https://github.com/ricmoo/QRCode diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c index 1d7caf64..a2ee801b 100644 --- a/applications/gui/gui_event.c +++ b/applications/gui/gui_event.c @@ -29,8 +29,8 @@ GUIEvent* gui_event_alloc() { 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); + 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); @@ -58,7 +58,8 @@ void gui_event_unlock(GUIEvent* gui_event) { } void gui_event_messsage_send(GUIEvent* gui_event, GUIMessage* message) { - assert(gui_event); assert(message); + assert(gui_event); + assert(message); osMessageQueuePut(gui_event->mqueue, message, 0, 0); } diff --git a/applications/menu/menu.c b/applications/menu/menu.c index f551c6af..8cafe6a4 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -187,7 +187,7 @@ void menu_task(void* p) { furiac_exit(NULL); } furiac_ready(); - + while(1) { MenuMessage m = menu_event_next(menu->event); diff --git a/applications/tests/furi_valuemutex_test.c b/applications/tests/furi_valuemutex_test.c index 960d1543..6279d188 100644 --- a/applications/tests/furi_valuemutex_test.c +++ b/applications/tests/furi_valuemutex_test.c @@ -39,7 +39,6 @@ void test_furi_valuemutex() { //read mutex blocking case } - /* TEST: concurrent access diff --git a/applications/tests/minunit_test.c b/applications/tests/minunit_test.c index f99b6204..37cc6b77 100644 --- a/applications/tests/minunit_test.c +++ b/applications/tests/minunit_test.c @@ -67,7 +67,6 @@ MU_TEST_SUITE(test_suite) { MU_RUN_TEST(mu_test_furi_create_open); MU_RUN_TEST(mu_test_furi_valuemutex); MU_RUN_TEST(mu_test_furi_concurrent_access); - } int run_minunit() { diff --git a/core/api-basic/value-expanders.h b/core/api-basic/value-expanders.h index cec0e75c..70adf856 100644 --- a/core/api-basic/value-expanders.h +++ b/core/api-basic/value-expanders.h @@ -12,15 +12,10 @@ void COPY_COMPOSE(void* ctx, void* state) { read_mutex((ValueMutex*)ctx, state, 0); } -typedef enum { - UiLayerBelowNotify - UiLayerNotify, - UiLayerAboveNotify -} UiLayer; +typedef enum { UiLayerBelowNotify UiLayerNotify, UiLayerAboveNotify } UiLayer; -ValueComposerHandle* add_compose_layer( - ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer -); +ValueComposerHandle* +add_compose_layer(ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer); bool remove_compose_layer(ValueComposerHandle* handle); @@ -44,7 +39,6 @@ typedef struct { PubSub pubsub; } ValueManager; - /* acquire value, changes it and send notify with current value. */ diff --git a/core/api-hal/api-spi.h b/core/api-hal/api-spi.h index b3bfe674..74559c54 100644 --- a/core/api-hal/api-spi.h +++ b/core/api-hal/api-spi.h @@ -17,13 +17,17 @@ For transmit/receive data use `spi_xfer` function. */ bool spi_xfer( SPI_HandleTypeDef* spi, - uint8_t* tx_data, uint8_t* rx_data, size_t len, - PubSubCallback cb, void* ctx); + uint8_t* tx_data, + uint8_t* rx_data, + size_t len, + PubSubCallback cb, + void* ctx); /* Blocking verison: */ -static inline bool spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) { +static inline bool +spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) { semaphoreInfo s; osSemaphore block = createSemaphoreStatic(s); if(!spi_xfer(spi, tx_data, rx_data, len, RELEASE_SEMAPHORE, (void*)block)) { @@ -52,15 +56,15 @@ typedef struct { ValueMutex* bus; ///< } SpiDevice; -## SPI IRQ device +##SPI IRQ device -/* + /* Many devices (like CC1101 and NFC) present as SPI bus and IRQ line. For work with it there is special entity `SpiIrqDevice`. Use `subscribe_pubsub` for subscribinq to irq events. */ -typedef struct { + typedef struct { ValueMutex* bus; ///< PubSub* irq; } SpiIrqDevice; @@ -75,7 +79,6 @@ typedef struct { ValueMutex* spi; ///< } DisplayBus; - typedef struct { ValueMutex* bus; ///< } DisplayDevice; diff --git a/docker/syntax_check.sh b/docker/syntax_check.sh index 3f54df83..0294b696 100755 --- a/docker/syntax_check.sh +++ b/docker/syntax_check.sh @@ -26,7 +26,7 @@ rust_syntax_rc=$? if [[ $rust_syntax_rc -eq 0 ]] && [[ $c_syntax_rc -eq 0 ]]; then echo "Code looks fine for me!" - exit 1 + exit 0 fi read -p "Do you want fix syntax? (y/n): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1 @@ -38,4 +38,4 @@ cd $PROJECT_DIR # We use root in container and clang-format rewriting files. We'll need change owner to original local_user=$(stat -c '%u' .clang-format) $CLANG_FORMAT_BIN -style=file -i $C_FILES -chown $local_user $C_FILES \ No newline at end of file +chown $local_user $C_FILES From 59740349fa3318017eadacf50bd0b109792e3c0b Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Fri, 16 Oct 2020 03:36:15 +1000 Subject: [PATCH 13/26] furi memory managment (#177) * memory managment calls now forwarded to freertos heap * memory managment tests * local target test compability * rename heap.c file to heap_4.c for local target and explicity init heap in single thread context * rebase BlockLink_t struct * check mutex in local heap Co-authored-by: aanper --- applications/applications.mk | 1 + applications/tests/furi_memmgr_test.c | 99 +++++++ applications/tests/minunit_test.c | 9 + core/api-basic/memmgr.c | 51 ++++ core/api-basic/memmgr.h | 13 + core/flipper_v2.h | 4 +- firmware/targets/local/Src/heap_4.c | 389 ++++++++++++++++++++++++++ firmware/targets/local/Src/main.c | 8 + firmware/targets/local/fatfs/heap.h | 37 +++ firmware/targets/local/target.mk | 3 + 10 files changed, 613 insertions(+), 1 deletion(-) create mode 100644 applications/tests/furi_memmgr_test.c create mode 100644 core/api-basic/memmgr.c create mode 100644 core/api-basic/memmgr.h create mode 100644 firmware/targets/local/Src/heap_4.c create mode 100644 firmware/targets/local/fatfs/heap.h diff --git a/applications/applications.mk b/applications/applications.mk index 906f6690..c24adee3 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -26,6 +26,7 @@ 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_memmgr_test.c endif APP_EXAMPLE_BLINK ?= 0 diff --git a/applications/tests/furi_memmgr_test.c b/applications/tests/furi_memmgr_test.c new file mode 100644 index 00000000..99925be8 --- /dev/null +++ b/applications/tests/furi_memmgr_test.c @@ -0,0 +1,99 @@ +#include "minunit.h" +#include +#include +#include +// 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); +} \ No newline at end of file diff --git a/applications/tests/minunit_test.c b/applications/tests/minunit_test.c index 37cc6b77..631c1892 100644 --- a/applications/tests/minunit_test.c +++ b/applications/tests/minunit_test.c @@ -14,6 +14,7 @@ bool test_furi_mute_algorithm(); void test_furi_create_open(); void test_furi_valuemutex(); void test_furi_concurrent_access(); +void test_furi_memmgr(); static int foo = 0; @@ -54,6 +55,12 @@ MU_TEST(mu_test_furi_concurrent_access) { test_furi_concurrent_access(); } +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); @@ -67,6 +74,8 @@ MU_TEST_SUITE(test_suite) { 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_memmgr); } int run_minunit() { diff --git a/core/api-basic/memmgr.c b/core/api-basic/memmgr.c new file mode 100644 index 00000000..e67b7e7b --- /dev/null +++ b/core/api-basic/memmgr.c @@ -0,0 +1,51 @@ +#include "memmgr.h" +#include + +extern void* pvPortMalloc(size_t xSize); +extern void vPortFree(void* pv); +extern size_t xPortGetFreeHeapSize(void); +extern size_t xPortGetMinimumEverFreeHeapSize(void); + +void* malloc(size_t size) { + return pvPortMalloc(size); +} + +void free(void* ptr) { + vPortFree(ptr); +} + +void* realloc(void* ptr, size_t size) { + if(size == 0) { + vPortFree(ptr); + return NULL; + } + + void* p; + p = pvPortMalloc(size); + if(p) { + // TODO implement secure realloc + // insecure, but will do job in our case + if(ptr != NULL) { + memcpy(p, ptr, size); + vPortFree(ptr); + } + } + return p; +} + +void* calloc(size_t count, size_t size) { + void* ptr = pvPortMalloc(count * size); + if(ptr) { + // zero the memory + memset(ptr, 0, count * size); + } + return ptr; +} + +size_t memmgr_get_free_heap(void) { + return xPortGetFreeHeapSize(); +} + +size_t memmgr_get_minimum_free_heap(void) { + return xPortGetMinimumEverFreeHeapSize(); +} \ No newline at end of file diff --git a/core/api-basic/memmgr.h b/core/api-basic/memmgr.h new file mode 100644 index 00000000..70a32e4b --- /dev/null +++ b/core/api-basic/memmgr.h @@ -0,0 +1,13 @@ +#pragma once +#include + +// define for test case "link against furi memmgr" +#define FURI_MEMMGR_GUARD 1 + +void* malloc(size_t size); +void free(void* ptr); +void* realloc(void* ptr, size_t size); +void* calloc(size_t count, size_t size); + +size_t memmgr_get_free_heap(void); +size_t memmgr_get_minimum_free_heap(void); diff --git a/core/flipper_v2.h b/core/flipper_v2.h index f1079f71..d16cbacd 100644 --- a/core/flipper_v2.h +++ b/core/flipper_v2.h @@ -4,4 +4,6 @@ //#include "api-basic/flapp.h" #include "cmsis_os2.h" #include "api-basic/valuemutex.h" -//#include "api-basic/pubsub.h" \ No newline at end of file +//#include "api-basic/pubsub.h" + +#include "api-basic/memmgr.h" \ No newline at end of file diff --git a/firmware/targets/local/Src/heap_4.c b/firmware/targets/local/Src/heap_4.c new file mode 100644 index 00000000..04abcbf0 --- /dev/null +++ b/firmware/targets/local/Src/heap_4.c @@ -0,0 +1,389 @@ +/* + * FreeRTOS Kernel V10.2.1 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://www.FreeRTOS.org + * http://aws.amazon.com/freertos + * + * 1 tab == 4 spaces! + */ + +/* + * A sample implementation of pvPortMalloc() and vPortFree() that combines + * (coalescences) adjacent memory blocks as they are freed, and in so doing + * limits memory fragmentation. + * + * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the + * memory management pages of http://www.FreeRTOS.org for more information. + */ +#include "heap.h" + +osMutexId_t heap_managment_mutex = NULL; + +/* Block sizes must not get too small. */ +#define heapMINIMUM_BLOCK_SIZE ((size_t)(xHeapStructSize << 1)) + +/* Assumes 8bit bytes! */ +#define heapBITS_PER_BYTE ((size_t)8) + +/* Allocate the memory for the heap. */ +#if(configAPPLICATION_ALLOCATED_HEAP == 1) +/* The application writer has already defined the array used for the RTOS + heap - probably so it can be placed in a special segment or address. */ +extern uint8_t ucHeap[configTOTAL_HEAP_SIZE]; +#else +static uint8_t ucHeap[configTOTAL_HEAP_SIZE]; +#endif /* configAPPLICATION_ALLOCATED_HEAP */ + +/* Define the linked list structure. This is used to link free blocks in order +of their memory address. */ +typedef struct A_BLOCK_LINK { + struct A_BLOCK_LINK* pxNextFreeBlock; /*<< The next free block in the list. */ + size_t xBlockSize; /*<< The size of the free block. */ +} BlockLink_t; +/*-----------------------------------------------------------*/ + +/* + * Inserts a block of memory that is being freed into the correct position in + * the list of free memory blocks. The block being freed will be merged with + * the block in front it and/or the block behind it if the memory blocks are + * adjacent to each other. + */ +static void prvInsertBlockIntoFreeList(BlockLink_t* pxBlockToInsert); + +// this function is not thread-safe, so it must be called in single thread context +bool prvHeapInit(void); + +/*-----------------------------------------------------------*/ + +/* The size of the structure placed at the beginning of each allocated memory +block must by correctly byte aligned. */ +static const size_t xHeapStructSize = (sizeof(BlockLink_t) + ((size_t)(portBYTE_ALIGNMENT - 1))) & + ~((size_t)portBYTE_ALIGNMENT_MASK); + +/* Create a couple of list links to mark the start and end of the list. */ +static BlockLink_t xStart, *pxEnd = NULL; + +/* Keeps track of the number of free bytes remaining, but says nothing about +fragmentation. */ +static size_t xFreeBytesRemaining = 0U; +static size_t xMinimumEverFreeBytesRemaining = 0U; + +/* Gets set to the top bit of an size_t type. When this bit in the xBlockSize +member of an BlockLink_t structure is set then the block belongs to the +application. When the bit is free the block is still part of the free heap +space. */ +static size_t xBlockAllocatedBit = 0; + +/*-----------------------------------------------------------*/ + +void* pvPortMalloc(size_t xWantedSize) { + BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink; + void* pvReturn = NULL; + + acquire_memalloc_mutex(); + { + /* If this is the first call to malloc then the heap will require + initialisation to setup the list of free blocks. */ + if(pxEnd == NULL) { + prvHeapInit(); + } else { + mtCOVERAGE_TEST_MARKER(); + } + + /* Check the requested block size is not so large that the top bit is + set. The top bit of the block size member of the BlockLink_t structure + is used to determine who owns the block - the application or the + kernel, so it must be free. */ + if((xWantedSize & xBlockAllocatedBit) == 0) { + /* The wanted size is increased so it can contain a BlockLink_t + structure in addition to the requested amount of bytes. */ + if(xWantedSize > 0) { + xWantedSize += xHeapStructSize; + + /* Ensure that blocks are always aligned to the required number + of bytes. */ + if((xWantedSize & portBYTE_ALIGNMENT_MASK) != 0x00) { + /* Byte alignment required. */ + xWantedSize += (portBYTE_ALIGNMENT - (xWantedSize & portBYTE_ALIGNMENT_MASK)); + configASSERT((xWantedSize & portBYTE_ALIGNMENT_MASK) == 0); + } else { + mtCOVERAGE_TEST_MARKER(); + } + } else { + mtCOVERAGE_TEST_MARKER(); + } + + if((xWantedSize > 0) && (xWantedSize <= xFreeBytesRemaining)) { + /* Traverse the list from the start (lowest address) block until + one of adequate size is found. */ + pxPreviousBlock = &xStart; + pxBlock = xStart.pxNextFreeBlock; + while((pxBlock->xBlockSize < xWantedSize) && (pxBlock->pxNextFreeBlock != NULL)) { + pxPreviousBlock = pxBlock; + pxBlock = pxBlock->pxNextFreeBlock; + } + + /* If the end marker was reached then a block of adequate size + was not found. */ + if(pxBlock != pxEnd) { + /* Return the memory space pointed to - jumping over the + BlockLink_t structure at its start. */ + pvReturn = + (void*)(((uint8_t*)pxPreviousBlock->pxNextFreeBlock) + xHeapStructSize); + + /* This block is being returned for use so must be taken out + of the list of free blocks. */ + pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock; + + /* If the block is larger than required it can be split into + two. */ + if((pxBlock->xBlockSize - xWantedSize) > heapMINIMUM_BLOCK_SIZE) { + /* This block is to be split into two. Create a new + block following the number of bytes requested. The void + cast is used to prevent byte alignment warnings from the + compiler. */ + pxNewBlockLink = (void*)(((uint8_t*)pxBlock) + xWantedSize); + configASSERT((((size_t)pxNewBlockLink) & portBYTE_ALIGNMENT_MASK) == 0); + + /* Calculate the sizes of two blocks split from the + single block. */ + pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize; + pxBlock->xBlockSize = xWantedSize; + + /* Insert the new block into the list of free blocks. */ + prvInsertBlockIntoFreeList(pxNewBlockLink); + } else { + mtCOVERAGE_TEST_MARKER(); + } + + xFreeBytesRemaining -= pxBlock->xBlockSize; + + if(xFreeBytesRemaining < xMinimumEverFreeBytesRemaining) { + xMinimumEverFreeBytesRemaining = xFreeBytesRemaining; + } else { + mtCOVERAGE_TEST_MARKER(); + } + + /* The block is being returned - it is allocated and owned + by the application and has no "next" block. */ + pxBlock->xBlockSize |= xBlockAllocatedBit; + pxBlock->pxNextFreeBlock = NULL; + } else { + mtCOVERAGE_TEST_MARKER(); + } + } else { + mtCOVERAGE_TEST_MARKER(); + } + } else { + mtCOVERAGE_TEST_MARKER(); + } + + traceMALLOC(pvReturn, xWantedSize); + } + release_memalloc_mutex(); + +#if(configUSE_MALLOC_FAILED_HOOK == 1) + { + if(pvReturn == NULL) { + extern void vApplicationMallocFailedHook(void); + vApplicationMallocFailedHook(); + } else { + mtCOVERAGE_TEST_MARKER(); + } + } +#endif + + configASSERT((((size_t)pvReturn) & (size_t)portBYTE_ALIGNMENT_MASK) == 0); + return pvReturn; +} +/*-----------------------------------------------------------*/ + +void vPortFree(void* pv) { + uint8_t* puc = (uint8_t*)pv; + BlockLink_t* pxLink; + + if(pv != NULL) { + /* The memory being freed will have an BlockLink_t structure immediately + before it. */ + puc -= xHeapStructSize; + + /* This casting is to keep the compiler from issuing warnings. */ + pxLink = (void*)puc; + + /* Check the block is actually allocated. */ + configASSERT((pxLink->xBlockSize & xBlockAllocatedBit) != 0); + configASSERT(pxLink->pxNextFreeBlock == NULL); + + if((pxLink->xBlockSize & xBlockAllocatedBit) != 0) { + if(pxLink->pxNextFreeBlock == NULL) { + /* The block is being returned to the heap - it is no longer + allocated. */ + pxLink->xBlockSize &= ~xBlockAllocatedBit; + + acquire_memalloc_mutex(); + { + /* Add this block to the list of free blocks. */ + xFreeBytesRemaining += pxLink->xBlockSize; + traceFREE(pv, pxLink->xBlockSize); + prvInsertBlockIntoFreeList(((BlockLink_t*)pxLink)); + } + release_memalloc_mutex(); + } else { + mtCOVERAGE_TEST_MARKER(); + } + } else { + mtCOVERAGE_TEST_MARKER(); + } + } +} +/*-----------------------------------------------------------*/ + +size_t xPortGetFreeHeapSize(void) { + return xFreeBytesRemaining; +} +/*-----------------------------------------------------------*/ + +size_t xPortGetMinimumEverFreeHeapSize(void) { + return xMinimumEverFreeBytesRemaining; +} +/*-----------------------------------------------------------*/ + +void vPortInitialiseBlocks(void) { + /* This just exists to keep the linker quiet. */ +} +/*-----------------------------------------------------------*/ + +bool prvHeapInit(void) { + BlockLink_t* pxFirstFreeBlock; + uint8_t* pucAlignedHeap; + size_t uxAddress; + size_t xTotalHeapSize = configTOTAL_HEAP_SIZE; + + /* Ensure the heap starts on a correctly aligned boundary. */ + uxAddress = (size_t)ucHeap; + + if((uxAddress & portBYTE_ALIGNMENT_MASK) != 0) { + uxAddress += (portBYTE_ALIGNMENT - 1); + uxAddress &= ~((size_t)portBYTE_ALIGNMENT_MASK); + xTotalHeapSize -= uxAddress - (size_t)ucHeap; + } + + pucAlignedHeap = (uint8_t*)uxAddress; + + /* xStart is used to hold a pointer to the first item in the list of free + blocks. The void cast is used to prevent compiler warnings. */ + xStart.pxNextFreeBlock = (void*)pucAlignedHeap; + xStart.xBlockSize = (size_t)0; + + /* pxEnd is used to mark the end of the list of free blocks and is inserted + at the end of the heap space. */ + uxAddress = ((size_t)pucAlignedHeap) + xTotalHeapSize; + uxAddress -= xHeapStructSize; + uxAddress &= ~((size_t)portBYTE_ALIGNMENT_MASK); + pxEnd = (void*)uxAddress; + pxEnd->xBlockSize = 0; + pxEnd->pxNextFreeBlock = NULL; + + /* To start with there is a single free block that is sized to take up the + entire heap space, minus the space taken by pxEnd. */ + pxFirstFreeBlock = (void*)pucAlignedHeap; + pxFirstFreeBlock->xBlockSize = uxAddress - (size_t)pxFirstFreeBlock; + pxFirstFreeBlock->pxNextFreeBlock = pxEnd; + + /* Only one block exists - and it covers the entire usable heap space. */ + xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize; + xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize; + + /* Work out the position of the top bit in a size_t variable. */ + xBlockAllocatedBit = ((size_t)1) << ((sizeof(size_t) * heapBITS_PER_BYTE) - 1); + + // now we can use malloc, so we init heap managment mutex + const osMutexAttr_t heap_managment_mutext_attr = { + .name = NULL, .attr_bits = 0, .cb_mem = NULL, .cb_size = 0U}; + + heap_managment_mutex = osMutexNew(&heap_managment_mutext_attr); + + return heap_managment_mutex != NULL; +} +/*-----------------------------------------------------------*/ + +static void prvInsertBlockIntoFreeList(BlockLink_t* pxBlockToInsert) { + BlockLink_t* pxIterator; + uint8_t* puc; + + /* Iterate through the list until a block is found that has a higher address + than the block being inserted. */ + for(pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; + pxIterator = pxIterator->pxNextFreeBlock) { + /* Nothing to do here, just iterate to the right position. */ + } + + /* Do the block being inserted, and the block it is being inserted after + make a contiguous block of memory? */ + puc = (uint8_t*)pxIterator; + if((puc + pxIterator->xBlockSize) == (uint8_t*)pxBlockToInsert) { + pxIterator->xBlockSize += pxBlockToInsert->xBlockSize; + pxBlockToInsert = pxIterator; + } else { + mtCOVERAGE_TEST_MARKER(); + } + + /* Do the block being inserted, and the block it is being inserted before + make a contiguous block of memory? */ + puc = (uint8_t*)pxBlockToInsert; + if((puc + pxBlockToInsert->xBlockSize) == (uint8_t*)pxIterator->pxNextFreeBlock) { + if(pxIterator->pxNextFreeBlock != pxEnd) { + /* Form one big block from the two blocks. */ + pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize; + pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock; + } else { + pxBlockToInsert->pxNextFreeBlock = pxEnd; + } + } else { + pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; + } + + /* If the block being inserted plugged a gab, so was merged with the block + before and the block after, then it's pxNextFreeBlock pointer will have + already been set, and should not be set here as that would make it point + to itself. */ + if(pxIterator != pxBlockToInsert) { + pxIterator->pxNextFreeBlock = pxBlockToInsert; + } else { + mtCOVERAGE_TEST_MARKER(); + } +} + +/* +at first run (heap init) it not work properly and prvHeapInit +is not thread-safe. But then we init mutex or die +*/ +void acquire_memalloc_mutex() { + if(heap_managment_mutex != NULL) { + osMutexAcquire(heap_managment_mutex, osWaitForever); + } +} + +void release_memalloc_mutex() { + if(heap_managment_mutex != NULL) { + osMutexRelease(heap_managment_mutex); + } +} \ No newline at end of file diff --git a/firmware/targets/local/Src/main.c b/firmware/targets/local/Src/main.c index 64b2d05b..d61403c2 100644 --- a/firmware/targets/local/Src/main.c +++ b/firmware/targets/local/Src/main.c @@ -1,3 +1,6 @@ +#include "heap.h" +#include "errno.h" + /* Flipper devices inc. @@ -7,5 +10,10 @@ Local fw build entry point. int app(); int main() { + // this function is not thread-safe, so it must be called in single thread context + if(!prvHeapInit()){ + return ENOMEM; + } + return app(); } \ No newline at end of file diff --git a/firmware/targets/local/fatfs/heap.h b/firmware/targets/local/fatfs/heap.h new file mode 100644 index 00000000..45c883eb --- /dev/null +++ b/firmware/targets/local/fatfs/heap.h @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +#define configTOTAL_HEAP_SIZE ((size_t)(8192 * 16)) +#define configAPPLICATION_ALLOCATED_HEAP 0 +#define portBYTE_ALIGNMENT 8 + +#if portBYTE_ALIGNMENT == 8 +#define portBYTE_ALIGNMENT_MASK (0x0007) +#endif + +/* No test marker by default. */ +#ifndef mtCOVERAGE_TEST_MARKER +#define mtCOVERAGE_TEST_MARKER() +#endif + +/* No tracing by default. */ +#ifndef traceMALLOC +#define traceMALLOC(pvReturn, xWantedSize) +#endif + +/* No tracing by default. */ +#ifndef traceFREE +#define traceFREE(pvReturn, xBlockSize) +#endif + +/* No assert by default. */ +#ifndef configASSERT +#define configASSERT(var) +#endif + +bool prvHeapInit(void); + +void acquire_memalloc_mutex(); +void release_memalloc_mutex(); \ No newline at end of file diff --git a/firmware/targets/local/target.mk b/firmware/targets/local/target.mk index 968adca1..741b16ab 100644 --- a/firmware/targets/local/target.mk +++ b/firmware/targets/local/target.mk @@ -15,5 +15,8 @@ LDFLAGS += -pthread CFLAGS += -I$(TARGET_DIR)/fatfs C_SOURCES += $(TARGET_DIR)/fatfs/syscall.c +# memory manager +C_SOURCES += $(TARGET_DIR)/Src/heap_4.c + run: all $(OBJ_DIR)/$(PROJECT).elf \ No newline at end of file From 05ef19b07a5f7aa383676249a9fd1b3b99892d11 Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Fri, 16 Oct 2020 04:23:18 +1000 Subject: [PATCH 14/26] Pubsub core api feature (#174) * fixed inline functions for modern C standart * pubsub api, base version * basic test for pubsub * update applications.mk, add test file * more test for pubsub * remove unimplemented files, cleanup header file * remove legacy tests, check unsubscribe not call cb * implement deleting mutex, fail test * release mutex before deleting Co-authored-by: aanper --- applications/applications.mk | 1 + applications/tests/furi_pubsub_test.c | 56 ++++++++ applications/tests/furi_record_test.c | 194 -------------------------- applications/tests/minunit_test.c | 16 +-- core/api-basic/pubsub.c | 90 ++++++++++++ core/api-basic/pubsub.c.unimplemented | 48 ------- core/api-basic/pubsub.h | 34 +++-- core/flipper_v2.h | 4 +- firmware/targets/local/Inc/cmsis_os.h | 1 + firmware/targets/local/Src/lo_os.c | 12 ++ 10 files changed, 188 insertions(+), 268 deletions(-) create mode 100644 applications/tests/furi_pubsub_test.c create mode 100644 core/api-basic/pubsub.c delete mode 100644 core/api-basic/pubsub.c.unimplemented diff --git a/applications/applications.mk b/applications/applications.mk index c24adee3..9991b367 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -26,6 +26,7 @@ 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 diff --git a/applications/tests/furi_pubsub_test.c b/applications/tests/furi_pubsub_test.c new file mode 100644 index 00000000..4447f542 --- /dev/null +++ b/applications/tests/furi_pubsub_test.c @@ -0,0 +1,56 @@ +#include +#include +#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 +} \ No newline at end of file diff --git a/applications/tests/furi_record_test.c b/applications/tests/furi_record_test.c index f3fdcf1c..4481602c 100644 --- a/applications/tests/furi_record_test.c +++ b/applications/tests/furi_record_test.c @@ -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; -} \ No newline at end of file diff --git a/applications/tests/minunit_test.c b/applications/tests/minunit_test.c index 631c1892..472dfa7d 100644 --- a/applications/tests/minunit_test.c +++ b/applications/tests/minunit_test.c @@ -7,13 +7,12 @@ 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; @@ -38,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(); @@ -55,6 +50,10 @@ 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 @@ -68,12 +67,11 @@ 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); } diff --git a/core/api-basic/pubsub.c b/core/api-basic/pubsub.c new file mode 100644 index 00000000..9b06705a --- /dev/null +++ b/core/api-basic/pubsub.c @@ -0,0 +1,90 @@ +#include "pubsub.h" + +bool init_pubsub(PubSub* pubsub) { + // mutex without name, + // no attributes (unfortunatly robust mutex is not supported by FreeRTOS), + // with dynamic memory allocation + const osMutexAttr_t value_mutex_attr = { + .name = NULL, .attr_bits = 0, .cb_mem = NULL, .cb_size = 0U}; + + pubsub->mutex = osMutexNew(&value_mutex_attr); + if(pubsub->mutex == NULL) return false; + + // construct list + list_pubsub_cb_init(pubsub->items); + + return true; +} + +bool delete_pubsub(PubSub* pubsub) { + if(osMutexAcquire(pubsub->mutex, osWaitForever) == osOK) { + bool result = osMutexDelete(pubsub->mutex) == osOK; + list_pubsub_cb_clear(pubsub->items); + return result; + } else { + return false; + } +} + +PubSubItem* subscribe_pubsub(PubSub* pubsub, PubSubCallback cb, void* ctx) { + if(osMutexAcquire(pubsub->mutex, osWaitForever) == osOK) { + // put uninitialized item to the list + PubSubItem* item = list_pubsub_cb_push_raw(pubsub->items); + + // initialize item + item->cb = cb; + item->ctx = ctx; + item->self = pubsub; + + // TODO unsubscribe pubsub on app exit + //flapp_on_exit(unsubscribe_pubsub, item); + + osMutexRelease(pubsub->mutex); + + return item; + } else { + return NULL; + } +} + +bool unsubscribe_pubsub(PubSubItem* pubsub_id) { + if(osMutexAcquire(pubsub_id->self->mutex, osWaitForever) == osOK) { + bool result = false; + + // iterate over items + list_pubsub_cb_it_t it; + for(list_pubsub_cb_it(it, pubsub_id->self->items); !list_pubsub_cb_end_p(it); + list_pubsub_cb_next(it)) { + const PubSubItem* item = list_pubsub_cb_cref(it); + + // if the iterator is equal to our element + if(item == pubsub_id) { + list_pubsub_cb_remove(pubsub_id->self->items, it); + result = true; + break; + } + } + + osMutexRelease(pubsub_id->self->mutex); + return result; + } else { + return false; + } +} + +bool notify_pubsub(PubSub* pubsub, void* arg) { + if(osMutexAcquire(pubsub->mutex, osWaitForever) == osOK) { + // iterate over subscribers + list_pubsub_cb_it_t it; + for(list_pubsub_cb_it(it, pubsub->items); !list_pubsub_cb_end_p(it); + list_pubsub_cb_next(it)) { + const PubSubItem* item = list_pubsub_cb_cref(it); + item->cb(arg, item->ctx); + } + + osMutexRelease(pubsub->mutex); + return true; + } else { + return false; + } +} diff --git a/core/api-basic/pubsub.c.unimplemented b/core/api-basic/pubsub.c.unimplemented deleted file mode 100644 index 77132f75..00000000 --- a/core/api-basic/pubsub.c.unimplemented +++ /dev/null @@ -1,48 +0,0 @@ -#include "pubsub.h" - -void init_pubsub(PubSub* pubsub) { - pubsub->count = 0; - - for(size_t i = 0; i < NUM_OF_CALLBACKS; i++) { - pubsub->items[i]. - } -} - -// TODO add mutex to reconfigurate PubSub -PubSubId* subscribe_pubsub(PubSub* pubsub, PubSubCallback cb, void* ctx) { - if(pubsub->count >= NUM_OF_CALLBACKS) return NULL; - - pubsub->count++; - PubSubItem* current = pubsub->items[pubsub->count]; - - current->cb = cb; - currrnt->ctx = ctx; - - pubsub->ids[pubsub->count].self = pubsub; - pubsub->ids[pubsub->count].item = current; - - flapp_on_exit(unsubscribe_pubsub, &(pubsub->ids[pubsub->count])); - - return current; -} - -void unsubscribe_pubsub(PubSubId* pubsub_id) { - // TODO: add, and rearrange all items to keep subscribers item continuous - // TODO: keep ids link actual - // TODO: also add mutex on every pubsub changes - - // trivial implementation for NUM_OF_CALLBACKS = 1 - if(NUM_OF_CALLBACKS != 1) return; - - if(pubsub_id != NULL || pubsub_id->self != NULL || pubsub_id->item != NULL) return; - - pubsub_id->self->count = 0; - pubsub_id->item = NULL; -} - -void notify_pubsub(PubSub* pubsub, void* arg) { - // iterate over subscribers - for(size_t i = 0; i < pubsub->count; i++) { - pubsub->items[i]->cb(arg, pubsub->items[i]->ctx); - } -} diff --git a/core/api-basic/pubsub.h b/core/api-basic/pubsub.h index bd38cc1f..1c235159 100644 --- a/core/api-basic/pubsub.h +++ b/core/api-basic/pubsub.h @@ -1,6 +1,7 @@ #pragma once -#include "flipper.h" +#include "flipper_v2.h" +#include "m-list.h" /* == PubSub == @@ -11,43 +12,46 @@ and also subscriber can set `void*` context pointer that pass into callback (you can see callback signature below). */ -typedef void(PubSubCallback*)(void*, void*); +typedef void (*PubSubCallback)(void*, void*); +typedef struct PubSubType PubSub; typedef struct { PubSubCallback cb; void* ctx; + PubSub* self; } PubSubItem; -typedef struct { - PubSub* self; - PubSubItem* item; -} PubSubId; +LIST_DEF(list_pubsub_cb, PubSubItem, M_POD_OPLIST); -typedef struct { - PubSubItem items[NUM_OF_CALLBACKS]; - PubSubId ids[NUM_OF_CALLBACKS]; ///< permanent links to item - size_t count; ///< count of callbacks -} PubSub; +struct PubSubType { + list_pubsub_cb_t items; + osMutexId_t mutex; +}; /* To create PubSub you should create PubSub instance and call `init_pubsub`. */ -void init_pubsub(PubSub* pubsub); +bool init_pubsub(PubSub* pubsub); + +/* +Since we use dynamic memory - we must explicity delete pubsub +*/ +bool delete_pubsub(PubSub* pubsub); /* Use `subscribe_pubsub` to register your callback. */ -PubSubId* subscribe_pubsub(PubSub* pubsub, PubSubCallback cb, void* ctx); +PubSubItem* subscribe_pubsub(PubSub* pubsub, PubSubCallback cb, void* ctx); /* Use `unsubscribe_pubsub` to unregister callback. */ -void unsubscribe_pubsub(PubSubId* pubsub_id); +bool unsubscribe_pubsub(PubSubItem* pubsub_id); /* Use `notify_pubsub` to notify subscribers. */ -void notify_pubsub(PubSub* pubsub, void* arg); +bool notify_pubsub(PubSub* pubsub, void* arg); /* diff --git a/core/flipper_v2.h b/core/flipper_v2.h index d16cbacd..9e3bc90e 100644 --- a/core/flipper_v2.h +++ b/core/flipper_v2.h @@ -4,6 +4,6 @@ //#include "api-basic/flapp.h" #include "cmsis_os2.h" #include "api-basic/valuemutex.h" -//#include "api-basic/pubsub.h" +#include "api-basic/pubsub.h" -#include "api-basic/memmgr.h" \ No newline at end of file +#include "api-basic/memmgr.h" diff --git a/firmware/targets/local/Inc/cmsis_os.h b/firmware/targets/local/Inc/cmsis_os.h index ebe04567..c1c574ab 100644 --- a/firmware/targets/local/Inc/cmsis_os.h +++ b/firmware/targets/local/Inc/cmsis_os.h @@ -94,5 +94,6 @@ typedef enum { osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout); osStatus_t osMutexRelease (osMutexId_t mutex_id); +osStatus_t osMutexDelete (osMutexId_t mutex_id); #define osWaitForever portMAX_DELAY diff --git a/firmware/targets/local/Src/lo_os.c b/firmware/targets/local/Src/lo_os.c index e3603990..94c570a3 100644 --- a/firmware/targets/local/Src/lo_os.c +++ b/firmware/targets/local/Src/lo_os.c @@ -253,3 +253,15 @@ osStatus_t osMutexRelease (osMutexId_t mutex_id) { return osError; } } + +osStatus_t osMutexDelete (osMutexId_t mutex_id) { + osMutexRelease(mutex_id); + + int res = 0; + if((res = pthread_mutex_destroy(&mutex_id->mutex)) == 0) { + return osOK; + } else { + printf("res = %d\n", res); + return osError; + } +} From 571f97e2b3c39f9a1bb3c8d8f9a49680753844c2 Mon Sep 17 00:00:00 2001 From: aanper Date: Fri, 16 Oct 2020 09:45:38 +0300 Subject: [PATCH 15/26] hotfix usb stack --- firmware/targets/f2/Inc/usbd_conf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/targets/f2/Inc/usbd_conf.h b/firmware/targets/f2/Inc/usbd_conf.h index 5c52dbc2..272ad1e8 100644 --- a/firmware/targets/f2/Inc/usbd_conf.h +++ b/firmware/targets/f2/Inc/usbd_conf.h @@ -92,10 +92,10 @@ /* Memory management macros */ /** Alias for memory allocation. */ -#define USBD_malloc malloc +#define USBD_malloc USBD_static_malloc /** Alias for memory release. */ -#define USBD_free free +#define USBD_free USBD_static_free /** Alias for memory set. */ #define USBD_memset memset From d715767cef509a7b4de0d836ba13a771ed3d266e Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 15:16:54 +0300 Subject: [PATCH 16/26] GUI: osWaitForeverfor in queue put, simplify osMessageQueueGet. --- applications/gui/gui_event.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/applications/gui/gui_event.c b/applications/gui/gui_event.c index 8d0914c2..44b4d5fc 100644 --- a/applications/gui/gui_event.c +++ b/applications/gui/gui_event.c @@ -19,7 +19,7 @@ void gui_event_input_events_callback(const void* value, size_t size, void* ctx) message.type = GuiMessageTypeInput; message.input = *(InputEvent*)value; - osMessageQueuePut(gui_event->mqueue, &message, 0, 0); + osMessageQueuePut(gui_event->mqueue, &message, 0, osWaitForever); } GuiEvent* gui_event_alloc() { @@ -67,8 +67,7 @@ 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) { - }; + assert(osMessageQueueGet(gui_event->mqueue, &message, NULL, osWaitForever) == osOK); gui_event_lock(gui_event); return message; } From e213954d2da3d3cbefc81559032ebb2059b0d39a Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 15:18:36 +0300 Subject: [PATCH 17/26] Menu: item callback function context, event osWaitForever timeouts. --- applications/menu/menu.c | 30 ++++++++++++++---------------- applications/menu/menu_event.c | 4 ++-- applications/menu/menu_item.c | 27 ++++++++++++++++++++++----- applications/menu/menu_item.h | 6 +++--- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 156e7f60..0ef37d67 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -49,22 +49,21 @@ void menu_build_main(Menu* 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_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)); 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_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); } @@ -149,8 +148,7 @@ void menu_ok(Menu* menu) { menu->position = 0; menu_update(menu); } else if(type == MenuItemTypeFunction) { - MenuItemCallback function = menu_item_get_function(item); - if(function) function(); + menu_item_function_call(item); } } diff --git a/applications/menu/menu_event.c b/applications/menu/menu_event.c index 82b32f8f..bd479972 100644 --- a/applications/menu/menu_event.c +++ b/applications/menu/menu_event.c @@ -19,7 +19,7 @@ void MenuEventimeout_callback(void* arg) { MenuEvent* menu_event = arg; MenuMessage message; message.type = MenuMessageTypeIdle; - osMessageQueuePut(menu_event->mqueue, &message, 0, 0); + osMessageQueuePut(menu_event->mqueue, &message, 0, osWaitForever); } MenuEvent* menu_event_alloc() { @@ -92,5 +92,5 @@ void menu_event_input_callback(InputEvent* input_event, void* context) { message.type = MenuMessageTypeUnknown; } - osMessageQueuePut(menu_event->mqueue, &message, 0, 0); + osMessageQueuePut(menu_event->mqueue, &message, 0, osWaitForever); } diff --git a/applications/menu/menu_item.c b/applications/menu/menu_item.c index 57072bc2..e4d3143f 100644 --- a/applications/menu/menu_item.c +++ b/applications/menu/menu_item.c @@ -10,6 +10,8 @@ struct MenuItem { void* icon; MenuItem* parent; void* data; + MenuItemCallback callback; + void* callback_context; }; MenuItem* menu_item_alloc() { @@ -31,27 +33,34 @@ MenuItem* menu_item_alloc_menu(const char* label, void* icon) { return menu_item; } -MenuItem* menu_item_alloc_function(const char* label, void* icon, MenuItemCallback function) { +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->data = function; + menu_item->callback = callback; + menu_item->callback_context = context; return menu_item; } void menu_item_release(MenuItem* menu_item) { - if(menu_item->type == MenuItemTypeMenu) free(menu_item->data); + 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; @@ -59,31 +68,39 @@ void menu_item_subitem_add(MenuItem* menu_item, MenuItem* 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; } -MenuItemCallback menu_item_get_function(MenuItem* menu_item) { +void menu_item_function_call(MenuItem* menu_item) { + assert(menu_item); assert(menu_item->type == MenuItemTypeFunction); - return menu_item->data; + + if(menu_item->callback) menu_item->callback(menu_item->callback_context); } diff --git a/applications/menu/menu_item.h b/applications/menu/menu_item.h index 57b81124..19f1a38e 100644 --- a/applications/menu/menu_item.h +++ b/applications/menu/menu_item.h @@ -9,13 +9,13 @@ typedef enum { } MenuItemType; typedef struct MenuItem MenuItem; -typedef void (*MenuItemCallback)(); +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 function); +MenuItem* menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context); void menu_item_release(MenuItem* menu_item); @@ -33,4 +33,4 @@ 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); +void menu_item_function_call(MenuItem* menu_item); From 23478fd175b9c3eb02430446217bce01c30a286d Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 16:11:51 +0300 Subject: [PATCH 18/26] Menu: code format --- applications/menu/menu_item.c | 3 ++- applications/menu/menu_item.h | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/applications/menu/menu_item.c b/applications/menu/menu_item.c index e4d3143f..87c081c1 100644 --- a/applications/menu/menu_item.c +++ b/applications/menu/menu_item.c @@ -33,7 +33,8 @@ MenuItem* menu_item_alloc_menu(const char* label, void* icon) { return menu_item; } -MenuItem* menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context) { +MenuItem* +menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context) { MenuItem* menu_item = menu_item_alloc(); menu_item->type = MenuItemTypeFunction; diff --git a/applications/menu/menu_item.h b/applications/menu/menu_item.h index 19f1a38e..06838d6e 100644 --- a/applications/menu/menu_item.h +++ b/applications/menu/menu_item.h @@ -9,13 +9,14 @@ typedef enum { } MenuItemType; typedef struct MenuItem MenuItem; -typedef void (*MenuItemCallback)(void *context); +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); +MenuItem* +menu_item_alloc_function(const char* label, void* icon, MenuItemCallback callback, void* context); void menu_item_release(MenuItem* menu_item); From 1a36dfc5d32eed8148a3f7d0078494ab35cf0403 Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 16:34:36 +0300 Subject: [PATCH 19/26] GUI: hide canvas inside --- applications/gui/canvas.c | 87 +++++++++++++++++++++++---------------- applications/gui/canvas.h | 29 +++---------- applications/menu/menu.c | 4 +- 3 files changed, 60 insertions(+), 60 deletions(-) diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c index 6dc7962c..c353bca3 100644 --- a/applications/gui/canvas.c +++ b/applications/gui/canvas.c @@ -4,6 +4,16 @@ #include #include +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); @@ -15,30 +25,26 @@ uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, vo uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr); CanvasApi* canvas_api_init() { - CanvasApi* api = furi_alloc(sizeof(CanvasApi)); + Canvas* canvas = furi_alloc(sizeof(Canvas)); u8g2_Setup_st7565_erc12864_alt_f( - &api->canvas.fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32); + &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(&api->canvas.fb); - u8g2_SetContrast(&api->canvas.fb, 36); + u8g2_InitDisplay(&canvas->fb); + u8g2_SetContrast(&canvas->fb, 36); - u8g2_SetPowerSave(&api->canvas.fb, 0); // wake up display - u8g2_SendBuffer(&api->canvas.fb); + u8g2_SetPowerSave(&canvas->fb, 0); // wake up display + u8g2_SendBuffer(&canvas->fb); - api->width = canvas_width; - api->height = canvas_height; - api->clear = canvas_clear; - api->set_color = canvas_color_set; - api->set_font = canvas_font_set; - api->draw_str = canvas_str_draw; + 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; - api->fonts = furi_alloc(sizeof(Fonts)); - api->fonts->primary = u8g2_font_Born2bSportyV2_tr; - api->fonts->secondary = u8g2_font_HelvetiPixel_tr; - - return api; + return (CanvasApi*)canvas; } void canvas_api_free(CanvasApi* api) { @@ -48,9 +54,9 @@ void canvas_api_free(CanvasApi* api) { void canvas_commit(CanvasApi* api) { assert(api); - - u8g2_SetPowerSave(&api->canvas.fb, 0); // wake up display - u8g2_SendBuffer(&api->canvas.fb); + Canvas* canvas = (Canvas *)api; + u8g2_SetPowerSave(&canvas->fb, 0); // wake up display + u8g2_SendBuffer(&canvas->fb); } void canvas_frame_set( @@ -60,43 +66,54 @@ void canvas_frame_set( uint8_t width, uint8_t height) { assert(api); - api->canvas.offset_x = offset_x; - api->canvas.offset_y = offset_y; - api->canvas.width = width; - api->canvas.height = height; + 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); - - return api->canvas.width; + Canvas* canvas = (Canvas *)api; + return canvas->width; } uint8_t canvas_height(CanvasApi* api) { assert(api); - - return api->canvas.height; + Canvas* canvas = (Canvas *)api; + return canvas->height; } void canvas_clear(CanvasApi* api) { assert(api); - u8g2_ClearBuffer(&api->canvas.fb); + Canvas* canvas = (Canvas *)api; + u8g2_ClearBuffer(&canvas->fb); } void canvas_color_set(CanvasApi* api, Color color) { assert(api); - u8g2_SetDrawColor(&api->canvas.fb, color); + Canvas* canvas = (Canvas *)api; + u8g2_SetDrawColor(&canvas->fb, color); } void canvas_font_set(CanvasApi* api, Font font) { assert(api); - u8g2_SetFontMode(&api->canvas.fb, 1); - u8g2_SetFont(&api->canvas.fb, font); + 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); - x += api->canvas.offset_x; - y += api->canvas.offset_y; - u8g2_DrawStr(&api->canvas.fb, x, y, str); + Canvas* canvas = (Canvas *)api; + x += canvas->offset_x; + y += canvas->offset_y; + u8g2_DrawStr(&canvas->fb, x, y, str); } diff --git a/applications/gui/canvas.h b/applications/gui/canvas.h index 27dd2efe..237a75de 100644 --- a/applications/gui/canvas.h +++ b/applications/gui/canvas.h @@ -8,30 +8,13 @@ typedef enum { ColorBlack = 0x01, } Color; -typedef const uint8_t* Font; - -typedef struct { - Font primary; - Font secondary; -} Fonts; - -struct _CanvasApi; - -typedef struct _CanvasApi CanvasApi; - -// Canvas is private but we need its declaration here -typedef struct { - u8g2_t fb; - uint8_t offset_x; - uint8_t offset_y; - uint8_t width; - uint8_t height; -} Canvas; - -struct _CanvasApi { - Canvas canvas; - Fonts* fonts; +typedef enum { + FontPrimary = 0x00, + FontSecondary = 0x01, +} Font; +typedef struct CanvasApi CanvasApi; +struct CanvasApi { uint8_t (*width)(CanvasApi* canvas); uint8_t (*height)(CanvasApi* canvas); diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 0ef37d67..a0261bf5 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -87,13 +87,13 @@ void menu_widget_callback(CanvasApi* canvas, void* context) { if(!menu->current) { canvas->clear(canvas); canvas->set_color(canvas, ColorBlack); - canvas->set_font(canvas, canvas->fonts->primary); + 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, canvas->fonts->secondary); + 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)); From 491343b683929fc1745e3338b78a932dd6803799 Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 16:36:05 +0300 Subject: [PATCH 20/26] GUI: reformat sources --- applications/gui/canvas.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/applications/gui/canvas.c b/applications/gui/canvas.c index c353bca3..842397bd 100644 --- a/applications/gui/canvas.c +++ b/applications/gui/canvas.c @@ -54,7 +54,7 @@ void canvas_api_free(CanvasApi* api) { void canvas_commit(CanvasApi* api) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; u8g2_SetPowerSave(&canvas->fb, 0); // wake up display u8g2_SendBuffer(&canvas->fb); } @@ -75,35 +75,35 @@ void canvas_frame_set( uint8_t canvas_width(CanvasApi* api) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; return canvas->width; } uint8_t canvas_height(CanvasApi* api) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; return canvas->height; } void canvas_clear(CanvasApi* api) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; u8g2_ClearBuffer(&canvas->fb); } void canvas_color_set(CanvasApi* api, Color color) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; u8g2_SetDrawColor(&canvas->fb, color); } void canvas_font_set(CanvasApi* api, Font font) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; u8g2_SetFontMode(&canvas->fb, 1); - if (font == FontPrimary) { + if(font == FontPrimary) { u8g2_SetFont(&canvas->fb, u8g2_font_Born2bSportyV2_tr); - } else if (font == FontSecondary) { + } else if(font == FontSecondary) { u8g2_SetFont(&canvas->fb, u8g2_font_HelvetiPixel_tr); } else { assert(0); @@ -112,7 +112,7 @@ void canvas_font_set(CanvasApi* api, Font font) { void canvas_str_draw(CanvasApi* api, uint8_t x, uint8_t y, const char* str) { assert(api); - Canvas* canvas = (Canvas *)api; + Canvas* canvas = (Canvas*)api; x += canvas->offset_x; y += canvas->offset_y; u8g2_DrawStr(&canvas->fb, x, y, str); From f0800e579f785c71998cae4ed03f36826a482392 Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 18:25:06 +0300 Subject: [PATCH 21/26] GUI: GuiApi isolation --- applications/gui/gui.c | 37 ++++++++++++++++--------------------- applications/gui/gui.h | 8 ++------ applications/gui/gui_i.h | 2 ++ applications/gui/widget.c | 2 +- applications/gui/widget_i.h | 2 +- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/applications/gui/gui.c b/applications/gui/gui.c index 6be88315..38243562 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -15,6 +15,7 @@ ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST); struct Gui { + GuiApi api; GuiEvent* event; CanvasApi* canvas_api; WidgetArray_t widgets_status_bar; @@ -25,39 +26,37 @@ struct Gui { void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) { assert(gui_api); - assert(gui_api->gui); + 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_api->gui->widgets_status_bar; + widget_array = &gui->widgets_status_bar; break; case WidgetLayerMain: - widget_array = &gui_api->gui->widgets; + widget_array = &gui->widgets; break; case WidgetLayerFullscreen: - widget_array = &gui_api->gui->widgets_fs; + widget_array = &gui->widgets_fs; break; case WidgetLayerDialog: - widget_array = &gui_api->gui->widgets_dialog; + widget_array = &gui->widgets_dialog; break; - default: break; } - assert(widget); assert(widget_array); - gui_event_lock(gui_api->gui->event); - WidgetArray_push_back((struct WidgetArray_s*)widget_array, widget); - widget_gui_set(widget, gui_api->gui); - gui_event_unlock(gui_api->gui->event); + gui_event_lock(gui->event); + WidgetArray_push_back(*widget_array, widget); + widget_gui_set(widget, gui); + gui_event_unlock(gui->event); - gui_update(gui_api->gui); + gui_update(gui); } void gui_update(Gui* gui) { @@ -133,6 +132,7 @@ void gui_input(Gui* gui, InputEvent* input_event) { Gui* gui_alloc() { Gui* gui = furi_alloc(sizeof(Gui)); + // Initialize widget arrays WidgetArray_init(gui->widgets_status_bar); WidgetArray_init(gui->widgets); @@ -143,22 +143,17 @@ Gui* gui_alloc() { 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(); - - GuiApi gui_api = { - .add_widget = gui_add_widget, - .gui = gui, - }; - // Create FURI record - if(!furi_create("gui", &gui_api)) { + if(!furi_create("gui", gui)) { printf("[gui_task] cannot create the gui record\n"); furiac_exit(NULL); } diff --git a/applications/gui/gui.h b/applications/gui/gui.h index 371c0434..8db1cc20 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -11,12 +11,8 @@ typedef enum { } WidgetLayer; typedef struct Widget Widget; -typedef struct Gui Gui; -struct _GuiApi; -typedef struct _GuiApi GuiApi; - -struct _GuiApi { +typedef struct GuiApi GuiApi; +struct GuiApi { void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer); - Gui* gui; }; diff --git a/applications/gui/gui_i.h b/applications/gui/gui_i.h index 6a9bf2a0..a64874c1 100644 --- a/applications/gui/gui_i.h +++ b/applications/gui/gui_i.h @@ -1,3 +1,5 @@ #pragma once +typedef struct Gui Gui; + void gui_update(Gui* gui); diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 3277c24d..4b22679d 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -10,7 +10,7 @@ // TODO add mutex to widget ops struct Widget { - void* gui; + Gui* gui; bool is_enabled; WidgetDrawCallback draw_callback; void* draw_callback_context; diff --git a/applications/gui/widget_i.h b/applications/gui/widget_i.h index f6cc2f14..b3d481e9 100644 --- a/applications/gui/widget_i.h +++ b/applications/gui/widget_i.h @@ -1,6 +1,6 @@ #pragma once -#include "gui.h" +#include "gui_i.h" void widget_gui_set(Widget* widget, Gui* gui); From 3c453a2a2090b1040bba3b14f32e0231479c920e Mon Sep 17 00:00:00 2001 From: aanper Date: Sat, 17 Oct 2020 11:48:29 +0300 Subject: [PATCH 22/26] refactor menu mutexes --- applications/menu/menu.c | 52 ++++++++++++++++++++++++---------- applications/menu/menu_event.c | 17 ----------- applications/menu/menu_event.h | 4 --- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/applications/menu/menu.c b/applications/menu/menu.c index a0261bf5..546fccea 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -25,23 +24,30 @@ struct Menu { void menu_widget_callback(CanvasApi* canvas, void* context); -Menu* menu_init() { +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(); - 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 GuiApi* gui = furi_open("gui"); assert(gui); gui->add_widget(gui, menu->widget, WidgetLayerFullscreen); - return menu; + 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) { @@ -79,10 +85,9 @@ void menu_settings_item_add(Menu* menu, MenuItem* item) { void menu_widget_callback(CanvasApi* canvas, void* context) { assert(canvas); assert(context); - - Menu* menu = context; - - menu_event_lock(menu->event); + + 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); @@ -102,7 +107,8 @@ void menu_widget_callback(CanvasApi* canvas, void* context) { } } - menu_event_unlock(menu->event); + release_mutex((ValueMutex*)context, menu); + } void menu_update(Menu* menu) { @@ -172,10 +178,22 @@ void menu_exit(Menu* menu) { } void menu_task(void* p) { - Menu* menu = menu_init(); - menu_build_main(menu); + ValueMutex* menu_mutex = menu_init(); - if(!furi_create_deprecated("menu", menu, sizeof(menu))) { + 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); } @@ -183,10 +201,12 @@ void menu_task(void* p) { furiac_ready(); while(1) { - MenuMessage m = menu_event_next(menu->event); + MenuMessage m = menu_event_next(menu_event); + + Menu* menu = acquire_mutex_block(menu_mutex); if(!menu->current && m.type != MenuMessageTypeOk) { - continue; + } else if(m.type == MenuMessageTypeUp) { menu_up(menu); } else if(m.type == MenuMessageTypeDown) { @@ -204,5 +224,7 @@ void menu_task(void* p) { } else { // TODO: fail somehow? } + + release_mutex(menu_mutex, menu); } } diff --git a/applications/menu/menu_event.c b/applications/menu/menu_event.c index bd479972..4470ffd6 100644 --- a/applications/menu/menu_event.c +++ b/applications/menu/menu_event.c @@ -12,7 +12,6 @@ struct MenuEvent { osMessageQueueId_t mqueue; osTimerId_t timeout_timer; - osMutexId_t lock_mutex; }; void MenuEventimeout_callback(void* arg) { @@ -32,29 +31,15 @@ MenuEvent* menu_event_alloc() { 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 @@ -63,10 +48,8 @@ void menu_event_activity_notify(MenuEvent* menu_event) { 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; } diff --git a/applications/menu/menu_event.h b/applications/menu/menu_event.h index 858dc27b..031b8f4e 100644 --- a/applications/menu/menu_event.h +++ b/applications/menu/menu_event.h @@ -25,10 +25,6 @@ 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); From 566ba378ff8abd8cb588a9d5231459def150b968 Mon Sep 17 00:00:00 2001 From: aanper Date: Sat, 17 Oct 2020 11:48:52 +0300 Subject: [PATCH 23/26] add apploader example --- applications/app-loader/app-loader.c | 18 ++++++++++++++++++ applications/applications.mk | 1 + applications/startup.h | 2 ++ core/flipper.h | 2 ++ core/flipper_v2.h | 2 ++ 5 files changed, 25 insertions(+) create mode 100644 applications/app-loader/app-loader.c diff --git a/applications/app-loader/app-loader.c b/applications/app-loader/app-loader.c new file mode 100644 index 00000000..e15787af --- /dev/null +++ b/applications/app-loader/app-loader.c @@ -0,0 +1,18 @@ +#include "flipper.h" + +/* +render_loader + +handle_input + +handle_menu +*/ + +void app_loader(void* p) { + osThreadId_t self_id = osThreadGetId(); + assert(self_id); + + printf("[app loader] start\n"); + + osThreadSuspend(self_id); +} \ No newline at end of file diff --git a/applications/applications.mk b/applications/applications.mk index 188a2abe..3b4c4f9a 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -16,6 +16,7 @@ APP_INPUT = 1 APP_GUI = 1 CFLAGS += -DAPP_MENU C_SOURCES += $(wildcard $(APP_DIR)/menu/*.c) +C_SOURCES += $(wildcard $(APP_DIR)/app-loader/*.c) endif APP_TEST ?= 0 diff --git a/applications/startup.h b/applications/startup.h index 847009dc..342a2234 100644 --- a/applications/startup.h +++ b/applications/startup.h @@ -27,6 +27,7 @@ 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 @@ -44,6 +45,7 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { #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 = ""}, diff --git a/core/flipper.h b/core/flipper.h index 8e4cd098..890f0d25 100644 --- a/core/flipper.h +++ b/core/flipper.h @@ -16,6 +16,8 @@ extern "C" { } #endif +#include + // Arduino defines #define pinMode app_gpio_init diff --git a/core/flipper_v2.h b/core/flipper_v2.h index 9e3bc90e..6d3fa47f 100644 --- a/core/flipper_v2.h +++ b/core/flipper_v2.h @@ -1,5 +1,7 @@ #pragma once +#include "flipper.h" + #include "api-basic/furi.h" //#include "api-basic/flapp.h" #include "cmsis_os2.h" From b9fbf7861610ed77ca04fd40be34355f3fb69246 Mon Sep 17 00:00:00 2001 From: aanper Date: Sat, 17 Oct 2020 12:58:14 +0300 Subject: [PATCH 24/26] sandbox gui --- applications/app-loader/app-loader.c | 118 +++++++++++++++++++++++++-- applications/applications.mk | 4 + applications/menu/menu.c | 13 +-- applications/menu/menu.h | 2 + applications/startup.h | 14 ---- core/flipper.h | 2 + 6 files changed, 121 insertions(+), 32 deletions(-) diff --git a/applications/app-loader/app-loader.c b/applications/app-loader/app-loader.c index e15787af..54d5465b 100644 --- a/applications/app-loader/app-loader.c +++ b/applications/app-loader/app-loader.c @@ -1,17 +1,123 @@ -#include "flipper.h" +#include "flipper_v2.h" +#include +#include "menu/menu.h" -/* -render_loader +typedef struct { + FuriApp* handler; + Widget* widget; + FlipperStartupApp* current_app; +} AppLoaderState; -handle_input +typedef struct { + AppLoaderState* state; + FlipperStartupApp* app; +} AppLoaderContext; -handle_menu -*/ +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){ + widget_enabled_set(ctx->widget, false); + } +} + +void handle_menu(void* _ctx) { + AppLoaderContext* ctx = (AppLoaderContext*)_ctx; + + ctx->state->current_app = ctx->app; + + widget_enabled_set(ctx->state->widget, true); + + // TODO create a dependency tree and run tasks in the desired order + // furiac_wait_libs(&FLIPPER_STARTUP[i].libs); + // handlers[i] = furiac_start(FLIPPER_STARTUP[i].app, FLIPPER_STARTUP[i].name, NULL); + // FuriApp* handlers[flipper_app_count]; +} + +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); diff --git a/applications/applications.mk b/applications/applications.mk index 3b4c4f9a..2f1c82dd 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -17,6 +17,10 @@ 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 diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 546fccea..70bb3b07 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -55,17 +55,6 @@ void menu_build_main(Menu* menu) { // Root point menu->root = menu_item_alloc_menu(NULL, NULL); - 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)); - 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)); @@ -206,7 +195,7 @@ void menu_task(void* p) { 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) { diff --git a/applications/menu/menu.h b/applications/menu/menu.h index 01c68be2..f8607fda 100644 --- a/applications/menu/menu.h +++ b/applications/menu/menu.h @@ -1,5 +1,7 @@ #pragma once +#include "menu/menu_item.h" + typedef struct Menu Menu; typedef struct MenuItem MenuItem; diff --git a/applications/startup.h b/applications/startup.h index 342a2234..e0a2683f 100644 --- a/applications/startup.h +++ b/applications/startup.h @@ -2,8 +2,6 @@ #include "flipper.h" -#define FURI_LIB (const char*[]) - #ifdef APP_TEST void flipper_test_app(void* p); #endif @@ -54,23 +52,11 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { {.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 diff --git a/core/flipper.h b/core/flipper.h index 890f0d25..6f867a5f 100644 --- a/core/flipper.h +++ b/core/flipper.h @@ -34,3 +34,5 @@ extern "C" { #define HIGH true void set_exitcode(uint32_t _exitcode); + +#define FURI_LIB (const char*[]) From 0f4799d70b7840bfba5fd7015d9ce8f14ebb8815 Mon Sep 17 00:00:00 2001 From: aanper Date: Sat, 17 Oct 2020 13:10:53 +0300 Subject: [PATCH 25/26] run/kill app, issue with multiple load --- applications/app-loader/app-loader.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/applications/app-loader/app-loader.c b/applications/app-loader/app-loader.c index 54d5465b..1c7219cb 100644 --- a/applications/app-loader/app-loader.c +++ b/applications/app-loader/app-loader.c @@ -29,6 +29,7 @@ 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); } } @@ -36,14 +37,13 @@ void input_callback(InputEvent* input_event, void* _ctx) { void handle_menu(void* _ctx) { AppLoaderContext* ctx = (AppLoaderContext*)_ctx; - ctx->state->current_app = ctx->app; - widget_enabled_set(ctx->state->widget, true); - // TODO create a dependency tree and run tasks in the desired order + // TODO how to call this? // furiac_wait_libs(&FLIPPER_STARTUP[i].libs); - // handlers[i] = furiac_start(FLIPPER_STARTUP[i].app, FLIPPER_STARTUP[i].name, NULL); - // FuriApp* handlers[flipper_app_count]; + + ctx->state->current_app = ctx->app; + ctx->state->handler = furiac_start(ctx->app->app, ctx->app->name, NULL); } void application_blink(void* p); From 66f6df08cc08a6d6b4eef1a0dfc93922c3559eda Mon Sep 17 00:00:00 2001 From: aanper Date: Sat, 17 Oct 2020 13:41:50 +0300 Subject: [PATCH 26/26] syntax fix --- applications/app-loader/app-loader.c | 10 +++------- applications/menu/menu.c | 4 +--- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/applications/app-loader/app-loader.c b/applications/app-loader/app-loader.c index 1c7219cb..81a9d84c 100644 --- a/applications/app-loader/app-loader.c +++ b/applications/app-loader/app-loader.c @@ -28,7 +28,7 @@ void render_callback(CanvasApi* canvas, void* _ctx) { void input_callback(InputEvent* input_event, void* _ctx) { AppLoaderState* ctx = (AppLoaderState*)_ctx; - if(input_event->state && input_event->input == InputBack){ + if(input_event->state && input_event->input == InputBack) { furiac_kill(ctx->handler); widget_enabled_set(ctx->widget, false); } @@ -94,12 +94,8 @@ void app_loader(void* p) { 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(FLIPPER_APPS[i].name, NULL, handle_menu, ctx)); } /* diff --git a/applications/menu/menu.c b/applications/menu/menu.c index 70bb3b07..b305c4cb 100644 --- a/applications/menu/menu.c +++ b/applications/menu/menu.c @@ -74,7 +74,7 @@ void menu_settings_item_add(Menu* menu, MenuItem* 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 @@ -97,7 +97,6 @@ void menu_widget_callback(CanvasApi* canvas, void* context) { } release_mutex((ValueMutex*)context, menu); - } void menu_update(Menu* menu) { @@ -195,7 +194,6 @@ void menu_task(void* p) { 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) {