Skip to content

Instantly share code, notes, and snippets.

@DeviNoles
Last active October 31, 2022 21:12
Show Gist options
  • Save DeviNoles/eb831b9626869b2ae0a7ffa73f5a303e to your computer and use it in GitHub Desktop.
Save DeviNoles/eb831b9626869b2ae0a7ffa73f5a303e to your computer and use it in GitHub Desktop.
// Paint example specifically for the TFTLCD breakout board.
// If using the Arduino shield, use the tftpaint_shield.pde sketch instead!
// DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO
#include <string.h>
#include <stdio.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <ArduinoJson.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
// software serial #2: RX = digital pin 8, TX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
const unsigned int MAX_INPUT = 50;
String prevGear;
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// D0 connects to digital pin 22
// D1 connects to digital pin 23
// D2 connects to digital pin 24
// D3 connects to digital pin 25
// D4 connects to digital pin 26
// D5 connects to digital pin 27
// D6 connects to digital pin 28
// D7 connects to digital pin 29
// For the Arduino Due, use digital pins 33 through 40
// (on the 2-row header at the end of the board).
// D0 connects to digital pin 33
// D1 connects to digital pin 34
// D2 connects to digital pin 35
// D3 connects to digital pin 36
// D4 connects to digital pin 37
// D5 connects to digital pin 38
// D6 connects to digital pin 39
// D7 connects to digital pin 40
//HEADERS
#define TITLE_GEAR_X tft.width()/2.88
#define TITLE_GEAR_Y tft.height()/4
#define TITLE_SPEED_Y tft.height()/4
#define TITLE_LAP_Y 2*tft.height()/4
//VALUES
#define GEAR_X (tft.width()/2.88) + 16
#define GEAR_Y tft.height()/2
#define SPEED_Y ((tft.height()/2 + tft.height()/3)/2)-6
#define LAP_Y TITLE_LAP_Y + 30
#define RPM_X (tft.width()/3)+15
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define LCD_RESET A4
//SCREENS
#define START 0
#define RACE 1
//DEBUG MODE 0 IS OFF 1 IS ON
#define DEV_MODE 1
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int oldcolor, currentcolor;
void testJSONHolder(){
StaticJsonDocument<200> doc;
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(200);
// Add values in the document
//
doc["id"] = 1;
doc["name"] = "main";
// Add an array.
//
JsonArray data = doc.createNestedArray("headers");
data.add("Gear");
data.add("Speed");
data.add("RPM");
// Generate the minified JSON and send it to the Serial port.
//
serializeJson(doc, Serial);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
Serial.println();
// Generate the prettified JSON and send it to the Serial port.
//
serializeJsonPretty(doc, Serial);
}
void setCurrentScreen(int sc){
if(sc==START){
tft.setTextSize(5);
Serial.print("START SCREEN ");
Serial.println(sc);
tft.fillScreen(BLACK);
tft.fillRect(0, 0, tft.width()/2, tft.height(), WHITE);
// tft.fillRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
tft.setCursor((tft.width()/4)-50, tft.height()/2);
tft.setTextColor(BLACK);
tft.print("MODE");
tft.setCursor(tft.width()-130, tft.height()/2);
tft.setTextColor(WHITE);
tft.print("RACE");
} //END STARTUP SCREEN
else if(sc==RACE){
tft.setCursor(0, 0);
//SETUP SHIT
tft.fillScreen(BLACK);
if(DEV_MODE==1){
tft.setTextSize(2);
tft.setCursor(TITLE_GEAR_X, TITLE_GEAR_Y);
tft.print("GEAR");
tft.setCursor(0, TITLE_SPEED_Y);
tft.print("SPEED");
tft.setCursor(0, TITLE_LAP_Y);
tft.print("LAP");
} //end dev mode if
//SET TOP ROW
tft.drawLine(tft.width()/3, 0, tft.width()/3, tft.height()/4, WHITE);
tft.drawLine(2*(tft.width()/3), 0, 2*(tft.width()/3),tft.height()/4, WHITE);
tft.drawLine(0, tft.height()/4, tft.width(), tft.height()/4, WHITE);
//SET SECOND ROW
tft.drawLine(tft.width()/5, tft.height()/4, tft.width()/5,tft.height(), WHITE);
tft.drawLine(tft.width()/2.88, tft.height()/4, tft.width()/2.88,3*(tft.height()/4), WHITE);
tft.drawLine(tft.width()-tft.width()/5, tft.height()/4, tft.width()-tft.width()/5,tft.height(), WHITE);
tft.drawLine(tft.width()-tft.width()/2.88, tft.height()/4, tft.width()-tft.width()/2.88, 3*(tft.height()/4), WHITE);
tft.drawLine(0, 2*( tft.height()/4), tft.width()/2.88, 2*( tft.height()/4), WHITE);
tft.drawLine(tft.width()-tft.width()/2.88, 2*( tft.height()/4), tft.width(), 2*( tft.height()/4), WHITE);
//SET THIRD ROW
tft.drawLine(0, 3*( tft.height()/4), tft.width(), 3*( tft.height()/4), WHITE);
//SET FOURTH ROW
} //end race else if
}
void start_up_procedure(){
// Start each software serial port
tft.reset();
Serial.setTimeout(10);
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
return;
}
tft.begin(identifier);
tft.setRotation(1);
}
// Example 2 - Receive with an end-marker
const byte numChars = 10;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup(void) {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
start_up_procedure();
setCurrentScreen(START);
delay(3000);
setCurrentScreen(RACE);
tft.setTextColor(WHITE);
writeBufferObject(40,LAP_Y, "1",3);
tft.setTextColor(GREEN);
writeBufferObject(0,17, "02:11:521",2);
writeBufferObject(40,(4*(tft.height()/5)), "0",3);
writeBufferObject(5*tft.width()/6-45, 15, "-0.14",3);
tft.setTextColor(WHITE);
writeBufferObject(5*tft.width()/6+10, tft.height()/4+17, "+5",3);
// tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
// tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
// pinMode(13, OUTPUT);
}
void loop() {
// Serial.print("HERE");
parseBuffer();
}
void parseBuffer(){
recvWithEndMarker();
showNewData();
}
void getCurrentScreen(){
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
// Serial.println(receivedChars);
parseInputData();
newData = false;
}
}
void resetData(int xpos, int ypos, int wi, int he, int co){
tft.fillRect(xpos, ypos, wi, he, co);
}
void writeBufferObject(int xpos, int ypos,char *valBufObj, int tSize){
if(valBufObj[0]=='<' || valBufObj[1]=='<' || valBufObj[2]=='<' || valBufObj[3]=='<'){
return;
}
Serial.println(valBufObj);
//tft.println(val);
tft.setTextSize(tSize);
tft.setCursor(xpos, ypos);
tft.print(valBufObj);
for (auto& a : receivedChars) a = 0;
// tft.drawRect(GEAR_X, GEAR_Y, 25, 30, "ST7735_BLUE");
}
void parseInputData(){
char *cutRChar = receivedChars + 1;
char *val1 = strtok(cutRChar,",");
char *val2 = strtok(NULL, ">");
Serial.println(val1);
Serial.println(val2);
if(strcmp(val1, "G") == 0){
Serial.println(val2);
resetData(GEAR_X, GEAR_Y, 50, 50, BLACK);
tft.setTextColor(WHITE);
writeBufferObject(GEAR_X,GEAR_Y, val2,5);
}
if(strcmp(val1, "R") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData((tft.width()/3)+2, 15,(tft.width()/3)-2, (tft.height()/4)-15, BLACK);
tft.setTextColor(RED);
writeBufferObject(RPM_X,20, pVal,3);
}
if(strcmp(val1, "S") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(0, SPEED_Y, 35, 25, BLACK);
tft.setTextColor(WHITE);
writeBufferObject(0,SPEED_Y, pVal,2);
}
if(strcmp(val1, "KC") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(0,(4*(tft.height()/5)), 35, 25, BLACK);
tft.setTextColor(GREEN);
writeBufferObject(0,(4*(tft.height()/5)), pVal,2);
}
if(strcmp(val1, "FL") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(tft.width()/5+11, tft.height()/4+15, 35, 25, BLACK);
tft.setTextColor(RED);
writeBufferObject(tft.width()/5+11, tft.height()/4+15, pVal,2);
}
if(strcmp(val1, "FR") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(4*tft.width()/6+8, tft.height()/4+15, 35, 25, BLACK);
tft.setTextColor(RED);
writeBufferObject(4*tft.width()/6+8, tft.height()/4+15, pVal,2);
}
if(strcmp(val1, "RL") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(tft.width()/5+8, LAP_Y, 35, 25, BLACK);
tft.setTextColor(RED);
writeBufferObject(tft.width()/5+8,LAP_Y, pVal,2);
}
if(strcmp(val1, "RR") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(4*tft.width()/6, LAP_Y, 35, 25, BLACK);
tft.setTextColor(RED);
writeBufferObject(4*tft.width()/6,LAP_Y, pVal,2);
}
if(strcmp(val1, "BB") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(5*tft.width()/6+5, SPEED_Y, 35, 25, BLACK);
tft.setTextColor(WHITE);
writeBufferObject(5*tft.width()/6+10,SPEED_Y, pVal,2);
}
if(strcmp(val1, "CL") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(40, LAP_Y, 35, 25, BLACK);
tft.setTextColor(WHITE);
writeBufferObject(40,LAP_Y, pVal,2);
}
if(strcmp(val1, "LL") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(0,0, 35, 25, BLACK);
writeBufferObject(0,0, val2,2);
}
if(strcmp(val1, "LD") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(5*tft.width()/6-45, 15, 35, 25, BLACK);
writeBufferObject(5*tft.width()/6-45, 15, val2,3);
}
if(strcmp(val1, "ET") == 0){
char *pVal = strtok(val2,".");
Serial.println(pVal);
resetData(((tft.width())/3)+15,(4*(tft.height()/5))+5, 45, 20, BLACK);
tft.setTextColor(YELLOW);
writeBufferObject(((tft.width())/3)+15,(4*(tft.height()/5))+5, val2,2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment