2020-10-08 14:37:19 +00:00
|
|
|
#include "u8g2/u8g2.h"
|
|
|
|
#include "fatfs/ff.h"
|
2020-10-26 09:26:15 +00:00
|
|
|
#include "flipper_v2.h"
|
2020-10-08 14:37:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-10-29 07:58:19 +00:00
|
|
|
extern uint8_t BSP_SD_Init();
|
|
|
|
|
2020-10-08 14:37:19 +00:00
|
|
|
// TODO currently we have small stack, so it will be static
|
|
|
|
FuriRecordSubscriber* furi_log;
|
|
|
|
#define STR_BUFFER_SIZE 128
|
|
|
|
char str_buffer[STR_BUFFER_SIZE];
|
|
|
|
uint8_t line_current = 0;
|
|
|
|
uint16_t line_position = 0;
|
|
|
|
|
|
|
|
// TODO this should be in the target driver
|
|
|
|
FATFS SD_FatFs;
|
|
|
|
char SD_Path[4];
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EventTypeStart,
|
|
|
|
EventTypeKey,
|
|
|
|
} AppEventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
InputEvent input;
|
|
|
|
} value;
|
|
|
|
AppEventType type;
|
|
|
|
} AppEvent;
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
static void event_cb(const void* value, void* ctx) {
|
2020-10-08 14:37:19 +00:00
|
|
|
QueueHandle_t event_queue = (QueueHandle_t)ctx;
|
|
|
|
|
|
|
|
AppEvent event;
|
|
|
|
event.type = EventTypeKey;
|
|
|
|
event.value.input = *(InputEvent*)value;
|
|
|
|
xQueueSend(event_queue, (void*)&event, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fatfs_list(void* p) {
|
|
|
|
const uint8_t line_size = 10;
|
|
|
|
const uint8_t lines_on_display = 6;
|
|
|
|
|
|
|
|
uint8_t bsp_result;
|
|
|
|
FRESULT result;
|
|
|
|
DIR dir;
|
|
|
|
FILINFO fno;
|
|
|
|
AppEvent event;
|
|
|
|
|
|
|
|
QueueHandle_t event_queue = xQueueCreate(2, sizeof(AppEvent));
|
|
|
|
|
|
|
|
furi_log = get_default_log();
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] app start\n");
|
2020-10-08 14:37:19 +00:00
|
|
|
|
2020-10-15 16:50:21 +00:00
|
|
|
FuriRecordSubscriber* fb_record =
|
|
|
|
furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
|
2020-10-08 14:37:19 +00:00
|
|
|
if(fb_record == NULL) {
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] cannot create fb record\n");
|
2020-10-08 14:37:19 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
PubSub* event_record = furi_open("input_events");
|
2020-10-08 14:37:19 +00:00
|
|
|
if(event_record == NULL) {
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] cannot open input_events record\n");
|
2020-10-26 09:26:15 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
PubSubItem* subscription = subscribe_pubsub(event_record, event_cb, event_queue);
|
|
|
|
if(subscription == NULL) {
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] cannot register input_events callback\n");
|
2020-10-08 14:37:19 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bsp_result = BSP_SD_Init();
|
|
|
|
|
|
|
|
if(bsp_result != 0) {
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] SD card init error\n");
|
2020-10-08 14:37:19 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = f_mount(&SD_FatFs, (TCHAR const*)SD_Path, 1);
|
|
|
|
|
|
|
|
if(result != FR_OK) {
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, "[fatfs_list] SD card mount error\n");
|
2020-10-08 14:37:19 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, now we can work with sd card
|
|
|
|
|
|
|
|
// send start event
|
|
|
|
event.type = EventTypeStart;
|
|
|
|
xQueueSend(event_queue, (void*)&event, 0);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(xQueueReceive(event_queue, (void*)&event, portMAX_DELAY)) {
|
|
|
|
// process buttons event
|
|
|
|
if(event.type == EventTypeKey) {
|
|
|
|
// button pressed
|
|
|
|
if(event.value.input.state == true) {
|
|
|
|
if(event.value.input.input == InputUp && line_position > 0) {
|
|
|
|
line_position--;
|
|
|
|
}
|
|
|
|
if(event.value.input.input == InputDown) {
|
|
|
|
line_position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
line_current = 1;
|
|
|
|
|
|
|
|
// open root dir
|
|
|
|
result = f_opendir(&dir, "");
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
// read a directory item
|
|
|
|
result = f_readdir(&dir, &fno);
|
|
|
|
|
|
|
|
if(result != FR_OK) {
|
|
|
|
// cannot read dir
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fno.fname[0] == 0) {
|
|
|
|
// Break on end of dir
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw files on display
|
|
|
|
if(line_current > line_position &&
|
|
|
|
line_current <= (line_position + lines_on_display)) {
|
|
|
|
if(fno.fattrib & AM_DIR) {
|
|
|
|
snprintf(str_buffer, STR_BUFFER_SIZE, "DIR %s\n", fno.fname);
|
|
|
|
} else {
|
|
|
|
snprintf(str_buffer, STR_BUFFER_SIZE, "FIL %s\n", fno.fname);
|
|
|
|
}
|
2020-11-06 08:31:59 +00:00
|
|
|
fuprintf(furi_log, str_buffer);
|
2020-10-08 14:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
line_current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = f_closedir(&dir);
|
|
|
|
|
|
|
|
furi_commit(fb_record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|