2020-01-29 20:51:49 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_heap_alloc_caps.h"
|
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "main";
|
2020-01-29 20:51:49 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "config.h"
|
|
|
|
#include "buttons.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "display.h"
|
|
|
|
}
|
2020-01-29 22:50:03 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
#include "Typesetter.h"
|
|
|
|
#include "TextStorage.h"
|
|
|
|
#include "PagePrinter.h"
|
2020-01-29 21:50:57 +00:00
|
|
|
|
2020-01-29 20:51:49 +00:00
|
|
|
static struct tm* tm_info;
|
|
|
|
static char tmp_buff[128];
|
|
|
|
static time_t time_now, time_last = 0;
|
|
|
|
static const char *file_fonts[3] = {"/spiffs/fonts/DotMatrix_M.fon", "/spiffs/fonts/Ubuntu.fon", "/spiffs/fonts/Grotesk24x48.fon"};
|
|
|
|
static const char tag[] = "[LilyBook]";
|
|
|
|
esp_err_t ret;
|
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
void sleep(unsigned int ms)
|
2020-01-29 20:51:49 +00:00
|
|
|
{
|
2020-01-31 19:54:19 +00:00
|
|
|
vTaskDelay(ms / portTICK_RATE_MS);
|
2020-01-29 20:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void app_main()
|
|
|
|
{
|
2020-01-31 19:54:19 +00:00
|
|
|
printf("\n LilyBook v%s\n\n", APP_VERSION);
|
2020-01-29 22:50:03 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
storage_init();
|
|
|
|
buttons_init();
|
2020-01-29 20:51:49 +00:00
|
|
|
display_init();
|
|
|
|
spi_init();
|
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
sleep(500);
|
2020-01-29 20:51:49 +00:00
|
|
|
|
|
|
|
display_connect();
|
2020-01-31 19:54:19 +00:00
|
|
|
display_splash_screen();
|
2020-01-29 20:51:49 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
sleep(500);
|
2020-01-31 05:46:35 +00:00
|
|
|
|
|
|
|
printf("==== START ====\r\n\n");
|
2020-01-29 21:50:57 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
Typesetter typesetter;
|
|
|
|
PagePrinter pagePrinter;
|
|
|
|
TextStorage textStorage;
|
|
|
|
TextReader* textReader = textStorage.open("/sdcard/book.txt");
|
2020-01-31 21:17:46 +00:00
|
|
|
Page* pageLast = NULL;
|
|
|
|
Page* pageCurrent = NULL;
|
2020-01-29 22:50:03 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
long bookmark = 0;
|
2020-01-31 21:17:46 +00:00
|
|
|
//bool displaySleeping = false;
|
2020-01-29 20:51:49 +00:00
|
|
|
|
2020-01-29 21:50:57 +00:00
|
|
|
while (1) {
|
2020-01-31 19:54:19 +00:00
|
|
|
char text[1024];
|
|
|
|
if (textReader != NULL) {
|
2020-01-31 21:17:46 +00:00
|
|
|
if (pageCurrent == NULL) {
|
|
|
|
size_t read = textReader->read(bookmark, text, sizeof(text));
|
2020-02-01 00:04:11 +00:00
|
|
|
pageCurrent = typesetter.preparePage(text, read);
|
|
|
|
pageCurrent->start = bookmark;
|
2020-01-31 21:17:46 +00:00
|
|
|
}
|
|
|
|
if (pageLast == NULL) {
|
2020-02-01 00:04:11 +00:00
|
|
|
// align with the start?
|
|
|
|
if (bookmark < sizeof(text)) {
|
|
|
|
size_t read = textReader->read(0, text, sizeof(text));
|
|
|
|
pageLast = typesetter.preparePage(text, read);
|
|
|
|
pageLast->start = 0;
|
|
|
|
} else {
|
|
|
|
size_t read = textReader->read((bookmark - sizeof(text)), text, sizeof(text));
|
|
|
|
pageLast = typesetter.preparePreviousPage(text, read);
|
|
|
|
pageLast->start = bookmark - pageLast->len;
|
|
|
|
}
|
2020-01-31 21:17:46 +00:00
|
|
|
}
|
2020-01-29 21:50:57 +00:00
|
|
|
} else {
|
2020-01-31 21:17:46 +00:00
|
|
|
typesetter.destroyPage(pageCurrent);
|
2020-01-31 19:54:19 +00:00
|
|
|
strcpy(text, "File could not be opened.");
|
2020-01-31 21:17:46 +00:00
|
|
|
pageCurrent = typesetter.preparePage(text, sizeof(text));
|
2020-01-29 21:50:57 +00:00
|
|
|
}
|
2020-01-31 21:17:46 +00:00
|
|
|
|
2020-01-31 19:54:19 +00:00
|
|
|
display_clear();
|
2020-01-31 21:17:46 +00:00
|
|
|
pagePrinter.print(pageCurrent);
|
2020-01-31 19:54:19 +00:00
|
|
|
display_update();
|
2020-01-29 20:51:49 +00:00
|
|
|
|
2020-01-31 21:17:46 +00:00
|
|
|
//time_t idleStart = clock();
|
2020-01-29 22:50:03 +00:00
|
|
|
while (1) {
|
2020-01-31 19:54:19 +00:00
|
|
|
sleep(10);
|
|
|
|
if (buttons_pressed_ok()) {
|
2020-01-29 22:50:03 +00:00
|
|
|
ESP_LOGI(TAG, "Clear page.");
|
2020-01-31 19:54:19 +00:00
|
|
|
display_refresh();
|
|
|
|
break;
|
2020-01-29 22:50:03 +00:00
|
|
|
}
|
2020-01-31 19:54:19 +00:00
|
|
|
if (buttons_pressed_plus()) {
|
2020-01-29 22:50:03 +00:00
|
|
|
ESP_LOGI(TAG, "Turn page PLUS.");
|
2020-01-31 21:17:46 +00:00
|
|
|
if (pageCurrent != NULL) {
|
2020-02-01 00:04:11 +00:00
|
|
|
bookmark = pageCurrent->start + pageCurrent->len;
|
|
|
|
// TODO: limit bookmark to file size
|
2020-01-31 21:17:46 +00:00
|
|
|
typesetter.destroyPage(pageLast);
|
|
|
|
pageLast = pageCurrent;
|
|
|
|
pageCurrent = NULL;
|
|
|
|
} else {
|
|
|
|
ESP_LOGW(TAG, "No current page.");
|
|
|
|
}
|
2020-01-29 22:50:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-01-31 19:54:19 +00:00
|
|
|
if (buttons_pressed_minus()) {
|
2020-01-29 22:50:03 +00:00
|
|
|
ESP_LOGI(TAG, "Turn page MINUS.");
|
2020-01-31 21:17:46 +00:00
|
|
|
if (pageLast != NULL) {
|
2020-02-01 00:04:11 +00:00
|
|
|
bookmark = pageLast->start;
|
2020-01-31 21:17:46 +00:00
|
|
|
typesetter.destroyPage(pageCurrent);
|
|
|
|
pageCurrent = pageLast;
|
|
|
|
pageLast = NULL;
|
|
|
|
} else {
|
|
|
|
ESP_LOGW(TAG, "No last page.");
|
|
|
|
}
|
2020-01-29 22:50:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-01-31 21:17:46 +00:00
|
|
|
/*if (!displaySleeping && (clock() - idleStart > DISPLAY_SLEEP_TIMEOUT)) {
|
|
|
|
displaySleeping = true;
|
|
|
|
ESP_LOGI(TAG, "Display going to sleep after %d ms.", DISPLAY_SLEEP_TIMEOUT);
|
|
|
|
display_sleep();
|
|
|
|
}*/
|
2020-01-29 22:50:03 +00:00
|
|
|
}
|
2020-01-31 21:17:46 +00:00
|
|
|
/*if (displaySleeping) {
|
|
|
|
displaySleeping = false;
|
|
|
|
ESP_LOGI(TAG, "Display waking up.");
|
|
|
|
display_wake();
|
|
|
|
}*/
|
2020-01-29 20:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|