// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
//
// Part 3-3: Add a TFT Display
// Pick your TFT here:
//#include <ILI9341_t3.h>
#include <ST7796_t3.h>
#if defined ST77XX_BLACK // see which TFT we're using
#include <st7735_t3_font_Arial.h>
#else
#include <font_Arial.h> // from ILI9341_t3
#define ST7735_BLACK ILI9341_BLACK
#define ST7735_RED ILI9341_RED
#define ST7735_YELLOW ILI9341_YELLOW
#define ST7735_GREEN ILI9341_GREEN
//#define ST7735_BLACK ILI9341_BLACK
#endif // defined ST77XX_BLACK
//#include <Adafruit_FT6206.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=136,65
AudioAnalyzePeak peak2; //xy=348,219
AudioAnalyzePeak peak1; //xy=358,171
AudioOutputI2S i2s1; //xy=380,92
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 0, peak1, 0);
AudioConnection patchCord3(playSdWav1, 1, i2s1, 1);
AudioConnection patchCord4(playSdWav1, 1, peak2, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=155,192
// GUItool: end automatically generated code
// Use these with the Teensy 4.x and Audio Shield Rev D or D2
#define TFT_DC 9
#define TFT_CS 22
//#define TFT_CS 10
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_MISO 12
#define LED_PWM 4
// Use these with the Teensy 3.2 and Audio Shield Rev C
//#define TFT_DC 20
//#define TFT_CS 21
//#define TFT_RST 255 // 255 = unused, connect to 3.3V
//#define TFT_MOSI 7
//#define TFT_SCLK 14
//#define TFT_MISO 12
#if defined ST77XX_BLACK
ST7796_t3 tft = ST7796_t3(TFT_CS, TFT_DC);
#else
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);//, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
#endif // defined ST77XX_BLACK
// Use these with the Teensy Audio Shield
//#define SDCARD_CS_PIN 10
//#define SDCARD_MOSI_PIN 7 // Teensy 4 ignores this, uses pin 11
//#define SDCARD_SCK_PIN 14 // Teensy 4 ignores this, uses pin 13
// Use these with the Teensy 3.5 & 3.6 & 4.1 SD card
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13
void setup() {
pinMode(LED_PWM, OUTPUT);
//analogWrite(LED_PWM, 64);
digitalWrite(LED_PWM,1);
Serial.begin(9600);
delay(500);
//tft.setClock(16000000);
// Setup the LCD screen
#if defined ST77XX_BLACK
tft.init(320, 480);
tft.setRotation(1); // Rotates screen to match the baseboard orientation
#else
tft.begin();
tft.setRotation(1); // Rotates screen to match the baseboard orientation
#endif // defined ST77XX_BLACK
//tft.invertDisplay(true); // LCD requires colors to be inverted
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_YELLOW);
tft.setFont(Arial_24);
//tft.setTextSize(3);
tft.setCursor(40, 8);
tft.println("Peak Meter");
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.3);
// SPI.setMOSI(SDCARD_MOSI_PIN);
// SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
//tft.setMaxTransaction(100000); // deliberately break SD playback!
tft.setMaxTransaction(2000); // default is 1000, but this should be OK
delay(1000);
}
elapsedMillis msecs;
void loop() {
if (playSdWav1.isPlaying() == false) {
Serial.println("Start playing");
//playSdWav1.play("SDTEST1.WAV");
//playSdWav1.play("SDTEST2.WAV");
//playSdWav1.play("SDTEST3.WAV");
playSdWav1.play("SDTEST4.WAV");
delay(10); // wait for library to parse WAV info
}
if (msecs > 15) {
msecs = 0;
if (peak1.available() && peak2.available()) {
float leftNumber = peak1.read();
float rightNumber = peak2.read();
Serial.print(leftNumber);
Serial.print(", ");
Serial.print(rightNumber);
//Serial.println();
// draw the vertical bars
// biggest single draw possible is
// 240x40 = 9,600 pixels, which
// is enough to cause issues with SD playback
#if defined ST77XX_BLACK
const int barWid = 40;
const int barMax = 240;
const int barBase = 280;
#else
const int barWid = 60;
const int barMax = 160;
const int barBase = 200;
#endif // defined ST77XX_BLACK
uint32_t maxMicros;
elapsedMicros em = 0;
int height = leftNumber * barMax;
tft.fillRect(10, barBase - height, barWid, height, ST7735_GREEN);
maxMicros = em; em = 0;
tft.fillRect(10, barBase - barMax, barWid, barMax - height, ST7735_BLACK);
if (em > maxMicros) maxMicros = em; em = 0;
height = rightNumber * barMax;
tft.fillRect(110, barBase - height, barWid, height, ST7735_GREEN);
if (em > maxMicros) maxMicros = em; em = 0;
tft.fillRect(110, barBase - barMax, barWid, barMax - height, ST7735_BLACK);
if (em > maxMicros) maxMicros = em; em = 0;
// a smarter approach would redraw only the changed portion...
/*
*/
// draw numbers underneath each bar
tft.setFont(Arial_14);
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);
tft.fillRect(60, barBase+4, 40, 16, ST7735_BLACK);
tft.setCursor(60, barBase+4);
tft.print(leftNumber);
tft.fillRect(140, barBase+4, 40, 16, ST7735_BLACK);
tft.setCursor(140, barBase+4);
// tft.print(rightNumber);
tft.print(msecs);
// tft.fillRect(140+80, barBase+4, 100, 16, ST7735_BLACK);
// print HUGE max time - also causes problems!
tft.setCursor(140+0*60, 84);
tft.setTextSize(9);
tft.setTextColor(ST7735_RED, ST7735_BLACK);
#if defined ST77XX_BLACK
//tft.setFont(nullptr);
tft.setFont(Arial_96);
#else
tft.setFontAdafruit();
#endif // defined ST77XX_BLACK
tft.print(maxMicros);
tft.setTextColor(ST7735_RED, 0x38E7);
if (maxMicros<10000) tft.print(' ');
uint32_t maxTrans = tft.maxTransactionLengthSeen / (F_CPU / 1'000'000);
Serial.printf("; maxMicros = %d; maxTransaction = %d %s\n",maxMicros,maxTrans, maxTrans > 2500?"*****":"");
tft.maxTransactionLengthSeen = 0;
msecs = 0;
}
}
}