fatfs_list.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "u8g2/u8g2.h"
  2. #include "fatfs/ff.h"
  3. #include "flipper_v2.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, 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. PubSub* event_record = furi_open("input_events");
  48. if(event_record == NULL) {
  49. fuprintf(furi_log, "[widget][fatfs_list] cannot open input_events record\n");
  50. furiac_exit(NULL);
  51. }
  52. PubSubItem* subscription = subscribe_pubsub(event_record, event_cb, event_queue);
  53. if(subscription == NULL) {
  54. fuprintf(furi_log, "[widget][fatfs_list] cannot register input_events callback\n");
  55. furiac_exit(NULL);
  56. }
  57. // clear display
  58. u8g2_t* fb = furi_take(fb_record);
  59. u8g2_ClearBuffer(fb);
  60. furi_commit(fb_record);
  61. // TODO these lines should be executed in the target driver
  62. // so i dont fix "implicit declaration of function 'BSP_SD_Init'"
  63. bsp_result = BSP_SD_Init();
  64. if(bsp_result != 0) {
  65. furi_take(fb_record);
  66. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  67. u8g2_SetDrawColor(fb, 1);
  68. u8g2_SetFontMode(fb, 1);
  69. u8g2_DrawStr(fb, 0, 12, "SD card init error");
  70. furi_commit(fb_record);
  71. furiac_exit(NULL);
  72. }
  73. result = f_mount(&SD_FatFs, (TCHAR const*)SD_Path, 1);
  74. if(result != FR_OK) {
  75. furi_take(fb_record);
  76. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  77. u8g2_SetDrawColor(fb, 1);
  78. u8g2_SetFontMode(fb, 1);
  79. u8g2_DrawStr(fb, 0, 12, "SD card mount error");
  80. furi_commit(fb_record);
  81. furiac_exit(NULL);
  82. }
  83. // ok, now we can work with sd card
  84. // send start event
  85. event.type = EventTypeStart;
  86. xQueueSend(event_queue, (void*)&event, 0);
  87. while(1) {
  88. if(xQueueReceive(event_queue, (void*)&event, portMAX_DELAY)) {
  89. // process buttons event
  90. if(event.type == EventTypeKey) {
  91. // button pressed
  92. if(event.value.input.state == true) {
  93. if(event.value.input.input == InputUp && line_position > 0) {
  94. line_position--;
  95. }
  96. if(event.value.input.input == InputDown) {
  97. line_position++;
  98. }
  99. }
  100. }
  101. // get display and draw
  102. furi_take(fb_record);
  103. u8g2_ClearBuffer(fb);
  104. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  105. u8g2_SetDrawColor(fb, 1);
  106. u8g2_SetFontMode(fb, 1);
  107. line_current = 1;
  108. // open root dir
  109. result = f_opendir(&dir, "");
  110. while(1) {
  111. // read a directory item
  112. result = f_readdir(&dir, &fno);
  113. if(result != FR_OK) {
  114. // cannot read dir
  115. break;
  116. }
  117. if(fno.fname[0] == 0) {
  118. // Break on end of dir
  119. break;
  120. }
  121. // draw files on display
  122. if(line_current > line_position &&
  123. line_current <= (line_position + lines_on_display)) {
  124. if(fno.fattrib & AM_DIR) {
  125. snprintf(str_buffer, STR_BUFFER_SIZE, "DIR %s\n", fno.fname);
  126. } else {
  127. snprintf(str_buffer, STR_BUFFER_SIZE, "FIL %s\n", fno.fname);
  128. }
  129. u8g2_DrawStr(fb, 0, line_size * (line_current - line_position), str_buffer);
  130. }
  131. line_current++;
  132. }
  133. result = f_closedir(&dir);
  134. furi_commit(fb_record);
  135. }
  136. }
  137. furiac_exit(NULL);
  138. }