// STM32 Nucleo-C031C6 SPI LCD Example
// Simulation: https://wokwi.com/projects/365549388158011393
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define ILI9341_VICTRONBLUE 0x0436
#define TFT_DC 2
#define TFT_CS 3
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define ORION_STATUS_Y 227
bool toCell = true;
void setup()
{
Serial.begin(115200);
tft.begin();
//tftDrawOrionsStatus(ORION_STATUS_Y);
tftDrawOverview();
}
void loop()
{
//tftUpdateOrionsStatus (ORION_STATUS_Y);
if(toCell == true)
toCell = false;
else
toCell = true;
tftUpdateOverview(toCell, toCell);
delay(10);
}
void tftDrawOverview()
{
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.fillRoundRect(5, 5, 105, 115, 20, ILI9341_LIGHTGREY);
tft.fillRoundRect(130, 5, 105, 115, 20, ILI9341_LIGHTGREY);
tft.fillRoundRect(5, 140, 230, 175, 20, ILI9341_LIGHTGREY);
tft.drawFastVLine(100, 165, 90, ILI9341_DARKGREY);
tft.drawFastVLine(140, 165, 90, ILI9341_DARKGREY);
tft.drawFastHLine(90, 165, 60, ILI9341_DARKGREY);
tft.drawFastHLine(100, 195, 40, ILI9341_DARKGREY);
tft.drawFastHLine(100, 225, 40, ILI9341_DARKGREY);
tft.drawFastHLine(100, 255, 41, ILI9341_DARKGREY);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(1);
tft.fillRoundRect(110, 155, 20, 20, 5, ILI9341_VICTRONBLUE);
tft.setCursor(110, 147);
tft.print("C1");
tft.fillRoundRect(110, 185, 20, 20, 5, ILI9341_VICTRONBLUE);
tft.setCursor(110, 177);
tft.print("C2");
tft.fillRoundRect(110, 215, 20, 20, 5, ILI9341_VICTRONBLUE);
tft.setCursor(110, 207);
tft.print("C3");
tft.fillRoundRect(110, 245, 20, 20, 5, ILI9341_VICTRONBLUE);
tft.setCursor(110, 237);
tft.print("E1");
tft.fillCircle(120, 165, 6, ILI9341_RED);
tft.fillCircle(120, 195, 6, ILI9341_RED);
tft.fillCircle(120, 225, 6, ILI9341_RED);
tft.fillCircle(120, 255, 6, ILI9341_RED);
tft.fillRoundRect(20, 155, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(19, 147);
tft.print("Hold");
tft.fillRect(76, 120, 8, 35, ILI9341_DARKGREY);
tft.fillRoundRect(70, 155, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(66, 177);
tft.print("Relay");
tft.fillRect(156, 120, 8, 35, ILI9341_DARKGREY);
tft.fillRoundRect(150, 155, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(146, 177);
tft.print("Relay");
tft.fillRoundRect(200, 155, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(199, 147);
tft.print("Hold");
tft.fillCircle(30, 165, 6, ILI9341_RED);
tft.fillCircle(80, 165, 6, ILI9341_RED);
tft.fillCircle(160, 165, 6, ILI9341_RED);
tft.fillCircle(210, 165, 6, ILI9341_RED);
tft.fillRoundRect(45, 91, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(42, 74);
tft.print("230V");
tft.setCursor(24, 82);
tft.print("Plugged");
tft.fillRoundRect(176, 91, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(176, 74);
tft.print("Engine");
tft.setCursor(176, 82);
tft.print("Ignition");
tft.fillCircle(55, 101, 6, ILI9341_RED);
tft.fillCircle(186, 101, 6, ILI9341_RED);
tft.fillRect(54, 110, 2, 70, ILI9341_DARKGREY);
tft.fillRect(29, 175, 2, 5, ILI9341_DARKGREY);
tft.drawFastHLine(23, 180, 39, ILI9341_DARKGREY);
tft.setCursor(23, 182);
tft.print("Logic");
tft.setCursor(23, 190);
tft.print("Supply");
tft.fillRect(185, 110, 2, 70, ILI9341_DARKGREY);
tft.fillRect(210, 175, 2, 5, ILI9341_DARKGREY);
tft.drawFastHLine(179, 180, 38, ILI9341_DARKGREY);
tft.setCursor(188, 182);
tft.print("Logic");
tft.setCursor(182, 190);
tft.print("Supply");
tft.drawFastVLine(70, 72, 48, ILI9341_BLACK);
tft.drawFastHLine(70, 72, 40, ILI9341_BLACK);
tftDrawBattery(78, 77, ILI9341_DARKGREY);
tftDrawPlug(78, 97, ILI9341_DARKGREY);
tft.drawFastVLine(170, 72, 48, ILI9341_BLACK);
tft.drawFastHLine(130, 72, 40, ILI9341_BLACK);
tftDrawBattery(138, 77, ILI9341_DARKGREY);
tftDrawAlternator(138, 97, ILI9341_DARKGREY);
tft.setCursor(10, 253);
tft.print("Output Limit");
tft.setCursor(147, 253);
tft.print("Input Limit ");
tft.setTextSize(2);
tft.fillRoundRect(10, 10, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(31, 13);
tft.print("DVCC");
tft.fillCircle(20, 20, 6, ILI9341_RED);
tft.fillRoundRect(10, 31, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(31, 34);
tft.print("ATC");
tft.fillCircle(20, 41, 6, ILI9341_RED);
tft.fillRoundRect(10, 52, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(31, 55);
tft.print("ATD");
tft.fillCircle(20, 62, 6, ILI9341_RED);
tft.setTextSize(2);
tft.fillRoundRect(135, 10, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(156, 13);
tft.print("ALT");
tft.fillCircle(145, 20, 6, ILI9341_RED);
tft.fillRoundRect(135, 31, 20, 20, 5, ILI9341_DARKGREY);
tft.setCursor(156, 34);
tft.print("RPM");
tft.fillCircle(145, 41, 6, ILI9341_RED);
}
void tftUpdateOverview(bool toCell, bool cellCurrentLimit)
{
char buf[16];
static bool toCellPrev = false;
static bool cellCurrentLimitPrev = false;
static bool displayChargersState = true;
static bool displayChargersStatePrev = false;
static long lastCall = 0;
tft.setTextColor(ILI9341_BLACK, ILI9341_LIGHTGREY);
if((millis() - lastCall) >= 5000)
{
displayChargersState = !displayChargersState;
lastCall = millis();
}
if(toCell != toCellPrev)
{
tft.setTextSize(1);
if(toCell == true)
{
tft.setCursor(10, 205);
tft.print("To Cell ");
tft.setCursor(147, 205);
tft.print("From Engine ");
}
else
{
tft.setCursor(10, 205);
tft.print("From Cell ");
tft.setCursor(147, 205);
tft.print("To Engine ");
}
}
tft.setTextSize(2);
//Cell Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(10, 215);
tft.print(buf);
//Cell Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(10, 231);
tft.print(buf);
//Eng Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(147, 215);
tft.print(buf);
//Eng Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(147, 232);
tft.print(buf);
if(cellCurrentLimit != cellCurrentLimitPrev)
{
tft.setTextSize(1);
if(cellCurrentLimit == true)
{
tft.setCursor(100, 268);
tft.print("<<<<< ");
}
else
{
tft.setCursor(98, 268);
tft.print(" >>>>>");
}
}
tft.setTextSize(2);
//Cell Current Limit
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(10, 263);
tft.print(buf);
//Engine Current Limit
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(147, 263);
tft.print(buf);
tft.setTextSize(1);
//Chargers State or Chargers Temperature label
if(displayChargersState != displayChargersStatePrev)
{
if(displayChargersState == true)
{
tft.setCursor(10, 285);
tft.print("Chargers State ");
}
else
{
tft.setCursor(10, 285);
tft.print("Chargers Temperature");
}
}
tft.setTextSize(2);
//Chargers State or Chargers Temperature
if(displayChargersState == true)
{
tft.setCursor(15, 295);
tft.print("External Control");
}
else
{
sprintf(buf, "88.8/88.8/88.8 C", 12.34);
tft.setCursor(15, 295);
tft.print(buf);
}
tft.setTextSize(1);
//DVCC Voltage
sprintf(buf, "88.8", 12.34);
tft.setCursor(80, 13);
tft.print(buf);
//DVCC Current
sprintf(buf, "888.8", 12.34);
tft.setCursor(80, 21);
tft.print(buf);
//Sensor Hub Current
sprintf(buf, "888 A", 12.34);
tft.setCursor(196, 13);
tft.print(buf);
//Sensor Hub Temperature
sprintf(buf, "888 C", 12.34);
tft.setCursor(196, 21);
tft.print(buf);
//RPM
sprintf(buf, "8k8", 12.34);
tft.setTextSize(2);
tft.setCursor(196, 34);
tft.print(buf);
toCellPrev = toCell;
cellCurrentLimitPrev = cellCurrentLimit;
displayChargersStatePrev = displayChargersState;
}
void tftDrawBattery(uint16_t x, uint16_t y, uint16_t color)
{
tft.fillRect(x, y + 3, 25, 15, color);
tft.fillRect(x + 3, y, 5, 3, color);
tft.fillRect(x + 17, y, 5, 3, color);
}
void tftDrawPlug(uint16_t x, uint16_t y, uint16_t color)
{
tft.fillRect(x + 5, y, 4, 19, color);
tft.fillRect(x, y + 5, 5, 2, color);
tft.fillRect(x, y + 12, 5, 2, color);
tft.fillRect(x + 9, y + 3, 8, 13, color);
tft.fillCircle(x + 18, y + 9, 6, color);
}
void tftDrawAlternator(uint16_t x, uint16_t y, uint16_t color)
{
tft.fillRoundRect(x + 8, y + 3, 17, 12, 2, color);
tft.fillRect(x, y + 7, 8, 4, color);
tft.drawFastVLine(x, y, 18, color);
tft.drawFastVLine(x + 3, y, 18, color);
tft.drawFastHLine(x + 10, y + 5, 13, ILI9341_BLACK);
tft.drawFastHLine(x + 10, y + 7, 13, ILI9341_BLACK);
tft.drawFastHLine(x + 10, y + 9, 13, ILI9341_BLACK);
tft.drawFastHLine(x + 10, y + 11, 13, ILI9341_BLACK);
tft.drawFastHLine(x + 10, y + 13, 13, ILI9341_BLACK);
}
/*void tftDrawOrionsStatus(uint16_t y)
{
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.drawRect(0, y, 240, 93, ILI9341_WHITE);
tft.drawFastHLine(0, y + 17, 240, ILI9341_WHITE);
tft.drawFastVLine(25, y, 93, ILI9341_WHITE);
tft.drawFastVLine(69, y, 93, ILI9341_WHITE);
tft.drawFastVLine(113, y, 93, ILI9341_WHITE);
tft.setCursor(2, y + 2);
tft.print("#");
tft.setCursor(27, y + 2);
tft.print("In");
tft.setCursor(71, y + 2);
tft.print("Out");
tft.setCursor(115, y + 2);
tft.print("Stat/T/CL");
tft.setCursor(2, y + 20);
tft.print("C1");
tft.setCursor(2, y + 39);
tft.print("C2");
tft.setCursor(2, y + 58);
tft.print("C3");
tft.setCursor(2, y + 77);
tft.print("E1");
}
void tftUpdateOrionsStatus (uint16_t y)
{
char buf[16];
unsigned long start = micros();
tft.setTextSize(1);
//C1 In Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(27, y + 19);
tft.print(buf);
//C1 In Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(27, y + 27);
tft.print(buf);
//C1 Out Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(71, y + 19);
tft.print(buf);
//C1 Out Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(71, y + 27);
tft.print(buf);
//C2 In Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(27, y + 38);
tft.print(buf);
//C2 In Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(27, y + 46);
tft.print(buf);
//C2 Out Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(71, y + 38);
tft.print(buf);
//C2 Out Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(71, y + 46);
tft.print(buf);
//C3 In Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(27, y + 57);
tft.print(buf);
//C3 In Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(27, y + 65);
tft.print(buf);
//C3 Out Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(71, y + 57);
tft.print(buf);
//C3 Out Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(71, y + 65);
tft.print(buf);
//E1 In Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(27, y + 76);
tft.print(buf);
//E1 In Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(27, y + 84);
tft.print(buf);
//E1 Out Volts
sprintf(buf, "88.88 V", 12.34);
tft.setCursor(71, y + 76);
tft.print(buf);
//E1 Out Amps
sprintf(buf, "88.88 A", 12.34);
tft.setCursor(71, y + 84);
tft.print(buf);
//C1 Status
sprintf(buf, "External Control", 12.34);
tft.setCursor(115, y + 19);
tft.print(buf);
//C1 Current Limit / Temperature / Ping
sprintf(buf, "88.8 A/88.8 *C/9999", 12.34);
tft.setCursor(115, y + 27);
tft.print(buf);
//C2 Status
sprintf(buf, "External Control", 12.34);
tft.setCursor(115, y + 38);
tft.print(buf);
//C2 Current Limit / Temperature / Ping
sprintf(buf, "88.8 A/88.8 *C/9999", 12.34);
tft.setCursor(115, y + 46);
tft.print(buf);
//C3 Status
sprintf(buf, "External Control", 12.34);
tft.setCursor(115, y + 57);
tft.print(buf);
//C3 Current Limit / Temperature / Ping
sprintf(buf, "88.8 A/88.8 *C/9999", 12.34);
tft.setCursor(115, y + 65);
tft.print(buf);
//E1 Status
sprintf(buf, "External Control", 12.34);
tft.setCursor(115, y + 76);
tft.print(buf);
//E1 Current Limit / Temperature / Ping
sprintf(buf, "88.8 A/88.8 *C/9999", 12.34);
tft.setCursor(115, y + 84);
tft.print(buf);
Serial.println(String(micros() - start));
}*/