#define SPI_CS 5
#define SPI_DC 4
#define SPI_RST 2
#define SPI_SCLK 18
#define SPI_MOSI 23
#define SPI_MISO 19
void loop()
{
for (int i = 0; i < SAMPLES; i++) {
newTime = micros()-oldTime;
oldTime = newTime;
vReal[i] = analogRead(34); // A conversion takes about 1uS on an ESP32
vImag[i] = 0;
while (micros() < (newTime + sampling_period_us)) { /* do nothing to wait */ }
}
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
tft.fillScreen(color[6]);
tft.setCursor(0, 210);
tft.setTextColor(0xf81f);
tft.setTextSize(2);
tft.println("01 02 05 1k 2k 4k 8k ");
for (int i = 2; i < (SAMPLES/2); i++){
if (vReal[i] > 1500) {
if (i<=2 ) displayBand(0,(int)vReal[i]); // 125Hz
if (i >2 && i<=4 ) displayBand(1,(int)vReal[i]); // 250Hz
if (i >4 && i<=7 ) displayBand(2,(int)vReal[i]); // 500Hz
if (i >7 && i<=15 ) displayBand(3,(int)vReal[i]); // 1000Hz
if (i >15 && i<=40 ) displayBand(4,(int)vReal[i]); // 2000Hz
if (i >40 && i<=70 ) displayBand(5,(int)vReal[i]); // 4000Hz
if (i >70 && i<=288 ) displayBand(6,(int)vReal[i]); // 8000Hz
if (i >288 ) displayBand(7,(int)vReal[i]); // 16000Hz
}
for (byte band = 0; band <= 7; band++) tft.drawLine(36*band,200-peak[band],36*band+28,200-peak[band],color[4]);
}
if (millis()%2 == 0) {for (byte band = 0; band <= 7; band++) {if (peak[band] > 4) peak[band] -= 4;if (peak[band] < 4) peak[band] = 0;}} // Decay the peak
}
void displayBand(int band, int dsize){
int dmax = 200;
dsize /= amplitude;
if (dsize > dmax) dsize = dmax;
if (band == 7) tft.drawLine(36*6,200, 36*6+28,200,color[7]);
for (int s = 0; s <= dsize; s=s+8) {
if (s <30 ) tft.fillRect(band*36,200-s, 28, 5, color[7]);
if (s >=30 ) tft.fillRect(band*36,200-s, 28, 5, color[4]);
}
if (dsize > peak[band]) {peak[band] = dsize;}
}