#include "unishox2.h"
#include <Button2.h>
#include "dict.h"
const int buttonPin = 12; // 按钮接在Pin 30
Button2 button; // 创建Button2对象
int currentLineStart = 0; // 当前行的起始位置
int totalLines = 1897; // 总行数(根据实际字符串行数修改)
void setup() {
Serial.begin(9600);
/*
button.begin(buttonPin); // 初始化按钮
button.setClickHandler(handleClick); // 设置单击事件处理函数
button.setLongClickHandler(handleLongClick); // 设置长按事件处理函数
// 初始化时显示第一行
displayCurrentLine();
*/
}
void loop() {
const char input[] = "answer [ˈɑːnsə(r); (US) ˈænsər] n. 回答,答复;答案 v. 回答,答复;(作出)答案";
char cbuf[128]; // Restrict buffer size to check that it does not overflow
char dbuf[128];
int clen, dlen;
clen = unishox2_compress_simple(input, sizeof(input), cbuf);
Serial.print(cbuf);
Serial.println(clen);
dlen = unishox2_decompress_simple(cbuf, clen, dbuf);
Serial.println(dlen);
dbuf[dlen] = '\0';
Serial.print("Decompressed output: ");
Serial.println(dbuf);
delay(1000);
}
// 单击事件处理函数
void handleClick(Button2 &btn) {
nextLine(); // 下翻一行
displayCurrentLine(); // 显示当前行
}
// 长按事件处理函数
void handleLongClick(Button2 &btn) {
previousLine(); // 上翻一行
displayCurrentLine(); // 显示当前行
}
// 显示当前行
void displayCurrentLine() {
int lineEnd = getNextLineEnd(currentLineStart);
if (lineEnd == -1) return; // 如果到达末尾,则不显示
char lineBuffer[128]; // 假设每行最多128个字符
readLine(currentLineStart, lineEnd, lineBuffer);
// 解析行内容
char english[64], phonetic[64], chinese[64];
parseLine(lineBuffer, english, phonetic, chinese);
// 输出当前位置/总行数
Serial.print("当前位置: ");
Serial.print(getCurrentLineNumber());
Serial.print("/");
Serial.println(totalLines);
// 输出英文、音标、中文
Serial.print("英文: ");
Serial.println(english);
//*
Serial.print("音标: ");
Serial.println(phonetic);
Serial.print("中文: ");
Serial.println(chinese);
Serial.println("-------------------");
//*/
}
// 解析行内容
void parseLine(const char* line, char* english, char* phonetic, char* chinese) {
// 初始化缓冲区
memset(english, 0, 64);
memset(phonetic, 0, 64);
memset(chinese, 0, 64);
// 查找英文部分
const char* phoneticStart = strchr(line, '[');
if (phoneticStart) {
strncpy(english, line, phoneticStart - line);
english[phoneticStart - line] = '\0'; // 结束字符串
}
// 查找音标部分
const char* phoneticEnd = strchr(phoneticStart, ']');
if (phoneticStart && phoneticEnd) {
strncpy(phonetic, phoneticStart + 1, phoneticEnd - phoneticStart - 1);
phonetic[phoneticEnd - phoneticStart - 1] = '\0'; // 结束字符串
}
// 查找中文部分
const char* chineseStart = strchr(phoneticEnd, ' ');
if (chineseStart) {
strcpy(chinese, chineseStart + 1);
}
}
// 获取当前行号
int getCurrentLineNumber() {
int lineNumber = 0; // 从 0 开始计数
int pos = 0;
while (pos < currentLineStart) {
if (pgm_read_byte(&stringTable[pos]) == '\n') {
lineNumber++;
}
pos++;
}
return lineNumber; // 返回实际行号(从 1 开始)
}
// 下翻一行
void nextLine() {
int lineEnd = getNextLineEnd(currentLineStart);
if (lineEnd == -1) return; // 如果到达末尾,则不翻页
currentLineStart = lineEnd + 1; // 更新当前行起始位置
if (currentLineStart >= strlen_P(stringTable)) {
currentLineStart = 0; // 如果超出范围,回到第一行
}
}
// 上翻一行
void previousLine() {
int lineStart = currentLineStart - 2; // 回到上一行的起始位置
while (lineStart >= 0) {
if (pgm_read_byte(&stringTable[lineStart]) == '\n' || lineStart == 0) {
currentLineStart = lineStart + 1; // 更新当前行起始位置
break;
}
lineStart--;
}
if (currentLineStart < 0) {
currentLineStart = 0; // 如果超出范围,回到第一行
}
}
// 获取下一行的结束位置
int getNextLineEnd(int start) {
int i = start;
while (true) {
char c = pgm_read_byte(&stringTable[i]);
if (c == '\n' || c == '\0') {
return i;
}
i++;
}
return -1;
}
// 读取一行字符串
void readLine(int start, int end, char* buffer) {
int j = 0;
for (int i = start; i < end; i++) {
buffer[j++] = pgm_read_byte(&stringTable[i]);
}
buffer[j] = '\0'; // 字符串结束符
}