#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <Debouncer.h>
#include "tl.h"
#include "tr.h"
#include "bl.h"
#include "br.h"
#include "lt.h"
#include "rt.h"
#include "cf.h"
#include "mc_logo.h"
#include "NotoSansBold15.h"
#define CF_OL24 &Orbitron_Light_24
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
const int gauge_w = 150;
const int gauge_h = 20;
const int gauge_r = 5;
const int gauge_t = 2;
const int gauge_border = 0x6b6d;
const int gauge_back = 0x4208;
const int gauge_fill = TFT_MAROON;
const int gauge_x = 5;
const int gauge_y = 30;
int pin = 32;
int duration_ms = 50;
Debouncer debouncer(pin, duration_ms);
TFT_eSPI tft = TFT_eSPI();
//TFT_eSprite cor = TFT_eSprite(&tft);
//TFT_eSprite cor1 = TFT_eSprite(&tft);
//TFT_eSprite side = TFT_eSprite(&tft);
void drawBack(char* label) {
tft.setSwapBytes(true);
static int tile_w = 5;
static int tile_h = 10;
for (int i = 65; i <= 240 - 65; i = i + tile_w) {
for (int j = 0; j <= 240 - tile_h; j = j + tile_h) {
tft.pushImage(i, j, tile_w, tile_h, cf);
}
}
tft.pushImage(0, 0, 65, 60, tl);
tft.pushImage(240 - 65, 0, 65, 60, tr);
tft.pushImage(0, 240 - 60, 65, 60, bl);
tft.pushImage(240 - 65, 240 - 60, 65, 60, br);
tft.pushImage(0, 60, 65, 60, lt);
tft.pushImage(0, 120, 65, 60, lt);
tft.pushImage(240 - 65, 60, 65, 60, rt);
tft.pushImage(240 - 65, 120, 65, 60, rt);
tft.setFreeFont(CF_OL24);
tft.setTextDatum(MC_DATUM);
tft.setCursor(5, 20);
tft.print(label);
tft.pushImage(10, 200, 200, 21, mastercraft_logo, 0xffffff);
}
void initGauge(int x, int y, char* label) {
int txt_corr = 5;
// Load the font
tft.loadFont(NotoSansBold15);
tft.setTextDatum(ML_DATUM);
tft.setCursor(x, y + gauge_h / 2 - txt_corr);
tft.print(label);
tft.fillSmoothRoundRect(x + 50, y, gauge_w, gauge_h, gauge_r, gauge_border, TFT_BLACK);
tft.fillSmoothRoundRect(x + 50 + gauge_t, y + gauge_t, gauge_w - 2 * gauge_t, gauge_h - 2 * gauge_t, gauge_r - gauge_t, gauge_back, gauge_border);
}
void updateGauge(int x, int y, float progress) {
if (progress <= 5) {
tft.fillSmoothRoundRect(x + 50 + gauge_t, y + gauge_t, gauge_w - 2 * gauge_t, gauge_h - 2 * gauge_t, gauge_r - gauge_t, gauge_back, gauge_border);
tft.fillSmoothRoundRect(x + 50 + gauge_t, y + gauge_t, 5, gauge_h - 2 * gauge_t, gauge_r - gauge_t, gauge_fill, gauge_border);
} else {
tft.fillSmoothRoundRect(x + 50 + gauge_t, y + gauge_t, gauge_w - 2 * gauge_t, gauge_h - 2 * gauge_t, gauge_r - gauge_t, gauge_back, gauge_border);
tft.fillSmoothRoundRect(x + 50 + gauge_t, y + gauge_t, (gauge_w - 2 * gauge_t) * (progress / 100), gauge_h - 2 * gauge_t, gauge_r - gauge_t, gauge_fill, gauge_border);
}
}
void setup() {
Serial.begin(115200);
debouncer.subscribe(Debouncer::Edge::CHANGED, [](const int state) {
drawBack("WAKE");
});
tft.init();
tft.setRotation(1);
tft.setTextSize(1);
tft.fillScreen(TFT_BLACK);
//tft.drawRect(0, 0, 240, 240, TFT_WHITE);
tft.setRotation(1);
drawBack("BALLAST");
initGauge(gauge_x, gauge_y, "PORT");
initGauge(gauge_x, gauge_y + 30, "KGB");
initGauge(gauge_x, gauge_y + 60, "STBD");
}
int ballast_port;
int ballast_port_old;
void loop() {
//debouncer.update();
//Serial.println(analogRead(34));
ballast_port = map(analogRead(34), 0, 4095, 0, 100);
if (ballast_port != ballast_port_old) {
updateGauge(gauge_x, gauge_y, ballast_port);
ballast_port_old = ballast_port;
}
//delay(1000);
}