Vous êtes sur la page 1sur 7

//TFT LCD Shield 3.

5''
/*

This program provides cartesian type graph function

Revisions
rev date author description
1 12-24-2015 kasprzak initial creation

Updated by Bodmer to be an example for the library here:


https://github.com/Bodmer/TFT_HX8357_Due

*/

#include <TFT_HX8357_Due.h> // Hardware-specific library

TFT_HX8357_Due tft = TFT_HX8357_Due(); // Invoke custom library

#define LTBLUE 0xB6DF


#define LTTEAL 0xBF5F
#define LTGREEN 0xBFF7
#define LTCYAN 0xC7FF
#define LTRED 0xFD34
#define LTMAGENTA 0xFD5F
#define LTYELLOW 0xFFF8
#define LTORANGE 0xFE73
#define LTPINK 0xFDDF
#define LTPURPLE 0xCCFF
#define LTGREY 0xE71C

#define BLUE 0x001F


#define TEAL 0x0438
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFC00
#define PINK 0xF81F
#define PURPLE 0x8010
#define GREY 0xC618
#define WHITE 0xFFFF
#define BLACK 0x0000

#define DKBLUE 0x000D


#define DKTEAL 0x020C
#define DKGREEN 0x03E0
#define DKCYAN 0x03EF
#define DKRED 0x6000
#define DKMAGENTA 0x8008
#define DKYELLOW 0x8400
#define DKORANGE 0x8200
#define DKPINK 0x9009
#define DKPURPLE 0x4010
#define DKGREY 0x4A49
// estas son las �nicas variables externas utilizadas por la funci�n gr�fica
// es un indicador para dibujar el sistema de coordenadas solo en la primera
llamada a la funci�n Graph ()
// y minimizar� el parpadeo
// tambi�n crea algunas variables para almacenar las viejas 'x' e 'y', si dibujas 2
gr�ficos en la misma pantalla
// necesitar�s almacenar ox y oy por cada pantalla
boolean display1 = true;//esta variable devuelve un true o un false
boolean update1 = true;

double ox = -999, oy = -999; // // Forzarlos a estar fuera de la pantalla

/*

Funci�n para dibujar un sistema de coordenadas cartesianas y trazar los datos que
desee
Solamente se debe pasar 'x' y 'y' y se dibujar� el gr�fico

LISTA DE ARGUMENTOS
&d nombre del display utilizado
x = punto x de datos(ES COMO EN MATLAB EL VECTOR X o N, solo q no es vector sino
que es un dato double)
y = punto y de datos(ES COMO EN MATLAB EL VECTOR DE LA FUNCION
Y.........''.......)
gx = ubicacion del grafico en x (abajo a la izquierda)
gy = ubicacion del grafico en x (abajo a la izquierda)
w = width of graph(ANCHO DEL GRAFICO)
h = height of graph(ALTURA DEL GRAFICO)
xlo = lower bound of x axis(l�mite inferior del eje x)
xhi = upper bound of x asis(l�mite superior del eje x)
xinc = division of x axis (distance not count)
ylo = lower bound of y axis
yhi = upper bound of y asis
yinc = division of y axis (distance not count)
title = title of graph
xlabel = x asis label
ylabel = y asis label
&redraw = flag to redraw graph on first call only(bandera para volver a dibujar
el gr�fico en la primera llamada solamente)
color = color de trazadao de la funcion Trace
*/

