#ifndef UTFT_h
#define UTFT_h
// User configuration
#define disp_ILI9325D
#define DISP_X_SIZE 240
#define DISP_Y_SIZE 320
#define DISP_TRANSFER 8
#define P_RS PORTC
#define B_RS 0x20 // RS
#define P_WR PORTC
#define B_WR 0x10 // WR
#define P_CS PORTC
#define B_CS 0x08 // CS
//#define P_RST PORTC
//#define B_RST 0x04 // RST
#define DISP_PRINTF // include printf functions (consumes
#include <avr/pgmspace.h>
class __FlashStringHelper;
#define F(s) (reinterpret_cast<const __FlashStringHelper*>(PSTR(s)))
#include <avr/io.h>
#include <stdio.h> // FILE
#include <stdarg.h> // va_list
#define UTFT_VERSION 282
#define PORTRAIT 0
#define LANDSCAPE 5
//*********************************
// COLORS
//*********************************
// VGA color palette
#define VGA_BLACK 0x0000
#define VGA_WHITE 0xFFFF
#define VGA_RED 0xF800
#define VGA_GREEN 0x07E0
#define VGA_BLUE 0x001F
#define VGA_SILVER 0xC618
#define VGA_GRAY 0x8410
#define VGA_MAROON 0x8000
#define VGA_YELLOW 0xFFE0
#define VGA_OLIVE 0x8400
#define VGA_LIME 0x07E0
#define VGA_AQUA 0x07FF
#define VGA_TEAL 0x0410
#define VGA_NAVY 0x0010
#define VGA_FUCHSIA 0xF81F
#define VGA_PURPLE 0x8010
#define ENDPIX 0x01 // set last pixel of a line; include end position for rectangles
#define TRANS 0x02 // font/bitblt operations use stencil transparency omitting background color
#define COOKED 0x04 // process '\n' character
#define MOVE 0x10 // given coordinates add to current position
#define pulse_high(reg, bitmask) reg|=bitmask; reg&=~(bitmask);
#define pulse_low(reg, bitmask) reg&=~(bitmask); reg|=bitmask;
typedef unsigned char byte;
typedef unsigned short word;
extern const byte BigFont[] PROGMEM;
extern const byte SmallFont[] PROGMEM;
extern const byte SevenSegNumFont[] PROGMEM;
extern int deb1,deb2;
#define SWAP(x,y) ({typeof(x) __t=x; x=y; y=__t;})
struct _current_font {
const byte*bits;
char cx; // width of character; negative for proporional fonts
char cy; // height of character
byte first; // first character code
byte count; // number of character codes
};
struct POINT {int x,y;};
#define RGB(r,g,b) ((r)<<8&0xF800|(g)<<5&0x7E0|(b)>>3&0x1F)
class UTFT {
public:
//protected:
// avoid intermediate calculations and stack movements using a couple of dependent variables
// All capital-letter coordinates are (Physical) DISPLAY coordinates
// All small-letter coordinates are (logical) user-space coordinates
int X,Y; // start point
int XE,YE; // end point
int XO,YO; // zero coordinate, used for absolute gotoXY() and targetXY()
int L,T,R,B; // window coordinates, in display coordinates, INCLUDING right/bottom, possibly clipped
int W,H; // width and height, ALWAYS POSITIVE
struct{
int L,T,R,B; // clipping area, in display coordinates, INCLUDING right/bottom
}clip; // Note: clipping is ALWAYS in effect!
int XC,YC; // start offset during clipping, 0 = no left/top clipping
// byte ori; // bit 0: Y counts first (for bitblts)
// bit 1: X counts downwards
// bit 2: Y counts downwards
// bit 3: valid after clipping
protected:
bool clipX();
bool clipY() {return ((UTFT*)((int*)this+1))->clipX();}
bool clipOk() const; // check X,Y against clip
void calcRect() {calcRectX();calcRectY();}
void calcRectX(); // from X,XE -> L,R,W
void calcRectY() {((UTFT*)((int*)this+1))->calcRectX();}
public:
void setClip(int l,int t,int r, int b);
void InitLCD(byte orientation=0);
void gotoXY(int x,int y); // sets X/Y, prepares point setting and text output
void targetXY(int x,int y); // sets XE/YE, prepares bitmap output
void targetWH(int w,int h) {flags|=MOVE; targetXY(w,h);}
void gotoXYWH(int x,int y,int w,int h) {gotoXY(x,y); targetWH(w,h); calcRect();}
void clrOrg() {YO=XO=0;}
void setOrg(int xo,int yo);
void clrScr() {fillScr(bc);} // fills clipping area, not the screen!
void clrClip() {setClip(-8192,-8192,8192,8192);} // sets clipping rectangle to entire screen
void drawPixel();
void drawPixel(int x,int y) {gotoXY(x,y); drawPixel();}
void lineTo(int x,int y);
void drawLine(int x,int y,int xe,int ye) {gotoXY(x,y); lineTo(xe,ye);}
void drawRect(int x1, int y1, int x2, int y2);
void drawRoundRect(int x1, int y1, int x2, int y2, int r=3);
void drawCircle(int x, int y, int radius);
void drawPoly(const POINT*, int);
static word _RGB(byte r,byte g,byte b) {return r<<8&0xF800|g<<5&0x7E0|b>>3&0x1F;}
void fillScr(byte r, byte g, byte b) {fillScr(_RGB(r,g,b));}
void fillScr(word color);
void fillRect(int x0,int y0,int x1,int y1) {gotoXYWH(x0,y0,x1-x0+(flags&1),y1-y0+(flags&1)); fillRect();}
void fillRectW(int x,int y,int w,int h) {gotoXYWH(x,y,w,h); fillRect();}
void fillRect(); // clip rectangle and fill
void fillRoundRect(int x1, int y1, int x2, int y2, int r=3);
void fillCircle(int x, int y, int radius);
void fillPoly(const POINT*,int);
void beginPath();
void closePath();
void stroke();
void fill();
void setColor(byte r, byte g, byte b) {setColor(_RGB(r,g,b));}
void setColor(word color) {fc=color;}
word getColor() const {return fc;}
void setBackColor(byte r, byte g, byte b) {setBackColor(_RGB(r,g,b));}
void setBackColor(word color) {bc=color; flags&=~TRANS;}
word getBackColor() const {return bc;}
void swapColors() {SWAP(fc,bc);}
void setTransparent(byte tr=TRANS) {flags=tr;}
byte getTransparent() const {return flags;}
void print(char c); // advances X or Y, depending on orientation
void print(const char*s);
void print(const __FlashStringHelper*s);
void print(const char*s,int x,int y) {gotoXY(x,y); print(s);}
void setFont(const byte*font);
int getFontHeight() const {return cfont.cy;}
int getTextExtent(char c);
int getTextExtent(const char*s);
int getTextExtent(const __FlashStringHelper*s);
void drawBitmap(int x,int y,int w,int h,const word*data,int wordsperline)
{gotoXYWH(x,y,w,h); bitblt(data,wordsperline);}
/*
void drawBitmap(int x,int y,int w,int h,const word*data,int wordsperline,int scale)
{gotoXYWH(x,y,w,h); stretchblt(data,wordsperline,scale);}
*/
static void lcdOff();
static void lcdOn();
static void setContrast(char c);
int getDisplayXSize() const {return orient&1?DISP_Y_SIZE:DISP_X_SIZE;}
int getDisplayYSize() const {return orient&1?DISP_X_SIZE:DISP_Y_SIZE;}
static void setBrightness(byte br);
static void setDisplayPage(byte page);
static void setWritePage(byte page);
/*
The functions and variables below should not normally be used.
They have been left publicly available for use in add-on libraries
that might need access to the lower level functions of UTFT.
Please note that these functions and variables are not documented
and I do not provide support on how to use them.
*/
void bitblt(int x,int y,int w,int h,const byte*data,int bitsperline,int startbit=0)
{gotoXYWH(x,y,w,h); bitblt(data,bitsperline,startbit);}
void drawHLine(int x,int y,int l) {fillRectW(x,y,l,1);}
void drawVLine(int x,int y,int l) {fillRectW(x,y,1,l);}
int whereX() const {return X-XO;}
int whereY() const {return Y-YO;}
#ifdef DISP_PRINTF
void vprintf(const char*fmt,va_list args);
void printf(const char*fmt,...);
void vprintf(const __FlashStringHelper*fmt,va_list args);
void printf(const __FlashStringHelper*fmt,...);
#endif
protected:
void setWindow(byte orient); // bit 0 = count vertical address first
// bit 1 = count horizontal address downwards
// bit 2 = count vertical address downwards
// bit 4 = set start position and prepare for pixel output
// bit 6 = set window
// bit 7 = set counting directions (bits 0..2)
// CS must be LOW!
void setXY(int x,int y,bool swap); // part of setWindow()
void setXY(int x1,int y1,int x2,int y2) {gotoXYWH(x1,y1,x2-x1+1,y2-y1+1); setWindow(0xF0);}
void setXY(int x,int y) {gotoXY(x,y); setWindow(0x30);}
void unsetWindow();
inline static void CS(bool state) {if (state) P_CS|=B_CS; else P_CS&=~B_CS;} // users can free bus manually (good?)
public:
byte orient; // display orientation, font/bitblt orientation and escapement
// bit 0 = display X/Y swap
// bit 1 = display X counting swap (origin = right)
// bit 2 = display Y counting swap (origin = bottom)
// bit 3 = font/bitblt X/Y swap [text can be displayed vertically, bitmaps can be rotated 90°]
// bit 4 = font/bitblt X reflection [bitmaps can be reflected]
// bit 5 = font/bitblt Y reflection [bitmaps can be reflected]
// bit 6 = font escapement X/Y swap [text (typically monospaced) can advance to bottom]
// bit 7 = font escapement direction swap [against bitmap definition; right-to-left text can be output]
#define TEXT_BOTTOM_TOP 0x68 // (ASCII) text readable from right
#define TEXT_TOP_BOTTOM 0x58 // (ASCII) text readable from left
byte flags; // bit 0 = include last coordinate
// bit 1 = transparent font/bitblt operations
// bit 2 = '\r', '\n' processing at print() functions (otherwise single-line output)
// bit 3 = X/Y relative to clipping rect (TODO: true origin
// bit 4 = X/Y are relative to last position (i.e. MOVE)
protected:
word fc,bc; // foreground color, background color
#if DISP_TRANSFER==1
byte display_serial_mode;
#endif
_current_font cfont;
static void LCD_Writ_Bus(word w);
#if DISP_TRANSFER==1
inline static void LCD_Writ_Bus(char VH,char VL) {LCD_Writ_Bus(VH<<8|VL);}
#endif
inline static void LCD_CmdB(byte b) {RS(0); LCD_Writ_Bus(b); RS(1);}
inline static void LCD_DataB(byte b) {
#if DISP_TRANSFER==1
LCD_Writ_Bus(1,b);
#else
LCD_Writ_Bus(b);
#endif
}
inline static void LCD_DataW(word w) {
#if DISP_TRANSFER==1
LCD_Writ_Bus(1,w>>8);
LCD_Writ_Bus(1,(byte)w);
#else
LCD_Writ_Bus(w);
#endif
}
static void LCD_Write_CMD_DATA(byte com1,word dat1);
inline static void RS(bool state) {
#if DISP_TRANSFER!=1
if (state) P_RS|=B_RS; else P_RS&=~B_RS;
#endif
}
#ifdef P_RST
inline static void RST(bool state) {if (state) P_RST|=B_RST; else P_RST&=~B_RST;}
#endif
static void _set_direction_registers();
static void fillW(word w, int rep1, int rep2);
void setPixel(); // sets pixel at X/Y
void setPixel(int x,int y) {gotoXY(x,y); setPixel();} // sets a single pixel in current foreground color
public:
void bitblt(const byte*data,int bitsperline,int startbit=0);
void bitblt(const word*data,int wordsperline);
void bitblt(FILE*f, int wordsperline);
private:
void advance(int,int); // for bitblt: set display's current position after some transparent pixels
void advance(); // for bitblt: relalize escapement for next bitblt (character)
#ifdef DISP_PRINTF
static int sendByte(char,FILE*); // for printf
#endif
#ifdef ENERGIA
volatile uint32_t* portOutputRegister(int value);
#endif
};
#endif
Detected encoding: UTF-8 | 0
|