/*--------------------------------------|
|File:		 lcd.c						|
|Date:   	 August 2011				|
|Author:	 J. Moertl					|
|Description:Source data to control the |
|			 EA DOGS102-6 lcd module	|
|			 including init, write and	|
|			 drawing functions.			|
|--------------------------------------*/

#include "main.h"

//static uint8_t lcd_buffer[XPIXEL+15][YPIXEL/4+1]={0xAE};  //nur 4 pixel pro page
//unsigned char lcd_buffer[XPIXEL][YPIXEL/8];  //Buffer to store display data. The buffer organization is equal to

static uint8_t lcd_buffer[175][27]={
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,
3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,
13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,
19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,
28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,
29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,
33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,
34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,
36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,
40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,
43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,
45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,
47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,
49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,
52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,
53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,
57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,
59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,
60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,
63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,
64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,
66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,
67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,
69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,
71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,
72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,
73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,
75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,
76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,
77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,
78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,
79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,
82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,
83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,
84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,
85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,
88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,
90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,
91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,
92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,
93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,
95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,
98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,
99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,
101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,
103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,
104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,
105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,
106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,
107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,
108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,
109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,
111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,
113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,
114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,
115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,
119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,
121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,
122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,
123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,
124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,
125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,
127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,
129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,
130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,
131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,
132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,
133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,
135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,
137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,
138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,
139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,
140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,
141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,
142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,
143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,
145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,
146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,
147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,
148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,
149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,
151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,
152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,
153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,
155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,
156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,
157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,
159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,
161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,
162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,
163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,
164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,
165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,
167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,
168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,
169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,
170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,
171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,
173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,
174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,
175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201
};
											 // the one of the display

uint8_t low_page(uint8_t byte, uint8_t color)
{
	uint8_t dummy=0, i=0;
	
	
	
	for (i=0;i<4;i++){
	
	// i-tes Bit auf LSB schieben, maskieren, mit Farbe multiplizieren und doppelt soweit zurückschieben	
	
	dummy |= (((byte>>i)&0x01)*color)<<(i*2);
	}
	
	return  (uint8_t)dummy;
	
}

uint8_t high_page(uint8_t byte, uint8_t color)
{
	uint8_t dummy=0, i=0;
	
	
	for (i=4;i<8;i++){
	
	// i-tes Bit auf LSB schieben, maskieren, mit Farbe multiplizieren und doppelt soweit zurückschieben	
		dummy |= (((byte>>i)&0x01)*color)<<((i-4)*2);
	}
	return  (uint8_t)dummy;
	
}


/*--------------------------------------
Function:	lcd_init()
Input:		---
Output:		---
Description:Initializes and clears lcd
--------------------------------------*/
void lcd_init(void)
{
	DDRB |= (1<<DRESET); //Set display reset as output

	PORTB|=(1<<DRESET);  //Set display reset to high -> LCD is running now
	
	//Initialize bottom view 3.3V (booster on) 8Bit SPI
	/*
	lcd_send_command(0x40); //Startline 0
	lcd_send_command(0xA1); //SEG reverse
	lcd_send_command(0xC0); //Set COM direction (COM0-COM63)
	lcd_send_command(0xA4); //Set all Pixel to on
	lcd_send_command(0xA6); //Display inverse off
	lcd_send_command(0xA2); //Set bias 1/9
	lcd_send_command(0x2F); //Booster, regulator, follower on
	lcd_send_command(0x27); //Set contrast
	lcd_send_command(0x81); //Set contrast
	lcd_send_command(0x10); //Set contrast
	lcd_send_command(0xFA); //Temperature compensation
	lcd_send_command(0x90); //Temperature compensation
	lcd_send_command(0xAF); //Display on

	lcd_clear_all(); //Clear display and set display buffer to 0
	*/
	lcd_send_command(0xF1); //Set COM End
	lcd_send_command(0x67); //...104lines
	lcd_send_command(0xC0); //Set COM direction TopView
	lcd_send_command(0x40); //Start line 0
	lcd_send_command(0x50); //..
	lcd_send_command(0x2B); //Set Panelloading to 28..38nF
	lcd_send_command(0xEB); //Set Bias to 1/12
	lcd_send_command(0x81); //Set contrast
	lcd_send_command(0x5F); //Set contrast
	
	//lcd_send_command(0x88); //no auto increament
	//lcd_send_command(0x8D); //-1 autoincreament
	//lcd_send_command(0x89); //Set auto increament
	lcd_send_command(0x8B); //+1 page autoincreament
	lcd_send_command(0xAF); //Display on
	
	

}

