const char fichier[] = R"--(.0|The quick brown fox jumps over the lazy dog
.1|The quick brown fox jumps over the lazy dog
.2|The quick brown fox jumps over the lazy dog
.3|The quick brown fox jumps over the lazy dog
.4|The quick brown fox jumps over the lazy dog
.5|The quick brown fox jumps over the lazy dog
.6|The quick brown fox jumps over the lazy dog
.7|The quick brown fox jumps over the lazy dog
.8|The quick brown fox jumps over the lazy dog
.9|The quick brown fox jumps over the lazy dog
10|The quick brown fox jumps over the lazy dog
11|The quick brown fox jumps over the lazy dog
12|The quick brown fox jumps over the lazy dog
13|The quick brown fox jumps over the lazy dog
14|The quick brown fox jumps over the lazy dog
15|The quick brown fox jumps over the lazy dog
16|The quick brown fox jumps over the lazy dog
17|The quick brown fox jumps over the lazy dog
18|The quick brown fox jumps over the lazy dog
19|The quick brown fox jumps over the lazy dog
20|The quick brown fox jumps over the lazy dog
21|The quick brown fox jumps over the lazy dog
22|The quick brown fox jumps over the lazy dog
23|The quick brown fox jumps over the lazy dog
24|The quick brown fox jumps over the lazy dog
25|The quick brown fox jumps over the lazy dog
26|The quick brown fox jumps over the lazy dog
27|The quick brown fox jumps over the lazy dog
28|The quick brown fox jumps over the lazy dog
29|The quick brown fox jumps over the lazy dog)--";
const size_t hauteurEcran = 5;
size_t ligneDebut = 0;
const char * debut = nullptr;
size_t nombreDeLignes = 0;
size_t compteLignes(const char * s) {
size_t n = *s == '\0' ? 0 : 1;
while (*s)
if ((*s++ == '\n') && (*s != '\0')) n++;
return n;
}
const char * pointeurDebutLigne(size_t numeroLigne) {
const char * ptr = debut;
for (size_t i = 0; i < numeroLigne; i++) {
while (*ptr && *ptr++ != '\n');
}
return ptr;
}
void imprime(size_t numeroLigne) {
char * ptr = pointeurDebutLigne(numeroLigne);
for (size_t i = 0; i < hauteurEcran; i++) {
while (*ptr && *ptr != '\n') Serial.write(*ptr++);
Serial.write('\n');
if (*ptr) ptr++;
}
Serial.println("----------");
}
void setup() {
Serial.begin(115200); Serial.println();
debut = fichier;
nombreDeLignes = compteLignes(debut);
if (nombreDeLignes == 0) {
Serial.println("erreur, fichier vide");
while (true) yield();
}
imprime(ligneDebut);
}
void loop() {
switch (Serial.read()) {
case 'u': // up
if (ligneDebut > 0) ligneDebut -= 1;;
imprime(ligneDebut);
break;
case 'U': // page up
if (ligneDebut >= hauteurEcran) ligneDebut -= hauteurEcran;
else ligneDebut = 0;
imprime(ligneDebut);
break;
case 'd': // down
if (ligneDebut < nombreDeLignes - 1) ligneDebut += 1;;
imprime(ligneDebut);
break;
case 'D': // page down
if (ligneDebut < nombreDeLignes - hauteurEcran) ligneDebut += hauteurEcran;
else ligneDebut = nombreDeLignes - 1;
imprime(ligneDebut);
break;
}
delay(1);
}