fatfs_list.c 4.0 KB

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