/*--------------------------------------
Function:	lcd_send()
Input:		data, cd
Output:		---
Description:send data, as command (cd=0)
			or simple data (cd=1)
--------------------------------------*/
void lcd_send(uint8_t data, uint8_t cd)
{
	
	PORTB &= ~(1<<DCS);			//Slave select (DCS to gnd)
	if (cd == 0)
		PORTB&=~(1<<DCD);	//Send command (DCD to 0)
	else
		PORTB|=(1<<DCD);	//Send data (DCD to 1)
	
		
	spi_byte(data);			//Send data

}


void lcd_send_command (uint8_t data)
{
	lcd_send(data,0);
}


void lcd_send_data (uint8_t data)
{
	lcd_send(data,1);
}
/*--------------------------------------
Function:	lcd_clear()
Input:		xs, ys, xe, ye
Output:		---
Description:clear a rectangle
--------------------------------------*/





void lcd_fill1(uint8_t xs, uint8_t ys, uint8_t xe, uint8_t ye,uint8_t color, uint8_t mode)
{
	//i,j are index variables
	//ps, pe are page start and endadress
	//yr is restpixels
	uint8_t i=0,j=0,pe=0, yr=0;
	
	j=ys;
	yr=(ye-ys)%8;
	
	while(j<=(ye-8))
	{
		for (i=xs;i<=xe;i++){lcd_byte(i,j,255,color,mode);};	
		j=j+8;	
	};
	for (i=xs;i<=xe;i++){lcd_byte(i,j,(255>>(8-yr)),color,mode);};
	
	

}


/*--------------------------------------
Function:	lcd_fill()
Input:		xs, ys, xe, ye
Output:		---
Description:fill a rectangle
--------------------------------------*/
void lcd_fill(uint8_t xs, uint8_t ys, uint8_t xe, uint8_t ye, uint8_t color)
{
	uint8_t i=0,j=0, mode=ADD, pe=0, yr=0;
	
	switch (color){
		case 0: lcd_fill1(xs,ys,xe,ye,3,DELETE); break;
		case 3: lcd_fill1(xs,ys,xe,ye,3,ADD); break;
		default: lcd_fill1(xs,ys,xe,ye,3,DELETE);
		lcd_fill1(xs,ys,xe,ye,color,ADD); break;

	};		
		
}


/*--------------------------------------
Function:	lcd_line()
Input:		xs, ys, xe, ye
Output:		---
Description:draw a stright line, please
			make sure, that the line is
			either horizontal or
			vertical
--------------------------------------*/
void lcd_line(uint8_t xs, uint8_t ys, uint8_t xe, uint8_t ye, uint8_t color)
{
	//i,j are index variables
	//ps, pe are page start and endadress
	//yr is restpixels
	uint8_t i=0,j=0, ps=0, pe=0, yr=0;

	//ps=ys/8; //calculate startpage
	ps=ys/4; //weil auf einer page nur 4 Zeilen sind
	//pe=ye/8; //calculate endpage
	pe=ye/4; //weil auf einer page nur 4 Zeilen sind


	//-------------Starpage--------------
	//Set coloumn adress
	lcd_send_command(xs&0x0F); 	   //LSB adress
	lcd_send_command(0x10+(xs>>4));//MSB adress

	//set page adress
	//lcd_send_command(0xB0+ps);
	lcd_send_command(0x60+ps);   //weil Maske 011xxxxx ist

	//yr=ys%8; //calculate the start within first page
	yr=ys%4;  //weil nur 4 Pixel auf einer Page


	if (ys==ye) //horizontal line
	{
		lcd_send_command(0x89); //Set auto increament
		

		for(i=xs; i<=xe; i++) //loop starting first colomn to last coloumn
		{

			lcd_buffer[i][ps]|=color<<(yr*2);     //fill the buffer
			lcd_send_data(lcd_buffer[i][ps]);//send the changed pages of the buffer to the display
		}
		lcd_send_command(0x8B); //+1 page autoincreament
	}
	else //vertical line
	{
		lcd_fill1(xs, ys, xe, ye,color, ADD);

	}

}


void lcd_pixel(uint8_t xs, uint8_t ys, uint8_t color)
{
	//i,j are index variables
	//ps, pe are page start and endadress
	//yr is restpixels
	uint8_t i=0,j=0, ps=0, pe=0, yr=0;

	ps=ys/4; //weil auf einer page nur 4 Zeilen sind

	//-------------Starpage--------------
	//Set coloumn adress
	lcd_send_command(xs&0x0F); 	   //LSB adress
	lcd_send_command(0x10+(xs>>4));//MSB adress

	//set page adress
	lcd_send_command(0x60+ps);   //weil Maske 011xxxxx ist

	yr=ys%4;  //weil nur 4 Pixel auf einer Page
	lcd_buffer[xs][ps]|=color<<(yr*2);     //fill the buffer
	lcd_send_data(lcd_buffer[xs][ps]);//send the changed pages of the buffer to the display
		
	

	

}






