First I had issues that using the MIDI class and TFT class from Adafruit then the system was freezing/crashing if I turned the knob too fast!
Then I changed my code to basic serial class instead of midi class and build my own simple midi interpretor. It works fine now without crash. But then another problem occured that some data (song changes / ProgramChanges) now gets lost sometimes on its writing to the TFT display using the Adafruit library.
On the positive side I have verified that:
- The MIDI-device are working
- The MIDI-cables are working
- The MIDI-data ARE coming into the program, as I can pass it on to the MIDI-out and receive it on a third external MIDI-device
- The MIDI-shield are also working, as I have teste forward of the data both on the THU and the OUT port.
- Some ProgramChange MIDI data values are not shown / jumped over / forgotten on its way to the TFT-display, even the same data can easily be printed or send elsewhere without problems.
- The RX LED on the Mega 2560 board are also not blinking when the data loss are happening
I have even optimized the writing to the TFT-display, so it only updates the small text area instead of the whole screen.
SIMPLIFIED CODE:
Code: Select all
/////////////////////////////////////////////////////////////////////////////////////
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h> // Arduino SPI library (funny enough the code works without this sentence! Its included 4 levels down in the Adafruit hierarchy)
/////////////////////////////////////////////////////////////////////////////////////
// TFT SPI 2" ST-7789V 320x240px DISPLAY
/////////////////////////////////////////////////////////////////////////////////////
// ST7789 TFT module connections
#define TFT_CS 53 // changed from 10 as its a Mega // define chip select pin
#define TFT_DC 9 // define data/command pin (special for the ST-7789V ?)
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
// Initialize an instance of the Adafruit ST7789 TFT library
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
bool firstTime = true;
/////////////////////////////////////////////////////////////////////////////////////
// MIDI INPUT FOR PROGRAM CHANGE
/////////////////////////////////////////////////////////////////////////////////////
const int MIDI_BaudRate = 31250;
byte MIDI_Byte1 = 0;
byte MIDI_Byte2 = 0;
const byte SERIAL_ProgramChange = 192; //~C0 i HEX
/////////////////////////////////////////////////////////////////////////////////////
// SETUP()
/////////////////////////////////////////////////////////////////////////////////////
void setup(void) {
//MIDI
Serial1.begin(MIDI_BaudRate);
//TFT
tft.init(240, 320, SPI_MODE0);
tft.setRotation(1);
tft.fillScreen(ST77XX_RED);
tft.setCursor(10, 10);
tft.setTextSize(5);
tft.print("READY");
} //setup()
/////////////////////////////////////////////////////////////////////////////////////
// LOOP()
/////////////////////////////////////////////////////////////////////////////////////
void loop() {
//READ 1. MIDI BYTE
MIDI_Byte1 = Serial1.read();
if (MIDI_Byte1 == SERIAL_ProgramChange) {
//READ 2. MIDI value "SONG"
MIDI_Byte2 = Serial1.read();
if (firstTime) {
tft.fillScreen(ST77XX_RED);
tft.setCursor(10, 10);
tft.setTextSize(3);
tft.print("SONG: ");
firstTime = false;
}
tft.fillRect(10+108, 10, 320-10-108, 23, ST77XX_RED); //Optimization of time for clearing the screen, now oinly behind the song number
tft.setCursor(10+108, 10);
//SHOW 2. MIDI value on TFT-display
tft.println(MIDI_Byte2+1);
}
}
- Is the Adafruit_ST7789.h library not suitable for my "Drive IC ST7789V" 2" 320x240px RGB TFT display (https://www.aliexpress.com/item/1005004237117445.html) ?
- Is there a setting / property / callback event / buffer / check / wait / timer / baud rate / interrupt I need to comply?
- Is the TFT display not the best? Are there some better/faster/more reliable displays with same size? 2" 320x240px around 52x36mm (visual display) 62x37mm (the PCB). (It could also be lower in the height. The important thing is that its wide, but max 62mm PCB in width)
- Has it something to do with the extra DC pin9 which the ST7789V model has? What is it used for? And how is proper use?
- I use Arduino Mega 2560
- I also have incoming MIDI BPM tempo sync ticks which I also need to interpret
- I need to write to 11 displays totally in the final project