Source file: /~heha/Mikrocontroller/Displays/utft/Kalender.zip/ILI9341/UTFT.h

#pragma once
#include "utft-config.h"

#include <avr/pgmspace.h>
class __FlashString;
#define FS(s) (reinterpret_cast<const __FlashString*>(s))
#define F(s) FS(PSTR(s))
#include <avr/io.h>
#include <stdio.h>	// FILE
#include <stdarg.h>	// va_list

#define PORTRAIT 0
#define LANDSCAPE 5

//*********************************
// COLORS
//*********************************
#define RGB(r,g,b) ((r)<<8&0xF800|(g)<<3&0x7E0|(b)>>3&0x1F)
// Some colors
#define COLOR_BLACK	RGB(0,0,0)
#define COLOR_WHITE	RGB(255,255,255)
#define COLOR_RED	RGB(255,0,0)
#define COLOR_GREEN	RGB(0,255,0)
#define COLOR_BLUE	RGB(0,0,255)
#define COLOR_SILVER	RGB(192,192,192)
#define COLOR_GRAY	RGB(128,128,128)
#define COLOR_MAROON	RGB(128,0,0)
#define COLOR_YELLOW	RGB(255,255,0)
#define COLOR_OLIVE	RGB(128,128,0)
#define COLOR_LIME	RGB(50,205,50)
#define COLOR_AQUA	RGB(0,255,255)
#define COLOR_TEAL	RGB(0,130,127)
#define COLOR_NAVY	RGB(0,0,128)
#define COLOR_FUCHSIA	RGB(255,0,128)
#define COLOR_PURPLE	RGB(128,0,128)

#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;

#ifdef DEBUG
extern int deb1,deb2;
#endif

#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
		// ==-16 for proportional fonts with character width deviation > 15
		// (and therefore using bytes instead of nibbles for the width vector)
 char cy;	// height of character
 wchar_t first;	// first character code
 wchar_t count;	// number of character codes
};

struct POINT {int x,y;};

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.
// All coordinates are in X/Y order so Y can be accessed by "X" routines
// using the object ("this") pointer offset[ed] by 2 bytes (= sizeof int)
 int X,Y;	// start point
 int XE,YE;	// end point
 int XO,YO;	// "Origin" = zero coordinate, used for absolute gotoXY() and targetXY()
 int L,T,R,B;	// Effective window coordinates for current bitblt operation,
		// in display coordinates, INCLUDING right/bottom, possibly clipped
		// ALWAYS 0 <= L <= R < DISP_X_SIZE and 0 <= T <= B < DISP_Y_SIZE
 int W,H;	// width and height, ALWAYS POSITIVE (>0)
 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
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!
						// BUGBUG: Funktioniert auf ILI9341 nicht richtig: Produziert Zebrastreifen.
 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, int w=1);
 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 RGB(r,g,b);}
 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 d=6);
 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(wchar_t c);			// advances X or Y, depending on orientation
 void print(const char*s);
 void print(const __FlashString*s);
 void print(const char*s,int x,int y)	{gotoXY(x,y); print(s);}
 void print(const __FlashString*s,int x,int y)	{gotoXY(x,y); print(s);}
 void setFont(const byte*font);
 int getFontHeight() const	{return cfont.cy;}
 int getTextExtent(wchar_t c);
 int getTextExtent(const char*s);
 int getTextExtent(const __FlashString*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);
 static wchar_t take_wchar(const char*&s);	// converts UTF-8 stream to (here) 16-bit character code
 static wchar_t take_wchar(const __FlashString*&s);

/*
	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 __FlashString*fmt,va_list args);
 void printf(const __FlashString*fmt,...);
#endif
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)
 byte align;	// TextAlign (not for printf processing!!)
		// bit 3~0 = horizontal align, 0=left, 1=right, 3=center
		// bit 7~4 = vertical align, 0=top, 1=bottom
 void bitblt(const byte*data,int bitsperline,int startbit=0);
 void bitblt(const word*data,int wordsperline);
 void bitblt(FILE*f, int wordsperline);
 void scroll(int v);
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();
 word fc,bc;	// foreground color, background color
 _current_font cfont;

 inline static void writeC(byte b) {
  DISP_RS(0);
#ifdef DISP_CMD16
  writeW(b);
#else
  writeB(b);
#endif
  DISP_RS(1);
 }
 static void writeB(byte b);
 static void writeW(word w);
 static void writeCW(byte b,word w);		// only some controllers
 static void writeCWW(byte b,word x,word y);	// only some controllers
 static void writeCBList(const byte /*_flash*/*z);	// only some controllers
 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
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)
 void realign(int);	// for print: move origin by text width/height
#ifdef DISP_PRINTF
 static int sendByte(char,FILE*);	// for printf
#endif
};
Detected encoding: UTF-80