/*--------------------------------------
Function:	lcd_rect()
Input:		xs, ys, xe, ye
Output:		---
Description:draw a rectanlge
--------------------------------------*/
void lcd_rect(uint8_t xs, uint8_t ys, uint8_t xe, uint8_t ye, uint8_t color)
{
	lcd_line(xs, ys, xe, ys, color); //upper line
	lcd_line(xs, ye, xe, ye, color); //lower line
	lcd_line(xs, ys, xs, ye, color); //left line
	lcd_line(xe, ys, xe, ye, color); //right line
}

void lcd_byte(uint8_t x, uint8_t y, uint8_t data, uint8_t color, uint8_t linkmode)
		
{
	uint8_t ys=0,shift=0, dummy;
	
	//calculate startpage
	ys=y/4;
	//calculate shift from first row within start page
	shift = y%4;
	//Set coloumn adress 
	lcd_send_command(x&0x0F);     //LSB adress
	lcd_send_command(0x10+(x>>4));//MSB adress
	//set page adress of first half of byte
	lcd_send_command(0x60+ys);
	//Adressen müssen für die einzelnen Pages nicht neu gesetzt werden, weil die pages automatisch hochzählen wenn richtig (0x8B) initialisiert
	
	//*****************First half byte***********
	
	lcd_buffer[x][ys]|=low_page(data<<shift,color);
	if (linkmode==DELETE){lcd_buffer[x][ys]&=~low_page(data<<shift,color);};
	if (linkmode==INVERS){lcd_buffer[x][ys]^=low_page(data<<shift,color);};
	lcd_send_data(lcd_buffer[x][ys]);
	
	
	//*********** Second half byte ******************
	lcd_buffer[x][ys+1]|=high_page(data<<shift,color);
	if (linkmode==DELETE){lcd_buffer[x][ys+1]&=~high_page(data<<shift,color);};
	if (linkmode==INVERS){lcd_buffer[x][ys+1]^=high_page(data<<shift,color);};
	lcd_send_data(lcd_buffer[x][ys+1]);
	
	//******** Shifted first half byte**************
	//first half of byte if shifted from page margin
	if (shift !=0){
		lcd_buffer[x][ys+2]|=low_page(data>>(8-shift),color);
		if (linkmode==DELETE){lcd_buffer[x][ys+2]&=~low_page(data>>(8-shift),color);};
		if (linkmode==INVERS){lcd_buffer[x][ys+2]^=low_page(data>>(8-shift),color);};
		lcd_send_data(lcd_buffer[x][ys+2]);
	}
	//**************shifted second half byte***************
	if (shift !=0){
		lcd_buffer[x][ys+1+2]|=high_page(data>>(8-shift),color);
		if (linkmode==DELETE){lcd_buffer[x][ys+1+2]&=~high_page(data>>(8-shift),color);};
		if (linkmode==INVERS){lcd_buffer[x][ys+1+2]^=high_page(data>>(8-shift),color);};
		lcd_send_data(lcd_buffer[x][ys+1+2]);
	}
//if (x>149){printf("x: %d ",x);printf("y: %d \n",y);};
	
	
	
}

