Cette page présente un terminal qui affiche chaque caractère envoyé par l'USB (liaison série) sur un écran TFT 2,8 pouces. L'Arduino lit le caractére et l'affiche sur l'écran. La réalisation présentée sur cette page a été testée avec les versions suivantes (logiciel et matériel) :
Pour que le code source suivant fonctionne, la bibliothèque de l'écran TFT doit être chargée dans l'IDE Arduino.
// Small TFT terminal
// Display each character sent through the USB
// Support only new line (\n)
#include <SPI.h>
#include <stdint.h>
#include <TFTv2.h>
// Coordinates of the cursor
INT16U x=0;
INT16U y=0;
// Clear terminal
void tftClearTerminal()
{
// Display a black rectangle in the terminal body
Tft.fillScreen(0, 230, 0, 320, BLACK);
}
// Increase Y (new line)
// If cursor is at the bottom of the display,
// Clear terminal to start a new page
void increase_Y()
{
// Increase Y and check if last line
if (++y>22)
{
// Clear terminal (new page)
tftClearTerminal();
x=y=0;
}
}
// Increase Y (next char)
// If cursor is a end of line,
// jump to a new line
void increase_X()
{
// Increase X and check if end of line
if (++x>31)
{
// New line
x=0;
increase_Y();
}
}
// Display a char
void tftPutchar(char c)
{
// Create an string for displaying the char
char string[2];
string[0]=c;
string[1]=0;
// New line ?
if (c=='\n')
{
x=0;
increase_Y();
}
else
{
// Display char
Tft.drawString(string, x*10, 220-y*10, 1, WHITE, LANDSCAPE);
increase_X();
}
}
// Display terminal header (top blue banner)
void tftDisplayHeader()
{
Tft.fillScreen(230, 240, 0, 320, BLUE);
Tft.drawString("TFTerminal - https://lucidar.me", 0, 230, 1, WHITE, LANDSCAPE);
}
void setup()
{
// Turn on the background light
TFT_BL_ON;
// Init TFT library
Tft.TFTinit();
// Display header and clear terminal
tftDisplayHeader();
tftClearTerminal();
// Initialize USB serial
Serial.begin(115200);
}
void loop()
{
// If a new char is available on serial link,
// Display the char
if (Serial.available())
{
char c=Serial.read();
tftPutchar(c);
// Uncomment for echoing
//Serial.print(c);
}
}
Le plus simple pour tester le terminal est d'utiliser l'IDE Arduino. Il faut spécifier une vitesse de 115200 bauds et NewLine.
Sous Linux, il est aussi possible d'utiliser cu :
sudo apt-install cu
cu -l /dev/ttyACM0 -s 115200
Taper ~.
pour quitter le terminal. Pour utiliser cu, il faudra remplacer \n
par \r
dans le code source pour le saut de ligne.
Le matériel présenté sur cette page a été gracieusement offert par Seeed.
Seeed est un fabricant de matériel IoT avec 10 ans d'expérience. Seeed propose une large gamme de matériels, plates-formes, capteur et modules prêts à l'emploi pour différentes plates-formes IoT existantes et un service de production de PCB et assemblage de carte PCB à bas coût. Seeed Studio propose une large sélection de pièces électroniques, pour Arduino, Raspberry Pi et de nombreuses autres plates-formes. En particulier le system Grove est un système de connecteurs unique qui assure la compatibilité entre différents modules. Seeed Studio a développé plus de 280 modules Grove couvrant une large gamme d'applications qui peuvent répondre à une variété de besoins.
Le code source Arduino et labibliothèque pour l'écran TFT peuvent être téléchargés ici: