Added support for SD Card browsing. Added missing CMakeLists.txt.

This commit is contained in:
dejvino
2020-02-02 22:13:42 +01:00
parent d93fc4b4c7
commit af712d9124
20 changed files with 206 additions and 45 deletions
+26 -13
View File
@@ -1,25 +1,23 @@
#include "core/common.h"
#include "core/buttons.h"
#include "core/display.h"
#include <EPD.h>
#include <epaper/EPD.h>
#include "ModeRunner.h"
#include "ReaderMode.h"
#include "AbstractMenuMode.h"
static int baseY = 35;
static int menuSkip = 20;
static int x = 40;
static int menu_options_size = 4;
static int x = 20;
void draw_menu_option(char* text, int line, bool selected)
void draw_menu_option(char* text, int textX, int textHeight, int line, bool selected)
{
EPD_print(text, CENTER, baseY + line * menuSkip);
EPD_print(text, textX, baseY + line * textHeight);
}
void draw_menu_cursor(int cursor)
void draw_menu_cursor(int cursor, int textHeight)
{
EPD_drawRect(x, baseY + cursor * menuSkip,
EPD_DISPLAY_WIDTH - 2*x, menuSkip + 1, 0x0F);
EPD_drawRect(x, baseY + cursor * textHeight,
EPD_DISPLAY_WIDTH - 2*x, textHeight + 1, 0x0F);
}
void AbstractMenuMode::start()
@@ -32,13 +30,18 @@ void AbstractMenuMode::loop()
EPD_print(this->getTitle(), CENTER, 0);
int line = 0;
EPD_setFont(DEJAVU18_FONT, NULL);
EPD_setFont(this->getOptionsFont(), NULL);
int menu_option_height = EPD_getfontheight() + 1;
int menu_options_limit = (EPD_DISPLAY_HEIGHT - baseY) / (EPD_getfontheight() + 1);
char** options = this->getOptions();
int menu_options_size = this->getOptionsSize();
for (int line = 0; line < menu_options_size; line++) {
draw_menu_option(options[line], line, this->cursor == line);
int start = (this->cursor / menu_options_limit) * menu_options_limit;
int stop = ((this->cursor / menu_options_limit) + 1) * menu_options_limit;
for (int line = start; line < menu_options_size && line < stop; line++) {
draw_menu_option(options[line], this->getOptionsX(), menu_option_height,
line % menu_options_limit, this->cursor == line);
}
draw_menu_cursor(this->cursor);
draw_menu_cursor(this->cursor % menu_options_limit, menu_option_height);
display_update();
while(1) {
@@ -60,3 +63,13 @@ void AbstractMenuMode::loop()
void AbstractMenuMode::finish()
{}
int AbstractMenuMode::getOptionsX()
{
return CENTER;
}
int AbstractMenuMode::getOptionsFont()
{
return DEJAVU18_FONT;
}
+2
View File
@@ -14,6 +14,8 @@ protected:
virtual char** getOptions();
virtual int getOptionsSize();
virtual void onOptionSelected(int option);
virtual int getOptionsX();
virtual int getOptionsFont();
private:
int cursor = 0;
+1 -1
View File
@@ -1,7 +1,7 @@
#include "core/common.h"
#include "core/buttons.h"
#include "core/display.h"
#include <EPD.h>
#include <epaper/EPD.h>
#include "InternalMemoryMenuMode.h"
void InternalMemoryMenuMode::start()
+2 -2
View File
@@ -1,7 +1,7 @@
#include "core/common.h"
#include "core/buttons.h"
#include "core/display.h"
#include <EPD.h>
#include <epaper/EPD.h>
#include "ModeRunner.h"
#include "ReaderMode.h"
#include "InternalMemoryMenuMode.h"
@@ -12,7 +12,7 @@
static char* options[] = {
"Continue Reading",
"Internal Memory",
"SC Card",
"SD Card",
"Settings"
};
+84 -10
View File
@@ -1,23 +1,87 @@
#include <string.h>
#include "core/common.h"
#include "core/buttons.h"
#include "core/display.h"
#include <EPD.h>
#include <epaper/EPD.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "SdCardMenuMode.h"
static char* options[] = {
"[Back]",
"/file.txt",
"/book.txt",
"/asdf.gif",
};
#include "esp_log.h"
static char* TAG = "SdCardMenuMode";
// TODO: make use of bytes
void SdCardMenuMode::start()
{
display_refresh();
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++;
}
}
void SdCardMenuMode::finish()
{}
{
delete this->options;
delete this->optionsNames;
delete this->optionsBytes;
}
char* SdCardMenuMode::getTitle()
{
@@ -26,12 +90,12 @@ char* SdCardMenuMode::getTitle()
char** SdCardMenuMode::getOptions()
{
return options;
return this->options;
}
int SdCardMenuMode::getOptionsSize()
{
return 4;
return this->optionsSize;
}
void SdCardMenuMode::onOptionSelected(int option)
@@ -42,3 +106,13 @@ void SdCardMenuMode::onOptionSelected(int option)
}
// TODO files
}
int SdCardMenuMode::getOptionsX()
{
return 35;
}
int SdCardMenuMode::getOptionsFont()
{
return SMALL_FONT;
}
+20 -6
View File
@@ -1,17 +1,31 @@
#include "AppMode.h"
#include "AbstractMenuMode.h"
#define DIR_ENTRY_NAME_SIZE 16
typedef struct {
char name[DIR_ENTRY_NAME_SIZE];
unsigned long bytes;
void* next;
} dir_entry_t;
class SdCardMenuMode : public AbstractMenuMode
{
public:
virtual void start();
virtual void finish();
void start();
void finish();
protected:
virtual char* getTitle();
virtual char** getOptions();
virtual int getOptionsSize();
virtual void onOptionSelected(int option);
char* getTitle();
char** getOptions();
int getOptionsSize();
void onOptionSelected(int option);
int getOptionsX();
int getOptionsFont();
private:
char* basedir = "/sdcard/";
char** options;
char* optionsNames;
unsigned long* optionsBytes;
int optionsSize;
};
+3 -3
View File
@@ -1,7 +1,7 @@
#include "core/common.h"
#include "core/buttons.h"
#include "core/display.h"
#include <EPD.h>
#include <epaper/EPD.h>
#include "SettingsMenuMode.h"
static char* options[] = {
@@ -38,7 +38,7 @@ void SettingsMenuMode::onOptionSelected(int option)
{
switch (option) {
case 0:
// TODO
this->setFinished();
return;
case 1:
// TODO
@@ -47,7 +47,7 @@ void SettingsMenuMode::onOptionSelected(int option)
// TODO
break;
case 3:
this->setFinished();
// TODO
break;
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
#include "core/display.h"
#include "EPD.h"
#include "epaper/EPD.h"
#include "PagePrinter.h"
#include "esp_log.h"
+2 -2
View File
@@ -1,6 +1,6 @@
#include "PageSettingsProvider.h"
#include "EPD.h"
#include "EPDspi.h" // TODO: remove after display config is extracted
#include "epaper/EPD.h"
#include "epaper/EPDspi.h" // TODO: remove after display config is extracted
int PageSettingsProvider::getWidth()
{