/*--------------------------------------
Function:	lcd_char()
Input:		x, y, data, *font, linkmode
Output:		---
Description:print a single char on x,y
--------------------------------------*/
void lcd_char(uint8_t x, uint8_t y, uint8_t data, uint8_t *font, uint8_t linkmode)
{
	//i,j are index variables
	//n is flag, if char has to be shifted in one page
	//ps, pe are page start and endadress
	//yr is restpixels
	//byte buffer to store char-data
	uint8_t i=0, j=0, n=1, k=0, ps=0, pe=0, yr=0, byte=0, byte_l=0, byte_h=0, color=3;

	unsigned int font_pos=0; //postion of data within the font array

	uint8_t f_width = pgm_read_byte(font+4);
	uint8_t f_height_bytes = pgm_read_byte(font+6);
	uint8_t f_height_pixel = pgm_read_byte(font+5);
	
	ps=y/4;
	//calculate endpage:
	//2*Fonthöhe in Bytes (weil ein byte 2 pages braucht) + startpage
	pe=2*pgm_read_byte(font + 6)+ps; 	

	
	//calculate positon of data in font array
	//(data*Bytes pro Char)-(start of chars * bytes pro char)+bytes for header
	font_pos=(data*pgm_read_byte(font + 7))-(pgm_read_byte(font + 2)*pgm_read_byte(font + 7))+8;

	//font_pos-=pgm_read_byte(font + 4); //subtract pixels for one char, because it's added again in loop

	if(linkmode==ADD)  //linkmode add, the pixels of char are added into display
		
	{
		for (i=0;i<f_height_bytes;i++){			
		for (j=0;j<f_width;j++){
			lcd_byte(x+j,y+(i*8),pgm_read_byte(font+j+font_pos),color,linkmode);			
		}
		font_pos+=f_width;
		};
		
		
	} 
	else //another linkmode, data is addad with xor
	{
		for(j=ps; j<pe;j++)  //loop starting first page to last page
		{
			//Set coloumn adress
			lcd_send_command(x&0x0F); 	   //LSB adress
			lcd_send_command(0x10+(x>>4)); //MSB adress

			//set page adress
			lcd_send_command(0xB0+j);

			font_pos+=pgm_read_byte(font + 4); //set new postion of data pointer to get next data for one page

			if(n) //jump alternating in if clause or else clause
			{
				for(i=x; i<pgm_read_byte(font + 4)+x; i++) //loop starting first colomn to last coloumn
				{
					byte=pgm_read_byte(font+i+font_pos-x); //get data from font buffer (subtract x because i is not started with 0)
					lcd_buffer[i][j]^=byte<<yr;      //write data into display buffer, but shifted if it is needed
					lcd_send_data(lcd_buffer[i][j]); //send the changed pages of the buffer to the display
				}

				if(yr) //next step is shifting the char the other direction, if the char is not writtin whithin one page
					n=0;
			}
			else //shift the char another direction
			{
				font_pos-=pgm_read_byte(font + 4); //set new postion of data pointer to get next data for one page (substract because it
												   //is added again in next loop
				for(i=x; i<pgm_read_byte(font + 4)+x; i++)
				{
					byte=pgm_read_byte(font+i+font_pos-x); //get data from font buffer (subtract x because i is not started with 0)
					lcd_buffer[i][j]^=byte>>(8-yr);	  //write data into display buffer, but shifted if it is needed
					lcd_send_data(lcd_buffer[i][j]);  //send the changed pages of the buffer to the display
				}
				n=1; //next step is shifting the char the other direction

				if(j<pe-1)
					j--; //the page recently filled has content from last part, the rest would remain empty.
						 //Because of that, the page is decremented, because in next loop the page is inceased again to the
						 //actual written page. New data is written into same page
			}
		}
	}

}


/*--------------------------------------
Function:	lcd_string()
Input:		x, y, data, *font, linkmode
Output:		---
Description:print a string on x,y
--------------------------------------*/
void lcd_string(uint8_t x, uint8_t y, char *data, uint8_t *font, uint8_t linkmode)
{
	//i is index variable
	//xe, ye is endpostion of string
	//length is number of chars
	uint8_t i=0, xe=0, ye=0, length=0;
/*
	if(linkmode==DELETE)
	{
		//get number of chars
		while(data[i])
		{
			i++;
		}
		length=i;

		xe=length*pgm_read_byte(font + 4)+x; //claculate length of string
		ye=y+pgm_read_byte(font + 5);        //calcutlate hight of string
		lcd_clear(x,y,xe,ye); //clear a rectangle where the string is printed later on
	}
	else if(linkmode==INVERS)
	{
		//get number of chars
		while(data[i])
		{
			i++;
		}
		length=i;

		xe=length*pgm_read_byte(font + 4)+x; //claculate length of string
		ye=y+pgm_read_byte(font + 5);		 //calcutlate hight of string
		lcd_fill(x,y,xe,ye); //fill a rectangle where the string is printed later on
	}
	else
	{
	 //other modes
	}

*/
	
	//write single char
	
	while( *data != '\0' )
	{
		
		lcd_char(x,y,*data++, font, linkmode); //write single char
		
		x+=pgm_read_byte(font + 4); //calculate next x postion for next char
		
	}
}

void lcd_clrscr(uint8_t fill)
{
	uint8_t i,j;
	
	for (i=0;i<YPIXEL/4+1;i++){
		for (j=0;j<XPIXEL+15;j++){printf("i %d",i);printf("j %d",j);printf("b %d\n",lcd_buffer[j][i]);
		}
	}

	
	for (i=0;i<(XPIXEL);i++){
		//printf("i %d",i);
		lcd_send_command(i&0x0F); 	   //LSB adress
		lcd_send_command(0x10+(i>>4));//MSB adress
		
		//set page adress of first half of byte
		lcd_send_command(0x60);

		for (j=0;j<((YPIXEL/4));j++)
		{lcd_send_data(fill);
		lcd_buffer[i][j]=fill;
		//printf("j %d\n",j);
		};
	};
		
}

//----------------- EOF lcd.c ------------

