2020-02-02 21:13:42 +00:00
|
|
|
#include <string.h>
|
2020-02-02 16:14:45 +00:00
|
|
|
#include "core/common.h"
|
|
|
|
#include "core/buttons.h"
|
|
|
|
#include "core/display.h"
|
2020-02-02 21:13:42 +00:00
|
|
|
#include <epaper/EPD.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2020-02-02 16:14:45 +00:00
|
|
|
#include "SdCardMenuMode.h"
|
|
|
|
|
2020-02-02 21:13:42 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
static char* TAG = "SdCardMenuMode";
|
|
|
|
|
|
|
|
// TODO: make use of bytes
|
2020-02-02 16:14:45 +00:00
|
|
|
|
|
|
|
void SdCardMenuMode::start()
|
|
|
|
{
|
|
|
|
display_refresh();
|
2020-02-02 21:13:42 +00:00
|
|
|
|
|
|
|
dir_entry_t* dir_chain_start = new dir_entry_t;
|
|
|
|
strcpy(dir_chain_start->name, "[Back]");
|
|
|
|
dir_entry_t* last_entry = dir_chain_start;
|
|
|
|
int entries = 1;
|
|
|
|
|
|
|
|
struct stat stats;
|
|
|
|
struct dirent *dir;
|
|
|
|
DIR* d = opendir(this->basedir);
|
|
|
|
if (d)
|
|
|
|
{
|
|
|
|
while ((dir = readdir(d)) != NULL)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "* %s", dir->d_name);
|
|
|
|
dir_entry_t* new_entry = new dir_entry_t;
|
|
|
|
strcpy(new_entry->name, dir->d_name);
|
|
|
|
char path[64];
|
|
|
|
strcpy(path, this->basedir);
|
|
|
|
strcat(path, dir->d_name);
|
|
|
|
if (stat(path, &stats) == 0) {
|
|
|
|
last_entry->bytes = stats.st_size;
|
|
|
|
}
|
|
|
|
new_entry->next = NULL;
|
|
|
|
if (dir_chain_start == NULL) {
|
|
|
|
dir_chain_start = new_entry;
|
|
|
|
}
|
|
|
|
if (last_entry == NULL) {
|
|
|
|
last_entry = new_entry;
|
|
|
|
} else {
|
|
|
|
last_entry->next = new_entry;
|
|
|
|
last_entry = new_entry;
|
|
|
|
}
|
|
|
|
entries++;
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Could not open dir.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this->optionsNames = new char[entries*DIR_ENTRY_NAME_SIZE];
|
|
|
|
this->options = new char*[entries];
|
|
|
|
this->optionsBytes = new unsigned long[entries];
|
|
|
|
this->optionsSize = entries;
|
|
|
|
last_entry = dir_chain_start;
|
|
|
|
int i = 0;
|
|
|
|
while (last_entry != NULL) {
|
|
|
|
this->options[i] = &this->optionsNames[i * DIR_ENTRY_NAME_SIZE];
|
|
|
|
memcpy(this->options[i], last_entry->name, DIR_ENTRY_NAME_SIZE);
|
|
|
|
this->optionsBytes[i] = last_entry->bytes;
|
|
|
|
|
|
|
|
dir_entry_t* next = (dir_entry_t*)last_entry->next;
|
|
|
|
delete last_entry;
|
|
|
|
last_entry = next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SdCardMenuMode::finish()
|
2020-02-02 21:13:42 +00:00
|
|
|
{
|
|
|
|
delete this->options;
|
|
|
|
delete this->optionsNames;
|
|
|
|
delete this->optionsBytes;
|
|
|
|
}
|
2020-02-02 16:14:45 +00:00
|
|
|
|
|
|
|
char* SdCardMenuMode::getTitle()
|
|
|
|
{
|
|
|
|
return "SD Card";
|
|
|
|
}
|
|
|
|
|
|
|
|
char** SdCardMenuMode::getOptions()
|
|
|
|
{
|
2020-02-02 21:13:42 +00:00
|
|
|
return this->options;
|
2020-02-02 16:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SdCardMenuMode::getOptionsSize()
|
|
|
|
{
|
2020-02-02 21:13:42 +00:00
|
|
|
return this->optionsSize;
|
2020-02-02 16:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SdCardMenuMode::onOptionSelected(int option)
|
|
|
|
{
|
|
|
|
if (option == 0) {
|
|
|
|
this->setFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO files
|
|
|
|
}
|
2020-02-02 21:13:42 +00:00
|
|
|
|
|
|
|
int SdCardMenuMode::getOptionsX()
|
|
|
|
{
|
|
|
|
return 35;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SdCardMenuMode::getOptionsFont()
|
|
|
|
{
|
|
|
|
return SMALL_FONT;
|
|
|
|
}
|