#include <SD.h>
#include <ezButton.h>
#include <LiquidCrystal_I2C.h>
#include "lcd_custom_character.h"
int book_num = 0;
int book_state = 0;
int prev_book_num = -1;
int page_num = 0;
int prev_page_num = -1;
String display_text = "";
ezButton next(2);
ezButton previous(3);
ezButton selectBook(4);
ezButton openBook(5);
File file;
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
SD.begin();
// put your setup code here, to run once:
Serial.begin(9600);
/*if(!SD.begin()){
Serial.println("SD card not detected");
}
else{
Serial.println("SD card detected");
}*/
next.setDebounceTime(50);
previous.setDebounceTime(50);
selectBook.setDebounceTime(50);
openBook.setDebounceTime(50);
lcd.init();
lcd.backlight();
lcd.clear();
welcome();
delay(1000);
animate();
/*file = SD.open("harry.txt",FILE_READ);
Serial.print("Total characters: ");
Serial.println(file.size());
if(file){
Serial.println("Harry Potter: ");
delay(2000);
while(file.available()){
char data = file.read();
}
Serial.println();
file.close();
}
Serial.println("File read successfully");*/
}
void loop() {
// put your main code here, to run repeatedly:
next.loop();
previous.loop();
selectBook.loop();
openBook.loop();
if(next.isPressed()){
if(book_state){
page_num ++;
}
else{
book_num++;
}
}
else if(previous.isPressed()){
if(book_state){
page_num --;
}
else{
book_num --;
}
}
else if(selectBook.isPressed()){
book_state = 0;
prev_book_num = -1;
prev_page_num = -1;
page_num = 0;
}
else if(openBook.isPressed()){
book_state = 1;
}
book_num = constrain(book_num,0,1);
if(page_num < 0){
page_num = 0;
}
check_book();
read_book();
}
void check_book(){
if(prev_book_num != book_num){
prev_book_num = book_num;
lcd.clear();
if(book_num){
lcd_print(0,0,"Think and Grow Rich");
}
else{
lcd_print(0,0,"Harry Potter");
}
}
}
void read_book(){
if(book_state){
if(book_num){
file = SD.open("think.txt",FILE_READ);
}
else{
file = SD.open("harry.txt",FILE_READ);
}
int total_pages = file.size()/80;
page_num = constrain(page_num,0,total_pages);
int pointer = page_num * 80;
if(file){
if(prev_page_num != page_num){
prev_page_num = page_num;
display_text = "";
file.seek(pointer);
while(display_text.length() != 80){
char data = file.read();
display_text.concat(data);
}
page_print();
}
}
}
file.close();
}
void lcd_print(int x, int y, String msg){
lcd.setCursor(x,y);
lcd.print(msg);
}
void display_char(int x, int y, int id){
lcd.setCursor(x,y);
lcd.write(id);
}
void animate(){
lcd.createChar(0,left_stick);
lcd.createChar(1,right_stick);
lcd.createChar(2,open_eye);
lcd.createChar(3,middle_stick);
lcd.createChar(4,closed_eye);
for(int i = 0; i < 5; i++){
display_char(11,3,1);
display_char(9,3,3);
display_char(7,3,0);
if(i % 2 == 0){
display_char(8,3,2);
display_char(10,3,2);
delay(1100);
}
else{
display_char(8,3,4);
display_char(10,3,4);
delay(100);
}
}
lcd.clear();
}
void welcome(){
lcd_print(1,0,"Welcome");
delay(1000);
lcd_print(8,1,"To");
delay(1000);
lcd_print(10,2,"DigiBook");
delay(1000);
}
void page_print(){
for(int i =0; i < 4; i++){
lcd_print(0,i,display_text.substring(i*20,(i+1)*20));
}
}