#include <GxEPD.h>
#include <GxFont_GFX.h>
#include <GxGDEM029T94/GxGDEM029T94.h>
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
#include <SPI.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <SD.h> // Include the SD library
#define CS1 5 // Chip select for display 1
#define DC1 22 // Data/Command for display 1
#define RSET1 21 // Reset for display 1
#define BUSY1 4 // Busy for display 1
#define CS2 15 // Chip select for display 2
#define DC2 2 // Data/Command for display 2
#define RSET2 0 // Reset for display 2
#define BUSY2 16 // Busy for display 2
#define MOSI 23 // Data in
#define SCLK 18 // Clock
#define CS_PIN 33 // Chip select for SD card
#define BUTTON_UP 12 // GPIO pin for the "up" button
#define BUTTON_DOWN 13 // GPIO pin for the "down" button
#define BUTTON_SELECT 14 // GPIO pin for the "select" button
GxIO_Class io1(SPI, CS1, DC1, RSET1);
GxEPD_Class display1(io1, RSET1, BUSY1);
GxIO_Class io2(SPI, CS2, DC2, RSET2);
GxEPD_Class display2(io2, RSET2, BUSY2);
const char TextBsp[] = "Irgendein Text";
int cnt;
File root; // Declare the root variable
File currentFile; // Declare the current file variable
int selectedFileIndex = 0; // Index of the selected file
int totalFiles = 0; // Total number of files
String fileNames[100]; // Array to store file names
int scrollPosition = 0; // Scroll position for reading text
void printDirectory(File dir, int numTabs, int &line);
void displayText(File file, int scrollPosition);
void printDirectory(File dir, int numTabs, int &line);
// will need to display 20 lins of text at a time. The screen on top will hold 10 inds of text
// the bottom screen will hold the next 10 lines of text.
void displayFileList();
void displayText(File file, int scrollPosition);
void setup() {
Serial.begin(115200);
SPI.begin(SCLK, MISO, MOSI);
display1.init();
display2.init();
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
display1.setRotation(1);
display1.fillScreen(GxEPD_WHITE);
display1.setTextColor(GxEPD_BLACK);
display1.setFont(&FreeMonoBold9pt7b);
display1.setCursor(10, 30);
display1.print("Initializing SD card... ");
display1.update();
display2.setRotation(1);
display2.fillScreen(GxEPD_WHITE);
display2.setTextColor(GxEPD_BLACK);
display2.setFont(&FreeMonoBold9pt7b);
display2.setCursor(10, 30);
display2.println("sheppard semiconductor");
display2.setCursor(20, 40);
display2.println("systems");
display2.update();
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
display1.println("Card initialization failed!");
display1.update();
while (true);
}
Serial.println("initialization done.");
display1.println("initialization done.");
display1.update();
Serial.println("Files in the card:");
display2.println("Files in the card:");
display1.fillScreen(GxEPD_WHITE);
display1.setCursor(10, 30);
display1.setFont(&FreeMonoBold9pt7b);
display1.println("Sheppard Semiconductor");
display1.setCursor(10, 40);
display1.println("Systems");
display1.setCursor(10, 60);
display1.println("What book would you like to read?");
display1.update();
root = SD.open("/");
int line = 0;
display2.fillScreen(GxEPD_WHITE);
display2.setTextColor(GxEPD_BLACK);
display2.setFont(&FreeMonoBold9pt7b);
display2.setCursor(10, 30);
printDirectory(root, 0, line);
display2.update();
}
void displayFileList() {
display2.fillScreen(GxEPD_WHITE);
display2.setCursor(10, 30);
for (int i = 0; i < totalFiles; i++) {
if (i == selectedFileIndex) {
display2.setTextColor(GxEPD_BLACK, GxEPD_WHITE); // Highlight selected file
} else {
display2.setTextColor(GxEPD_BLACK);
}
display2.println(fileNames[i]);
}
display2.update();
};
void displayText(File file, int scrollPosition) {
display1.fillScreen(GxEPD_WHITE);
display2.fillScreen(GxEPD_WHITE);
display1.setCursor(10, 30);
display2.setCursor(10, 30);
display1.setTextColor(GxEPD_BLACK);
display2.setTextColor(GxEPD_BLACK);
display1.setFont(&FreeMonoBold9pt7b);
display2.setFont(&FreeMonoBold9pt7b);
int line = 0;
int charCount = 0;
file.seek(scrollPosition); // Start reading from the scroll position
while (file.available()) {
char c = file.read();
if (line < 10) {
display1.write(c);
} else if (line < 20) {
display2.write(c);
}
charCount++;
if (c == '\n') {
line++;
if (line == 10) {
display1.update();
} else if (line == 20) {
display2.update();
break; // Stop reading after 20 lines
}
}
}
};
void printDirectory(File dir, int numTabs, int &line) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
if (!entry.isDirectory()) {
fileNames[totalFiles] = entry.name();
totalFiles++;
}
entry.close();
}
displayFileList();
};
void loop() {
if (digitalRead(BUTTON_UP) == LOW) {
selectedFileIndex = max(0, selectedFileIndex - 1);
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_DOWN) == LOW) {
selectedFileIndex = min(totalFiles - 1, selectedFileIndex + 1);
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_SELECT) == LOW) {
currentFile = SD.open(fileNames[selectedFileIndex]);
scrollPosition = 0;
displayText(currentFile, scrollPosition);
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_UP) == HIGH && currentFile) {
scrollPosition = max(0, scrollPosition - 200); // Adjust scroll amount as needed
displayText(currentFile, scrollPosition);
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_DOWN) == HIGH && currentFile) {
scrollPosition += 200; // Adjust scroll amount as needed
displayText(currentFile, scrollPosition);
delay(200); // Debounce delay
}
// Update the scroll position for the next read
//scrollPosition += charCount;
display1.update();
display2.update();
};