/***************************************************
Adafruit graphicstest used as a base.
****************************************************/
#include "SPI.h"
#include "ILI9341_t3.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST);
#define backlight 6
//Test perams
int frequency2 = 259;
float BatterySOC = 8500; // percentage of charge (assumed)
float BatterySOCold = 0;
volatile int32_t executiontime = 0;
byte boxset = 0;
byte drivestate = 1;
byte drivestateold = 0;
byte Redfull = 0;
byte Yellowfull = 0;
byte Greenfull = 0;
int a = 0;
int b = 0;
float xold = 0;
float yold = 0;
float zold = 0;
float aold;
float bold;
float cold;
float dold;
int h = 0;
int i = 0;
void setup() {
pinMode(backlight, OUTPUT);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(3);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.println("Initializing.....");
delay(1000);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(5, 5, 20, 230, ILI9341_WHITE); //blank battery bar once
mainScreen();
analogWrite(backlight, 192); //50% backlight and 488Hz pwm
}
void loop() {
refreshtime();
mainScreen();
// delay(50);
// frequency2++;
h++;
i++;
if(h == 30){
if(frequency2 >= 999){
a = 1;
}
if(frequency2 <= 0){
a = 0;
}
if(a == 0){
frequency2 = frequency2 + 1;
}
else
{
frequency2 = frequency2 - 1;
}
h = 0;
}
if(i == 2){
if(BatterySOC >= 9999){
b = 1;
}
if(BatterySOC <= 0){
b = 0;
}
if(b == 0){
BatterySOC = BatterySOC + 1;
}
else
{
BatterySOC = BatterySOC - 1;
}
i = 0;
}
}
unsigned long refreshtime(){
tft.fillRect(35, 110, 75, 15, ILI9341_BLACK);
tft.setCursor(35, 110);
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.print(executiontime);
tft.setCursor(35, 130);
tft.print("Microseconds");
}
unsigned long mainScreen() {
elapsedMicros elapsedtesttime;
if(boxset < 1){ //removes redraw for no reason
tft.drawRect(145, 5, 170, 30, ILI9341_GREEN); //drive status box
tft.drawRect(35, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(105, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(175, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(245, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(5, 5, 20, 230, ILI9341_WHITE); //battery bar outline
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.setCursor(74, 5);
tft.print(".");
tft.setCursor(110, 5);
tft.print("%");
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.setCursor(50, 22);
tft.print("Charge");
tft.setTextColor(ILI9341_BLUE); tft.setTextSize(4);
tft.setCursor(240, 150);
tft.println("MPH");
tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(5);
tft.setCursor(255, 110);
tft.print(".");
boxset = 1;
}
if(BatterySOCold > BatterySOC){
float w = (1 - (BatterySOC / 10000)) * 228;
tft.fillRect(6, 6, 18, w, ILI9341_WHITE); //blank battery bar
Redfull = 0;
Yellowfull = 0;
}
if(BatterySOC <= 3333 && BatterySOC != 0){ //red battery bar only
float x;
float y;
x = BatterySOC / 3333;
y = x * 76; //rectangle size
x = 235 - y; //rectangle starting position
tft.fillRect(6, x, 18, y, ILI9341_RED);
Redfull = 0;
Yellowfull = 0;
}
if(BatterySOC <= 6666 && BatterySOC > 3333){ //red and yellow battery bars only
float x;
float y;
x = BatterySOC - 3333; //remove red region
x = x / 3333;
y = x * 76; //rectangle size
x = 159 - y; //rectangle starting postion
if(Redfull == 0){
tft.fillRect(6, 158, 18, 76, ILI9341_RED);
Redfull = 1;
}
tft.fillRect(6, x, 18, y, ILI9341_YELLOW);
Yellowfull = 0;
}
if(BatterySOC <= 10000 && BatterySOC > 6666){ //red, yellow and green battery bars only
float x;
float y;
x = BatterySOC - 6666; //remove red and yellow region
x = x / 3333;
y = x * 76; //rectangle size
x = 83 - y; //rectangle starting postion
if(Redfull == 0){
tft.fillRect(6, 158, 18, 76, ILI9341_RED);
Redfull = 1;
}
if(Yellowfull == 0){
tft.fillRect(6, 82, 18, 76, ILI9341_YELLOW);
Yellowfull = 1;
}
tft.fillRect(6, x, 18, y, ILI9341_GREEN);
}
// tft.fillRect(50, 5, 59, 14, ILI9341_BLACK); //clears % value
// tft.setTextColor(ILI9341_BLACK); tft.setTextSize(2);
// tft.setCursor(50, 5);
// tft.print((float)BatterySOCold * 100,2); tft.print("%");
//1234 as example
float a = BatterySOC / 1000; //1.234
float b = BatterySOC / 100; //12.34
float c = BatterySOC / 10; //123.4
int aa = a; //1
int bb = b; //12
int cc = c; //123
float aaa = aa; //1
float bbb = bb; //12
float ccc = cc; //123
a = aaa; //1
b = bbb - (aaa * 10); //2
c = ccc - (bbb * 10); //3
float d = BatterySOC - (ccc * 10); //4
tft.setTextSize(2);
if(a != aold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(50, 5);
tft.print(aold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 5);
tft.print(a, 0);
aold = a;
}
if(b != bold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(62, 5);
tft.print(bold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(62, 5);
tft.print(b, 0);
bold = b;
}
////decimal is printed once with rest of boxes
if(c != cold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(86, 5);
tft.print(cold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(86, 5);
tft.print(c, 0);
cold = c;
}
if(d != dold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(98, 5);
tft.print(dold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(98, 5);
tft.print(d, 0);
dold = d;
}
BatterySOCold = BatterySOC;
// tft.setCursor(50, 5);
// tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
// tft.print((float)BatterySOC * 100,2); tft.print("%");
// tft.fillRect(195, 110, 115, 35, ILI9341_BLACK); //clears MPH Value
//239 as example
float x = frequency2 / 100; //2.39
float y = (frequency2 / 10); //23.9
int xx = x; //2
int yy = y; //23
float xxx = xx; //2
float yyy = yy; //23
x = xxx; //2
y = yyy - (x * 10); //3
float z = frequency2 - (yyy * 10); //9
tft.setTextSize(5);
if(x != xold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(195, 110);
tft.print(xold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(195, 110);
tft.print(x, 0);
xold = x;
}
if(y != yold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(225, 110);
tft.print(yold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(225, 110);
tft.print(y, 0);
yold = y;
}
////decimal is printed once with rest of boxes
if(z != zold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(285, 110);
tft.print(zold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(285, 110);
tft.print(z, 0);
zold = z;
}
//tft.print((float)frequency2, 1); // this rounds 25.98765 to 26.0 :(
if(drivestateold != drivestate){ //only change is drivestate changes
tft.setTextColor(ILI9341_RED); tft.setTextSize(2);
tft.setCursor(170, 12);
tft.println("DriveStatus");
drivestateold = 1;
}
executiontime = elapsedtesttime;
}