You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

119 rivejä
2.7 KiB

  1. #include <string.h>
  2. #include "core/common.h"
  3. #include "core/buttons.h"
  4. #include "core/display.h"
  5. #include <epaper/EPD.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. #include "SdCardMenuMode.h"
  12. #include "esp_log.h"
  13. static char* TAG = "SdCardMenuMode";
  14. // TODO: make use of bytes
  15. void SdCardMenuMode::start()
  16. {
  17. display_refresh();
  18. dir_entry_t* dir_chain_start = new dir_entry_t;
  19. strcpy(dir_chain_start->name, "[Back]");
  20. dir_entry_t* last_entry = dir_chain_start;
  21. int entries = 1;
  22. struct stat stats;
  23. struct dirent *dir;
  24. DIR* d = opendir(this->basedir);
  25. if (d)
  26. {
  27. while ((dir = readdir(d)) != NULL)
  28. {
  29. ESP_LOGI(TAG, "* %s", dir->d_name);
  30. dir_entry_t* new_entry = new dir_entry_t;
  31. strcpy(new_entry->name, dir->d_name);
  32. char path[64];
  33. strcpy(path, this->basedir);
  34. strcat(path, dir->d_name);
  35. if (stat(path, &stats) == 0) {
  36. last_entry->bytes = stats.st_size;
  37. }
  38. new_entry->next = NULL;
  39. if (dir_chain_start == NULL) {
  40. dir_chain_start = new_entry;
  41. }
  42. if (last_entry == NULL) {
  43. last_entry = new_entry;
  44. } else {
  45. last_entry->next = new_entry;
  46. last_entry = new_entry;
  47. }
  48. entries++;
  49. }
  50. closedir(d);
  51. } else {
  52. ESP_LOGE(TAG, "Could not open dir.");
  53. }
  54. this->optionsNames = new char[entries*DIR_ENTRY_NAME_SIZE];
  55. this->options = new char*[entries];
  56. this->optionsBytes = new unsigned long[entries];
  57. this->optionsSize = entries;
  58. last_entry = dir_chain_start;
  59. int i = 0;
  60. while (last_entry != NULL) {
  61. this->options[i] = &this->optionsNames[i * DIR_ENTRY_NAME_SIZE];
  62. memcpy(this->options[i], last_entry->name, DIR_ENTRY_NAME_SIZE);
  63. this->optionsBytes[i] = last_entry->bytes;
  64. dir_entry_t* next = (dir_entry_t*)last_entry->next;
  65. delete last_entry;
  66. last_entry = next;
  67. i++;
  68. }
  69. }
  70. void SdCardMenuMode::finish()
  71. {
  72. delete this->options;
  73. delete this->optionsNames;
  74. delete this->optionsBytes;
  75. }
  76. char* SdCardMenuMode::getTitle()
  77. {
  78. return "SD Card";
  79. }
  80. char** SdCardMenuMode::getOptions()
  81. {
  82. return this->options;
  83. }
  84. int SdCardMenuMode::getOptionsSize()
  85. {
  86. return this->optionsSize;
  87. }
  88. void SdCardMenuMode::onOptionSelected(int option)
  89. {
  90. if (option == 0) {
  91. this->setFinished();
  92. return;
  93. }
  94. // TODO files
  95. }
  96. int SdCardMenuMode::getOptionsX()
  97. {
  98. return 35;
  99. }
  100. int SdCardMenuMode::getOptionsFont()
  101. {
  102. return SMALL_FONT;
  103. }