// Display Imports
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// EEPROM Imports
#include <EEPROM.h>
#define TFT_DC 48
#define TFT_CS 53
#define RPM_IN A0
#define SPEED_IN A1
#define FUEL_IN A2
#define GEAR_IN A3
#define TEMP_IN A4
// Theme Colors
#define THEME_BG 0x0000
#define THEME_PRIMARY 0x15B4
#define THEME_SECONDARY 0xFCA0
#define THEME_FADE 0x7BCF
#define THEME_TEXT 0xFFFF
#define THEME_RPM_REDLINE 0xF800
// 'Fuel', 16x16px
const unsigned char epd_bitmap_Fuel [] PROGMEM = {
0x3f, 0x80, 0x7f, 0xcc, 0x60, 0xc6, 0x60, 0xc6, 0x60, 0xc7, 0x60, 0xc7, 0x7f, 0xc3, 0x7f, 0xc1,
0x7f, 0xf1, 0x7f, 0xd9, 0x7f, 0xc9, 0x7f, 0xc9, 0x7f, 0xcb, 0x7f, 0xce, 0xff, 0xe0, 0xff, 0xe0
};
// 'Temp', 16x16px
const unsigned char epd_bitmap_Temp [] PROGMEM = {
0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0xe0, 0x01, 0xe0, 0x01, 0xe0, 0x01, 0xe0, 0x01, 0xe0,
0x01, 0xe0, 0x01, 0x80, 0x03, 0xc0, 0xff, 0xff, 0x5b, 0xda, 0x7f, 0xfc, 0x7f, 0xfc, 0x00, 0x00
};
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
float currentRPM = 0.0;
String currentSpeed = "000";
int currentFuel = 0;
char currentGear = 'P';
String currentODO = "000000";
String currentTrip = "000000";
String currentTemperature = " 0";
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(45);
tft.fillScreen(THEME_BG);
emulateEEPROM();
getCurrent();
}
void loop() {
getCurrent();
mainScreen();
delay(500);
}
void getCurrent(){
currentRPM = getRPM();
currentSpeed = getSpeed();
currentFuel = getFuel();
currentGear = getGear();
currentODO = getODO();
currentTrip = getTrip();
currentTemperature = getTemperature();
}
void mainScreen(){
// Layout
tft.fillRect(60, 180, 320, 2, THEME_PRIMARY); // Vertical Primary Line
tft.fillRect(60, 0, 2, 240, THEME_PRIMARY); // Horizontal Primary Line
tft.drawFastHLine(62, 210, 320, THEME_FADE); // Faded Horizontal Ruler
tft.drawFastVLine(220, 182, 28, THEME_FADE); // Faded Vertical Ruler
// RPM Element
tft.fillRect(92, 167, 16, -136, THEME_FADE); // RPM Vertical Background
tft.fillRect(108, 32, 186, 16, THEME_FADE); // RPM Horizontal Background
int RPMLevelV = 0;
int RPMLevelH = 0;
if(currentRPM <= 3.0){
RPMLevelV = -1 * (currentRPM * 40);
RPMLevelH = 0;
}
else{
RPMLevelV = -120;
RPMLevelH = (currentRPM - 3.0) * 40;
}
tft.fillRect(92, 167, 16, RPMLevelV, THEME_SECONDARY); // RPM Vertical Level
tft.fillRect(92, 32, RPMLevelH, 16, THEME_SECONDARY); // RPM Horizontal Level
tft.setTextColor(THEME_TEXT);
tft.setTextSize(2);
int RPM_V_Seg_Loc_X = 78, RPM_V_Seg_Loc_Y = 156;
for(int i=0; i<=3; i++){
tft.setCursor(RPM_V_Seg_Loc_X, RPM_V_Seg_Loc_Y);
tft.println(i);
tft.drawFastHLine(RPM_V_Seg_Loc_X+14, RPM_V_Seg_Loc_Y+12, 16, THEME_TEXT);
RPM_V_Seg_Loc_Y -= 40;
}
int RPM_H_Seg_Loc_X = 126, RPM_H_Seg_Loc_Y = 16;
for(int i=4; i<=8; i++){
if(i >= 7) tft.setTextColor(THEME_RPM_REDLINE);
tft.setCursor(RPM_H_Seg_Loc_X, RPM_H_Seg_Loc_Y);
tft.drawFastVLine(RPM_H_Seg_Loc_X+6, RPM_H_Seg_Loc_Y+16, 16, THEME_TEXT);
tft.println(i);
RPM_H_Seg_Loc_X += 40;
}
tft.setTextColor(THEME_TEXT);
tft.setTextSize(1);
tft.setCursor(240, 56);
tft.println("x1000 RPM");
// Gear Element
tft.setTextColor(THEME_TEXT);
tft.setTextSize(2);
tft.setCursor(0, 16);
tft.println("GEAR");
tft.setTextColor(THEME_PRIMARY, THEME_BG);
tft.setTextSize(6);
tft.setCursor(8, 36);
tft.println(currentGear);
// Fuel Element
int FUEL_Seg_Loc_Y = 224;
for(int i=1; i<=6; i++){
tft.fillRect(28, FUEL_Seg_Loc_Y, 16, -16, THEME_FADE);
FUEL_Seg_Loc_Y -= 17;
}
FUEL_Seg_Loc_Y = 224;
for(int i=1; i<=currentFuel; i++){
tft.fillRect(28, FUEL_Seg_Loc_Y, 16, -16, THEME_SECONDARY);
FUEL_Seg_Loc_Y -= 17;
}
tft.drawBitmap(4, 166, epd_bitmap_Fuel, 16, 16, THEME_TEXT);
tft.setTextColor(THEME_TEXT);
tft.setTextSize(2);
tft.setCursor(12, 210);
tft.println("E");
tft.setTextColor(THEME_TEXT);
tft.setTextSize(2);
tft.setCursor(12, 126);
tft.println("F");
// Trip Element
tft.setTextSize(1);
tft.setCursor(72, 192);
tft.println("TRIP");
tft.setTextSize(2);
tft.setCursor(124, 188);
tft.println(currentTrip);
tft.setTextSize(1);
tft.setCursor(200, 192);
tft.println("km");
// Odo Element
tft.setTextSize(1);
tft.setCursor(172, 224);
tft.println("ODO");
tft.setTextSize(2);
tft.setCursor(224, 220);
tft.println(currentODO);
tft.setTextSize(1);
tft.setCursor(300, 224);
tft.println("km");
// Temperature Element
tft.drawBitmap(230, 188, epd_bitmap_Temp, 16, 16, THEME_TEXT);
tft.setTextColor(THEME_TEXT, THEME_BG);
tft.setTextSize(2);
tft.setCursor(254, 188);
tft.println(currentTemperature);
tft.cp437(true);
tft.setTextSize(1);
tft.setCursor(294, 192);
tft.write(0xF8);
tft.print(" C");
// Speed Element
tft.setTextColor(THEME_TEXT, THEME_BG);
tft.setTextSize(8);
tft.setCursor(160, 100);
tft.println(currentSpeed);
tft.setTextSize(1);
tft.setCursor(270, 162);
tft.println("kmph");
}
// Functions
String getODO(){
String odoStr = "";
for(int i=0; i<=5; i++){
odoStr += (char)EEPROM.read(i);
}
return odoStr;
}
String getTrip(){
String tripStr = "";
for(int i=6; i<=11; i++){
tripStr += (char)EEPROM.read(i);
}
return tripStr;
}
String getTemperature(){
const float BETA = 3950;
int analogValue = analogRead(TEMP_IN);
int celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
String temperature = "";
if(celsius < 10) temperature += " ";
else if(celsius < 100) temperature += " ";
temperature += (String)celsius;
return temperature;
}
int getFuel(){
int fuelLevel = analogRead(FUEL_IN)/170;
return fuelLevel;
}
char getGear(){
char gear;
int gearVal = analogRead(GEAR_IN)/256;
switch(gearVal){
case 0: gear = 'P'; break;
case 1: gear = 'R'; break;
case 2: gear = 'N'; break;
case 3: gear = 'D'; break;
}
return gear;
}
String getSpeed(){
int speedVal = analogRead(SPEED_IN)/4;
String speed = "";
if(speedVal < 10) speed += " ";
else if(speedVal < 100) speed+= " ";
speed += (String) speedVal;
return speed;
}
float getRPM(){
float rpm = (float)analogRead(RPM_IN)/128.0;
return rpm;
}
void emulateEEPROM(){
// Sample ODO
EEPROM.write(0, '1');
EEPROM.write(1, '2');
EEPROM.write(2, '0');
EEPROM.write(3, '6');
EEPROM.write(4, '1');
EEPROM.write(5, '3');
// Sample Trip
EEPROM.write(6, '0');
EEPROM.write(7, '0');
EEPROM.write(8, '0');
EEPROM.write(9, '1');
EEPROM.write(10, '2');
EEPROM.write(11, '8');
}