Skip to content

Commit dbb8e31

Browse files
committed
+bookmarks menu
1 parent 3a23708 commit dbb8e31

File tree

1 file changed

+169
-5
lines changed

1 file changed

+169
-5
lines changed

ArduinoBook/ArduinoBook.ino

+169-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ int gap = 3;
4141
//int fontWidth=11;//11 for font16, 17 for font24, 7 for font12
4242
int fontWidth = 7; //11 for font16, 17 for font24, 7 for font12
4343

44+
int bookMenuPos = 0; //0- back to book,1-goto page menu, 2- close book
45+
int bootMenuPos = 0; //0 - load bookmarks list, 1- load files list
46+
int bookmarkMenuPos = 0;
47+
int totalBookmarks = 0;
4448

4549
UWORD Image_Width_Byte;
4650
UWORD Bmp_Width_Byte;
@@ -60,7 +64,7 @@ int buttonState2 = 0; // variable for reading the pushbutton status
6064
int cbPage = 0;
6165
String currentBook = "";
6266
int currentBookIdx = 0;
63-
int menuMode = 0; //0-files, 1-text book inside, 2- CB book inside, 3-book menu(goto page,back to files), 4-goto page menu
67+
int menuMode = 0; //0-files, 1-text book inside, 2- CB book inside, 3-book menu(goto page,back to files), 4-goto page menu, 5-boot menu, 6 - bookmarks list
6468
int gotoPage = 0;
6569
int gotoPageMenu = 0;
6670

@@ -397,7 +401,16 @@ void nextPage() {
397401
page++;
398402
}
399403

