more display testing
[my-code/arm.git] / betty / display.c
1 /*
2  * display.c - handling the display
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 /*
9  * some comments on alpha blending!
10  *
11  * ...
12  *
13  */
14
15 #include "display.h"
16 #include "system.h"
17 #include "default_logo.h"
18 #include "uart.h"
19
20 /*
21  * functions
22  */
23
24 void display_fill_screen(u8 fill) {
25
26         u8 page,width;
27         u8 buf[2];
28
29         buf[0]=0;
30         buf[1]=0;
31
32         switch(fill) {
33                 case DISPLAY_FILL_LG:
34                         buf[1]=0xff;
35                         break;
36                 case DISPLAY_FILL_DG:
37                         buf[0]=0xff;
38                         break;
39                 case DISPLAY_FILL_B:
40                         buf[0]=0xff;
41                         buf[1]=0xff;
42                         break;
43                 case DISPLAY_FILL_W:
44                 default:
45                         break;
46         }
47
48         for(page=0;page<DISPLAY_PAGE_MAX;page++) {
49                 DISPLAY_SET_C_ADDR(0);
50                 DISPLAY_SET_PAGE_ADDR(page);
51                 for(width=0;width<DISPLAY_DIMX;width++) {
52                         DISPLAY_DATA=buf[0];
53                         DISPLAY_DATA=buf[1];
54                 }
55         }
56 }
57
58 void display_clear_screen(void) {
59
60         display_fill_screen(DISPLAY_FILL_W);
61 }
62
63 void display_init(void) {
64
65         /* oscillator, regulator, boost, opamp, contrast */
66         DISPLAY_START_OSCILLATOR;
67         DISPLAY_SET_REGULATOR(7);
68         DISPLAY_SET_CONTRAST(0x38);
69         DISPLAY_SET_CONV_FACTOR(0x01);
70         DISPLAY_SET_POWER(DISPLAY_V_BOOST|DISPLAY_REGULATOR|DISPLAY_OPAMP);
71
72         // gray scale palette
73         DISPLAY_SET_WHITE(0,0,0,0);
74         DISPLAY_SET_LGRAY(3,3,3,3);
75         DISPLAY_SET_DGRAY(6,6,6,6);
76         DISPLAY_SET_BLACK(9,9,9,9);
77
78         /* normal mode, display depending ram content */
79         DISPLAY_RAM_CONTENTS_ON;
80         display_clear_screen();
81         DISPLAY_NORMAL;
82
83         /* switch on the display */
84         DISPLAY_SET_ON;
85 }
86
87 void display_load_logo(u8 *src) {
88
89         u8 *s;
90         u8 page,width;
91
92         s=src;
93         if(s==0)
94                 s=default_logo;
95
96         for(page=0;page<DISPLAY_PAGE_MAX;page++) {
97                 DISPLAY_SET_C_ADDR(0);
98                 DISPLAY_SET_PAGE_ADDR(page);
99                 for(width=0;width<DISPLAY_DIMX;width++) {
100                         DISPLAY_DATA=*s++;
101                         DISPLAY_DATA=*s++;
102                 }
103         }
104 }
105
106 void display_draw_rectangle(int x,int y,int w,int h,u8 fill,u8 alpha) {
107
108         int c,r,rmax;
109         u8 p,spage,epage;
110         u8 b[2],a[2];           // c = a over b => c=alpha*a+(1-alpha)*b
111
112         a[0]=0;
113         a[1]=0;
114         switch(fill) {
115                 case DISPLAY_FILL_LG:
116                         a[1]=1;
117                         break;
118                 case DISPLAY_FILL_DG:
119                         a[0]=1;
120                         break;
121                 case DISPLAY_FILL_B:
122                         a[0]=1;
123                         a[1]=1;
124                 case DISPLAY_FILL_W:
125                 default:
126                         break;
127         }
128
129         spage=y>>3;                                     // start page = y/8
130         epage=(y+h)>>3;                                 // end page (y+h)/8
131
132         for(p=spage;p<=epage;p++) {
133                 DISPLAY_SET_PAGE_ADDR(p);
134                 for(c=x;c<x+w;c++) {
135                         DISPLAY_SET_C_ADDR(c);
136                         b[0]=DISPLAY_DATA;              // dummy read
137                         b[0]=DISPLAY_DATA;
138                         b[1]=DISPLAY_DATA;
139                         rmax=y+8>y+h?y+h:y+8;
140                         for(r=y;r<rmax;r++) {
141                                 b[0]&=~(1<<r);
142                                 b[1]&=~(1<<r);
143                                 b[0]|=a[0]<<r;
144                                 b[1]|=a[1]<<r;
145                         }
146                         DISPLAY_SET_C_ADDR(c);
147                         DISPLAY_DATA=b[0];
148                         DISPLAY_DATA=b[1];
149                 }
150                 y+=8;
151         }
152 }
153
154 void display_bl_init(void) {
155
156         IODIR0|=(1<<4);
157         IOSET0=(1<<4);  // off by default
158 }
159
160 void display_bl_toggle(void) {
161
162         if(IOPIN0&(1<<4))
163                 IOCLR0=(1<<4);
164         else
165                 IOSET0=(1<<4);
166 }
167
168 void display_bl_on(void) {
169
170         IOCLR0=(1<<4);
171 }
172
173 void display_bl_off(void) {
174
175         IOSET0=(1<<4);
176 }
177