#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft= Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define KEY_UP 3
#define KEY_SEL 4
#define KEY_DOWN 5
int file_num = 0; // поточний (вибраний) файл
int file_cnt = 0; // кількість файлів у каталозі
File fl; // наш робочий файл
int char_x, char_y; // ширина і висота знакомісця
#define MAXY 300
long index[100]; // масив індексів першого символа кожної сторінки
int page=0; // номер поточної сторінки
void setup() {
pinMode(KEY_UP, INPUT_PULLUP);
pinMode(KEY_SEL, INPUT_PULLUP);
pinMode(KEY_DOWN, INPUT_PULLUP);
tft.begin();
Serial.begin(115200);
if ( SD.begin( 7 ) ){
Serial.println("Card is found");
while (true) {
select_file();
open_file();
show_pages();
}
}
}
void show_index(){
for( int i=0; i<10; i++) {
Serial.print( index[i] );
Serial.print(",");
}
Serial.println();
}
void next_page(){
//tft.println("Next page");
prepare_screen();
index[ page ] = fl.position(); // номер наступного байта у файлі
while ( true ) {
if ( fl.available() == 0 ) break;
char c = fl.read();
tft.write( c );
if ( tft.getCursorY() + char_y >= MAXY ) break;
}
tft.drawLine(0, MAXY, 239, MAXY, ILI9341_RED );
show_index();
}
void show_pages(){
next_page();
while ( true ) {
if ( digitalRead(KEY_UP) == 0 ) {
if ( page > 0 ) page--;
fl.seek( index[page] ); // перевод вказівника який байт буде наступним
next_page();
}
if ( digitalRead(KEY_DOWN) == 0 ) {
if ( fl.available() ) page++;
next_page();
}
if ( digitalRead(KEY_SEL) == 0 ) {
fl.close();
tft.fillScreen ( ILI9341_BLACK );
break;
}
}
}
void prepare_screen(){
tft.fillScreen( ILI9341_BLACK );
tft.setCursor(0,0);
tft.setTextSize(2);
tft.println();
tft.print(" ");
char_x = tft.getCursorX();
char_y = tft.getCursorY();
tft.setCursor(0,0);
Serial.print("char_x=");Serial.print(char_x);Serial.print(" char_y=");Serial.println(char_y);
}
void open_file(){
File dir = SD.open("/");
int cur_file = 0;
while ( true ) {
fl = dir.openNextFile();
if ( fl ) {
if ( file_num == cur_file ) {
Serial.print("File ");
Serial.print( fl.name() );
Serial.println(" is opened!");
break;
}
cur_file++;
}
fl.close();
}
dir.close();
}
void select_file(){
show_dir();
for (;;) {
if ( digitalRead(KEY_UP) == 0 ) {
if (file_num > 0) file_num--;
show_dir();
}
if ( digitalRead(KEY_DOWN) == 0 ) {
if ( file_cnt > file_num+1 ) file_num++;
show_dir();
}
if ( digitalRead(KEY_SEL) == 0 ) {
break;
}
}
Serial.print("Selected file ");
Serial.println( file_num );
}
void show_dir() { // показати вміст головного каталогу
tft.setTextSize(2);
tft.setCursor(0,0);
File dir = SD.open("/");
int cur_file = 0;
while ( true ) {
File f = dir.openNextFile();
if ( f ) {
/*
Serial.print( f.name() );
Serial.print( "\t" );
Serial.println( f.size() );
*/
if ( file_num == cur_file ) {
tft.setTextColor( ILI9341_WHITE );
tft.print("--> ");
}
else {
tft.setTextColor( ILI9341_BLACK );
tft.print("--> ");
tft.setTextColor( ILI9341_WHITE );
}
tft.println( f.name() );
cur_file++;
file_cnt = cur_file;
}
else {
break;
}
f.close();
}
dir.close();
}
void loop() {
// put your main code here, to run repeatedly:
}