Il tutorial Adafruit l'ho letto e riletto e provato a mettere in pratica i suggerimenti , ma misba.
ok Ok mi hai beccato a scopiazzare! infatti ho preso qua e là dagli esempi forniti con l'hardware, così ho "ereditato" quella libreria.
Proverò ad eliminarla vediamo che succede...
Questo è il codice,
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
//Technical support:goodtft@163.com
// ANEMOMETRO
// 4 GIRI/s = 10 kM/H 1 M/s = 3,6 kM/H sensore 16 finestre
// 1 NODO = 1,852 kM/H 1 M/s = 1,944 Nodi
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <registers.h>
#include <pin_magic.h>
#include <TouchScreen.h>
#include <Fonts/FreeSerif18pt7b.h>
// #include <MCUFRIEND_kbv.h>
// MCUFRIEND_kbv tft;
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
Adafruit_TFTLCD tft;
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 100
#define TS_MAXX 920
#define TS_MINY 70
#define TS_MAXY 900
/*
#define TS_MINX 125
#define TS_MAXX 913
#define TS_MINY 118
#define TS_MAXY 833
*/
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define MINPRESSURE 10
#define MAXPRESSURE 1000
int Colore = WHITE;
int Displ1 = 2;
int Velocita = 0;
int VelMax = 250;
int Displ2 = 2;
int Vunitmis = 1;
int OldVel = 0;
int a = 0;
int PulsUnit = 0;
int PulsLiMax = 0;
int Kiave = 0;
int PulsSuu = 0;
int PulsGiu = 0;
int PaccoDati [8];
int Temperatura = 0;
int Umidita = 0;
unsigned long VaiTime = 0;
unsigned long EraTime = 0;
unsigned long Refresh = 2000;
void negat ( ) {
tft.setRotation(1);
tft.setCursor(10, 210);
tft.setTextSize(3);
if (PulsLiMax != 32) {
// tft.fillRect(0, 185, 155, 60, (BLACK));
tft.setTextColor(MAGENTA, BLACK);
tft.println("CHIUDE A");
}
else {
// tft.fillRect(0, 200, 160, 60, (MAGENTA));
tft.setTextColor(WHITE, MAGENTA);
tft.println("CHIUDE A");
}
}
void testSuu() {
tft.setRotation(2); tft.setCursor(20, 20);
tft.setTextColor(GREEN); tft.setTextSize(6);
tft.println("< "); tft.setRotation(1);
}
void testGiu() {
tft.setRotation(2); tft.setCursor(180, 20);
tft.setTextColor(RED); tft.setTextSize(6);
tft.println(">"); tft.setRotation(1);
}
/*
void testText() {
tft.setRotation(1); tft.setCursor(0, 10);
tft.setTextColor(WHITE); tft.setTextSize(4);
tft.println("Vento ");
}*/
void testUnit() {
// tft.fillRect(0, 0, 200, 50, BLACK);
tft.setRotation(1); tft.setCursor(0, 10);
tft.setTextColor(WHITE, BLACK); tft.setTextSize(3);
switch (Vunitmis) {
case 0:
tft.println("Vento m/s ");
break;
case 1:
tft.println("Vento Km/h ");
break;
case 2:
tft.println("Vento Nodi ");
break;
case 3:
tft.println("Temperatura ");
break;
default:
tft.println("Umidita' % ");
break;
}
}
void testVent() {
tft.setRotation(1); if (Displ1 > 0) {
if (Displ1 < 100) tft.setCursor(85, 60);
else tft.setCursor(5, 60); tft.setFont(FreeSerif18pt7b);
tft.setTextColor(Colore, BLACK); tft.setTextSize(14);
tft.fillRect(0, 50, 320, 135, BLACK);
tft.print(int (Displ1 / 10));
tft.setCursor(175, 90);
tft.setTextSize(10);
tft.print(int (Displ1 % 10));
}
}
void testMax() {
// tft.fillRect(155, 185, 90, 70, BLACK);
tft.setRotation(1); tft.setCursor(180, 200);
tft.setTextColor(MAGENTA, BLACK); tft.setTextSize(5);
if (Displ2 > 0) {
if (Displ2 < 100) tft.print(" "); tft.setCursor(180, 200);
tft.println(int (Displ2 / 10));
}
}
void setup(void) {
Serial.begin(19200);
while (!Serial) {
;
}
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(BLACK);
/* // Attivare per avere un riferimento
tft.drawRect(0, 200, 155, 40, MAGENTA); // testo
tft.drawRect(155, 200, 60, 40, MAGENTA); // numero
// tft.drawRect(0, 150, 155, 40, YELLOW); // testo
// tft.fillRect(155, 150, 60, 40, YELLOW); // numero
tft.drawRect(240, 150, 80, 80, RED);
tft.drawRect(240, 0, 80, 80, GREEN);
tft.drawRect(130, 0, 80, 50, BLUE);
// tft.drawRect(0, 55, 225, 90, WHITE);
// tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE);
*/
pinMode(13, OUTPUT);
}
void loop(void) {
// Visualizza schermata
// testVent ();
// testText ();
testSuu ();
testGiu ();
// testUnit ();
// testMax ();
// negat ();
Serial.flush();
a = 0;
if (Serial.available() >= 8) { // ricevo pacco Dati
for (a = 0; a < 8; a++) {
PaccoDati [a] = Serial.read();
}
}
VelMax = ( PaccoDati [0] * 256 + PaccoDati[1]);
Velocita = ( PaccoDati [2] * 256 + PaccoDati[3]);
Umidita = (PaccoDati [4] * 256 + PaccoDati[5]);
Temperatura = ( PaccoDati [6] * 256 + PaccoDati[7]);
if (VelMax > 900 || VelMax < 1) VelMax = 10;
if (Velocita > 999 || Velocita < 1) Velocita = 1;
// VelMax = 250;
// Velocita = random (255);
if (Velocita > VelMax) Colore = RED;
else Colore = WHITE;
switch (Vunitmis) {
case 0:
Displ1 = Velocita / 3.6;
Displ2 = VelMax / 3.6;
break;
case 1:
Displ1 = Velocita;
Displ2 = VelMax;
break;
case 2:
Displ1 = Velocita / 1.852;
Displ2 = VelMax / 1.852;
break;
case 3:
Displ1 = Temperatura;
Displ2 = 0;
break;
default:
Displ1 = Umidita;
Displ2 = 0;
break;
}
negat ();
testUnit ();
testMax ();
VaiTime = millis();
if (VaiTime - EraTime > Refresh) {
testVent ();
EraTime = VaiTime;
}
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
// if sharing pins, you'll need to fix the directions of the touchscreen pins
// pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
/*
Serial.print("X = "); Serial.print(p.x);
Serial.print(" Y = "); Serial.print(p.y);
Serial.print(" Press "); Serial.println(p.z);
*/
if (p.x < 390 && p.y > 710) {
tft.fillRect(240, 0, 80, 80, GREEN);
PulsSuu = 32 ;
if (PulsLiMax == 32) {
Kiave = 0;
}
else Kiave = 32;
delay(50); // PulsSuu
tft.fillRect(240, 0, 80, 80, BLACK);
// tft.drawRect(240, 0, 80, 80, GREEN);
}
if (p.x > 640 && p.y > 715) {
tft.fillRect(240, 150, 80, 80, RED);
PulsGiu = 32;
if (PulsLiMax == 32) {
Kiave = 0;
}
else Kiave = 32;
delay(50); // Pulsante Giu
tft.fillRect(240, 150, 80, 80, BLACK);
// tft.drawRect(240, 150, 80, 80, RED);
}
if (p.x < 300 && p.y > 430 && p.y < 620) {
tft.fillRect(130, 0, 80, 50, BLUE);
PulsUnit = 32; // Pulsante Unità di misura
delay(50);
tft.fillRect(130, 0, 80, 50, BLACK);
// tft.drawRect(130, 0, 80, 50, BLUE);
}
if (p.x > 800 && p.y < 440) {
tft.fillRect(0, 200, 140, 40, MAGENTA);
delay(100); // Pulsante settaggio limite Max
PulsLiMax = PulsLiMax + 8;
if (PulsLiMax > 32) PulsLiMax = 0;
tft.fillRect(0, 200, 140, 40, BLACK);
}
}
if ( PulsUnit == 32) {
Vunitmis++;
PulsUnit = 8;
if (Vunitmis > 4) Vunitmis = 0;
}
Serial.flush();
if (Serial.availableForWrite () >= 4) {
Serial.write(PulsLiMax);
Serial.write(PulsSuu); // invio pacchetto pulsanti
Serial.write(PulsGiu);
Serial.write(Kiave);
}
PulsSuu = PulsLiMax;
PulsSuu = 4;
PulsGiu = 5;
Kiave = 6;
}