From 1a369403243a5981d61f496b299f3a12243ea277 Mon Sep 17 00:00:00 2001
From: Stephen Crane <jscrane@gmail.com>
Date: Sat, 3 Nov 2018 13:30:49 +0000
Subject: [PATCH] support TFT_eSPI

---
 hardware.cpp    |   3 --
 hardware.h      |  25 +++++++++---
 r65emu.h        |   2 +-
 tftdisplay.cpp  | 105 ++++++++++++++++++++++++++++++++++++++++++++++++
 tftdisplay.h    |  43 ++++++++++++++++++++
 utftdisplay.cpp |  55 -------------------------
 utftdisplay.h   |  19 ---------
 7 files changed, 168 insertions(+), 84 deletions(-)
 create mode 100644 tftdisplay.cpp
 create mode 100644 tftdisplay.h
 delete mode 100644 utftdisplay.cpp
 delete mode 100644 utftdisplay.h

diff --git a/hardware.cpp b/hardware.cpp
index b0d3407..33942ab 100644
--- a/hardware.cpp
+++ b/hardware.cpp
@@ -10,8 +10,6 @@
 #include <SPIFFS.h>
 #endif
 
-#include <UTFT.h>
-
 #include "memory.h"
 #include "ps2drv.h"
 #include "CPU.h"
@@ -25,7 +23,6 @@ spiram sram(SPIRAM_SIZE);
 Memory memory;
 PS2Driver ps2;
 
