1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-10-08 22:54:38 +00:00
r65emu/display.cpp

180 lines
3.8 KiB
C++
Raw Normal View History

2018-11-03 13:30:49 +00:00
#include <stdint.h>
#include "hardware.h"
#include "memory.h"
2023-07-25 13:10:35 +00:00
#include "display.h"
2018-11-03 13:30:49 +00:00
#if defined(USE_UTFT)
2020-07-08 13:33:31 +00:00
#pragma message "UTFT configured"
2018-11-03 13:30:49 +00:00
#include <UTFT.h>
#include "TinyFont.h"
2019-03-03 14:38:47 +00:00
#if !defined(TFT_SER)
#define TFT_SER 0
#endif
static UTFT utft(TFT_MODEL, TFT_RS, TFT_WR, TFT_CS, TFT_RST, TFT_SER);
2018-11-03 13:30:49 +00:00
#elif defined(USE_ESPI)
2021-02-27 13:32:49 +00:00
#pragma message "Configure TFT_eSPI in Makefile or <TFT_eSPI/User_Setup.h>"
2018-11-03 13:30:49 +00:00
#include <TFT_eSPI.h>
static TFT_eSPI espi;
2020-07-08 13:33:31 +00:00
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
#pragma message "FabGL VGA configured"
#include <fabgl.h>
static fabgl::VGAController vga;
static fabgl::Canvas canvas(&vga);
static const fabgl::RGB888 rgb(colour_t c) {
switch(c) {
case BLACK: return Color::Black;
2023-07-26 12:47:15 +00:00
case RED: return Color::Red;
2023-07-25 13:10:35 +00:00
case GREEN: return Color::Green;
case YELLOW: return Color::Yellow;
case BLUE: return Color::Blue;
case MAGENTA: return Color::Magenta;
case CYAN: return Color::Cyan;
2023-07-26 12:47:15 +00:00
case WHITE: return Color::White;
2023-07-25 13:10:35 +00:00
}
2023-07-26 12:47:15 +00:00
return Color::BrightWhite;
2023-07-25 13:10:35 +00:00
}
2020-07-08 13:33:31 +00:00
#else
#pragma error "Display not configured!"
2018-11-03 13:30:49 +00:00
#endif
static inline void setColor(colour_t c) {
#if defined(USE_UTFT)
utft.setColor(c);
#elif defined(USE_ESPI)
espi.setTextColor(c);
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
canvas.setPenColor(rgb(c));
2018-11-03 13:30:49 +00:00
#endif
}
2023-07-26 14:32:11 +00:00
void Display::begin(colour_t bg, colour_t fg, orientation_t orient, unsigned dispx, unsigned dispy) {
begin(bg, fg, orient);
_xoff = (_dx - dispx) / 2;
_yoff = (_dy - dispy) / 2;
_dx -= _xoff;
_dy -= _yoff;
2023-08-30 13:01:30 +00:00
#if defined(DEBUGGING)
Serial.printf("xoff %d yoff %d dx %d dy %d", _xoff, _yoff, _dx, _dy);
Serial.println();
#endif
2023-07-26 14:32:11 +00:00
}
void Display::begin(colour_t bg, colour_t fg, orientation_t orient) {
2018-11-03 13:30:49 +00:00
_bg = bg;
_fg = fg;
2023-07-26 14:32:11 +00:00
_xoff = _yoff = 0;
2018-11-03 13:30:49 +00:00
#if defined(USE_UTFT)
utft.InitLCD(orient);
_dx = utft.getDisplayXSize();
_dy = utft.getDisplayYSize();
utft.setFont((uint8_t *)TinyFont);
_cx = utft.getFontXsize();
_cy = utft.getFontYsize();
2020-07-08 13:33:31 +00:00
2018-11-03 13:30:49 +00:00
#elif defined(USE_ESPI)
espi.init();
espi.setRotation(orient);
_dx = espi.width();
_dy = espi.height();
_cy = espi.fontHeight();
2018-11-12 22:18:44 +00:00
_cx = 6; // FIXME
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
static bool init;
if (init)
vga.end();
init = true;
vga.begin();
2023-08-30 08:59:31 +00:00
vga.setResolution(VGA_RESOLUTION);
2023-07-25 13:10:35 +00:00
canvas.setBrushColor(rgb(_bg));
canvas.clear();
canvas.setGlyphOptions(GlyphOptions().FillBackground(true));
canvas.selectFont(&fabgl::FONT_5x7);
_cy = canvas.getFontInfo()->height;
_cx = canvas.getFontInfo()->width;
_dx = canvas.getWidth();
_dy = canvas.getHeight();
2023-08-30 13:01:30 +00:00
#if defined(DEBUGGING)
Serial.printf("w %d h %d", _dx, _dy);
Serial.println();
#endif
2018-11-03 13:30:49 +00:00
#endif
setColor(fg);
_oxs = _dx;
}
2023-07-25 13:10:35 +00:00
void Display::clear() {
2018-11-03 13:30:49 +00:00
#if defined(USE_UTFT)
utft.fillScr(_bg);
#elif defined(USE_ESPI)
espi.fillScreen(_bg);
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
canvas.clear();
2018-11-03 13:30:49 +00:00
#endif
}
2023-07-25 13:10:35 +00:00
void Display::status(const char *s) {
2018-11-03 13:30:49 +00:00
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)
2018-11-10 13:34:36 +00:00
espi.fillRect(_dx - _oxs, _dy - _cy, _oxs, _cy, _bg);
2018-11-03 13:30:49 +00:00
_oxs = espi.textWidth(s);
2018-11-10 13:34:36 +00:00
espi.setTextDatum(BR_DATUM);
espi.drawString(s, _dx, _dy);
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
canvas.fillRectangle(_dx - _oxs, _dy - _cy, _dx, _dy);
_oxs = canvas.textExtent(s) + _cx;
canvas.drawText(_dx - _oxs, _dy - _cy, s);
2018-11-03 13:30:49 +00:00
#endif
}
2023-07-25 13:10:35 +00:00
void Display::drawPixel(unsigned x, unsigned y, colour_t col) {
2023-07-26 14:32:11 +00:00
x += _xoff;
y += _yoff;
2018-11-03 13:30:49 +00:00
#if defined(USE_UTFT)
utft.setColor(col);
utft.drawPixel(x, y);
#elif defined(USE_ESPI)
2018-11-10 13:34:36 +00:00
espi.drawPixel(x, y, col);
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
canvas.setPixel(x, y, rgb(col));
2018-11-03 13:30:49 +00:00
#endif
}
2018-11-12 07:46:21 +00:00
2023-07-25 13:10:35 +00:00
void Display::drawString(const char *s, unsigned x, unsigned y) {
2023-07-26 14:32:11 +00:00
x += _xoff;
y += _yoff;
2018-11-12 07:46:21 +00:00
#if defined(USE_UTFT)
utft.print(s, x, y);
#elif defined(USE_ESPI)
2018-11-12 22:18:44 +00:00
espi.setTextDatum(TL_DATUM);
unsigned w = espi.textWidth(s);
espi.fillRect(x, y, w, _cy, _bg);
2018-11-12 07:46:21 +00:00
espi.drawString(s, x, y);
2023-07-25 13:10:35 +00:00
#elif defined(USE_VGA)
canvas.drawText(x, y, s);
2018-11-12 07:46:21 +00:00
#endif
}