////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
OLED graphical LCD routines, for Densitron OLED with SEPS525 controller
CCS compiler

TechDesign Electronics (c) March 2008. 

www.techdesign.be
info@techdesign.be
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


unsigned int8 CONST TABLE7[240]=				{0x00,0x00,0x00,0x00,0x00,	// 20 space	 		ASCII table for NOKIA LCD: 96 rows * 5 bytes= 480 bytes
						0x00,0x00,0x5f,0x00,0x00,	// 21 !
						0x00,0x07,0x00,0x07,0x00,	// 22 "
						0x14,0x7f,0x14,0x7f,0x14,	// 23 #
						0x24,0x2a,0x7f,0x2a,0x12,	// 24 $
						0x23,0x13,0x08,0x64,0x62,	// 25 %
						0x36,0x49,0x55,0x22,0x50,	// 26 &
						0x00,0x05,0x03,0x00,0x00,	// 27 '
						0x00,0x1c,0x22,0x41,0x00,	// 28 (
						0x00,0x41,0x22,0x1c,0x00,	// 29 )
						0x14,0x08,0x3e,0x08,0x14,	// 2a *
						0x08,0x08,0x3e,0x08,0x08,	// 2b +
						0x00,0x50,0x30,0x00,0x00,	// 2c ,
						0x08,0x08,0x08,0x08,0x08,	// 2d -
						0x00,0x60,0x60,0x00,0x00,	// 2e .
						0x20,0x10,0x08,0x04,0x02,	// 2f /
						0x3e,0x51,0x49,0x45,0x3e,	// 30 0
						0x00,0x42,0x7f,0x40,0x00,	// 31 1
						0x42,0x61,0x51,0x49,0x46,	// 32 2
						0x21,0x41,0x45,0x4b,0x31,	// 33 3
						0x18,0x14,0x12,0x7f,0x10,	// 34 4
						0x27,0x45,0x45,0x45,0x39,	// 35 5
						0x3c,0x4a,0x49,0x49,0x30,	// 36 6
						0x01,0x71,0x09,0x05,0x03,	// 37 7
						0x36,0x49,0x49,0x49,0x36,	// 38 8
						0x06,0x49,0x49,0x29,0x1e,	// 39 9
						0x00,0x36,0x36,0x00,0x00,	// 3a :
						0x00,0x56,0x36,0x00,0x00,	// 3b ;
						0x08,0x14,0x22,0x41,0x00,	// 3c <
						0x14,0x14,0x14,0x14,0x14,	// 3d =
						0x00,0x41,0x22,0x14,0x08,	// 3e >
						0x02,0x01,0x51,0x09,0x06,	// 3f ?
						0x32,0x49,0x79,0x41,0x3e,	// 40 @
						0x7e,0x11,0x11,0x11,0x7e,	// 41 A
						0x7f,0x49,0x49,0x49,0x36,	// 42 B
						0x3e,0x41,0x41,0x41,0x22,	// 43 C
						0x7f,0x41,0x41,0x22,0x1c,	// 44 D
						0x7f,0x49,0x49,0x49,0x41,	// 45 E
						0x7f,0x09,0x09,0x09,0x01,	// 46 F
						0x3e,0x41,0x49,0x49,0x7a,	// 47 G
						0x7f,0x08,0x08,0x08,0x7f,	// 48 H
						0x00,0x41,0x7f,0x41,0x00,	// 49 I
						0x20,0x40,0x41,0x3f,0x01,	// 4a J
						0x7f,0x08,0x14,0x22,0x41,	// 4b K
						0x7f,0x40,0x40,0x40,0x40,	// 4c L
						0x7f,0x02,0x0c,0x02,0x7f,	// 4d M
						0x7f,0x04,0x08,0x10,0x7f,	// 4e N
						0x3e,0x41,0x41,0x41,0x3e};	// 4f O


