#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
//#include <Fonts/FreeMono12pt7b.h>
#define CS_CARD 8
#define TFT_CS 10
#define TFT_DC 6
#define TFT_RST 7
Adafruit_ILI9341 tft= Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define KEY_UP 3
#define KEY_SEL 4
#define KEY_DOWN 5
#define NORMAL_COLOR ILI9341_WHITE
#define SELECTED_COLOR ILI9341_RED
int selected_file = 0; // вибраний файл
int count_files = 0; // кількість файлів у каталозі
File fl; // робочий файл
#define MAX_Y 300
int mas[100]; // масив номерів перших символів на кожній сторінці
int page; // номер поточної сторінки
void setup() {
pinMode( KEY_UP, INPUT_PULLUP );
pinMode( KEY_SEL, INPUT_PULLUP );
pinMode( KEY_DOWN, INPUT_PULLUP );
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.setTextSize(2);
tft.setTextColor( ILI9341_WHITE );
//tft.setFont(&FreeMono12pt7b);
if ( SD.begin( CS_CARD ) ) {
Serial.println("Card OK");
//tft.println("Card is present");
while ( true ) {
show_catalog();
select_file();
open_file();
page = 0;
select_pages();
}
}
else {
Serial.println("No card!");
tft.println("Card not found!");
while ( true ) ;
}
}
void open_file(){
int current_file=0;
File cat = SD.open("/");
if ( cat ) {
while ( true ) {
fl = cat.openNextFile();
if ( fl ) {
if (current_file == selected_file) {
Serial.print( current_file ); Serial.print(" ");
Serial.println( fl.name() );
break;
}
current_file ++ ;
fl.close();
}
else break;
}
cat.close();
}
}
void show_mas(){
for (int i=0; i<10; i++) {
Serial.print( mas[i] );
Serial.print(", ");
}
Serial.println();
}
void next_page(){
tft.fillScreen( ILI9341_BLACK );
tft.setTextColor( ILI9341_ORANGE );
tft.setTextSize(1);
tft.setCursor(0,0);
tft.println();
int fontHeight = tft.getCursorY(); // взнали висоту шрифта
tft.setCursor(0,0);
mas[ page ] = fl.position(); // номер наступного байта з файла
show_mas();
while (true) {
if ( fl.available() ) {
char c = fl.read();
tft.write( c );
if ( tft.getCursorY() + fontHeight >= MAX_Y ) {
break;
}
}
else break;
}
tft.drawLine(0, MAX_Y, 239, MAX_Y, ILI9341_WHITE);
tft.setTextSize(2);
tft.setTextColor( tft.color565(127,127,127) );
tft.setCursor(2, MAX_Y+5);
tft.print( fl.name() );
tft.setCursor(156, MAX_Y+5);
tft.print("Pg: ");
tft.print( page+1 );
}
void select_pages(){
next_page();
while ( true ) {
if ( digitalRead(KEY_UP) == 0 ){
page -= 1;
if (page < 0) page = 0;
fl.seek( mas[page] );
next_page();
}
if ( digitalRead(KEY_DOWN) == 0 ){
if ( fl.available() ) {
page += 1;
next_page();
}
}
if ( digitalRead(KEY_SEL) == 0 ){
tft.fillScreen( ILI9341_BLACK );
break;
}
}
}
void select_file(){
while ( true ) {
if ( digitalRead(KEY_UP) == 0 ) {
selected_file--;
if ( selected_file < 0 ) selected_file = 0;
show_catalog();
}
if ( digitalRead(KEY_SEL) == 0 ) {
break;
}
if ( digitalRead(KEY_DOWN) == 0 ) {
selected_file++;
if ( count_files == selected_file ) selected_file = count_files - 1;
Serial.print("selected_file="); Serial.println( selected_file );
show_catalog();
}
}
}
void show_catalog(){
int current_file = 0;
File cat = SD.open("/");
if ( cat ) {
//tft.fillScreen( ILI9341_BLACK );
tft.setCursor(0,0);
tft.setTextColor( NORMAL_COLOR );
tft.setTextSize(2);
tft.println(" List of files:");
tft.println();
while ( true ) {
File f = cat.openNextFile();
if ( f ) {
//Serial.println( f.name() );
if ( current_file == selected_file ) tft.setTextColor( SELECTED_COLOR );
else tft.setTextColor( NORMAL_COLOR );
tft.print(" ");
tft.println( f.name() );
current_file ++ ;
f.close();
}
else break;
}
count_files = current_file;
Serial.print("count_files="); Serial.println(count_files);
}
cat.close();
}
void loop() {
// put your main code here, to run repeatedly:
}