#include <SD.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*
* Pin 2 - OLED Data (SDA)
* Pin 3 - OLED Clock (SCK)
* Pin 18 - SD Chip Select (CS)
* Pin 15 - SD (CLK)
* Pin 14 - SD (MISO)
* Pin 16 - SD (MOSI)
*/
const int screenDataPin = 2;
const int screenClockPin = 3;
const int sdChipSelectPin = 10;
const int upButtonPin = 9;
const int downButtonPin = 6;
const int testButtonPin = 5;
const int linePerPage = 4;
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 u8x8(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
File root;
int fileCount, firstOption, selectedLine;
String fileNames[16] = {};
int x=0;
void bitmap(char* fle,int groundUp, Adafruit_SSD1306 oled_1)
{
char bootfile[32];
sprintf(bootfile, "%s", fle);
SD.begin();
root = SD.open(bootfile);
if (root) {
x=0;
int y=0;
if(groundUp == 0)
{
y=0;
}
else {
y=64;
}
int addr=0x00;
oled_1.clearDisplay();
// read from the file until there's nothing else in it:
while (root.available()) {
unsigned char readB = root.read();
if(readB > 0)
{
oled_1.drawPixel(x,y,1);
}
else {
oled_1.drawPixel(x,y,0);
}
if(y >= 63)
{
oled_1.display();
//delay(10);
y=-1;
x=0;
}
x++;
if(x >= 128)
{
if(groundUp == 0)
{
y++;
}
else {
y--;
}
x=0;
}
}
delay(1000);
}
}
void setup() {
Serial.begin(9600);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(testButtonPin, INPUT_PULLUP);
//setup screen
u8x8.begin();
u8x8.clear();
u8x8.setCursor(0,0);
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.print("Screen Ready.");
u8x8.setCursor(0,1);
if (!SD.begin(sdChipSelectPin)) {
u8x8.print("SD Init Fail!");
while (1);
}
u8x8.print("SD Card Ready.");
u8x8.setCursor(0,2);
root = SD.open("/");
setFileCounts(root);
String countLabel = "File Count: ";
countLabel.concat(fileCount);
u8x8.print(countLabel);
refreshMenu();
u8x8.setCursor(0, selectedLine);
u8x8.print(">" + cleanFileName(fileNames[firstOption + selectedLine]));
}
void loop() {
if (digitalRead(upButtonPin) == LOW) {
moveSelector(0);
delay(500);
}
if (digitalRead(downButtonPin) == LOW) {
moveSelector(1);
delay(500);
}
if (digitalRead(testButtonPin) == LOW) {
typeFileContents();
delay(500);
}
}
void typeFileContents() {
String fileName = fileNames[firstOption + selectedLine];
File myFile;
myFile = SD.open(fileName);
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening " + fileName);
}
}
String cleanFileName(String fileName) {
fileName.substring(0,15);
fileName.replace(".TXT","");
return fileName;
}
void moveSelector(int direction) {
u8x8.setCursor(0,selectedLine);
u8x8.print(" " + cleanFileName(fileNames[firstOption + selectedLine]));
if(direction == 0)
{
selectedLine++;
}
else {
selectedLine--;
}
if(selectedLine == linePerPage) {
selectedLine = 0;
if(firstOption + linePerPage >= fileCount) {
firstOption = 0;
} else {
firstOption += linePerPage;
}
refreshMenu();
}
u8x8.setCursor(0,selectedLine);
u8x8.print(">" + cleanFileName(fileNames[firstOption + selectedLine]));
//typeFileContents();
}
void refreshMenu() {
int count = 0;
u8x8.clear();
for(int i = firstOption; i <= firstOption + linePerPage; i++) {
u8x8.setCursor(0,count);
u8x8.print(" " + cleanFileName(fileNames[i]));
count++;
}
}
void setFileCounts(File dir) {
fileCount = 0;
while(true) {
File entry = dir.openNextFile();
if(!entry) {
dir.rewindDirectory();
break;
} else {
if(!entry.isDirectory()) {
fileNames[fileCount] = entry.name();
fileCount++;
}
}
}
}