unsigned int8 CONST TABLE8[240]=				{0x7f,0x09,0x09,0x09,0x06,	// 50 P
						0x3e,0x41,0x51,0x21,0x5e,	// 51 Q
						0x7f,0x09,0x19,0x29,0x46,	// 52 R
						0x46,0x49,0x49,0x49,0x31,	// 53 S
						0x01,0x01,0x7f,0x01,0x01,	// 54 T
						0x3f,0x40,0x40,0x40,0x3f,	// 55 U
						0x1f,0x20,0x40,0x20,0x1f,	// 56 V
						0x3f,0x40,0x38,0x40,0x3f,	// 57 W
						0x63,0x14,0x08,0x14,0x63,	// 58 X
						0x07,0x08,0x70,0x08,0x07,	// 59 Y
						0x61,0x51,0x49,0x45,0x43,	// 5a Z
						0x00,0x7f,0x41,0x41,0x00,	// 5b [
						0x02,0x04,0x08,0x10,0x20,	// 5c \
						0x00,0x41,0x41,0x7f,0x00,	// 5d ]
						0x04,0x02,0x01,0x02,0x04,	// 5e ^
						0x40,0x40,0x40,0x40,0x40,	// 5f _
						0x00,0x01,0x02,0x04,0x00,	// 60 `
						0x20,0x54,0x54,0x54,0x78,	// 61 a
						0x7f,0x48,0x44,0x44,0x38,	// 62 b
						0x38,0x44,0x44,0x44,0x20,	// 63 c
						0x38,0x44,0x44,0x48,0x7f,	// 64 d
						0x38,0x54,0x54,0x54,0x18,	// 65 e
						0x08,0x7e,0x09,0x01,0x02,	// 66 f
						0x0c,0x52,0x52,0x52,0x3e,	// 67 g
						0x7f,0x08,0x04,0x04,0x78,	// 68 h
						0x00,0x44,0x7d,0x40,0x00,	// 69 i
						0x20,0x40,0x44,0x3d,0x00,	// 6a j 
						0x7f,0x10,0x28,0x44,0x00,	// 6b k
						0x00,0x41,0x7f,0x40,0x00,	// 6c l
						0x7c,0x04,0x18,0x04,0x78,	// 6d m
						0x7c,0x08,0x04,0x04,0x78,	// 6e n
						0x38,0x44,0x44,0x44,0x38,	// 6f o
						0x7c,0x14,0x14,0x14,0x08,	// 70 p
						0x08,0x14,0x14,0x18,0x7c,	// 71 q
						0x7c,0x08,0x04,0x04,0x08,	// 72 r
						0x48,0x54,0x54,0x54,0x20,	// 73 s
						0x04,0x3f,0x44,0x40,0x20,	// 74 t
						0x3c,0x40,0x40,0x20,0x7c,	// 75 u
						0x1c,0x20,0x40,0x20,0x1c,	// 76 v
						0x3c,0x40,0x30,0x40,0x3c,	// 77 w
						0x44,0x28,0x10,0x28,0x44,	// 78 x
						0x0c,0x50,0x50,0x50,0x3c,	// 79 y
						0x44,0x64,0x54,0x4c,0x44,	// 7a z
						0x00,0x08,0x36,0x41,0x00,	// 7b {
						0x00,0x00,0x7f,0x00,0x00,	// 7c |
						0x00,0x41,0x36,0x08,0x00,	// 7d }
						0x10,0x08,0x08,0x10,0x08,	// 7e ~
						0x78,0x46,0x41,0x46,0x78};	// 7f ¦
				

//


unsigned int8 CONST TABLE9[195]=				{0x1e,0x21,0x61,0x21,0x12,	// 80 Ç		// extended ascii (selected chars)
						0x3c,0x41,0x40,0x21,0x7c,	// 81 ü
						0x38,0x54,0x56,0x55,0x18,	// 82 é
						0x20,0x55,0x55,0x55,0x78,	// 83 â
						0x20,0x55,0x54,0x55,0x78,	// 84 ä
						0x20,0x55,0x56,0x54,0x78,	// 85 à
						0x20,0x54,0x55,0x54,0x78,	// 86 å
						0x1c,0x22,0x62,0x22,0x10,	// 87 ç
						0x38,0x55,0x55,0x55,0x18,	// 88 ê
						0x38,0x55,0x54,0x55,0x18,	// 89 ë
						0x38,0x54,0x55,0x56,0x18,	// 8a è
						0x00,0x45,0x7c,0x41,0x00,	// 8b ï
						0x00,0x45,0x7d,0x41,0x00,	// 8c î
						0x00,0x45,0x7e,0x40,0x00,	// 8d ì
						0x78,0x15,0x14,0x15,0x78,	// 8e Ä
						0x78,0x14,0x15,0x14,0x78,	// 8f Å

						0x7c,0x54,0x56,0x55,0x44,	// 90 É
						0x24,0x54,0x78,0x54,0x48,	// 91 æ
						0x7e,0x09,0x7e,0x49,0x49,	// 92 Æ
						0x38,0x45,0x45,0x45,0x38,	// 93 ô
						0x38,0x45,0x44,0x45,0x38,	// 94 ö
						0x38,0x45,0x46,0x44,0x38,	// 95 ò
						0x3c,0x41,0x41,0x21,0x7c,	// 96 û
						0x3c,0x41,0x42,0x20,0x7c,	// 97 ù
						0x0c,0x51,0x50,0x51,0x3c,	// 98 ÿ
						0x38,0x45,0x44,0x45,0x38,	// 99 Ö
						0x3c,0x41,0x40,0x41,0x3c,	// 9a Ü
						0x00,0x00,0x00,0x00,0x00,	// 9b 
						0x20,0x48,0x3e,0x49,0x62,	// 9c £
						0x00,0x00,0x00,0x00,0x00,	// 9d 
						0x00,0x00,0x00,0x00,0x00,	// 9e 
						0x40,0x68,0x3e,0x09,0x02,	// 9f ƒ

						0x20,0x54,0x56,0x55,0x78,	// a0 á
						0x00,0x44,0x7e,0x41,0x00,	// a1 í
						0x38,0x44,0x46,0x45,0x38,	// a2 ó
						0x3c,0x40,0x42,0x21,0x7c,	// a3 ú
						0x7c,0x09,0x05,0x05,0x78,	// a4 ñ
						0x7c,0x09,0x11,0x21,0x7c,	// a5 Ñ

						0x7e,0x25,0x25,0x26,0x18};	// a6 ß

unsigned int16 CONST SPRITES1[40]={0x00f,0x00f,0x00f,0x00f,0x00f,0x00f,0x00f,0x00f,	// nr.5: blue, gps pos
						0x00f,0x000,0x000,0x000,0x00f,0x00f,0x00f,0x00f,
						0x00f,0x000,0x000,0x00f,0x00f,0x00f,0x00f,0x00f,
						0x00f,0x000,0x00f,0x000,0x00f,0x00f,0x00f,0x00f,
						0x00f,0x00f,0x00f,0x00f,0x000,0x00f,0x00f,0x00f};

unsigned int16 CONST SPRITES2[40]={0x0f0,0x0f0,0x0f0,0x000,0x0f0,0x0f0,0x0f0,0x0f0,	// nr.6: green, ok
						0x0f0,0x0f0,0x000,0x0f0,0x0f0,0x0f0,0x0f0,0x0f0,
						0x0f0,0x000,0x000,0x0f0,0x0f0,0x0f0,0x0f0,0x0f0,
						0x0f0,0x0f0,0x0f0,0x000,0x000,0x0f0,0x0f0,0x0f0,
						0x0f0,0x0f0,0x0f0,0x0f0,0x0f0,0x000,0x000,0x0f0};		
				
unsigned int16 CONST SPRITES3[40]={0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,	// nr.7: white/red, roadworks
						0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,
						0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,
						0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,
						0xfff,0xf00,0xfff,0xf00,0xfff,0xf00,0xfff,0xf00};
						
unsigned int16 CONST SPRITES4[40]={0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,	// nr.8, red, danger
						0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,
						0xf00,0xf00,0xfff,0xf00,0xfff,0xfff,0xfff,0xf00,
						0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,
						0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00,0xf00};				

//
char charsele;
unsigned int8 charrow,charposi,chardat; 
unsigned int16 oled_xxx,oled_yyy;		
unsigned int16 tFcolour, tBcolour;
unsigned int8 font_mult_x,font_mult_y;


void 	oled_init(void);
void 	oled_clear_screen(void);


void 	oled_write_register(char reg_index,char reg_data);
void 	oled_write_register2(unsigned int16 reg_index_data);
void 	oled_write_display(char dis_R,char dis_G,char dis_B);
char 	oled_read_register(char reg_index);

void 	oled_gotoxy(char cx, char cy);
//void 	oled_gotoxy2(unsigned int16 cx, unsigned int16 cy);
void	oled_erase_y(char yy);

