Ebook — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by tareq-neoaz.

Components

Sketch code

#include <SPI.h>
#include <SD.h>
#include <GxEPD2_BW.h>
#include <Fonts/FreeSans12pt7b.h> 

// --- PINS ---
#define SD_CS 15
#define EPD_CS 5
#define EPD_DC 17
#define EPD_RST 16
#define EPD_BUSY 4
#define BTN_NEXT 25
#define BTN_PREV 26

// --- 7.5" E-PAPER SETUP ---
GxEPD2_BW<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT> display(
  GxEPD2_750_T7(/*CS=*/ EPD_CS, /*DC=*/ EPD_DC, /*RST=*/ EPD_RST, /*BUSY=*/ EPD_BUSY));

String bookText = "";
int currentPage = 0;

const int LINES_PER_PAGE = 20; 
const int CHARS_PER_LINE = 55; 

void setup() {
  Serial.begin(115200);
  pinMode(BTN_NEXT, INPUT_PULLUP);
  pinMode(BTN_PREV, INPUT_PULLUP);

  // Init E-Paper
  display.init(115200);
  display.setRotation(1); 
  
  // Init SD Card (SCK=18, MISO=19, MOSI=23, CS=15)
  SPI.begin(18, 19, 23, SD_CS); 
  
  if (!SD.begin(SD_CS)) {
    Serial.println("SD Card failed!");
    // Draw error on screen
    display.setFullWindow();
    display.firstPage();
    do {
      display.fillScreen(GxEPD_WHITE);
      display.setTextColor(GxEPD_BLACK);
      display.setFont(&FreeSans12pt7b);
      display.setCursor(50, 100);
      display.print("SD Card Error!");
    } while (display.nextPage());
    return;
  }

  // Read the book file
  File file = SD.open("/book.txt");
  if (file) {
    while (file.available()) {
      bookText += (char)file.read();
    }
    file.close();
    Serial.println("Book loaded!");
  } else {
    Serial.println("book.txt not found!");
  }

  drawPage();
}

void drawPage() {
  display.setFullWindow();
  display.firstPage();
  do {
    display.fillScreen(GxEPD_WHITE);
    display.setTextColor(GxEPD_BLACK);
    display.setFont(&FreeSans12pt7b); 
    
    int startIndex = currentPage * (LINES_PER_PAGE * CHARS_PER_LINE);
    
    if (startIndex >= bookText.length()) {
      display.setCursor(50, 100);
      display.print("The End.");
      return; 
    }

    String pageText = bookText.substring(startIndex, startIndex + (LINES_PER_PAGE * CHARS_PER_LINE));
    
    int y = 40;
    while (pageText.length() > 0 && y < 450) {
      String line = pageText.substring(0, CHARS_PER_LINE);
      display.setCursor(30, y);
      display.print(line);
      pageText.remove(0, CHARS_PER_LINE);
      y += 22; 
    }
    
  } while (display.nextPage());
}

void loop() {
  if (digitalRead(BTN_NEXT) == LOW) {
    if ((currentPage + 1) * (LINES_PER_PAGE * CHARS_PER_LINE) < bookText.length()) {
      currentPage++;
      drawPage();
    }
    delay(500); 
  }
  
  if (digitalRead(BTN_PREV) == LOW) {
    if (currentPage > 0) {
      currentPage--;
      drawPage();
    }
    delay(500); 
  }
}

More projects by tareq-neoaz