Switch to TFT_ILI9341 for waaaaaaaaaay faster text rendering, deprecate old consoletext

This commit is contained in:
ThirstyShark
2019-03-14 21:50:17 -05:00
parent 6340186e15
commit 02c6d58080
5 changed files with 4524 additions and 4239 deletions

View File

@ -1,13 +1,26 @@
#include <TFT_FastPin.h>
#include <TFT_ILI9341.h>
#include <User_Setup.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
//#include "./PDQ_ILI9341_config.h"
//#include "PDQ_GFX.h"
//#include "PDQ_ILI9341.h"
#include "SD.h"
#include <Fonts/FreeSerif12pt7b.h> // include fancy serif font
#include <Fonts/FreeSans12pt7b.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 48
#define TFT_CS 49
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
TFT_ILI9341 tft;
extern "C" char __data_start[]; // start of SRAM data
extern "C" char _end[]; // end of SRAM data (used to check amount of SRAM this program's variables use)
extern "C" char __data_load_end[]; // end of FLASH (used to check amount of Flash this program's code and data uses)
#include <TouchScreen.h>
#include <avr/power.h>
#include <avr/sleep.h>
@ -127,25 +140,35 @@ int linecount = 0;
void setup(void) {
Serial.begin(9600);
//tft.reset();
#if defined(ILI9341_RST_PIN) // reset like Adafruit does
FastPin<ILI9341_RST_PIN>::setOutput();
FastPin<ILI9341_RST_PIN>::hi();
FastPin<ILI9341_RST_PIN>::lo();
delay(1);
FastPin<ILI9341_RST_PIN>::hi();
#endif
tft.begin();
yield();
//tft.reset();
tft.setRotation(1);
boolean didDraw = true;
if(!fastboot) {
if (SD.begin(SD_CS)) {
tft.fillScreen(ILI9341_WHITE);
bmpDraw("gbs.bmp", 0, 0);
delay(4000);
} else {
//if (SD.begin(SD_CS)) {
//tft.fillScreen(ILI9341_WHITE);
//bmpDraw("gbs.bmp", 0, 0);
//delay(4000);
//} else {
didDraw = false;
}
// }
tft.fillScreen(ILI9341_WHITE);
yield();
//yield();
//tft.fillRect(0, 0, 320, 23, ILI9341_BLACK);
}
printFastCText("Booting GreenhouseOS...||");
clearLog();
printFastCText("Booting GreenhouseOS...");
printFastCText("");
printFastCText("");
delay(250);
if(!didDraw) {
printFastCText("[FAILED] SD Card not detected!");
@ -201,7 +224,7 @@ void loop(void) {
String txtin = "Serial: " + Serial.readString();
int x = 0;
while(txtin.length() > 53) {
printFastCText(txtin.substring(0, 53);
printFastCText(txtin.substring(0, 53));
txtin = txtin.substring(53);
}
printFastCText(txtin.substring(0, txtin.length() - 1));
@ -239,9 +262,12 @@ void loop(void) {
tmp += mil % 1000;
tmp += "]";*/
//printConsoleText(tmp);
//tft.scrollTo(320);
if(mil % 60000 < 3000) {
getMoisture();
delay(500);
//tft.scrollTo(480);
//tft.setAddrWindow(320, 0, 320, 240);
}
// change upd to true if screen update needed
@ -261,7 +287,7 @@ void loop(void) {
}
#define BUFFPIXEL 80
/*
void bmpDraw(char *filename, int16_t x, int16_t y) {
File bmpFile;
@ -391,7 +417,7 @@ void bmpDraw(char *filename, int16_t x, int16_t y) {
bmpFile.close();
if(!goodBmp) Serial.println(F("BMP format not recognized."));
}
*/
uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
@ -555,8 +581,10 @@ void printS4(float t, int sm) {
void enablePS(int level) {
if (level > 0) {
for(int x = 0; x <= 19; x++) {
pinMode(x, OUTPUT);
digitalWrite(x, LOW);
if(x != 8) {
pinMode(x, OUTPUT);
digitalWrite(x, LOW);
}
}
for(int x = 32; x <= 53; x++) {
pinMode(x, OUTPUT);
@ -596,86 +624,14 @@ void clearLog() {
outtext = "";
outtext2 = "";
linecount = 0;
}
void printConsoleText(String text) {
if (outtext.equals("") && debug) {
tft.fillScreen(ILI9341_BLACK);
yield();
}
oldtext = outtext;
if (text.length() <= 53) {
outtext += text + "|";
} else {
int l = text.length() - text.length() % 53;
for (int x = 0; x < l; x += 53) {
boolean keep = true;
for (int y = 0; y < 53; y++) {
if (text.substring(x + y, x + y + 1).equals("|")) {
keep = false;
}
}
if (keep) {
outtext += text.substring(x, x + 53) + "|";
} else {
outtext += text.substring(x, x + 53);
}
}
if (text.length() % 53 != 0) {
outtext += text.substring(text.length() - text.length() % 53, text.length()) + "|";
}
}
int count = countSemicolons(outtext);
while (count > 30) {
outtext.remove(0, outtext.indexOf("|") + 1);
count--;
}
if (debug) {
//tft.setCursor(0, 0);
//tft.fillScreen(BLACK);
tft.setCursor(0, (count) * 8);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
for (int x = count - 1; x >= 0; x--) {
String tmp = getString(outtext, x);
tft.setCursor(0, tft.getCursorY() - 8);
if (!tmp.equals(getString(oldtext, x))) {
tft.fillRect(0, tft.getCursorY(), 320, 8, BLACK);
tft.print(tmp);
}
}
}
}
int countSemicolons(String s) {
int newlines = 0;
int l = s.length();
for (int x = 0; x < l; x++) {
if (s.substring(x, x + 1).equals("|")) {
newlines ++;
}
}
return newlines;
}
String getString(String s, int index) {
int count = 0;
int lastsemi = 0;
for (int x = 0; x < s.length(); x++) {
if (s.substring(x, x + 1).equals("|")) {
count ++;
if (count - 1 == index) {
return s.substring(lastsemi, x);
}
lastsemi = x + 1;
}
}
return "";
tft.fillScreen(ILI9341_BLACK);
}
void printFastCText(String text) { // NOTE: text MUST be under 53 characters long! (this is how the code runs faster)
linecount ++;
if (linecount > 30) {
outtext2 = outtext2.substring(30);
outtext2 = outtext2.substring(53);
//scrollAddress(8);
}
if (text.length() < 53) {
outtext2 += text;
@ -688,15 +644,21 @@ void printFastCText(String text) { // NOTE: text MUST be under 53 characters lon
if (debug) {
//tft.setCursor(0, 0);
//tft.fillScreen(BLACK);
int lc2 = linecount;
tft.setCursor(0, lc2 * 8);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(1);
for (int x = count - 1; x >= 0; x--) {
String tmp = outtext2.substring(x * 53, x*53 + 53);
tft.setCursor(0, tft.getCursorY() - 8);
tft.fillRect(0, tft.getCursorY(), 320, 8, BLACK);
tft.print(tmp);
}
//int cursory = linecount * 8;
//for (int x = linecount - 1; x >= 0; x--) {
//tft.setCursor(0, cursory - 8);
//tft.fillRect(0, cursory - 8, 320, 8, BLACK);
//tft.fillScreen(ILI9341_BLACK);
tft.print(outtext2);
//cursory -= 8;
//}
}
}
}
void scrollAddress(uint16_t vsp) {
tft.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
tft.writedata(vsp>>8);
tft.writedata(vsp);
}