draw rectangle assumed fixes/fins (untested!)
[my-code/arm.git] / betty / display.c
index fad0804..ce8fd7c 100644 (file)
  *
  */
 
+/*
+ * some comments on alpha blending!
+ * ################################
+ *
+ * if c is the result of layer a over layer b with opacity alpha:
+ * 
+ * c=alpha*a+(1-alpha)*b and if alpha is a byte (0-255)
+ * => c=alpha*a+(255-alpha)*b
+ * 
+ */
+
 #include "display.h"
+#include "system.h"
+#include "default_logo.h"
+#include "uart.h"
 
 /*
  * functions
  */
 
-void display_clear_screen(void) {
-
-       u32 cnt;
+void display_fill_screen(u8 fill) {
+
+       u8 page,width;
+       u8 buf[2];
+
+       buf[0]=0;
+       buf[1]=0;
+
+       switch(fill) {
+               case DISPLAY_FILL_LG:
+                       buf[1]=0xff;
+                       break;
+               case DISPLAY_FILL_DG:
+                       buf[0]=0xff;
+                       break;
+               case DISPLAY_FILL_B:
+                       buf[0]=0xff;
+                       buf[1]=0xff;
+                       break;
+               case DISPLAY_FILL_W:
+               default:
+                       break;
+       }
+
+       for(page=0;page<DISPLAY_PAGE_MAX;page++) {
+               DISPLAY_SET_C_ADDR(0);
+               DISPLAY_SET_PAGE_ADDR(page);
+               for(width=0;width<DISPLAY_DIMX;width++) {
+                       DISPLAY_DATA=buf[0];
+                       DISPLAY_DATA=buf[1];
+               }
+       }
+}
 
-       DISPLAY_SET_PAGE_ADDR(0);
-       DISPLAY_SET_C_ADDR(0);
+void display_clear_screen(void) {
 
-       for(cnt=0;cnt<DISPLAY_RAM_CONTENT;cnt++)
-               DISPLAY_DATA=0x00;
+       display_fill_screen(DISPLAY_FILL_W);
 }
 
 void display_init(void) {
 
-       /* configure the ext mem bank interface */
-       BCFG1=0x00000c42;
-
-       DISPLAY_SOFT_RESET;
-
-       pause(0xffffff);
-
+       /* oscillator, regulator, boost, opamp, contrast */
        DISPLAY_START_OSCILLATOR;
-
-       DISPLAY_SET_POWER(DISPLAY_V_BOOST|DISPLAY_REGULATOR);
-
+       DISPLAY_SET_REGULATOR(7);
+       DISPLAY_SET_CONTRAST(0x38);
+       DISPLAY_SET_CONV_FACTOR(0x01);
+       DISPLAY_SET_POWER(DISPLAY_V_BOOST|DISPLAY_REGULATOR|DISPLAY_OPAMP);
+
+       // gray scale palette
+       DISPLAY_SET_WHITE(0,0,0,0);
+       DISPLAY_SET_LGRAY(5,5,5,5);     // obviously 3, but it's too bright!
+       DISPLAY_SET_DGRAY(7,7,7,7);     // obviously 6, but again too bright!
+       DISPLAY_SET_BLACK(9,9,9,9);
+
+       /* normal mode, display depending ram content */
+       DISPLAY_RAM_CONTENTS_ON;
        display_clear_screen();
+       DISPLAY_NORMAL;                 // 00 -> white, 11 -> black
+       DISPLAY_SET_COM_ODIR_REMAPPED;  // start counting at the top
 
+       /* switch on the display */
        DISPLAY_SET_ON;
 }
 
 void display_load_logo(u8 *src) {
 
-       u32 cnt;
+       u8 *s;
+       u8 page,width;
+
+       s=src;
+       if(s==0)
+               s=default_logo;
+
+       for(page=0;page<DISPLAY_PAGE_MAX;page++) {
+               DISPLAY_SET_C_ADDR(0);
+               DISPLAY_SET_PAGE_ADDR(page);
+               for(width=0;width<DISPLAY_DIMX;width++) {
+                       DISPLAY_DATA=*s++;
+                       DISPLAY_DATA=*s++;
+               }
+       }
+}
+
+#define display_m2i(m,r) (((((m)[0]>>r)&1)<<1)|(((m)[1]>>r)&1))
+
+void display_draw_rectangle(u8 x,u8 y,u8 w,u8 h,u8 fill,u8 alpha) {
+
+       u8 page,col,row,rmax,cnt;
+       u8 d[2];
+       int ca,cb;
+       u8 a,b,c;                       // c = a over b
+
+       a=fill&0x03;
+       ca=0;
+       for(cnt=0;cnt<a;cnt++)          // contribution of a: alpha*a
+               ca+=alpha;
+
+       for(page=(y>>3);page<=((y+h)>>3);page++) {      // page = y/8
+               DISPLAY_SET_PAGE_ADDR(page);
+               rmax=(page+1)<<3;                       // row max
+               if(rmax>y+h)
+                       rmax=y+h;
+               for(col=x;col<x+w;col++) {
+                       DISPLAY_SET_C_ADDR(col);
+                       d[0]=DISPLAY_DATA;              // dummy read (p.16)
+                       d[0]=DISPLAY_DATA;
+                       d[1]=DISPLAY_DATA;
+                       for(row=y;row<rmax;row++) {
+                               // contribution of b: (255-alpha)*b
+                               b=display_m2i(d,row);
+                               cb=0;
+                               for(cnt=0;cnt<b;cnt++)
+                                       cb+=(255-alpha);
+                               // finally there is c
+                               c=(ca+cb)>>8;
+                               d[0]&=~(1<<row);
+                               d[1]&=~(1<<row);
+                               d[0]|=((c>>1)&1)<<row;
+                               d[1]|=(c&1)<<row;
+                       }
+                       DISPLAY_SET_C_ADDR(col);
+                       DISPLAY_DATA=b[0];
+                       DISPLAY_DATA=b[1];
+               }
+               y=rmax;
+       }
+}
 
-       DISPLAY_SET_PAGE_ADDR(0);
-       DISPLAY_SET_C_ADDR(0);
+void display_draw_buf(u8 x,u8 y,u8 w,u8 h,u8 *buf,u8 alpha) {
 
-       for(cnt=0;cnt<DISPLAY_RAM_CONTENT;cnt++)
-               DISPLAY_DATA=src[cnt];
 }
 
 void display_bl_init(void) {
 
-       PINSEL0&=~(1<<9|(1<<8));
        IODIR0|=(1<<4);
+       IOSET0=(1<<4);  // off by default
 }
 
 void display_bl_toggle(void) {