//////////////////////////////////////////////////////////
// Title:		drm3310.h								//
// Version:		v1.0									//
// Date:		12.08.2004								//
// Last Change:	15.08.2004								//
// Desc:		Nokia 3310 Lcd Ansteuerung				//
// Author:		Thomas Weger							//
// Compiler:	AVR-GCC: WinAVR-20040720				//
//////////////////////////////////////////////////////////

#ifndef Nokia_3310_H
#define Nokia_3310_H

// Lcd 

#if defined (__AVR_ATmega8__)
	// Lcd Port Settings ATmega8 
	#define N3310_SER_PORT		PORTB
	#define N3310_SER_DDR		DDRB
	#define N3310_SCLK			PINB5	// clock
	#define N3310_MOSI			PINB3	// send data
	#define N3310_CS_           PINB2   // SS

	#define N3310_CTRL_PORT		PORTD
	#define N3310_CTRL_DDR		DDRD

	#define N3310_DC			5		// Trasfer DATA(high) or Control(low)
	#define N3310_CS			6		// Chip Enable
	#define N3310_RES_			7		// RESET

#elif defined (__AVR_ATmega128__)
	// Lcd Port Settings atmega128
	#define N3310_SER_PORT		PORTB
	#define N3310_SER_DDR		DDRB
	#define N3310_SCLK			PINB1	// clock
	#define N3310_MOSI			PINB2	// send data
	#define N3310_CS_          	PINB0   // SS


	#define N3310_CTRL_PORT		PORTB
	#define N3310_CTRL_DDR		DDRB

	#define N3310_DC			PINB7	// Trasfer DATA(high) or Control(low)
	#define N3310_CS			PINB4	// Chip Enable
	#define N3310_RES_			PINB6	// RESET
#elif defined (__AVR_ATmega16__)
   // Lcd Port Settings atmega16

   #define N3310_SER_PORT PORTB
   #define N3310_SER_DDR	DDRB
   #define N3310_SCLK		PINB7
   #define N3310_MOSI		PINB5
   #define N3310_CS_		PINB4


   #define N3310_CTRL_PORT PORTD
   #define N3310_CTRL_DDR	DDRD

   #define N3310_DC		PIND2
   #define N3310_CS		PIND3
   #define N3310_RES_		PIND4	
#else
  #error Please define in Nokia_3310.h the hardware pins for your device !
#endif
/*
				*********************************
				*  ----->	 X					*
				* |								*
				* |								*
				* |								*
				* V								*
				*  Y							*
				*								*
				*								*
				*								*
				*								*
				*								*
				*								*
				*********************************
*/
#define N3310_B_X				83
#define N3310_H_Y				47
#define N3310_PIXELX			84
#define N3310_PIXELY			48
#define N3310_CACHE_SIZE		((N3310_PIXELX * N3310_PIXELY) / 8)

// Lcd Commands (normal Functionset)
#define N3310_FUNCTIONSET	0x20
#define N3310_SETXADDR		0x80
#define N3310_SETYADDR		0x40
#define	N3310_DISPLAYBLANK	0x08
#define	N3310_DISPLAYFLUSH	0x09
#define	N3310_DISPLAYNORMAL	0x0C
#define	N3310_DISPLAYINVERT	0x0D

// Lcd Commands (extended Functionset)
#define N3310_FUNCTIONSETEXT	0x21
#define N3310_BIAS				0x13	// Bias 1:48
#define N3310_TEMPCOEF			0x06	// TF = 2
#define N3310_SETVOP			0x80

// Farben
#define	NONE	2
#define	WHITE	0
#define	BLACK	1

// Definition von daten typen
typedef uint8_t 	N3310_Coord_t;          	// Koordinaten

typedef struct {
    N3310_Coord_t X;
    N3310_Coord_t Y;
} N3310_Point_t;

typedef struct {
    N3310_Coord_t X1;
    N3310_Coord_t Y1;
    N3310_Coord_t X2;
    N3310_Coord_t Y2;
} N3310_Rect_t;


N3310_Point_t     N3310_Cursor;          // aktuelle Position für Schriften
N3310_Rect_t      N3310_Clip;            // Clipping Koordinaten, alles was ausserhalb ist wird nicht dargestellt

// Hintergrundbeleuchtung. Einfach ignorieren wenn nicht gebraucht !
void N3310_SetBackLightInit(uint8_t Helligkeit);
void N3310_SetBackLight(uint8_t Helligkeit);

// Init 
void N3310_InitDisplay(void);
void N3310_wrdata(uint8_t myData);
void N3310_wrcmd(uint8_t myData);
void N3310_spiSendByte(uint8_t myByte);
void N3310_spiInit(void);
void N3310_SetCharAddrXY(uint8_t myX, uint8_t myY);		
void N3310_Contrast ( uint8_t contrast );							//0x00 to 0x7F
void N3310_WriteCache(uint8_t x, uint8_t page, uint16_t size);
void N3310_ClearLcd(void);

// Bilder
void	N3310_PutRawBmp16(uint8_t x, uint8_t y, uint8_t *pic, uint8_t w, uint8_t h);
// Grafik
void 	N3310_SetPixel		(uint8_t X, uint8_t Y, uint8_t mode);		// mode 0 = Clear, mode 1 = Draw
void 	N3310_DrawLine		(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t N3310_FgColor);
void 	N3310_DrawFilledRect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color);
void 	N3310_DrawRect		(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color);
void 	N3310_RoundRect		(N3310_Coord_t x1, N3310_Coord_t y1, N3310_Coord_t x2, N3310_Coord_t y2, N3310_Coord_t a, N3310_Coord_t b, uint8_t fgcolor, uint8_t bkcolor);
void 	N3310_Ellipse		(N3310_Coord_t x1, N3310_Coord_t y1, N3310_Coord_t x2, N3310_Coord_t y2, uint16_t fgcolor, uint8_t bkcolor);
void 	N3310_Circle		(N3310_Coord_t x, N3310_Coord_t y, N3310_Coord_t r, uint16_t fgcolor, uint16_t bkcolor); 