void 	oled_printchar1(char cvar);
//void 	oled_printchar2(char cvar);
void 	table1_to_oled(void);

void 	ascii_extended_corr(void);
void 	oled_pixelxy(unsigned int16 cnokia_color,unsigned int8 cnokia_xx,unsigned int8 cnokia_yy);
void 	oled_drawlinexy(int8 style, int16 cnokia_color1,int16 cnokia_color2,unsigned int16 cnokia_x1,unsigned int16 cnokia_x2,unsigned int16 cnokia_y1,unsigned int16 cnokia_y2);

void 	oled_printsprite(char sprite);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_init(void)
{
OUTPUT_BIT(oledcsb,1);

OUTPUT_BIT(oledresetb,0);
delay_ms(10);
OUTPUT_BIT(oledresetb,1);
delay_ms(1);

//oled_write_register(0x05,0x01);	// soft reset
delay_ms(100);	
oled_write_register(0x04,0x01);		// diplay off, normal current
delay_ms(1);
oled_write_register(0x04,0x04);		// display on, half current (see 0x10..0x12)
delay_ms(1);


//oled_write_register(0x16,0x46);	// memory write mode: 9bits bus, hc=1,vc=1,hv=0		HOR WRITE
//oled_write_register(0x16,0x45);	// memory write mode: 9bits bus, hc=1,vc=0,hv=1		VERT. WRITE
oled_write_register(0x16,0x45);	// memory write mode: 9bits bus, hc=1,vc=0,hv=1		VERT. WRITE
oled_write_register(0x02,0x41);	// oscillator


oled_write_register(0x08,0x03);	// precharge time R *** was 03
oled_write_register(0x09,0x05);	// precharge time G *** was 05
oled_write_register(0x0a,0x05);	// precharge time B

oled_write_register(0x0b,0x48);	// precharge current R *** was 0x56
oled_write_register(0x0c,0x4d);	// precharge current G
oled_write_register(0x0d,0x46);	// precharge current B

oled_write_register(0x10,0x40);	// driving current R *** was 0x0a
oled_write_register(0x11,0x40);	// driving current G ***
oled_write_register(0x12,0x40);	// driving current B ***

oled_write_register(0x04,0x00);	// normal current
delay_ms(1);
//oled_write_register(0x06,0x01);	// display ON
delay_ms(1);

oled_write_register(0x3b,0x00);	// screen saver off

/*
t02= oled_read_register(0x01);fprintf(ser2," r01=%x ",t02);
t02= oled_read_register(0x02);fprintf(ser2," r02=%x ",t02);
t02= oled_read_register(0x06);fprintf(ser2," r06=%x ",t02);
t02= oled_read_register(0x16);fprintf(ser2," r16=%x ",t02);
*/

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_clear_screen(void)
{
unsigned int8 xx,yy;
unsigned int16 pix_count;	// 160*128=20480

SET_TRIS_D(0x700);


oled_write_register(0x17,0x00);		// MX1 start window access X =0
oled_write_register(0x18,0x9f);		// MX1 end window access X =159

oled_write_register(0x19,0x00);		// MY1 start window access Y =0
oled_write_register(0x1a,0x7f);		// MY1 end window access Y =127

oled_write_register(0x20,0x00);		// MAC X
oled_write_register(0x21,0x7f);		// MAC Y

SET_TRIS_D(0x700);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x822);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);



for (pix_count=0; pix_count!=20480; pix_count++){oled_write_display(0x00,0x00,0x00);}




}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void oled_write_register(char reg_index,char reg_data)
{

OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(reg_index+0x800);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledcsb,1);

OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(reg_data+0x800);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledcsb,1);


}
/////////////////////////////////////////////////////////////////////////////////
void oled_write_register2(unsigned int16 reg_index_data)
{
//unsigned int16 reg_index;


OUTPUT_BIT(oledcsb,0);
//OUTPUT_BIT(oledrs,0);	B9 //OUTPUT_BIT(oledrdb,1); B12 //OUTPUT_BIT(oledwrb,0); B13
OUTPUT_D(0x800);

//reg_index=reg_index_data;reg_index>>=8;
OUTPUT_D((reg_index_data>>8)+0x800);
//OUTPUT_BIT(oledwrb,1);
OUTPUT_BIT(oledcsb,1);

OUTPUT_BIT(oledcsb,0);
//OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x800);
OUTPUT_D((reg_index_data&0x00ff)+0x800);
//OUTPUT_BIT(oledwrb,1);
OUTPUT_BIT(oledcsb,1);

}
/////////////////////////////////////////////////////////////////////////////////
void oled_write_display(char dis_R,char dis_G,char dis_B)
{
char dis_data1, dis_data2;
int1 dis_db91, dis_db92;

// now put tha RGB-data into right place to fit 2* 9 bit bus mode

dis_data1=((dis_R<<2)|(dis_G>>4));
dis_db91= bit_test(dis_G,3);

dis_data2=((dis_G<<5)|(dis_B>>1));
dis_db92= bit_test(dis_B,0);


OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(dis_data1+0x800); OUTPUT_BIT(oleddb9,dis_db91);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledcsb,1);

OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(dis_data2+0x800); OUTPUT_BIT(oleddb9,dis_db92);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledcsb,1);


}
/////////////////////////////////////////////////////////////////////////////////

char 	oled_read_register(char reg_index)
{
unsigned int8 t03;

SET_TRIS_D(0x700);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(reg_index+0x800);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledcsb,1);



SET_TRIS_D(0x7ff);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,0);OUTPUT_BIT(oledwrb,1);
t03= INPUT_D()&0xff;
OUTPUT_BIT(oledrdb,1);;OUTPUT_BIT(oledcsb,1);

SET_TRIS_D(0x700);
return t03;
}
//////////////////////////////////////////////////////////////////////////////////
void oled_gotoxy(char cx, char cy)		// Position cursor
{
oled_write_register(0x17,0x00);		// MX1 start window access X =0
oled_write_register(0x18,0x9f);		// MX1 end window access X =159

oled_write_register(0x19,0x00);		// MY1 start window access Y =0
oled_write_register(0x1a,0x7f);		// MY1 end window access Y =127



oled_write_register(0x20,cx);		// MAC X
oled_write_register(0x21,cy);		// MAC Y

oled_xxx=cx;
oled_yyy=cy;

SET_TRIS_D(0x700);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x822);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);


}
//////////////////////////////////////////////////////////////////////////////////
/*
void oled_gotoxy2(unsigned int16 cx, unsigned int16 cy)		// Position cursor
{
//oled_write_register(0x17,0x00);		// MX1 start window access X =0
oled_write_register2(0x1700);		// MX1 start window access X =0
//oled_write_register(0x18,0x9f);		// MX1 end window access X =159
oled_write_register2(0x189f);		// MX1 end window access X =159
//oled_write_register(0x19,0x00);		// MY1 start window access Y =0
oled_write_register2(0x1900);		// MY1 start window access Y =0
//oled_write_register(0x1a,0x7f);		// MY1 end window access Y =127
oled_write_register2(0x1a7f);		// MY1 end window access Y =127


//oled_write_register(0x20,cx);		// MAC X
oled_write_register2(0x2000|cx);		// MAC X
//oled_write_register(0x21,cy);		// MAC Y
oled_write_register2(0x2100|cy);		// MAC Y



oled_xxx=cx;
oled_yyy=cy;



//SET_TRIS_D(0xff00);
//return;

OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x22);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);


}
*/
//////////////////////////////////////////////////////////////////////////////////
void	oled_erase_y(char yy)
{
unsigned int8 s;

oled_gotoxy(0,(yy*8));

for(s=0;s!=27;s++){oled_printchar1(' ');}

}
//////////////////////////////////////////////////////////////////////////////////
void	oled_erase_y2(char yy)
{
unsigned int8 s;

oled_gotoxy(0,(yy));

for(s=0;s!=27;s++){oled_printchar1(' ');}

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_printchar1(char cvar)					// Write 1 character to LCD 
{

// font_mult

oled_write_register(0x17,oled_xxx);		// MX1 start window access X =0
oled_write_register(0x18,oled_xxx+(6*font_mult_x));		// MX1 end window access X =159

oled_write_register(0x19,oled_yyy);		// MY1 start window access Y =0
oled_write_register(0x1a,oled_yyy+(8*font_mult_y)-1);		// MY1 end window access Y =127

oled_write_register(0x20,oled_xxx);		// MAC X
oled_write_register(0x21,oled_yyy+(8*font_mult_y)-1);		// MAC Y


SET_TRIS_D(0x700);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x822);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);

