Skip to content

Instantly share code, notes, and snippets.

@akay25
Created January 27, 2020 02:11
Show Gist options
  • Select an option

  • Save akay25/b170d27b8aa61aec0ab4ac9f4dfe7563 to your computer and use it in GitHub Desktop.

Select an option

Save akay25/b170d27b8aa61aec0ab4ac9f4dfe7563 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <SD.h>
#include <Arduino.h>
#include <U8x8lib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
U8X8_SSD1306_128X32_UNIVISION_SW_I2C display(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
uint8_t cur_x = 0, cur_y = 0;
#define CHIP_SELECT 4
char file_id = '0';
int f_cursor = 0;
#define CHAR_LIMIT 60
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if(!display.begin()) {
Serial.println(F("Allocation failed for LCD"));
for(;;);
}
display.setPowerSave(0);
if (!SD.begin(CHIP_SELECT)) {
Serial.println("initialization failed!");
while (1);
}
display.setFont(u8x8_font_chroma48medium8_r);
}
void loop() {
if(Serial.available() > 0){
if(file_id == '0'){ // If file id is not set then listen for file input
file_id = Serial.read();
Serial.print("Received: ");
Serial.println(file_id);
if ((file_id >= 65 && file_id <= 91) || (file_id >= 97 && file_id <= 123)){
char *filename = getfile(file_id);
if(!SD.exists(filename)){
Serial.print("File is not present");
Serial.println(filename);
}else{
readFile(filename, false);
}
}else{
Serial.println("Invalid filename");
}
}else{
char opt = Serial.read();
char *filename = getfile(file_id);
switch(opt){
case '2': readFile(filename, false); break;
case '8': readFile(filename, true); break;
case '0':file_id = '0';
}
}
}
}
char* getfile(char opt){
switch(opt){
case 'A': case 'a': return "A.TXT";
case 'B': case 'b': return "B.TXT";
case 'C': case 'c': return "C.TXT";
case 'D': case 'd': return "D.TXT";
case 'E': case 'e': return "E.TXT";
case 'F': case 'f': return "F.TXT";
default: return "0.TXT";
}
}
void clear_screen(){
cur_x = cur_y = 0;
display.clear();
}
void lcd_write(char c){
display.setCursor(cur_x, cur_y);
display.print(c);
cur_x++;
if(cur_x == 15){
cur_y++;
cur_x = 0;
}
if(cur_y == 4){
cur_y = 0;
}
}
void readFile(char* filename, bool dir){
int till_count = f_cursor;
if(!dir){
till_count += CHAR_LIMIT;
}else{
till_count-= CHAR_LIMIT;
}
File f = SD.open(filename);
if (f) {
int index = 0;
if(f.available() && ((f_cursor < till_count && !dir) || (f_cursor > till_count && dir && f_cursor >= 0))){
clear_screen();
}
while (f.available() && f_cursor < till_count && !dir) {
char r = f.read();
if(index >= f_cursor){
Serial.write(r);
lcd_write(r);
f_cursor++;
}
index++;
}
while(f.available() && f_cursor > till_count && dir && f_cursor >= 0) {
char r = f.read();
if(index >= till_count){
Serial.write((char)r);
lcd_write(r);
f_cursor--;
}
index++;
}
f.close();
delay(10);
}else{
Serial.println("Failed to open file");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment