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