// Graph(tft, x, y, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "", "", "",
display1, YELLOW);
void Graph(TFT_HX8357_Due &tft, double x, double y, byte dp,
double gx, double gy, double w, double h,
double xlo, double xhi, double xinc,
double ylo, double yhi, double yinc,
char *title, char *xlabel, char *ylabel,//cuando le
pongo * es porque era de otro tipo y le estoy pasando a otro tipo char en este caso
boolean &redraw, unsigned int color) {

double ydiv, xdiv;


double i;
double temp;
int rot, newrot;

// gcolor = graph grid colors (COLOR DE LA CUADRICULA)


// acolor = axes line colors (COLOR DE LA LINEA DE LOS EJES X Y Y)
// pcolor = color of your plotted data(COLOR DE LA FUNCION QUE VAS A GRAFICAR)
// tcolor = text color(COLOR DEL TEXTO)
// bcolor = background color(COLOR DEL FONDO DE PANTALLA)
unsigned int gcolor = DKBLUE;
unsigned int acolor = RED;
unsigned int pcolor = color;
unsigned int tcolor = WHITE;
unsigned int bcolor = BLACK;

if (redraw == true) {//COMPRUEBO QUE EL DISPLAY CON EL QUE TRABAJO sea el


indicado

redraw = false;
// initialize old x and old y in order to draw the first point of the graph
// pero guarda el valor transformado
// note que mi funci�n de transformaci�n es la misma que la funci�n map,
excepto que map usa long y necesitamos double
//ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
//oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;

tft.setTextDatum(MR_DATUM);// Centre text on x,y position,


OJOOOOO quitar y ver que pasa

// draw y scale
for ( i = ylo; i <= yhi; i += yinc) {//inicia desde el limite inferior de y
hasta el limite superior de y en pasos de la division del eje y
// compute the transform
temp = (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;

if (i == 0) {
tft.drawLine(gx, temp, gx + w, temp, acolor);
tft.setTextColor(acolor, bcolor);
tft.drawString(xlabel, (int)(gx + w) , (int)temp, 2);
}
else {
tft.drawLine(gx, temp, gx + w, temp, gcolor);
}
// draw the axis labels
tft.setTextColor(tcolor, bcolor);
// precision is default Arduino--this could really use some format control
tft.drawFloat(i, dp, gx - 4, temp, 1);
}

// draw x scale
for (i = xlo; i <= xhi; i += xinc) {

// compute the transform


temp = (i - xlo) * ( w) / (xhi - xlo) + gx;
if (i == 0) {
tft.drawLine(temp, gy, temp, gy - h, acolor);
tft.setTextColor(acolor, bcolor);
tft.setTextDatum(BC_DATUM);
tft.drawString(ylabel, (int)temp, (int)(gy - h - 8) , 2);
}
else {
tft.drawLine(temp, gy, temp, gy - h, gcolor);
}
// draw the axis labels
tft.setTextColor(tcolor, bcolor);
tft.setTextDatum(TC_DATUM);
// precision is default Arduino--this could really use some format control
tft.drawFloat(i, dp, temp, gy + 7, 1);
}

//now draw the graph labels


tft.setTextColor(tcolor, bcolor);
tft.drawString(title, (int)(gx + w / 2) , (int)(gy - h - 30), 4);
}

// the coordinates are now drawn, plot the data


// the entire plotting code are these few lines...
// recall that ox and oy are initialized above
//x = (x - xlo) * ( w) / (xhi - xlo) + gx;
//y = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
//tft.drawLine(ox, oy, x, y, pcolor);
// it's up to you but drawing 2 more lines to give the graph some thickness
//tft.drawLine(ox, oy + 1, x, y + 1, pcolor);
//tft.drawLine(ox, oy - 1, x, y - 1, pcolor);
//ox = x;
//oy = y;

//Trace(tft, n, ln, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "Sin(x) + Cos(x) +
Tan(x)", " x", "fn(x)", update1, CYAN);
void Trace(TFT_HX8357_Due &tft, double x, double y, byte dp,
double gx, double gy,
double w, double h,
double xlo, double xhi, double xinc,
double ylo, double yhi, double yinc,
char *title, char *xlabel, char *ylabel,
boolean &update1, unsigned int color)
{
double ydiv, xdiv;
double i;
double temp;
int rot, newrot;

//unsigned int gcolor = DKBLUE; // gcolor = graph grid color


unsigned int acolor = RED; // acolor = main axes and label color
unsigned int pcolor = color; // pcolor = color of your plotted data
unsigned int tcolor = WHITE; // tcolor = text color
unsigned int bcolor = BLACK; // bcolor = background color

if (update1) {
update1 = false;

ox = (x - xlo) * ( w) / (xhi - xlo) + gx;


oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;

if ((ox < gx) || (ox > gx+w)) {update1 = true; return;}


if ((oy < gy-h) || (oy > gy)) {update1 = true; return;}

tft.setTextDatum(MR_DATUM);

// draw y scale
for ( i = ylo; i <= yhi; i += yinc) {
// compute the transform
temp = (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
if (i == 0) {
tft.setTextColor(acolor, bcolor);
tft.drawString(xlabel, (int)(gx + w) , (int)temp, 2);
}
// draw the axis labels
tft.setTextColor(tcolor, bcolor);
// precision is default Arduino--this could really use some format control
tft.drawFloat(i, dp, gx - 4, temp, 1);
}

// draw x scale
for (i = xlo; i <= xhi; i += xinc) {

// compute the transform


temp = (i - xlo) * ( w) / (xhi - xlo) + gx;
if (i == 0) {
tft.setTextColor(acolor, bcolor);
tft.setTextDatum(BC_DATUM);
tft.drawString(ylabel, (int)temp, (int)(gy - h - 8) , 2);
}

// draw the axis labels


tft.setTextColor(tcolor, bcolor);
tft.setTextDatum(TC_DATUM);
// precision is default Arduino--this could really use some format control
tft.drawFloat(i, dp, temp, gy + 7, 1);
}

//now draw the graph labels


tft.setTextColor(tcolor, bcolor);
tft.drawString(title, (int)(gx + w / 2) , (int)(gy - h - 30), 4);
}

// the coordinates are now drawn, plot the data


// the entire plotting code are these few lines...
// recall that ox and oy are initialized above
x = (x - xlo) * ( w) / (xhi - xlo) + gx;
y = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;

if ((x < gx) || (x > gx+w)) {update1 = true; return;}


if ((y < gy-h) || (y > gy)) {update1 = true; return;}

tft.drawLine(ox, oy, x, y, pcolor);


// it's up to you but drawing 2 more lines to give the graph some thickness
//tft.drawLine(ox, oy + 1, x, y + 1, pcolor);
//tft.drawLine(ox, oy - 1, x, y - 1, pcolor);
ox = x;
oy = y;

/*

End of graphing function

*/
void setup() {
double x, y, duracion,dax;
int F, Fs;
float Td;
double T, aux;
int Tm,i ;

tft.begin();
tft.fillScreen(BLACK);
tft.setRotation(1);

update1 = true;
F=1;//frecuencia de la se�al
Fs=200*F;//numero de muestras (200) por cada periodo
Tm=5;//periodos muestrales en el lcd
duracion = Tm*(1/((float)F));//duracion que sea 3 periodos de mi se�al
dax=(1/((float)F));//division para el eje x... ojo que este maximo imprime 0.1
no 0.15 debo tener en cuenta esto asi que hay que cambiar
Td=1/((float)Fs);
T=1/((float)F);

Graph(tft, x, y, 1, 60, 290, 390, 260, 0, duracion, dax, -1, 1, .1, "", "", "",
display1, YELLOW);//Primero se grafica el sistema de coordenadas

delay(1000);//espera 1 segundo

for (x = 0; x <= duracion; x += Td) {


y = sin(2*PI*x*F);
Trace(tft, x, y, 1, 60, 290, 390, 260, 0, duracion, dax, -1, 1, .1, "Sin(x)",
"x", "fn(x)", update1, YELLOW);
}
/*delay(1000);//espera 1 segundo

update1 = true;

for (x = 0; x <= duracion; x += Td) {


aux=x;
for(i=1;i<Tm;i++){
if(aux>=T){
aux=aux-T;
}else{
break;
}
}
if(aux>=(T/2)){
y=-1;
}else{
y=1;
}
Trace(tft, x, y, 1, 60, 290, 390, 260, 0, duracion, dax, -1, 1, .1, "Sin(x)",
"x", "fn(x)", update1, GREEN);
}
*/
}
void loop(void) {

Vous aimerez peut-être aussi