fatfs_list.c 4.6 KB

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