#include "Arduino.h" #include "SPI.h" class TFT { public: TFT(int CS, int DC, int RST); // SPI based TFT(int CS, int DC, int MOSI, int SCLK, int RST); // Software based bool isSPI; int CS; int DC; int RST; int MOSI; int SCLK; void begin(); void width(); void height(); void fill(int r, int g, int b); void noFill(); void stroke(int r, int g, int b); void noStroke(); void setTextSize(int i); void text(char* msg, int xPos, int yPos); void point(int x, int y); void background(int r, int g, int b); void rect(int xStart, int yStart, int width, int height); void line(int xStart, int yStart, int xEnd, int yEnd); void circle(int x, int y, int r); }; TFT::TFT(int CS, int DC, int RST) { this->CS = CS; this->DC = DC; this->RST = RST; this->isSPI = true; this->MOSI = 11; this->SCLK = 13; pinMode(CS, OUTPUT); pinMode(DC, OUTPUT); pinMode(RST, OUTPUT); digitalWrite(RST, HIGH); }; TFT::TFT(int CS, int DC, int MOSI, int SCLK, int RST) { this->CS = CS; this->DC = DC; this->RST = RST; this->MOSI = MOSI; this->SCLK = SCLK; this->isSPI = false; pinMode(CS, OUTPUT); pinMode(DC, OUTPUT); pinMode(MOSI, OUTPUT); pinMode(SCLK, OUTPUT); pinMode(RST, OUTPUT); digitalWrite(RST, HIGH); }; void TFT::text(char* msg, int xPos, int yPos) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(xPos); SPI.transfer(yPos); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, xPos); shiftOut(this->MOSI, this->SCLK, LSBFIRST, yPos); } int i = 0; while (msg[i] != 0) { if (this->isSPI) { SPI.transfer(msg[i]); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, msg[i]); } msg[i] = 0; i++; } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(11); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 11); } } void TFT::setTextSize(int i) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(i); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, i); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(10); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 10); } } void TFT::begin() { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); __tick(1); digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(0); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 0); } } int TFT::width() { return 160; } int TFT::height() { return 128; } void TFT::background(int r, int g, int b) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(r); SPI.transfer(g); SPI.transfer(b); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, r); shiftOut(this->MOSI, this->SCLK, LSBFIRST, g); shiftOut(this->MOSI, this->SCLK, LSBFIRST, b); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(1); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 1); } } void TFT::fill(int r, int g, int b) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(r); SPI.transfer(g); SPI.transfer(b); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, r); shiftOut(this->MOSI, this->SCLK, LSBFIRST, g); shiftOut(this->MOSI, this->SCLK, LSBFIRST, b); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(2); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 2); } } void TFT::noFill() { digitalWrite(this->DC, LOW); __tick(1); digitalWrite(this->DC, HIGH); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(3); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 3); } } void TFT::stroke(int r, int g, int b) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(r); SPI.transfer(g); SPI.transfer(b); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, r); shiftOut(this->MOSI, this->SCLK, LSBFIRST, g); shiftOut(this->MOSI, this->SCLK, LSBFIRST, b); } digitalWrite(DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(4); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 4); } } void TFT::noStroke() { digitalWrite(this->DC, LOW); __tick(1); __tick(1); digitalWrite(this->DC, HIGH); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(5); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 5); } } void TFT::point(int x, int y) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(x); SPI.transfer(y); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, x); shiftOut(this->MOSI, this->SCLK, LSBFIRST, y); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(7); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 7); } } void TFT::rect(int xStart, int yStart, int width, int height) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(xStart); SPI.transfer(yStart); SPI.transfer(width); SPI.transfer(height); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, xStart); shiftOut(this->MOSI, this->SCLK, LSBFIRST, yStart); shiftOut(this->MOSI, this->SCLK, LSBFIRST, width); shiftOut(this->MOSI, this->SCLK, LSBFIRST, height); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(6); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 6); } } void TFT::line(int xStart, int yStart, int xEnd, int yEnd) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(xStart); SPI.transfer(yStart); SPI.transfer(xEnd); SPI.transfer(yEnd); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, xStart); shiftOut(this->MOSI, this->SCLK, LSBFIRST, yStart); shiftOut(this->MOSI, this->SCLK, LSBFIRST, xEnd); shiftOut(this->MOSI, this->SCLK, LSBFIRST, yEnd); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(8); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 8); } } void TFT::circle(int x, int y, int r) { digitalWrite(this->DC, LOW); digitalWrite(this->CS, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(x); SPI.transfer(y); SPI.transfer(r); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, x); shiftOut(this->MOSI, this->SCLK, LSBFIRST, y); shiftOut(this->MOSI, this->SCLK, LSBFIRST, r); } digitalWrite(this->DC, HIGH); if (this->isSPI) { __tick(1); __tick(1); SPI.transfer(9); } else { shiftOut(this->MOSI, this->SCLK, LSBFIRST, 9); } }