-UTFT utft(TFT_MODEL, TFT_RS, TFT_WR, TFT_CS, TFT_RST);
 static CPU *_cpu;
 
 bool hardware_reset() {
diff --git a/hardware.h b/hardware.h
index 7815323..0db7833 100644
--- a/hardware.h
+++ b/hardware.h
@@ -6,6 +6,10 @@
 #define __HARDWARE_H__
 
 // TFT display...
+//#define USE_UTFT
+#define USE_ESPI
+
+#if defined(USE_UTFT)
 #undef TFT_BACKLIGHT
 #define TFT_MODEL	ILI9325C
 #define TFT_RS		32
@@ -13,16 +17,28 @@
 #define TFT_CS		27
 #define TFT_RST		26
 
+#elif defined(USE_ESPI)
+#define USER_SETUP_LOADED
+#define ILI9341_DRIVER
+#define TFT_CS		PIN_D6
+#define TFT_DC		PIN_D8
+#define TFT_RST		-1
+#define SPI_FREQUENCY	40000000
+#define LOAD_GLCD
+#endif
+
 // PS/2 keyboard
-#define KBD_DATA	14
-#define KBD_IRQ		0
+//#define KBD_DATA	14
+//#define KBD_IRQ	0
+#define KBD_DATA	34
+#define KBD_IRQ		35
 
 // SPI-RAM
 #undef SPIRAM_CS
 
 // "tape" storage...
 #undef SD_CS
-#define USE_SPIFFS	1
+#define USE_SPIFFS
 
 // sound
 #define DAC_SOUND	25
@@ -40,9 +56,6 @@ extern class PS2Driver ps2;
 #if defined(__SPIRAM_H__) && defined(SPIRAM_CS)
 extern class spiram sram;
 #endif
-#ifdef UTFT_h
-extern class UTFT utft;
-#endif
 #ifdef __MEMORY_H__
 extern class Memory memory;
 #endif
diff --git a/r65emu.h b/r65emu.h
index 14f501a..15d65c9 100644
--- a/r65emu.h
+++ b/r65emu.h
@@ -7,7 +7,7 @@
 #include "spiram.h"
 #include "prom.h"
 #include "ps2drv.h"
-#include "utftdisplay.h"
+#include "tftdisplay.h"
 #include "keyboard.h"
 #include "sdtape.h"
 #include "timed.h"
diff --git a/tftdisplay.cpp b/tftdisplay.cpp
new file mode 100644
index 0000000..5154d9c
--- /dev/null
+++ b/tftdisplay.cpp
@@ -0,0 +1,105 @@
+#include <stdint.h>
+#include "hardware.h"
+#include "memory.h"
+#include "tftdisplay.h"
+
+#if defined(USE_UTFT)
+#include <UTFT.h>
+#include "TinyFont.h"
+
+static UTFT utft(TFT_MODEL, TFT_RS, TFT_WR, TFT_CS, TFT_RST);
+
+#elif defined(USE_ESPI)
+#include <TFT_eSPI.h>
+
+static TFT_eSPI espi;
+#endif
+
+static inline void setColor(colour_t c) {
+#if defined(USE_UTFT)
+	utft.setColor(c);
+#elif defined(USE_ESPI)
+	espi.setTextColor(c);
+#endif
+}
+
+void TFTDisplay::begin(unsigned bg, unsigned fg, orientation_t orient) {
+	_bg = bg;
+	_fg = fg;
+
+#if defined(USE_UTFT)
+	utft.InitLCD(orient);
+	_dx = utft.getDisplayXSize();
+	_dy = utft.getDisplayYSize();
+
+	utft.setFont((uint8_t *)TinyFont);
+	_cx = utft.getFontXsize();  
+	_cy = utft.getFontYsize();
+#elif defined(USE_ESPI)
+	espi.init();
+	espi.setRotation(orient);
+	_dx = espi.width();
+	_dy = espi.height();
+	_cy = espi.fontHeight();
+#endif
+
+	setColor(fg);
+	_oxs = _dx;
+}
+
+void TFTDisplay::clear() {
+#if defined(USE_UTFT)
+	utft.fillScr(_bg);
+#elif defined(USE_ESPI)
+	espi.fillScreen(_bg);
+#endif
+}
+
+void TFTDisplay::error(char *s) {
+	setColor(_fg);
+	char *lines[5];
+	int l = 0;
+	for (char *p = s, *q = s; *p; p++)
+		if (*p == '\n') {
+			*p++ = 0;
+			lines[l++] = q;
+			q = p;
+		}
+	unsigned y = (_dy - l*_cy)/2;
+	for (int i = 0; i < l; i++) {
+		char *p = lines[i];
+#if defined(USE_UTFT)
+		unsigned x = (_dx - strlen(p)*_cx)/2;
+		utft.print(p, x, y);
+#elif defined(USE_ESPI)
+		unsigned x = (_dx - espi.textWidth(p))/2;
+		espi.drawCentreString(p, x, y, 0);
+#endif
+		y += _cy;
+	}
+}
+
+void TFTDisplay::status(const char *s) {
+	setColor(_fg);
+
+#if defined(USE_UTFT)
+	unsigned y = _dy - _cy, n = strlen(s), xs = _dx - n*_cx;
+	for (unsigned x = _oxs; x < xs; x += _cx)
+		utft.print(" ", x, y);
+	utft.print(s, xs, y);
+	_oxs = xs;
+#elif defined(USE_ESPI)
+	espi.fillRect(_oxs, _dx, _dy - _cy, _dy, _bg);
+	_oxs = espi.textWidth(s);
+	espi.drawRightString(s, _oxs, _dy - _cy, 0);
+#endif
+}
+
+void TFTDisplay::drawPixel(unsigned x, unsigned y, colour_t col) {
+#if defined(USE_UTFT)
+	utft.setColor(col);
+	utft.drawPixel(x, y);
+#elif defined(USE_ESPI)
+	espi.drawPixel(x, y, col);
+#endif
+}
diff --git a/tftdisplay.h b/tftdisplay.h
new file mode 100644
index 0000000..9a5ae06
--- /dev/null
+++ b/tftdisplay.h
@@ -0,0 +1,43 @@
+#ifndef __TFTDISPLAY_H__
+#define __TFTDISPLAY_H__
+
+typedef enum {
+	portrait, landscape
+} orientation_t;
+
+typedef unsigned colour_t;
+
+const colour_t BLACK = 0x0000;
+const colour_t NAVY = 0x000F;
+const colour_t DARKGREEN = 0x03E0;
+const colour_t DARKCYAN = 0x03EF;
+const colour_t MAROON = 0x7800;
+const colour_t PURPLE = 0x780F;
+const colour_t OLIVE = 0x7BE0;
+const colour_t LIGHTGREY = 0xC618;
+const colour_t DARKGREY = 0x7BEF;
+const colour_t BLUE = 0x001F;
+const colour_t GREEN = 0x07E0;
+const colour_t CYAN = 0x07FF;
+const colour_t RED = 0xF800;
+const colour_t MAGENTA = 0xF81F;
+const colour_t YELLOW = 0xFFE0;
+const colour_t WHITE = 0xFFFF;
+const colour_t ORANGE = 0xFDA0;
+const colour_t GREENYELLOW = 0xB7E0;
+const colour_t PINK = 0xFC9F;
+
+class TFTDisplay {
+public:
+	void begin(colour_t bg, colour_t fg, orientation_t o = landscape);
+	void clear();
+	void error(char *);
+	void status(const char *);
+
+	void drawPixel(unsigned x, unsigned y, colour_t col);
+
+protected:
+	unsigned _bg, _fg, _cx, _cy, _dx, _dy, _oxs;
+};
+
+#endif
diff --git a/utftdisplay.cpp b/utftdisplay.cpp
deleted file mode 100644
index 4d5f2ec..0000000
--- a/utftdisplay.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-#include <UTFT.h>
-
-#include "memory.h"
-#include "utftdisplay.h"
-#include "TinyFont.h"
-
-extern UTFT utft;
-
-void UTFTDisplay::begin(unsigned bg, unsigned fg, orientation_t orient) {
-	_bg = bg;
-	_fg = fg;
-	utft.InitLCD(orient);
-	_dx = utft.getDisplayXSize();
-	_dy = utft.getDisplayYSize();
-
-	utft.setFont((uint8_t *)TinyFont);
-	utft.setColor(fg);
-	_cx = utft.getFontXsize();  
-	_cy = utft.getFontYsize();
-	_oxs = _dx;
-}
-
-void UTFTDisplay::clear() {
-	utft.fillScr(_bg);
-}
-
-void UTFTDisplay::error(char *s)
-{
-	utft.setColor(_fg);
-	char *lines[5];
-	int l = 0;
-	for (char *p = s, *q = s; *p; p++)
-		if (*p == '\n') {
-			*p++ = 0;
-			lines[l++] = q;
-			q = p;
-		}
-	unsigned y = (_dy - l*_cy)/2;
-	for (int i = 0; i < l; i++) {
-		char *p = lines[i];
-		unsigned x = (_dx - strlen(p)*_cx)/2;
-		utft.print(p, x, y);
-		y += _cy;
-	}
-}
-
-void UTFTDisplay::status(const char *s)
-{
-	utft.setColor(_fg);
-	unsigned y = _dy - _cy, n = strlen(s), xs = _dx - n*_cx;
-	for (unsigned x = _oxs; x < xs; x += _cx)
-		utft.print(" ", x, y);
-	utft.print(s, xs, y);
-	_oxs = xs;
-}
diff --git a/utftdisplay.h b/utftdisplay.h
deleted file mode 100644
index 178659a..0000000
--- a/utftdisplay.h
+++ /dev/null
@@ -1,19 +0,0 @@
-#ifndef __UTFT_DISPLAY_H__
-#define __UTFT_DISPLAY_H__
-
-typedef enum {
-	portrait, landscape
-} orientation_t;
-
-class UTFTDisplay {
-public:
-	void begin(unsigned bg, unsigned fg, orientation_t o = landscape);
-	void clear();
-	void error(char *);
-	void status(const char *);
-
-protected:
-	unsigned _bg, _fg, _cx, _cy, _dx, _dy, _oxs;
-};
-
-#endif