From 05eda44df795389386b27beb5da59628df2df47a Mon Sep 17 00:00:00 2001 From: Anthony Rabine Date: Thu, 27 Jul 2023 15:32:34 +0200 Subject: [PATCH] New: HMI thread (user inputs and LCD manager) --- software/system/filesystem.c | 86 ++++++++++++++++++++++++++++++++++++ software/system/filesystem.h | 9 ++++ software/system/main.c | 55 +++++++++++++++++++++-- 3 files changed, 146 insertions(+), 4 deletions(-) diff --git a/software/system/filesystem.c b/software/system/filesystem.c index 419b014..449777b 100644 --- a/software/system/filesystem.c +++ b/software/system/filesystem.c @@ -1,7 +1,10 @@ +#include #include #include "debug.h" #include "ost_hal.h" +#include "filesystem.h" + // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h, // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT. #define SD_FAT_TYPE 2 @@ -65,3 +68,86 @@ void filesystem_mount() scan_files(""); } + +// Ouvre le fichier d'index d'histoires +// Le format est le suivant : +// - Fichier texte, avec fin de ligne \n +// - Une ligne par histoire +// - la ligne contient le nom du répertoire au format UUIDv4 (36 caractères ASCII avec 4 tirets '-') + +// Exemple: d0ad13e4-06de-4e00-877c-d922fdd37d13 + +int is_multiple_of_37(int nombre) +{ + int plusGrandMultiple = (nombre / 37) * 37; + return (nombre - plusGrandMultiple) == 0; +} + +/* +// Loop in all directories +void disk_start() +{ + // default is current directory + const char* dpath = "."; + if (argc) dpath = args; + + DIR dir; + if ( FR_OK != f_opendir(&dir, dpath) ) + { + printf("cannot access '%s': No such file or directory\r\n", dpath); + return; + } + + FILINFO fno; + while( (f_readdir(&dir, &fno) == FR_OK) && (fno.fname[0] != 0) ) + { + if ( fno.fname[0] != '.' ) // ignore . and .. entry + { + if ( fno.fattrib & AM_DIR ) + { + // directory + printf("/%s\r\n", fno.fname); + }else + { + printf("%-40s", fno.fname); + if (fno.fsize < 1024) + { + printf("%lu B\r\n", fno.fsize); + }else + { + printf("%lu KB\r\n", fno.fsize / 1024); + } + } + } + } + + f_closedir(&dir); +} +*/ + +bool filesystem_read_index_file(ost_context_t *ctx) +{ + FILINFO fno; + FRESULT fr = f_stat("index.ost", &fno); + + ctx->number_of_stories = 0; + ctx->current_story = 0; + + if (fr == FR_OK) + { + bool valid_file = false; + int size = fno.fsize; + // une ligne = 36 octets (UUID) + 1 octet (\n) = 37 + // Déjà, la taille doit être multiple de 37 + if (is_multiple_of_37(size) && (size > 0)) + { + valid_file = true; + ctx->number_of_stories = size / 37; + debug_printf("SUCCESS: found %d stories\r\n", ctx->number_of_stories); + } + } + else + { + debug_printf("ERROR: index.ost not found\r\n"); + } +} diff --git a/software/system/filesystem.h b/software/system/filesystem.h index e1cf52a..8691cc6 100644 --- a/software/system/filesystem.h +++ b/software/system/filesystem.h @@ -1,6 +1,15 @@ #ifndef FILESYSTEM_H #define FILESYSTEM_H +#include + +typedef struct +{ + uint32_t number_of_stories; + uint32_t current_story; +} ost_context_t; + +bool filesystem_read_index_file(ost_context_t *ctx); void filesystem_mount(); #endif // FILESYSTEM_H diff --git a/software/system/main.c b/software/system/main.c index 8275cb9..a131563 100644 --- a/software/system/main.c +++ b/software/system/main.c @@ -19,6 +19,50 @@ void ost_hal_panic() { } +// =========================================================================================================== +// GLOBAL STORY VARIABLES +// =========================================================================================================== +static ost_context_t OstContext; + +// =========================================================================================================== +// HMI TASK (user interface, buttons manager, LCD) +// =========================================================================================================== +static qor_tcb_t HmiTcb; +static uint32_t HmiStack[4096]; + +static qor_mbox_t HmiMailBox; + +typedef struct +{ + uint8_t ev; +} ost_hmi_event_t; + +static ost_hmi_event_t HmiEvent; + +ost_hmi_event_t HmiQueue[10]; + +void HmiTask(void *args) +{ + qor_mbox_init(&HmiMailBox, (void **)&HmiQueue, 10); + + ost_hmi_event_t *e = NULL; + + filesystem_read_index_file(&OstContext); + + while (1) + { + uint32_t res = qor_mbox_wait(&HmiMailBox, (void **)&e, 1000); + + if (res == QOR_MBOX_OK) + { + } + else + { + debug_printf("H"); // pour le debug only + } + } +} + // =========================================================================================================== // VIRTUAL MACHINE TASK // =========================================================================================================== @@ -39,13 +83,14 @@ static uint8_t m_rom_data[16 * 1024]; static uint8_t m_ram_data[16 * 1024]; static chip32_ctx_t m_chip32_ctx; +// Index file parameter, reference an index in the file + uint8_t vm_syscall(chip32_ctx_t *ctx, uint8_t signum) { } void VmTask(void *args) { - // VM Initialize m_chip32_ctx.stack_size = 512; @@ -61,6 +106,8 @@ void VmTask(void *args) chip32_initialize(&m_chip32_ctx); + qor_mbox_init(&VmMailBox, (void **)&VmQueue, 10); + chip32_result_t run_result; ost_vm_event_t *e = NULL; @@ -82,7 +129,7 @@ void VmTask(void *args) } // =========================================================================================================== -// FILE SYSTEM TASK +// AUDIO TASK // =========================================================================================================== static qor_tcb_t AudioTcb; static uint32_t AudioStack[4096]; @@ -123,8 +170,7 @@ void show_duration(uint32_t millisecondes) void AudioTask(void *args) { - picture_show("example.bmp"); - // ost_audio_play("out2.wav"); + // picture_show("example.bmp"); wake_up.ev = 34; @@ -205,6 +251,7 @@ int main() qor_init(125000000UL); // 5. Initialize the tasks + qor_create_thread(&HmiTcb, HmiTask, HmiStack, sizeof(HmiStack) / sizeof(HmiStack[0]), 1, "HmiTask"); // less priority is the HMI (user inputs and LCD) qor_create_thread(&VmTcb, VmTask, VmStack, sizeof(VmStack) / sizeof(VmStack[0]), 2, "VmTask"); qor_create_thread(&AudioTcb, AudioTask, AudioStack, sizeof(AudioStack) / sizeof(AudioStack[0]), 3, "AudioTask"); ///< High priority for audio