charsele=cvar;
table1_to_oled();


oled_xxx+=(6*font_mult_x);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
void oled_printchar2(char cvar)					// Write 1 character to LCD 
{

// font_mult

oled_write_register2(0x1700|oled_xxx);		// MX1 start window access X =0
oled_write_register2(0x1800|oled_xxx+(6*font_mult_x));		// MX1 end window access X =159

oled_write_register2(0x1900|oled_yyy);		// MY1 start window access Y =0
oled_write_register2(0x1a00|oled_yyy+(8*font_mult_y)-1);		// MY1 end window access Y =127

oled_write_register2(0x2000|oled_xxx);		// MAC X
oled_write_register2(0x2100|oled_yyy+(8*font_mult_y)-1);		// MAC Y


SET_TRIS_D(0xff00);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x22);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);

charsele=cvar;
table1_to_oled();


oled_xxx+=(6*font_mult_x);

}
*/

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
void table1_to_oled(void)	// extract ascii from tables & write to LCD
{
unsigned int8 s,mx,my;
unsigned int16 oledR,oledG,oledB;

//if(diag){fprintf(ser2," %x ",charsele);}



if (charsele<0x20){return;}
else if (charsele>0x7f){ascii_extended_corr();}


if (charsele>0xa6){return;}



for (charrow=0;charrow<5;charrow++) {		// 5 bytes
	
if (charsele<0x50){charposi=(((charsele&0xff)-0x20)*5);chardat=TABLE7[(charposi+charrow)];}						// use TABLE7
else if (charsele<0x80){charposi=(((charsele&0xff)-0x50)*5);chardat=TABLE8[(charposi+charrow)];}				// use TABLE8							
else if (charsele<0xa7){charposi=(((charsele&0xff)-0x80)*5);chardat=TABLE9[(charposi+charrow)]; }				// use TABLE9							
		

for(mx=0;mx!=font_mult_x;mx++){
	
	for(s=7;s!=255;s-=1){
						oledR=((tBcolour&0xf00)>>6);	// 4 bits res. to upper 4 of 6 bits
						oledG=((tBcolour&0x0f0)>>2);
						oledB=((tBcolour&0x00f)<<2);

						if(bit_test(chardat,s)==1){
													oledR=((tFcolour&0xf00)>>6);
													oledG=((tFcolour&0x0f0)>>2);
													oledB=((tFcolour&0x00f)<<2);

													}


	
						//oled_write_display(0xff,0x00,0x00);
						for(my=0;my!=font_mult_y;my++){oled_write_display((int8)oledR,(int8)oledG,(int8)oledB);}
						//delay_ms(250);


						}
							}							


									}



	oledR=((tBcolour&0xf00)>>6);	// 4 bits res. to upper 4 of 6 bits
	oledG=((tBcolour&0x0f0)>>2);
	oledB=((tBcolour&0x00f)<<2);


for(mx=0;mx!=font_mult_x;mx++){

	for(s=0;s!=8;s++){
						for(my=0;my!=font_mult_y;my++){oled_write_display((int8)oledR,(int8)oledG,(int8)oledB);}				// background (blank)	

						}

								}

}
//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ascii_extended_corr(void)
{
//printf( "charsele_in= %x ",charsele);

if(charsele==0xc7){charsele=0x80;}			// Ç
else if(charsele==0xfc){charsele=0x81;}		// ü
else if(charsele==0xe9){charsele=0x82;}		// é
else if(charsele==0xe2){charsele=0x83;}		// 
else if(charsele==0xe4){charsele=0x84;}		// 
else if(charsele==0xe0){charsele=0x85;}		// 
else if(charsele==0xe5){charsele=0x86;}		//
else if(charsele==0xe7){charsele=0x87;}		// 
else if(charsele==0xea){charsele=0x88;}		// 
else if(charsele==0xeb){charsele=0x89;}		// 
else if(charsele==0xe8){charsele=0x8a;}		// 
else if(charsele==0xef){charsele=0x8b;}		// 
else if(charsele==0xee){charsele=0x8c;}		// 
else if(charsele==0xec){charsele=0x8d;}		// 
else if(charsele==0xc4){charsele=0x8e;}		// 
else if(charsele==0xc5){charsele=0x8f;}		// Å

else if(charsele==0xc9){charsele=0x90;}		// É
else if(charsele==0xe6){charsele=0x91;}		// æ
else if(charsele==0xc6){charsele=0x92;}		// Æ
else if(charsele==0xf4){charsele=0x93;}		// 
else if(charsele==0xf6){charsele=0x94;}		// 
else if(charsele==0xf2){charsele=0x95;}		// 
else if(charsele==0xfb){charsele=0x96;}		//
else if(charsele==0xf9){charsele=0x97;}		// 
else if(charsele==0xff){charsele=0x98;}		// 
else if(charsele==0xd6){charsele=0x99;}		// 
else if(charsele==0xdc){charsele=0x9a;}		// 
else if(charsele==0xf8){charsele=0x9b;}		// 
else if(charsele==0xa3){charsele=0x9c;}		// 
else if(charsele==0xd8){charsele=0x9d;}		// 
else if(charsele==0xd7){charsele=0x9e;}		// 
else if(charsele==0x83){charsele=0x9f;}		// ƒ

else if(charsele==0xe1){charsele=0xa0;}		// á
else if(charsele==0xed){charsele=0xa1;}		// í
else if(charsele==0xf3){charsele=0xa2;}		// ó
else if(charsele==0xfa){charsele=0xa3;}		// 
else if(charsele==0xf1){charsele=0xa4;}		// 
else if(charsele==0xd1){charsele=0xa5;}		// Ñ

else if(charsele==0xdf){charsele=0xa6;}		// ß


}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_pixelxy(int16 cnokia_color,unsigned int8 cnokia_xx,unsigned int8 cnokia_yy)
{
unsigned int16 red,green,blue;

//delay_ms(500);
//fprintf(ser2,"\n\r cnokia_xx=%u ",cnokia_xx);

if ((cnokia_xx==0)||(cnokia_yy>127)){return;}	// calc error correction

oled_gotoxy(cnokia_xx,cnokia_yy);
//
red= (cnokia_color&0x000f)<<2;
cnokia_color>>=4;
green= (cnokia_color&0x000f)<<2;
cnokia_color>>=4;
blue= (cnokia_color&0x000f)<<2;
//

oled_write_display((int8)red,(int8)green,(int8)blue);





}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_drawlinexy(int8 style, int16 cnokia_color1, int16 cnokia_color2,unsigned int16 cnokia_x1,unsigned int16 cnokia_x2,unsigned int16 cnokia_y1,unsigned int16 cnokia_y2)
{
signed int16 dy,dx;
signed int16 stepx, stepy,fraction;
char previous='-';

dy = cnokia_y2 - cnokia_y1;
dx = cnokia_x2 - cnokia_x1;

if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
dy <<= 1;							// dy is now 2*dy
dx <<= 1;							// dx is now 2*dx




oled_pixelxy(cnokia_color1,cnokia_x1, cnokia_y1);

if (dx > dy) {


	if(style>1){
	       if(cnokia_x2<cnokia_x1){
								//printf(" *a-* ");
								oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-1);
								oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-2);
								}
		else if(cnokia_x2>cnokia_x1){
								//printf(" *b-* ");
								oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+1);
								oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+2);
									}
		else if(cnokia_x2==cnokia_x1){
								//printf(" *c-* ");



									}


				}



    fraction = dy - (dx >> 1);	// same as 2*dy - dx
    while (cnokia_x1 != cnokia_x2) {


				        if (fraction >= 0) {
								            cnokia_y1 += stepy;
								            fraction -= dx;			// same as fraction -= 2*dx
				       						 }
				        cnokia_x1 += stepx;
				        fraction += dy;				// same as fraction -= 2*dy
						oled_pixelxy(cnokia_color1,cnokia_x1, cnokia_y1);
						if(style>1){
					        if(cnokia_x2<cnokia_x1){
													//printf(" *a* ");
													previous='a';
													oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-1);
													oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-2);
													}
							else if(cnokia_x2>cnokia_x1){
													//printf(" *b* ");
													previous='b';
													oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+1);
													oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+2);
														}
							else if(cnokia_x2==cnokia_x1){
													//printf(" *c* ");
													if(previous=='a'){
																	oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-1);
																	oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1-2);
																	}
													else if(previous=='b'){
																	oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+1);
																	oled_pixelxy(cnokia_color2,cnokia_x1, cnokia_y1+2);
																	}

														}

									}

   									 }
			} 
