#include <fix_fft.h>
char im[128], data[128] ;
int i = 0, val;
#include <TFT.h>
#include <SPI.h>
#define cs   10
#define dc   8
#define rst  9
int x0 = 0;
int x1 = 0;
int y1 = 0;
TFT TFTscreen = TFT(cs, dc, rst);
int alt;  //altura do diplay automatica
int larg; // largura do display automatica

void setup()
{
  TFTscreen.begin();
  TFTscreen.background(0, 0, 0);
  alt = TFTscreen.height();
  larg = TFTscreen.width();
}

void loop()
{
  x0++;
  if (x0 == 100) {
    TFTscreen.background(0, 0, 0); //executa 100 vezes depois limpa
    x0 = 0;
  }
  TFTscreen.stroke(0, 255, 255);
  for (i = 1; i < 128; i++) {
    val = analogRead(5);
    data[i] = val / 4 - 128;
    im[i] = 0;
  }
  fix_fft(data, im, 7, 0);
  for (i = 1; i < 64; i++) {
    data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
    x1 = i;
    y1 = 5 * data[i];
    if (y1 > 126) y1 = 126;
    // TFTscreen.line(x1, alt, x1, alt - y1);// Uma coluna
    TFTscreen.line(2 * x1, alt, 2 * x1, alt - y1);
    TFTscreen.line (2 * x1 - 1, alt, 2 * x1 - 1, alt - y1); // duas colucas
    //  TFTscreen.line(3*x1-2, alt, 3*x1-2, alt - y1);
    //  TFTscreen.line (3*x1-1, alt, 3*x1-1, alt - y1);
    //  TFTscreen.line (3*x1, alt, 3*x1, alt - y1);// tres colunas
    // TFTscreen.line(1, 2* x1, y1, 2*x1);//128x160 invertendo eixos
    // TFTscreen.line(1, 2* x1 -1, y1, 2*x1-1);//128x160 invertendo eixos
  }
}