// Textverarbeitung
uint8_t N3310_PutChar		(uint8_t c); 
uint8_t	N3310_PutStr		(char *c);
void 	N3310_PutInt		(uint16_t z);
void 	N3310_PutLong		(uint32_t z);


// Setzen der Variablen der Textverarbeitung
void 	N3310_SetFont(uint8_t *pointer);
void 	N3310_SetRot(uint8_t r);
void 	N3310_SetTextFormat(uint8_t Format);		//links=0 zentriert=1 rechts=2
void 	N3310_SetTextColor(uint8_t color);
void 	N3310_SetTextTyp(uint8_t typ);
void 	N3310_SetFNull(uint8_t fn);

#define 	N3310_clipping_off()			{N3310_Clip.X1 = 0; N3310_Clip.Y1 = 0; N3310_Clip.X2 = N3310_B_X; N3310_Clip.Y2 = N3310_H_Y;}  
#define 	N3310_clipping_on(x1,y1,x2,y2)	{N3310_Clip.X1 = x1; N3310_Clip.Y1 = y1; N3310_Clip.X2 = x2; N3310_Clip.Y2 = y2;}

#define 	N3310_SetRect(r,x1,y1,x2,y2) 	{r.X1 = x1; r.Y1 = y1; r.X2 = x2; r.Y2 = y2;}
#define 	N3310_SwapCoord(x, y) 			{N3310_Coord_t t = x; x = y; y = t;}
#define 	N3310_CheckCoord(x, y) 			{if (x > y) {N3310_SwapCoord(x, y)}}

#define 	N3310_goto(x, y) 				{N3310_Cursor.X = x; N3310_Cursor.Y = y;}





// FASTLCD "IMAGE MODE"
#define Bmp_N3310_Tarja_X 84
#define Bmp_N3310_Tarja_Y 48

static unsigned char __attribute__ ((progmem)) const apple[] = {

0x42,0x4D,0xCE,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x28,0x00,
0x00,0x00,0x32,0x00,0x00,0x00,0x32,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
0x00,0x00,0x90,0x01,0x00,0x00,0x13,0x0B,0x00,0x00,0x13,0x0B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0xFF,0xFE,
0x0F,0xFE,0x0F,0xFF,0xC0,0x00,0xFF,0xFC,0x03,0xF8,0x03,0xFF,0xC0,0x00,0xFF,0xF8,
0x00,0x00,0x01,0xFF,0xC0,0x00,0xFF,0xF0,0x00,0x00,0x00,0xFF,0xC0,0x00,0xFF,0xE0,
0x00,0x00,0x00,0x7F,0xC0,0x00,0xFF,0xC0,0x00,0x00,0x00,0x7F,0xC0,0x00,0xFF,0x80,
0x00,0x00,0x00,0x3F,0xC0,0x00,0xFF,0x80,0x00,0x00,0x00,0x3F,0xC0,0x00,0xFF,0x00,
0x00,0x00,0x00,0x1F,0xC0,0x00,0xFF,0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0xFE,0x00,
0x00,0x00,0x00,0x0F,0xC0,0x00,0xFE,0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0xFC,0x00,
0x00,0x00,0x00,0x07,0xC0,0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0xFC,0x00,
0x00,0x00,0x00,0x1F,0xC0,0x00,0xF8,0x00,0x00,0x00,0x00,0x3F,0xC0,0x00,0xF8,0x00,
0x00,0x00,0x00,0x7F,0xC0,0x00,0xF8,0x00,0x00,0x00,0x00,0xFF,0xC0,0x00,0xF8,0x00,
0x00,0x00,0x00,0xFF,0xC0,0x00,0xF8,0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0xF0,0x00,
0x00,0x00,0x01,0xFF,0xC0,0x00,0xF0,0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0xF0,0x00,
0x00,0x00,0x03,0xFF,0xC0,0x00,0xF0,0x00,0x00,0x00,0x03,0xFF,0xC0,0x00,0xF0,0x00,
0x00,0x00,0x03,0xFF,0xC0,0x00,0xF0,0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0xF8,0x00,
0x00,0x00,0x01,0xFF,0xC0,0x00,0xF8,0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0xF8,0x00,
0x00,0x00,0x00,0xFF,0xC0,0x00,0xF8,0x00,0x00,0x00,0x00,0xFF,0xC0,0x00,0xFC,0x00,
0x00,0x00,0x00,0x7F,0xC0,0x00,0xFC,0x00,0x00,0x00,0x00,0x3F,0xC0,0x00,0xFE,0x00,
0x00,0x00,0x00,0x1F,0xC0,0x00,0xFF,0x00,0x00,0x00,0x00,0x1F,0xC0,0x00,0xFF,0x80,
0x00,0x00,0x00,0x1F,0xC0,0x00,0xFF,0xC0,0x00,0x00,0x00,0x7F,0xC0,0x00,0xFF,0xF0,
0x03,0xF0,0x00,0xFF,0xC0,0x00,0xFF,0xFC,0x1F,0xFE,0x07,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0xBF,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0x87,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0x81,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0x80,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0x80,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0xC0,0x7F,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0xC0,0x3F,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0xE0,0x3F,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0xF0,0x3F,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0xF8,0x1F,0xFF,0xC0,0x00,0xFF,0xFF,
0xFF,0xFC,0x1F,0xFF,0xC0,0x00,0xFF,0xFF,0xFF,0xFF,0x1F,0xFF,0xC0,0x00,0x00
};

#endif
