#include <TFT_eSPI.h>
#include <Wire.h>
//#define SPI_FREQUENCY 40000000
//#define SPI_READ_FREQUENCY 16000000
#define PIN_IN1 27
#define PIN_IN2 14
#define RED2RED 0
#define GREEN2GREEN 1
#define BLUE2BLUE 2
#define BLUE2RED 3
#define GREEN2RED 4
#define RED2GREEN 5
#define RAINBOW 6
TFT_eSPI tft = TFT_eSPI();
void setup() {
tft.begin();
tft.writecommand(0x11);
tft.setRotation(3);
}
void loop() {
tft.fillScreen(TFT_BLACK);
tft.drawCircle(120, 120, 120, TFT_WHITE);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum(TL_DATUM);
for (int i=0 ; i<8 ; i++) {
linearMeter(random(0,34), 30, 40+i*20, 3, 15, 0, 35, GREEN2RED);
tft.drawString("(A) Extern", 142, 42+i*20);
}
delay(5000);
}
void linearMeter(int val, int x, int y, int w, int h, int g, int n, byte s) {
// Variable to save "value" text colour from scheme and set default
int colour = TFT_BLUE;
// Draw n colour blocks
for (int b = 1; b <= n; b++) {
if (val > 0 && b <= val) { // Fill in coloured blocks
switch (s) {
case 0: colour = TFT_RED; break; // Fixed colour
case 1: colour = TFT_GREEN; break; // Fixed colour
case 2: colour = TFT_BLUE; break; // Fixed colour
case 3: colour = rainbowColor(map(b, 0, n, 127, 0)); break; // Blue to red
case 4: colour = rainbowColor(map(b, 0, n, 63, 0)); break; // Green to red
case 5: colour = rainbowColor(map(b, 0, n, 0, 63)); break; // Red to green
case 6: colour = rainbowColor(map(b, 0, n, 0, 159)); break; // Rainbow (red to violet)
}
tft.fillRect(x + b*(w+g), y, w, h, colour);
}
else // Fill in blank segments
{
tft.fillRect(x + b*(w+g), y, w, h, TFT_DARKGREY);
}
}
}
uint16_t rainbowColor(uint8_t spectrum) {
spectrum = spectrum%192;
uint8_t red = 0; // Red is the top 5 bits of a 16 bit colour spectrum
uint8_t green = 0; // Green is the middle 6 bits, but only top 5 bits used here
uint8_t blue = 0; // Blue is the bottom 5 bits
uint8_t sector = spectrum >> 5;
uint8_t amplit = spectrum & 0x1F;
switch (sector)
{
case 0:
red = 0x1F;
green = amplit; // Green ramps up
blue = 0;
break;
case 1:
red = 0x1F - amplit; // Red ramps down
green = 0x1F;
blue = 0;
break;
case 2:
red = 0;
green = 0x1F;
blue = amplit; // Blue ramps up
break;
case 3:
red = 0;
green = 0x1F - amplit; // Green ramps down
blue = 0x1F;
break;
case 4:
red = amplit; // Red ramps up
green = 0;
blue = 0x1F;
break;
case 5:
red = 0x1F;
green = 0;
blue = 0x1F - amplit; // Blue ramps down
break;
}
return red << 11 | green << 6 | blue;
}