-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNovelComicReader.ino
More file actions
147 lines (127 loc) · 5.01 KB
/
NovelComicReader.ino
File metadata and controls
147 lines (127 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "src/config/config.h"
#include "src/core/display.h"
#include "src/core/sdcard.h"
#include "src/core/router.h"
#include "src/pages/pages.h" // Includes base Page and factory declarations
#include "src/core/touch.h" // Include the new Touch class header
#include "src/core/font.h" // Include Font class header
#include <SPI.h> // Re-add for direct access
#include <XPT2046_Touchscreen.h> // Re-add for direct access (needed by Touch class indirectly)
#include <TFT_eSPI.h> // Re-add for direct access
// 布尔类型存储是否息屏 true 是开
bool isScreenOn = true;
// SPIClass touchSPI = SPIClass(TOUCH_SPI); // Removed, handled by Touch class
SPIClass sdSPI = SPIClass(HSPI);
// XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ); // Removed, handled by Touch class
// Get Singleton instances
Router& router = Router::getInstance();
Display& display = Display::getInstance();
SDCard &sd = SDCard::getInstance();
Touch& touch = Touch::getInstance(); // Get Touch instance
void setup() {
Serial.begin(115200);
Serial.println("Starting Novel/Comic Reader...");
// Initialize Touch Screen (now uses Touch class)
touch.begin();
// Initialize SD Card SPI
sdSPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
// 初始化显示屏
display.begin();
display.clear();
Serial.println("Initializing...");
display.drawCenteredText("Initializing...", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// 初始化SD卡
if (!sd.begin()) {
display.clear();
Serial.println("SD Card Error!");
display.drawCenteredText("SD Card Error!", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
while (1) delay(100); // 停止执行
}
// Display loading message and load fast font cache
display.clear();
display.drawCenteredText("Loading Font Cache...", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,1); // Changed text
bool cacheLoaded = Font::getInstance().loadFastFontCache();
if (cacheLoaded) {
Serial.println("Fast cache loaded.");
} else {
Serial.println("Fast cache not loaded (or failed).");
// Optional: Display a message if loading failed or took time
// display.drawCenteredText("Cache Load Complete", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// delay(500); // Show message briefly
}
display.clear(); // Clear loading message/progress bar
// 注册页面路由 (使用函数指针)
router.registerPage("browser", createFileBrowserPage);
router.registerPage("viewer", createImageViewerPage);
router.registerPage("comic", createComicViewerPage);
router.registerPage("text", createTextViewerPage); // Register TextViewerPage using function pointer
router.registerPage("menu", createMenuPage); // Register MenuPage using function pointer
// 导航到菜单页面 (设置为默认启动页面)
router.navigateTo("menu");
Serial.println("Initialization complete!");
}
void loop() {
static unsigned long pressStartTime = 0; // 记录按下时间
if (digitalRead(BUTTON_IO0) == LOW)
{ // 检测按键按下
if (pressStartTime == 0)
{
pressStartTime = millis(); // 记录按下的时间
}
if (millis() - pressStartTime >= 2000)
{ // 长按超过 2 秒
Serial.println("Entering deep sleep mode...");
esp_deep_sleep_start();
}
}
else
{ // 按键释放
if (pressStartTime > 0 && millis() - pressStartTime < 2000)
{
Serial.println("short click buttom");
// 在这里写你的普通功能
if (isScreenOn)
{
Serial.println("close screen");
digitalWrite(TFT_BL, LOW); // 关闭屏幕
isScreenOn = false;
}
else
{
Serial.println("open screen");
digitalWrite(TFT_BL, HIGH); // 开启屏幕
isScreenOn = true;
}
}
pressStartTime = 0; // 复位计时
}
if (!isScreenOn)
{
delay(100); // 如果屏幕关闭,延时避免过度占用CPU
return;
}
// Check for touch input using the Touch class
uint16_t touchX, touchY;
if (touch.getPoint(touchX, touchY)) { // getPoint now returns true if touched and provides mapped coordinates
Serial.println("Touch detected: (" + String(touchX) + ", " + String(touchY) + ")");
Page *currentPage = router.getCurrentPage();
if (currentPage)
{
currentPage->handleTouch(touchX, touchY);
}
// Optional: Add debounce or touch release logic here if needed
// delay(50); // Simple debounce
// 等待触摸释放
//while (touch.isTouched()) {
// touch.update();
// delay(10);
//}
}
// Handle periodic tasks for the current page
Page* currentPage = router.getCurrentPage();
if (currentPage) {
currentPage->handleLoop();
}
// 其他系统任务
delay(10); // 防止过度占用CPU
}