Hola a todos, a ver si alguien me puede ayudar o aconsejar.
Estoy realizando un proyecto donde uso una pantalla TFT táctil, ya la hago funcionar, dibujar botones y detectar su pulsación, sé que hay librerías para esto pero quiero hacer la mía, el código es el siguiente.
// Librerías Incluidas
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include "TouchScreen.h"
////Clase encarga de dibujar botones////
// Estructuras de los Menus
// Definiciones
// Pines de conexión del LCD
#define LCD_CS A3 // Chip Select - Pin Analógico 3
#define LCD_DC A2 // Command/Data - Pin Analógico 2
#define LCD_WR A1 // LCD Write - Pin Analógico 1
#define LCD_RD A0 // LCD Read - Pin Analógico 0
#define LCD_RESET A4 // LCD Reset - Pin Analógico 4
#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
// Definimos los colores para poder referirnos a ellos con su nombre en lugar de usar el código hexadecimal de cada uno.
#define NEGRO 0x0000
#define ROJO 0xF800
#define VERDE 0x07E0
#define BLANCO 0xFFFF
#define AZUL 0x001F
#define AZULC 0x07FF
#define AMARILLO 0xFFE0
#define ROSA 0xF81F
#define GRIS 0xC618
#define GRISOSCURO 0x7BEF
// Objetos globales
Adafruit_TFTLCD tft= Adafruit_TFTLCD(LCD_CS,LCD_DC,LCD_WR,LCD_RD,LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
class Boton
{
public:
Boton(int posicionx, int posiciony, int alto, int ancho, String texto);
~Boton();
void DibujarBoton(void);
void DibujarBotonPesionado(void);
void SetPosicionX(int posx);
void SetPosicionY(int posy);
void SetAlto(int alto);
void SetAncho(int ancho);
void SetTexto(String texto);
int GetPosicionX(void);
int GetPosicionY(void);
int GetAlto(void);
int GetAncho(void);
String GetTexto(void);
bool Pulsado(int pulx, int puly, int pult);
private:
int PosicionX, PosicionY, Alto, Ancho;
String Texto="";
bool _Pulsado = false;
void ColocarTexto();
};
Boton::Boton(int posicionx = 20, int posiciony = 0, int alto = 30, int ancho = 200, String texto = "PULSAME")
{
PosicionX = posicionx;
PosicionY = posiciony;
Ancho = ancho;
Alto = alto;
Texto = texto;
_Pulsado = false;
DibujarBoton();
}
Boton::~Boton()
{
}
void Boton::DibujarBoton(void)
{
tft.drawLine(PosicionX, PosicionY, PosicionX + Ancho, PosicionY, BLANCO);
tft.drawLine(PosicionX, PosicionY + Alto, PosicionX + Ancho, PosicionY + Alto, NEGRO);
tft.drawLine(PosicionX, PosicionY, PosicionX, PosicionY + Alto, BLANCO);
tft.drawLine(PosicionX + Ancho, PosicionY, PosicionX + Ancho, PosicionY + Alto, NEGRO);
tft.setTextColor(ROJO);
ColocarTexto();
}
void Boton::DibujarBotonPesionado(void)
{
tft.drawLine(PosicionX, PosicionY, PosicionX + Ancho, PosicionY, NEGRO);
tft.drawLine(PosicionX, PosicionY + Alto, PosicionX + Ancho, PosicionY + Alto, BLANCO);
tft.drawLine(PosicionX, PosicionY, PosicionX, PosicionY + Alto, NEGRO);
tft.drawLine(PosicionX + Ancho, PosicionY, PosicionX + Ancho, PosicionY + Alto, BLANCO);
tft.setTextColor(AZUL);
ColocarTexto();
}
void Boton::SetPosicionX(int posx)
{
PosicionX = posx;
}
void Boton::SetPosicionY(int posy)
{
PosicionY = posy;
}
void Boton::SetAlto(int alto)
{
Alto = alto;
}
void Boton::SetAncho(int ancho)
{
Ancho = ancho;
}
void Boton::SetTexto(String texto)
{
Texto = texto;
}
int Boton::GetPosicionX(void)
{
return PosicionX;
}
int Boton::GetPosicionY(void)
{
return PosicionY;
}
int Boton::GetAlto(void)
{
return Alto;
}
int Boton::GetAncho(void)
{
return Ancho;
}
String Boton::GetTexto(void)
{
return Texto;
}
bool Boton::Pulsado(int pulx, int puly, int pult)
{
if (pulx > PosicionX && pulx < (PosicionX + Ancho) && puly > PosicionY && (puly < PosicionY + Alto) && pult >10)
{
DibujarBotonPesionado();
delay(500);
_Pulsado = true;
}
else
{
DibujarBoton();
_Pulsado = false;
}
return _Pulsado;
}
void Boton::ColocarTexto(void)
{
int centrox = (Ancho / 2 - (Texto.length() * 6)); // Multiplicamos el número de letras por lo que ocupa cada una
//int centrox = Ancho / 2;
int centroy = Alto / 2 - 6; // restamos lo que ocupa una letra
tft.setCursor(PosicionX + centrox, PosicionY + centroy);
tft.setTextSize(2); // 6 pix alto x 4 pix ancho
tft.print(Texto);
}
Boton boton1(20,50,30,200,"SIMULAR CELULA");
Boton boton2(20,90,30,200,"LEER CELULA");
Boton boton3(20, 130, 30, 200, "LEER TERMOPAR");
Boton boton4(20, 170, 30, 200, "LEER TEMPERATURA");
Boton boton5(20, 210, 30, 200, "CONFIGURACION");
Boton boton6(20, 250, 30, 200, "PULSAME");
// Configuración de Arduino, solo se ejecuta una vez
void setup()
{
Serial.begin(115200);
tft.begin(0x9341);
tft.fillScreen(GRIS); // Colocamos el fondo del LCD en Negro y borra la pantalla
tft.setRotation(0); // Establecemos la posición de la pantalla Vertical u Horizontal
//pinMode(XM, OUTPUT);
//pinMode(YP, OUTPUT);
}
// Bucle principal del programa
void loop()
{
TSPoint ps = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
ps.x = map(ps.x, 150, 900, tft.width(),0); // Para rotación 0
ps.y = map(ps.y, 110, 900, tft.height(),0); // Para rotación 0
//ps.x = map(ps.x, 150, 900, 0, tft.width()); // Para rotación 1
//ps.y = map(ps.y, 110, 900, 0, tft.height()); // Para rotación 1
boton1.Pulsado(ps.x,ps.y,ps.z);
boton2.Pulsado(ps.x,ps.y,ps.z);
boton3.Pulsado(ps.x,ps.y,ps.z);
boton4.Pulsado(ps.x,ps.y,ps.z);
boton5.Pulsado(ps.x,ps.y,ps.z);
if (boton6.Pulsado(ps.x,ps.y,ps.z))
{
tft.fillScreen(GRIS); // Colocamos el fondo del LCD en Negro y borra la pantalla
delay(2000);
}
// Serial.print(ps.z); // Comprobar la pulsación en la pantalla tactil
}
Como veis, creo una clase y a partir de ahí los botones que necesito, esto funciona correctamente. Ahora lo que quiero es socar la parte donde creo la clase a una librería, para ello he creado los ficheros .cpp y .h
El fichero boton.h contiene lo siguiente:
#ifndef Boton_h
#define Boton_h
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
class Boton
{
public:
//Boton();
Boton(int posicionx, int posiciony, int alto, int ancho, String texto);
//~Boton();
void DibujarBoton(void);
void DibujarBotonPesionado(void);
void SetPosicionX(int posx);
void SetPosicionY(int posy);
void SetAlto(int alto);
void SetAncho(int ancho);
void SetTexto(String texto);
int GetPosicionX(void);
int GetPosicionY(void);
int GetAlto(void);
int GetAncho(void);
String GetTexto(void);
bool Pulsado(int pulx, int puly, int pult);
private:
int PosicionX, PosicionY, Alto, Ancho;
String Texto="";
bool _Pulsado = false;
void ColocarTexto();
// Definimos los colores para poder referirnos a ellos con su nombre en lugar de usar el código hexadecimal de cada uno.
#define NEGRO 0x0000
#define ROJO 0xF800
#define VERDE 0x07E0
#define BLANCO 0xFFFF
#define AZUL 0x001F
#define CELESTE 0x07FF
#define AMARILLO 0xFFE0
#define MAGENTA 0xF81F
#define GRIS 0xC618
#define GRISOSCURO 0x7BEF
};
#endif
y el fichero boton.cpp
#include "Boton.h"
// Boton::Boton()
// {
// PosicionX = 0;
// PosicionY = 0;
// Alto = 30;
// Ancho = 60;
// Texto = "Boton";
// _Pulsado = false;
// }
Boton::Boton(int posicionx = 20, int posiciony = 0, int alto = 30, int ancho = 200, String texto = "PULSAME")
{
PosicionX = posicionx;
PosicionY = posiciony;
Ancho = ancho;
Alto = alto;
Texto = texto;
_Pulsado = false;
DibujarBoton();
}
//Boton::~Boton()
//{
//}
void Boton::DibujarBoton(void)
{
tft.drawLine(PosicionX, PosicionY, PosicionX + Ancho, PosicionY, BLANCO);
tft.drawLine(PosicionX, PosicionY + Alto, PosicionX + Ancho, PosicionY + Alto, GRIS);
tft.drawLine(PosicionX, PosicionY, PosicionX, PosicionY + Alto, BLANCO);
tft.drawLine(PosicionX + Ancho, PosicionY, PosicionX + Ancho, PosicionY + Alto, GRIS);
tft.setTextColor(ROJO);
ColocarTexto();
}
void Boton::DibujarBotonPesionado(void)
{
tft.drawLine(PosicionX, PosicionY, PosicionX + Ancho, PosicionY, GRIS);
tft.drawLine(PosicionX, PosicionY + Alto, PosicionX + Ancho, PosicionY + Alto, BLANCO);
tft.drawLine(PosicionX, PosicionY, PosicionX, PosicionY + Alto, GRIS);
tft.drawLine(PosicionX + Ancho, PosicionY, PosicionX + Ancho, PosicionY + Alto, BLANCO);
tft.setTextColor(AZUL);
ColocarTexto();
}
void Boton::SetPosicionX(int posx)
{
PosicionX = posx;
}
void Boton::SetPosicionY(int posy)
{
PosicionY = posy;
}
void Boton::SetAlto(int alto)
{
Alto = alto;
}
void Boton::SetAncho(int ancho)
{
Ancho = ancho;
}
void Boton::SetTexto(String texto)
{
Texto = texto;
}
int Boton::GetPosicionX(void)
{
return PosicionX;
}
int Boton::GetPosicionY(void)
{
return PosicionY;
}
int Boton::GetAlto(void)
{
return Alto;
}
int Boton::GetAncho(void)
{
return Ancho;
}
String Boton::GetTexto(void)
{
return Texto;
}
bool Boton::Pulsado(int pulx, int puly, int pult)
{
if (pulx > PosicionX && pulx < (PosicionX + Ancho) && puly > PosicionY && (puly < PosicionY + Alto) && pult >10)
{
DibujarBotonPesionado();
delay(500);
_Pulsado = true;
}
else
{
DibujarBoton();
_Pulsado = false;
}
return _Pulsado;
}
void Boton::ColocarTexto(void)
{
int centrox = (Ancho / 2 - (Texto.length() * 6)); // Multiplicamos el número de letras por lo que ocupa cada una
//int centrox = Ancho / 2;
int centroy = Alto / 2 - 6; // restamos lo que ocupa una letra
tft.setCursor(PosicionX + centrox, PosicionY + centroy);
tft.setTextSize(2); // 6 pix alto x 4 pix ancho
tft.print(Texto);
}
Los dos ficheros están en una carpeta llamada boton que a su vez la pongo en la carpeta del proyecto de arduino, el fichero .ino contiene lo siguiente
// Librerías Incluidas
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include "TouchScreen.h"
#include "Boton/Boton.h"
////Clase encarga de dibujar botones////
// Estructuras de los Menus
// Definiciones
// Pines de conexión del LCD
#define LCD_CS A3 // Chip Select - Pin Analógico 3
#define LCD_DC A2 // Command/Data - Pin Analógico 2
#define LCD_WR A1 // LCD Write - Pin Analógico 1
#define LCD_RD A0 // LCD Read - Pin Analógico 0
#define LCD_RESET A4 // LCD Reset - Pin Analógico 4
#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
// Objetos globales
Adafruit_TFTLCD tft= Adafruit_TFTLCD(LCD_CS,LCD_DC,LCD_WR,LCD_RD,LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
Boton boton1(20,50,30,200,"SIMULAR CELULA");
Boton boton2(20,90,30,200,"LEER CELULA");
Boton boton3(20, 130, 30, 200, "LEER TERMOPAR");
Boton boton4(20, 170, 30, 200, "LEER TEMPERATURA");
Boton boton5(20, 210, 30, 200, "CONFIGURACION");
Boton boton6(20, 250, 30, 200, "PULSAME");
// Configuración de Arduino, solo se ejecuta una vez
void setup()
{
Serial.begin(115200);
tft.begin(0x9341);
tft.fillScreen(GRIS); // Colocamos el fondo del LCD en Negro y borra la pantalla
tft.setRotation(0); // Establecemos la posición de la pantalla Vertical u Horizontal
}
// Bucle principal del programa
void loop()
{
TSPoint ps = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
ps.x = map(ps.x, 150, 900, tft.width(),0); // Para rotación 0
ps.y = map(ps.y, 110, 900, tft.height(),0); // Para rotación 0
//ps.x = map(ps.x, 150, 900, 0, tft.width()); // Para rotación 2
//ps.y = map(ps.y, 110, 900, 0, tft.height()); // Para rotación 2
boton1.Pulsado(ps.x,ps.y,ps.z);
boton2.Pulsado(ps.x,ps.y,ps.z);
boton3.Pulsado(ps.x,ps.y,ps.z);
boton4.Pulsado(ps.x,ps.y,ps.z);
boton5.Pulsado(ps.x,ps.y,ps.z);
if (boton6.Pulsado(ps.x,ps.y,ps.z))
{
tft.fillScreen(GRIS); // Colocamos el fondo del LCD en Negro y borra la pantalla
delay(2000);
}
}
Cuando compilo recibo los siguientes mensajes de erro:
C:\Users\rodri\AppData\Local\Temp\ccc506Wd.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_PruebasPantallaTFT2.ino.cpp.o.3648':
<artificial>:(.text.startup+0x3fe): undefined reference to `Boton::Boton(int, int, int, int, String)'
<artificial>:(.text.startup+0x422): undefined reference to `Boton::Boton(int, int, int, int, String)'
<artificial>:(.text.startup+0x446): undefined reference to `Boton::Boton(int, int, int, int, String)'
<artificial>:(.text.startup+0x46a): undefined reference to `Boton::Boton(int, int, int, int, String)'
<artificial>:(.text.startup+0x48e): undefined reference to `Boton::Boton(int, int, int, int, String)'
C:\Users\rodri\AppData\Local\Temp\ccc506Wd.ltrans0.ltrans.o:<artificial>:(.text.startup+0x4b2): more undefined references to `Boton::Boton(int, int, int, int, String)' follow
C:\Users\rodri\AppData\Local\Temp\ccc506Wd.ltrans0.ltrans.o: In function `loop':
C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:61: undefined reference to `Boton::Pulsado(int, int, int)'
C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:62: undefined reference to `Boton::Pulsado(int, int, int)'
C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:63: undefined reference to `Boton::Pulsado(int, int, int)'
C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:64: undefined reference to `Boton::Pulsado(int, int, int)'
C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:65: undefined reference to `Boton::Pulsado(int, int, int)'
C:\Users\rodri\AppData\Local\Temp\ccc506Wd.ltrans0.ltrans.o:C:\Users\rodri\OneDrive\Documentos\Programacion\Proyectos Arduino\PruebasPantallaTFT2/PruebasPantallaTFT2.ino:66: more undefined references to `Boton::Pulsado(int, int, int)' follow
collect2.exe: error: ld returned 1 exit status
exit status 1
y la verdad, no sé que me falta o que esto haciendo mal. Por favor, ¿alguien me puede ayudar a ver que me falla?
Gracias

