1
0
mirror of https://github.com/Dejvino/lilybook.git synced 2024-11-14 20:33:28 +00:00
lilybook/main/main.cpp

145 lines
4.2 KiB
C++
Raw Normal View History

#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-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"
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-31 19:54:19 +00:00
vTaskDelay(ms / portTICK_RATE_MS);
}
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();
display_init();
spi_init();
2020-01-31 19:54:19 +00:00
sleep(500);
display_connect();
2020-01-31 19:54:19 +00:00
display_splash_screen();
2020-01-31 19:54:19 +00:00
sleep(500);
printf("==== START ====\r\n\n");
2020-01-31 19:54:19 +00:00
Typesetter typesetter;
PagePrinter pagePrinter;
TextStorage textStorage;
TextReader* textReader = textStorage.open("/sdcard/book.txt");
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;
//bool displaySleeping = false;
while (1) {
2020-01-31 19:54:19 +00:00
char text[1024];
if (textReader != NULL) {
if (pageCurrent == NULL) {
size_t read = textReader->read(bookmark, text, sizeof(text));
pageCurrent = typesetter.preparePage(text, read);
pageCurrent->start = bookmark;
}
if (pageLast == NULL) {
// 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;
}
}
} else {
typesetter.destroyPage(pageCurrent);
2020-01-31 19:54:19 +00:00
strcpy(text, "File could not be opened.");
pageCurrent = typesetter.preparePage(text, sizeof(text));
}
2020-01-31 19:54:19 +00:00
display_clear();
pagePrinter.print(pageCurrent);
2020-01-31 19:54:19 +00:00
display_update();
//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.");
if (pageCurrent != NULL) {
bookmark = pageCurrent->start + pageCurrent->len;
// TODO: limit bookmark to file size
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.");
if (pageLast != NULL) {
bookmark = pageLast->start;
typesetter.destroyPage(pageCurrent);
pageCurrent = pageLast;
pageLast = NULL;
} else {
ESP_LOGW(TAG, "No last page.");
}
2020-01-29 22:50:03 +00:00
break;
}
/*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
}
/*if (displaySleeping) {
displaySleeping = false;
ESP_LOGI(TAG, "Display waking up.");
display_wake();
}*/
}
}