404+
bool fileExist(const char* path) {
400405

406+
File fileB = sd.open(path, MFILE_READ);
407+
if (fileB) {
408+
fileB.close();
409+
} else {
410+
return false;
411+
}
412+
return true;
413+
}
401414
void setup() {
402415
clock_prescale_set(clock_div_2);
403416

@@ -421,8 +434,16 @@ void setup() {
421434
Paint_NewImage(IMAGE_BW, EPD_5IN83_WIDTH, EPD_5IN83_HEIGHT, IMAGE_ROTATE_0, IMAGE_COLOR_INVERTED);
422435
SDCard_Init();
423436

437+
u8g2.begin();
438+
439+
if (fileExist("bookmarks.txt")) {
440+
menuMode = 5;
441+
drawBootMenu();
442+
} else {
443+
424444
menuMode = 0;
425445
drawFileList();
446+
}
426447

427448
pinMode(buttonPin, INPUT);
428449
//pinMode(buttonPin2, INPUT);
@@ -433,8 +454,83 @@ void setup() {
433454
/////////////
434455
}
435456

457+
458+
459+
String getBookmarkName(int idx) {
460+
461+
File fileB = sd.open("bookmarks.txt", MFILE_READ);
462+
size_t n;
463+
int cntr = 0;
464+
//Serial.print(buffer);
465+
if (fileB) {
466+
while ((n = fileB.fgets(line, sizeof(line))) > 0) {
467+
char* pch;
468+
pch = strtok(line, ";");
469+
470+
if (cntr == idx) {
471+
String str = String(pch);
472+
fileB.close();
473+
return str;
474+
}
475+
cntr++;
476+
}
477+
fileB.close();
478+
} else {
479+
//Serial.println("bookmarks.txt not found");
480+
}
481+
}
482+
483+
void drawBookmarksList() {
484+
485+
486+
u8g2.clearBuffer(); // clear the internal memory
487+
//u8g2.setFont(u8g2_font_4x6_t_cyrillic); // choose a suitable font
488+
u8g2.setFont(u8g2_font_9x15_t_cyrillic); // choose a suitable font
489+
//u8g2_font_cu12_t_cyrillic
490+
u8g2.setContrast(0x5);
491+
// Serial.print("getb");
492+
Paint_Clear(WHITE);
493+
494+
File fileB = sd.open("bookmarks.txt", MFILE_READ);
495+
size_t n;
496+
int cntr = 0;
497+
//Serial.print(buffer);
498+
if (fileB) {
499+
while ((n = fileB.fgets(line, sizeof(line))) > 0) {
500+
char* pch;
501+
pch = strtok(line, ";");
502+
Paint_DrawString_EN(0, cntr * fontHeight, pch, &Font12, WHITE, BLACK);
503+
if (cntr == 0)
504+
u8g2.drawStr(0, 16, pch); // write something to the internal memory
505+
506+
pch = strtok(NULL, ";");
507+
int page = atoi(pch);
508+
509+
510+
//Serial.println(filename);
511+
512+
513+
514+
Paint_DrawString_EN(400, cntr * fontHeight, pch, &Font12, WHITE, BLACK);
515+
516+
517+
cntr++;
518+
if (cntr > 14)
519+
break;
520+
}
521+
fileB.close();
522+
} else {
523+
//Serial.println("bookmarks.txt not found");
524+
}
525+
526+
527+
totalBookmarks = cntr;
528+
//display.display();
529+
u8g2.sendBuffer(); // transfer internal memory to the display
530+
EPD_5IN83_Display();
531+
}
436532
void drawFileList() {
437-
u8g2.begin();
533+
438534

439535
u8g2.clearBuffer(); // clear the internal memory
440536
//u8g2.setFont(u8g2_font_4x6_t_cyrillic); // choose a suitable font
@@ -468,7 +564,7 @@ void drawFileList() {
468564
String filename = String(f_name);
469565

470566
Paint_DrawString_EN(0, cntr * fontHeight, filename.c_str(), &Font12, WHITE, BLACK);
471-
u8g2.drawStr(0, cntr * fontHeight, filename.c_str()); // write something to the internal memory
567+
//u8g2.drawStr(0, cntr * fontHeight, filename.c_str()); // write something to the internal memory
472568

473569
cntr++;
474570

@@ -583,7 +679,6 @@ String getFileN(int n) {
583679
}
584680

585681

586-
int bookMenuPos = 0; //0- back to book,1-goto page menu, 2- close book
587682

588683
void drawGotoPageMenu() {
589684
u8g2.clearBuffer();
@@ -712,7 +807,44 @@ void myISR() {
712807
u8g2.drawStr(0, 16, "Wait.."); // write something to the internal memory
713808
}
714809
u8g2.sendBuffer(); // transfer internal memory to the display
715-
if (menuMode == 1) {
810+
811+
if (menuMode == 6) {
812+
813+
String str = getBookmarkName(bookmarkMenuPos);
814+
root_dir = str;
815+
816+
for (int j = root_dir.length() - 2; j >= 0; j--) {
817+
if (root_dir[j] == '/') {
818+
root_dir = root_dir.substring(0, j + 1);
819+
currentBook = str.substring(j + 1);
820+
break;
821+
}
822+
}
823+
//set currentBook
824+
SDCard_ReadCB((root_dir + currentBook).c_str());
825+
menuMode = 2;
826+
int p = getBookmark();
827+
828+
u8g2.clearBuffer(); // clear the internal memory
829+
u8g2.sendBuffer();
830+
831+
int temp = p;
832+
cbPage = 0;
833+
skipPagesCB(temp);
834+
fastNextPageCB();
835+
fastDisplayBuffer();
836+
fastNextPageCB();
837+
838+
} else if (menuMode == 5) {
839+
if (bootMenuPos == 0) { //draw bookmarks
840+
menuMode = 6;
841+
drawBookmarksList();
842+
843+
} else if (bootMenuPos == 1) { //files
844+
menuMode = 0;
845+
drawFileList();
846+
}
847+
} else if (menuMode == 1) {
716848
nextPage();
717849
} else if (menuMode == 3) {
718850
processBookMenu();
@@ -812,6 +944,30 @@ void myISR() {
812944
}
813945
}
814946

947+
948+
void drawBootMenu() {
949+
u8g2.clearBuffer();
950+
u8g2.setFont(u8g2_font_9x15_t_cyrillic);
951+
if (bootMenuPos == 0)
952+
u8g2.drawStr(0, 16, "bookmarks");
953+
if (bootMenuPos == 1) {
954+
u8g2.drawStr(0, 16, "files");
955+
}
956+
957+
u8g2.sendBuffer();
958+
}
959+
960+
void drawBookmarkMenu() {
961+
u8g2.clearBuffer();
962+
u8g2.setFont(u8g2_font_9x15_t_cyrillic);
963+
964+
String name = getBookmarkName(bookmarkMenuPos);
965+
966+
u8g2.drawStr(0, 16, name.c_str());
967+
968+
u8g2.sendBuffer();
969+
}
970+
815971
void drawBookMenu() {
816972
u8g2.clearBuffer();
817973
u8g2.setFont(u8g2_font_cu12_t_cyrillic); // choose a suitable font
@@ -854,7 +1010,15 @@ void myISR2() {
8541010
drawBookMenu();
8551011

8561012

1013+
} else if (menuMode == 6) {
1014+
bookmarkMenuPos++;
1015+
if (bookmarkMenuPos == totalBookmarks) bookmarkMenuPos = 0;
1016+
drawBookmarkMenu();
8571017

1018+
} else if (menuMode == 5) {
1019+
bootMenuPos++;
1020+
if (bootMenuPos == 2) bootMenuPos = 0;
1021+
drawBootMenu();
8581022
} else if (menuMode == 3) {
8591023
bookMenuPos++;
8601024
if (bookMenuPos == 7) bookMenuPos = 0;

0 commit comments

Comments
 (0)