fatfs_list.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "u8g2/u8g2.h"
  2. #include "fatfs/ff.h"
  3. #include "flipper.h"
  4. #include <stdio.h>
  5. // TODO currently we have small stack, so it will be static
  6. FuriRecordSubscriber* furi_log;
  7. #define STR_BUFFER_SIZE 128
  8. char str_buffer[STR_BUFFER_SIZE];
  9. uint8_t line_current = 0;
  10. uint16_t line_position = 0;
  11. // TODO this should be in the target driver
  12. FATFS SD_FatFs;
  13. char SD_Path[4];
  14. typedef enum {
  15. EventTypeStart,
  16. EventTypeKey,
  17. } AppEventType;
  18. typedef struct {
  19. union {
  20. InputEvent input;
  21. } value;
  22. AppEventType type;
  23. } AppEvent;
  24. static void event_cb(const void* value, size_t size, void* ctx) {
  25. QueueHandle_t event_queue = (QueueHandle_t)ctx;
  26. AppEvent event;
  27. event.type = EventTypeKey;
  28. event.value.input = *(InputEvent*)value;
  29. xQueueSend(event_queue, (void*)&event, 0);
  30. }
  31. void fatfs_list(void* p) {
  32. const uint8_t line_size = 10;
  33. const uint8_t lines_on_display = 6;
  34. uint8_t bsp_result;
  35. FRESULT result;
  36. DIR dir;
  37. FILINFO fno;
  38. AppEvent event;
  39. QueueHandle_t event_queue = xQueueCreate(2, sizeof(AppEvent));
  40. furi_log = get_default_log();
  41. FuriRecordSubscriber* fb_record =
  42. furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
  43. if(fb_record == NULL) {
  44. fuprintf(furi_log, "[widget][fatfs_list] cannot create fb record\n");
  45. furiac_exit(NULL);
  46. }
  47. FuriRecordSubscriber* event_record =
  48. furi_open_deprecated("input_events", false, false, event_cb, NULL, event_queue);
  49. if(event_record == NULL) {
  50. fuprintf(furi_log, "[widget][fatfs_list] cannot register input_events callback\n");
  51. furiac_exit(NULL);
  52. }
  53. // clear display
  54. u8g2_t* fb = furi_take(fb_record);
  55. u8g2_ClearBuffer(fb);
  56. furi_commit(fb_record);
  57. // TODO these lines should be executed in the target driver
  58. // so i dont fix "implicit declaration of function 'BSP_SD_Init'"
  59. bsp_result = BSP_SD_Init();
  60. if(bsp_result != 0) {
  61. furi_take(fb_record);
  62. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  63. u8g2_SetDrawColor(fb, 1);
  64. u8g2_SetFontMode(fb, 1);
  65. u8g2_DrawStr(fb, 0, 12, "SD card init error");
  66. furi_commit(fb_record);
  67. furiac_exit(NULL);
  68. }
  69. result = f_mount(&SD_FatFs, (TCHAR const*)SD_Path, 1);
  70. if(result != FR_OK) {
  71. furi_take(fb_record);
  72. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  73. u8g2_SetDrawColor(fb, 1);
  74. u8g2_SetFontMode(fb, 1);
  75. u8g2_DrawStr(fb, 0, 12, "SD card mount error");
  76. furi_commit(fb_record);
  77. furiac_exit(NULL);
  78. }
  79. // ok, now we can work with sd card
  80. // send start event
  81. event.type = EventTypeStart;
  82. xQueueSend(event_queue, (void*)&event, 0);
  83. while(1) {
  84. if(xQueueReceive(event_queue, (void*)&event, portMAX_DELAY)) {
  85. // process buttons event
  86. if(event.type == EventTypeKey) {
  87. // button pressed
  88. if(event.value.input.state == true) {
  89. if(event.value.input.input == InputUp && line_position > 0) {
  90. line_position--;
  91. }
  92. if(event.value.input.input == InputDown) {
  93. line_position++;
  94. }
  95. }
  96. }
  97. // get display and draw
  98. furi_take(fb_record);
  99. u8g2_ClearBuffer(fb);
  100. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  101. u8g2_SetDrawColor(fb, 1);
  102. u8g2_SetFontMode(fb, 1);
  103. line_current = 1;
  104. // open root dir
  105. result = f_opendir(&dir, "");
  106. while(1) {
  107. // read a directory item
  108. result = f_readdir(&dir, &fno);
  109. if(result != FR_OK) {
  110. // cannot read dir
  111. break;
  112. }
  113. if(fno.fname[0] == 0) {
  114. // Break on end of dir
  115. break;
  116. }
  117. // draw files on display
  118. if(line_current > line_position &&
  119. line_current <= (line_position + lines_on_display)) {
  120. if(fno.fattrib & AM_DIR) {
  121. snprintf(str_buffer, STR_BUFFER_SIZE, "DIR %s\n", fno.fname);
  122. } else {
  123. snprintf(str_buffer, STR_BUFFER_SIZE, "FIL %s\n", fno.fname);
  124. }
  125. u8g2_DrawStr(fb, 0, line_size * (line_current - line_position), str_buffer);
  126. }
  127. line_current++;
  128. }
  129. result = f_closedir(&dir);
  130. furi_commit(fb_record);
  131. }
  132. }
  133. furiac_exit(NULL);
  134. }