flipperzero-firmware/core/furi_ac.c

191 lines
4.8 KiB
C
Raw Normal View History

Core api concept (#144) * add input debounce code from old fw * exampl of input api * change input API to get/release * revert input API to read * pointer instead of instance * add input API description * add display API * rewrite display names * migrate to valuemanager * add LED API * add closing brakets * add sound api * fix led api * basic api * rename API pages * change pubsub implementation * move FURI AC -> flapp, add valuemutex example, add valuemanager implementation * pubsub usage example * user led example * update example * simplify input * add composed display * add SPI/GPIO and CC1101 bus * change cc1101 api * spi api and devices * spi api and devices * move SPI to page, add GPIO * not block pin open * backlight API and more * add minunit tests * fix logging * ignore unexisting time service on embedded targets * fix warning, issue with printf * Deprecate furi_open and furi_close (#167) Rename existing furi_open and furi_close to deprecated version * add exitcode * migrate to printf * indicate test by leds * add testing description * rename furi.h * wip basic api * add valuemutex, pubsub, split files * add value expanders * value mutex realization and tests * valuemutex test added to makefile * do not build unimplemented files * fix build furmware target f2 * redesigned minunit tests to allow testing in separate files * test file for valuemutex minunit testing * minunit partial test valuemutex * local cmsis_os2 mutex bindings * implement furi open/create, tests * migrate concurrent_access to ValueMutex * add spi header * Lib: add mlib submodule. Co-authored-by: rusdacent <rusdacentx0x08@gmail.com> Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2020-10-13 08:22:43 +00:00
#include "flipper.h"
// TODO: this file contains printf, that not implemented on uC target
#ifdef FURI_DEBUG
#include <stdio.h>
#endif
#include <string.h>
#define DEFAULT_STACK_SIZE 2048 // Stack size in bytes
2020-10-29 07:13:56 +00:00
#define MAX_TASK_COUNT 10
#define INVALID_TASK_ID UINT16_MAX
static StaticTask_t task_info_buffer[MAX_TASK_COUNT];
static StackType_t stack_buffer[MAX_TASK_COUNT][DEFAULT_STACK_SIZE / 4];
static FuriApp task_buffer[MAX_TASK_COUNT];
static size_t current_buffer_idx = 0;
uint16_t furiac_get_task_id_by_name(const char* app_name) {
for(size_t i = 0; i < MAX_TASK_RECORDS; i++) {
if(strcmp(task_buffer[i].name, app_name) == 0) return i;
}
return INVALID_TASK_ID;
}
void furiac_wait_libs(const FlipperAppLibrary* libs) {
for(uint8_t i = 0; i < libs->count; i++) {
uint16_t app_id = furiac_get_task_id_by_name(libs->name[i]);
if(app_id == INVALID_TASK_ID) {
#ifdef FURI_DEBUG
printf("[FURIAC] Invalid library name %s\n", lib_name);
#endif
} else {
while(!task_buffer[app_id].ready) {
#ifdef FURI_DEBUG
printf("[FURIAC] waiting for library \"%s\"\n", lib_name);
#endif
osDelay(50);
}
}
}
}
// find task pointer by handle
FuriApp* find_task(TaskHandle_t handler) {
FuriApp* res = NULL;
for(size_t i = 0; i < MAX_TASK_COUNT; i++) {
if(task_equal(task_buffer[i].handler, handler)) {
res = &task_buffer[i];
}
}
return res;
}
FuriApp* furiac_start(FlipperApplication app, const char* name, void* param) {
#ifdef FURI_DEBUG
printf("[FURIAC] start %s\n", name);
#endif
// TODO check first free item (.handler == NULL) and use it
if(current_buffer_idx >= MAX_TASK_COUNT) {
// max task count exceed
#ifdef FURI_DEBUG
printf("[FURIAC] max task count exceed\n");
#endif
return NULL;
}
// application ready
task_buffer[current_buffer_idx].ready = false;
// create task on static stack memory
2020-10-02 06:32:11 +00:00
task_buffer[current_buffer_idx].handler = xTaskCreateStatic(
(TaskFunction_t)app,
(const char* const)name,
DEFAULT_STACK_SIZE / 4, // freertos specify stack size in words
(void* const)param,
tskIDLE_PRIORITY + 3, // normal priority
stack_buffer[current_buffer_idx],
&task_info_buffer[current_buffer_idx]);
// save task
task_buffer[current_buffer_idx].application = app;
task_buffer[current_buffer_idx].prev_name = NULL;
task_buffer[current_buffer_idx].prev = NULL;
task_buffer[current_buffer_idx].records_count = 0;
task_buffer[current_buffer_idx].name = name;
current_buffer_idx++;
return &task_buffer[current_buffer_idx - 1];
}
bool furiac_kill(FuriApp* app) {
#ifdef FURI_DEBUG
printf("[FURIAC] kill %s\n", app->name);
#endif
// check handler
if(app == NULL || app->handler == NULL) return false;
// kill task
vTaskDelete(app->handler);
// cleanup its registry
// TODO realy free memory
app->handler = NULL;
return true;
}
void furiac_exit(void* param) {
// get current task handler
FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
// run prev
if(current_task != NULL) {
#ifdef FURI_DEBUG
printf("[FURIAC] exit %s\n", current_task->name);
#endif
if(current_task->prev != NULL) {
furiac_start(current_task->prev, current_task->prev_name, param);
} else {
#ifdef FURI_DEBUG
printf("[FURIAC] no prev\n");
#endif
}
// cleanup registry
// TODO realy free memory
current_task->handler = NULL;
}
// kill itself
vTaskDelete(NULL);
}
void furiac_switch(FlipperApplication app, char* name, void* param) {
// get current task handler
FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
if(current_task == NULL) {
#ifdef FURI_DEBUG
printf("[FURIAC] no current task found\n");
#endif
}
#ifdef FURI_DEBUG
printf("[FURIAC] switch %s to %s\n", current_task->name, name);
#endif
// run next
FuriApp* next = furiac_start(app, name, param);
if(next != NULL) {
// save current application pointer as prev
next->prev = current_task->application;
next->prev_name = current_task->name;
// kill itself
vTaskDelete(NULL);
}
}
// set task to ready state
void furiac_ready() {
/*
TODO:
Currently i think that better way is to use application name
and restrict applications to "one task per application"
*/
FuriApp* app = find_task(xTaskGetCurrentTaskHandle());
if(app == NULL) {
#ifdef FURI_DEBUG
printf("[FURIAC] cannot find task to set ready state\n");
#endif
} else {
#ifdef FURI_DEBUG
printf("[FURIAC] task is ready\n");
#endif
app->ready = true;
}
}