re-written, re-formatted. always GFXfonts. default solid print

This commit is contained in:
prenticedavid
2019-05-14 16:49:14 +01:00
parent 08a1a44fe1
commit 027269fbfa

View File

@@ -1,10 +1,16 @@
/* /*
* utftglue.h * utftglue.h @@@@@@@@@@@@@@@@@@@@@@@@ LIBRARY @@@@@@@@@@@@@@@@@@@@@@@@@
* *
* Created: 02/03/2013 14:25:06 * Created: 12/05/2019 14:25:06
* Author: David Prentice * Author: David Prentice
*
* Might just as well ALWAYS use FreeFonts.
* knows FreeSmallFont, FreeBigFont and FreeSevenSegNumFont
* any other Fonts just need to #include "FreeOtherXXX.h"
* and #define OtherXXX &FreeOtherXXX
*/ */
#warning @@@@@@@@@@@@@@@@@@@@@@@@@ <UTFTGLUE.h> @@@@@@@@@@@@@@@@@@@@@@@@@@
#ifndef UTFTGLUE_H_ #ifndef UTFTGLUE_H_
#define UTFTGLUE_H_ #define UTFTGLUE_H_
@@ -19,37 +25,21 @@
#include <MCUFRIEND_kbv.h> #include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
//#if !defined(AVR) && !defined(ESP32)
#if defined(__arm) #if defined(__arm)
#include <avr/dtostrf.h> #include <avr/dtostrf.h>
#endif #endif
#undef _GFXFONT_H_ //comment this line to enable FreeFonts
#if defined(_GFXFONT_H_)
#include <FreeDefaultFonts.h> #include <FreeDefaultFonts.h>
#define SmallFont &FreeSmallFont #define SmallFont &FreeSmallFont
#define BigFont &FreeBigFont #define BigFont &FreeBigFont
#define SevenSegNumFont &FreeSevenSegNumFont #define SevenSegNumFont &FreeSevenSegNumFont
#else
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
#endif
struct _current_font
{
uint8_t* font;
uint8_t x_size;
uint8_t y_size;
uint8_t offset;
uint8_t numchars;
};
class UTFTGLUE : public MCUFRIEND_kbv class UTFTGLUE : public MCUFRIEND_kbv
{ {
public: public:
// UTFTGLUE() : MCUFRIEND_kbv() {} // UTFTGLUE() : MCUFRIEND_kbv() {}
UTFTGLUE(byte model, int RS, int WR,int CS, int RST, int RD = A0) : MCUFRIEND_kbv(CS, RS, WR, RD, RST) {} UTFTGLUE(byte model, int RS, int WR,int CS, int RST, int RD = A0)
: MCUFRIEND_kbv(CS, RS, WR, RD, RST) {}
void InitLCD(byte orientation=LANDSCAPE) { void InitLCD(byte orientation=LANDSCAPE) {
MCUFRIEND_kbv::reset(); MCUFRIEND_kbv::reset();
uint16_t ID = MCUFRIEND_kbv::readID(); uint16_t ID = MCUFRIEND_kbv::readID();
@@ -57,13 +47,11 @@ class UTFTGLUE : public MCUFRIEND_kbv
// if (ID == 0x8989) ID = 0x1289; // if (ID == 0x8989) ID = 0x1289;
// if (ID == 0xD3D3) ID = 0x9481; //write-only controller // if (ID == 0xD3D3) ID = 0x9481; //write-only controller
// if (ID == 0xD3D3) ID = 0x9486; //write-only controller // if (ID == 0xD3D3) ID = 0x9486; //write-only controller
if (ID == 0xD3D3) ID = 0x9090; //write-only controller ? HX8357-C if (ID == 0xD3D3) ID = 0x9090; //write-only controller HX8357-D
// if (ID == 0xD3D3) ID = 0x1289; //write-only controller SSD1289
// if (ID == 0x9327 && orientation == LANDSCAPE) orientation = 3; // if (ID == 0x9327 && orientation == LANDSCAPE) orientation = 3;
MCUFRIEND_kbv::begin(ID); MCUFRIEND_kbv::begin(ID);
MCUFRIEND_kbv::setRotation(_orient = orientation); MCUFRIEND_kbv::setRotation(_orient = orientation);
_radius = 4; _radius = 4;
_fontsize = 1;
} }
void clrScr() { MCUFRIEND_kbv::fillScreen(0x0000);} void clrScr() { MCUFRIEND_kbv::fillScreen(0x0000);}
void drawPixel(int x, int y) { MCUFRIEND_kbv::drawPixel(x, y, _fcolor);} void drawPixel(int x, int y) { MCUFRIEND_kbv::drawPixel(x, y, _fcolor);}
@@ -95,10 +83,14 @@ class UTFTGLUE : public MCUFRIEND_kbv
} }
void drawCircle(int x, int y, int radius) { MCUFRIEND_kbv::drawCircle(x, y, radius, _fcolor);} void drawCircle(int x, int y, int radius) { MCUFRIEND_kbv::drawCircle(x, y, radius, _fcolor);}
void fillCircle(int x, int y, int radius) { MCUFRIEND_kbv::fillCircle(x, y, radius, _fcolor);} void fillCircle(int x, int y, int radius) { MCUFRIEND_kbv::fillCircle(x, y, radius, _fcolor);}
void setColor(byte r, byte g, byte b) { MCUFRIEND_kbv::setTextColor(_fcolor = setrgb(r, g, b), _bcolor);} void setColor(byte r, byte g, byte b) { setColor(setrgb(r, g, b));}
void setColor(uint16_t c) { MCUFRIEND_kbv::setTextColor(_fcolor = c, _bcolor); } void setColor(uint16_t c) { MCUFRIEND_kbv::setTextColor(_fcolor = c, _bcolor); }
void setBackColor(byte r, byte g, byte b) { MCUFRIEND_kbv::setTextColor(_fcolor, _bcolor = setrgb(r, g, b));} void setBackColor(byte r, byte g, byte b) { setBackColor(setrgb(r, g, b)); }
void setBackColor(uint32_t c) { if (c == 0xFFFFFFFF) _bcolor = _fcolor; MCUFRIEND_kbv::setTextColor(_fcolor, _bcolor = c);} void setBackColor(uint32_t c) {
if (c == 0xFFFFFFFF) _bcolor = _fcolor;
else _bcolor = c;
MCUFRIEND_kbv::setTextColor(_fcolor, _bcolor);
}
void print(const char *st, int x, int y, int deg=0) { void print(const char *st, int x, int y, int deg=0) {
settextcursor((char*)st, x, y); MCUFRIEND_kbv::print(st);} settextcursor((char*)st, x, y); MCUFRIEND_kbv::print(st);}
void print(char *st, int x, int y, int deg=0) { void print(char *st, int x, int y, int deg=0) {
@@ -106,30 +98,29 @@ class UTFTGLUE : public MCUFRIEND_kbv
void print(String st, int x, int y, int deg=0) { void print(String st, int x, int y, int deg=0) {
settextcursor((char*)(st.c_str()), x, y); MCUFRIEND_kbv::print(st);} settextcursor((char*)(st.c_str()), x, y); MCUFRIEND_kbv::print(st);}
void printNumI(long num, int x, int y, int length=0, char filler=' ') { void printNumI(long num, int x, int y, int length=0, char filler=' ') {
// char buf[17]; ltoa(num, buf, 10); printNumF(num, 0, x, y, '.', length, filler);
// settextcursor(buf, x, y); MCUFRIEND_kbv::print(buf);
// settextcursor((char*)"", x, y); MCUFRIEND_kbv::print(num);
char buf[17], len = 0, *p = buf + 17, sign = num < 0;
if (sign) num = -num;
*--p = '\0';
if (num == 0) *--p = '0';
else while (num > 0 && len < 10) { //int32_t -2147483648 i.e. 10 digits + sign
*--p = '0' + num % 10;
num /= 10;
len++;
}
if (sign) *--p = '-';
while (len++ < length) *--p = filler;
settextcursor(p, x, y); //calculates where to start cursor for CENTER, RIGHT etc
MCUFRIEND_kbv::print(p); //actually print the data
} }
void printNumF(double num, byte dec, int x, int y, char divider='.', int length=0, char filler=' ') { void printNumF(double num, byte dec, int x, int y, char divider='.', int length=0, char filler=' ') {
// settextcursor((char*)"", x, y); MCUFRIEND_kbv::print(num, dec); char buf[20];
char buf[20]; dtostrf(num, length, dec, buf); settextcursor(buf, x, y); MCUFRIEND_kbv::print(buf); dtostrf(num, length, dec, buf);
for (int i = 0; buf[i] == ' '; i++) buf[i] = filler;
settextcursor(buf, x, y, length * _dig_wid * MCUFRIEND_kbv::textsize);
MCUFRIEND_kbv::print(buf);
}
void setTextSize(int sz) { MCUFRIEND_kbv::setTextSize(gfxFont == NULL ? sz : 1); } // ####### GFX ########
void setFont(GFXfont* font) {
MCUFRIEND_kbv::setFont(font);
_ascend = 8;
_descend = 1;
_dig_wid = 6;
if (font != NULL) {
int16_t x = 0, y = 0, x1, y1 = 0, x2, y2 = 0;
MCUFRIEND_kbv::setTextSize(1);
charBounds('0', &x, &y, &x1, &y1, &x2, &y2);
_ascend = y2 - y1; //yAdvance
_dig_wid = x; //xAdvance
}
} }
#if !defined(_GFXFONT_H_)
void setFont(uint8_t* font) { MCUFRIEND_kbv::setTextSize(_fontsize = 1);}
#endif
void drawBitmap(int x, int y, int sx, int sy, const uint16_t *data, int scale=1) { void drawBitmap(int x, int y, int sx, int sy, const uint16_t *data, int scale=1) {
uint16_t color; uint16_t color;
MCUFRIEND_kbv::setAddrWindow(x, y, x + sx*scale - 1, y + sy*scale - 1); MCUFRIEND_kbv::setAddrWindow(x, y, x + sx*scale - 1, y + sy*scale - 1);
@@ -151,33 +142,30 @@ class UTFTGLUE : public MCUFRIEND_kbv
int getDisplayYSize() { return MCUFRIEND_kbv::height(); } int getDisplayYSize() { return MCUFRIEND_kbv::height(); }
// void LCD_Write_DATA(char VH,char VL); // void LCD_Write_DATA(char VH,char VL);
// void dispBitmap(File inFile); // void dispBitmap(File inFile);
uint8_t _ascend, _descend, _dig_wid;
protected: protected:
uint16_t _fcolor; uint16_t _fcolor;
uint16_t _bcolor; uint16_t _bcolor;
uint8_t _fontsize; // uint8_t _ascend, _descend, _dig_wid;
uint8_t _radius; uint8_t _radius;
uint8_t _orient; uint8_t _orient;
void settextcursor(char *st, int x, int y) { void settextcursor(char *st, int x, int y, int pad = 0) {
int16_t pos, x1, y1; int16_t pos, x1, y1;
uint16_t len, w, h; uint16_t len, w, h;
#if defined(_GFXFONT_H_) bool is_gfx = (MCUFRIEND_kbv::gfxFont != NULL);
if (MCUFRIEND_kbv::gfxFont != NULL) y += pgm_read_byte(&gfxFont->yAdvance); getTextBounds(st, 0, 0, &x1, &y1, &w, &h); //always
if (x == CENTER || x == RIGHT) { len = x1 + w + 0; // assumes the final right padding = 1.
if (MCUFRIEND_kbv::gfxFont != NULL) { if (pad >= len) pad = pad - len;
getTextBounds(st, 0, 0, &x1, &y1, &w, &h); pos = (MCUFRIEND_kbv::width() - len - pad);
len = x1 - 0 + w; // misses the final right padding.
} else
#else
if (x == CENTER || x == RIGHT) {
#endif
{
len = strlen(st) * 6 * _fontsize;
}
pos = (MCUFRIEND_kbv::width() - len);
if (x == CENTER) x = pos/2; if (x == CENTER) x = pos/2;
else x = pos - 1; else if (x == RIGHT) x = pos - 1;
if (is_gfx) { //only GFX fonts need to paint background
h = _ascend + _descend; //pgm_read_byte(&gfxFont->yAdvance);
if (_fcolor != _bcolor) MCUFRIEND_kbv::fillRect(x, y, len + pad + 1, h, _bcolor);
y += h;
} }
MCUFRIEND_kbv::setCursor(x, y); MCUFRIEND_kbv::setCursor(x + pad, y);
} }
uint16_t setrgb(byte r, byte g, byte b) { return ((r&0xF8) << 8) | ((g&0xFC) << 3) | (b>>3);} uint16_t setrgb(byte r, byte g, byte b) { return ((r&0xF8) << 8) | ((g&0xFC) << 3) | (b>>3);}
}; };