fatfs_list.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. fuprintf(furi_log, "[fatfs_list] app start\n");
  43. fuprintf(furi_log, "[fatfs_list] wait for sd insert\n");
  44. while(!hal_gpio_read_sd_detect()) {
  45. delay(100);
  46. }
  47. fuprintf(furi_log, "[fatfs_list] sd inserted\n");
  48. FuriRecordSubscriber* fb_record =
  49. furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
  50. if(fb_record == NULL) {
  51. fuprintf(furi_log, "[fatfs_list] cannot create fb record\n");
  52. furiac_exit(NULL);
  53. }
  54. PubSub* event_record = furi_open("input_events");
  55. if(event_record == NULL) {
  56. fuprintf(furi_log, "[fatfs_list] cannot open input_events record\n");
  57. furiac_exit(NULL);
  58. }
  59. PubSubItem* subscription = subscribe_pubsub(event_record, event_cb, event_queue);
  60. if(subscription == NULL) {
  61. fuprintf(furi_log, "[fatfs_list] cannot register input_events callback\n");
  62. furiac_exit(NULL);
  63. }
  64. bsp_result = BSP_SD_Init();
  65. if(bsp_result != 0) {
  66. fuprintf(furi_log, "[fatfs_list] SD card init error\n");
  67. furiac_exit(NULL);
  68. }
  69. result = f_mount(&SD_FatFs, (TCHAR const*)SD_Path, 1);
  70. if(result != FR_OK) {
  71. fuprintf(furi_log, "[fatfs_list] SD card mount error\n");
  72. furiac_exit(NULL);
  73. }
  74. // ok, now we can work with sd card
  75. // send start event
  76. event.type = EventTypeStart;
  77. xQueueSend(event_queue, (void*)&event, 0);
  78. while(1) {
  79. if(xQueueReceive(event_queue, (void*)&event, portMAX_DELAY)) {
  80. // process buttons event
  81. if(event.type == EventTypeKey) {
  82. // button pressed
  83. if(event.value.input.state == true) {
  84. if(event.value.input.input == InputUp && line_position > 0) {
  85. line_position--;
  86. }
  87. if(event.value.input.input == InputDown) {
  88. line_position++;
  89. }
  90. }
  91. }
  92. line_current = 1;
  93. // open root dir
  94. result = f_opendir(&dir, "");
  95. while(1) {
  96. // read a directory item
  97. result = f_readdir(&dir, &fno);
  98. if(result != FR_OK) {
  99. // cannot read dir
  100. break;
  101. }
  102. if(fno.fname[0] == 0) {
  103. // Break on end of dir
  104. break;
  105. }
  106. // draw files on display
  107. if(line_current > line_position &&
  108. line_current <= (line_position + lines_on_display)) {
  109. if(fno.fattrib & AM_DIR) {
  110. snprintf(str_buffer, STR_BUFFER_SIZE, "DIR %s\n", fno.fname);
  111. } else {
  112. snprintf(str_buffer, STR_BUFFER_SIZE, "FIL %s\n", fno.fname);
  113. }
  114. fuprintf(furi_log, str_buffer);
  115. }
  116. line_current++;
  117. }
  118. result = f_closedir(&dir);
  119. furi_commit(fb_record);
  120. }
  121. }
  122. furiac_exit(NULL);
  123. }