else {

	if(style>1){
		if(cnokia_y2<cnokia_y1){
								//printf(" *d-* ");
								oled_pixelxy(cnokia_color2, cnokia_x1+1, cnokia_y1);
								oled_pixelxy(cnokia_color2, cnokia_x1+2, cnokia_y1);
								}
	    else if(cnokia_y2>cnokia_y1){
								//printf(" *e-* ");
								oled_pixelxy(cnokia_color2, cnokia_x1-1, cnokia_y1);
								oled_pixelxy(cnokia_color2, cnokia_x1-2, cnokia_y1);
									}
	else if(cnokia_y2==cnokia_y1){
								//printf(" *f-* ");

									}

				}



    fraction = dx - (dy >> 1);
    while (cnokia_y1 != cnokia_y2) {


				        if (fraction >= 0) {
								            cnokia_x1 += stepx;
								            fraction -= dy;
				       						 }
				        cnokia_y1 += stepy;
				        fraction += dx;

						oled_pixelxy(cnokia_color1,cnokia_x1, cnokia_y1);
						if(style>1){
							if(cnokia_y2<cnokia_y1){
													//printf(" *d* ");
													previous='d';
													oled_pixelxy(cnokia_color2, cnokia_x1+1, cnokia_y1);
													oled_pixelxy(cnokia_color2, cnokia_x1+2, cnokia_y1);
													}
					        else if(cnokia_y2>cnokia_y1){
													//printf(" *e* ");
													previous='e';
													oled_pixelxy(cnokia_color2, cnokia_x1-1, cnokia_y1);
													oled_pixelxy(cnokia_color2, cnokia_x1-2, cnokia_y1);
														}
							else if(cnokia_y2==cnokia_y1){
														//printf(" *f* ");
														if(previous=='d'){
																	oled_pixelxy(cnokia_color2, cnokia_x1+1, cnokia_y1);
																	oled_pixelxy(cnokia_color2, cnokia_x1+2, cnokia_y1);
																			}
														else if(previous=='e'){
																	oled_pixelxy(cnokia_color2, cnokia_x1-1, cnokia_y1);
																	oled_pixelxy(cnokia_color2, cnokia_x1-2, cnokia_y1);
																				}
														}

									}

   									 }
		}

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oled_printsprite(char sprite)					// Write 1 sprite to LCD 
{
char i;
unsigned int16 sprite_rgb;
unsigned int16 sprite_r,sprite_g,sprite_b;

oled_write_register(0x17,oled_xxx);		// MX1 start window access X =0
oled_write_register(0x18,oled_xxx+(6*font_mult_x));		// MX1 end window access X =159

oled_write_register(0x19,oled_yyy);		// MY1 start window access Y =0
oled_write_register(0x1a,oled_yyy+(8*font_mult_y)-1);		// MY1 end window access Y =127

oled_write_register(0x20,oled_xxx);		// MAC X
oled_write_register(0x21,oled_yyy+(8*font_mult_y)-1);		// MAC Y


SET_TRIS_D(0x700);
OUTPUT_BIT(oledcsb,0);OUTPUT_BIT(oledrs,0);OUTPUT_BIT(oledrdb,1);OUTPUT_BIT(oledwrb,0);
OUTPUT_D(0x822);
OUTPUT_BIT(oledwrb,1);OUTPUT_BIT(oledrs,1);OUTPUT_BIT(oledcsb,1);


for(i=0;i!=40;i++){
					
					if(sprite==1){sprite_rgb=SPRITES1[i];}
					else if(sprite==2){sprite_rgb=SPRITES2[i];}
					else if(sprite==3){sprite_rgb=SPRITES3[i];}
					else if(sprite==4){sprite_rgb=SPRITES4[i];}

					sprite_b=(sprite_rgb&0x00f)<<2;
					sprite_rgb>>=4;
					sprite_g=(sprite_rgb&0x00f)<<2;
					sprite_rgb>>=4;
					sprite_r=(sprite_rgb&0x00f)<<2;
					sprite_rgb>>=4;

					oled_write_display((int8)sprite_r ,(int8)sprite_g ,(int8)sprite_b );}

oled_xxx+=(6*font_mult_x);


}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


