/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
#define BUTTON_PIN1 7
#define BUTTON_PIN2 6
#define BUTTON_PIN3 5
#define BUTTON_PIN4 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
byte X = 0;
byte Y = 40;
byte W = 240;
byte H = 50;
byte progress = 0;
const int analogInPin = A0;
int outputValue = 0;
int previousValue = 0;
int currentBank = 0;
int currentPreset = 0;
int presetByBank = 4;
bool isSync = true;
class Parameter
{
private:
String label;
int CC;
int CurrentValue;
public:
Parameter(String label, int CC)
{
this->label = label;
this->CC = CC;
this->CurrentValue = 0;
}
String getLabel() {
return label;
}
int getCurrentValue() {
return CurrentValue;
}
int setCurrentValue(int value) {
CurrentValue = value;
}
};
Parameter parameters[] =
{
Parameter("Volume", 12),
Parameter("Mix", 13),
Parameter("Wha", 14),
Parameter("Pitch", 15),
Parameter("Dly Mix", 16),
Parameter("Dly Feed", 17),
};
void setup()
{
Serial.begin(9600);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
pinMode(BUTTON_PIN3, INPUT_PULLUP);
pinMode(BUTTON_PIN4, INPUT_PULLUP);
Parameter p = parameters[1];
p.setCurrentValue(99);
DrawInfoPresets();
}
void loop()
{
getExpressionValue();
bar();
changePresetEvent();
delay(10);
}
void changePresetEvent()
{
int value1 = digitalRead(BUTTON_PIN1);
if (value1 == LOW)
{
currentPreset = 0;
DrawInfoPresets();
isSync = false;
}
int value2 = digitalRead(BUTTON_PIN2);
if (value2 == LOW)
{
currentPreset = 1;
DrawInfoPresets();
isSync = false;
}
int value3 = digitalRead(BUTTON_PIN3);
if (value3 == LOW)
{
currentPreset = 2;
DrawInfoPresets();
isSync = false;
}
int value4 = digitalRead(BUTTON_PIN4);
if (value4 == LOW)
{
currentPreset = 3;
DrawInfoPresets();
isSync = false;
}
}
int maxHeight = 260;
void getExpressionValue()
{
previousValue = outputValue;
int sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, maxHeight);
int valueMidi = map(sensorValue, 0, 1023, 0, 127);
int index = (0 * currentBank * presetByBank) + currentPreset;
if (isSync)
{
parameters[index].setCurrentValue(valueMidi);
}
}
void bar()
{
int X = 90;
int Y = 30;
int width = 60;
static unsigned long timer = 0;
unsigned long interval = 20;
//if (millis() - timer >= interval)
{
tft.fillRect(X, Y + maxHeight - outputValue, width, outputValue, isSync ? ILI9341_GREEN : ILI9341_ORANGE);
tft.fillRect(X, Y, width, maxHeight - outputValue, ILI9341_BLACK);
int value10 = map(outputValue, 0, maxHeight, 0, 10);
int previousValue10 = map(previousValue, 0, maxHeight, 0, 10);
if (value10 != previousValue10)
{
//tft.fillRect(150, 100, 100, 100, ILI9341_BLUE); // erase old data
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(7);
tft.setCursor(previousValue10 == 10 ? 0 : 30, 140);
tft.println(previousValue10);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(value10 == 10 ? 0 : 30, 140);
tft.println(value10);
}
if (!isSync)
{
Parameter p = parameters[(0 * currentBank * presetByBank) + currentPreset];
int value = map(p.getCurrentValue(), 0, 127, 0, maxHeight);
tft.fillRect(150, Y + maxHeight - value - 5, 240, 5, ILI9341_RED);
if (abs(outputValue - value) < 5)
{
isSync = true;
tft.fillRect(150, Y, 240, maxHeight + 5, ILI9341_BLACK); //Clean info droite
}
}
}
}
void DrawInfoPresets()
{
tft.fillRect(150, Y, 240, maxHeight + 5, ILI9341_BLACK); //Clean info droite
int bottomY = 295;
DrawInfoPreset(0, 0, true, 0);
DrawInfoPreset(120, 0, false, 1);
DrawInfoPreset(0, bottomY, true, 2);
DrawInfoPreset(120, bottomY, false, 3);
}
void DrawInfoPreset(int X, int Y, bool alignLeft, int index)
{
Parameter parameter = parameters[(0 * currentBank * presetByBank) + index];
int width = 120;
int height = 25;
tft.fillRect(X, Y, X + width, Y + height, ILI9341_BLACK); // erase old data
if (index == currentPreset)
{
tft.fillRect(X, Y, width - (alignLeft ? 2 : 0), height, ILI9341_WHITE); // erase old data
tft.setTextColor(ILI9341_BLACK);
}
else
{
tft.setTextColor(ILI9341_WHITE);
}
int additionnalX = alignLeft ? 0 : (10 - parameter.getLabel().length()) * 12;
tft.setCursor(X + additionnalX, Y);
tft.setTextSize(2);
tft.println(parameter.getLabel());
int value = map(parameter.getCurrentValue(), 0, 127, 0, 120);
tft.drawRect(X, Y + 20, X + width - 2, 5, ILI9341_WHITE);
tft.fillRect(X, Y + 20, value - 2, 5, ILI9341_WHITE);
}