Fork of KS0108 by Ryan Tseng

Revision:
1:741ceb02f8e2
Parent:
0:14a7c22dfbe8
--- a/KS0108.cpp	Mon Jun 18 16:46:41 2012 +0000
+++ b/KS0108.cpp	Mon Apr 14 18:30:31 2014 +0000
@@ -12,7 +12,6 @@
     E.write(0);                       
     ClearScreen();                      //clear display
     WriteInstruction(LCD_ON, BOTH);     //turn on lcd  
-    Inverted = 0;     
 }
 
 
@@ -49,76 +48,66 @@
 }
 
 void KS0108::WriteData(unsigned int data) {
-    unsigned int displayData, yOffset, chip;
+    unsigned int displayData = 0, yOffset, chip;
     
-    if(Coord.x >= SCREEN_WIDTH)
-        return;
-     chip = Coord.x/CHIP_WIDTH; 
-     wait(0.000000450); // 300ns     
+    //wait(0.000000450); // 300ns     
+    if(Coord.x % CHIP_WIDTH == 0 && chip > 0){
+        GotoXY(Coord.x, Coord.y);
+    }
 
-     if(Coord.x % CHIP_WIDTH == 0 && chip > 0){         
-     GotoXY(Coord.x, Coord.y);
-     }
-
-    DI.write(1);                    // D/I = 1
-    RW.write(0);                      // R/W = 0    
-    DB.output();                    // data port is output
-    
     yOffset = Coord.y%8;
 
     if(yOffset != 0) {                 // first page
 
-    displayData = ReadData();
+        GotoXY(Coord.x, Coord.y);
+        displayData = ReadData();
+        displayData = ReadData();
+        GotoXY(Coord.x, Coord.y);
+    
+        DI.write(1);                            // D/I = 1
+        RW.write(0);                              // R/W = 0    
+        DB.output();
+                            // data port is output             
+        displayData |= (data << yOffset) & 0xff;
+        DB.write(displayData);                     // write data
+        wait(0.0000003);                         // 300ns
+        E.write(1);
+        wait(0.0000001);
+        E.write(0);
 
-    DI.write(1);                            // D/I = 1
-    RW.write(0);                              // R/W = 0    
-    SelectSide(chip);    
-    DB.output();
-                                            // data port is output             
-    displayData |= data << yOffset;
-    if(Inverted)    displayData = ~displayData;
-    DB.write(displayData);                     // write data
-    wait(0.0000003);                         // 300ns
-    E.write(1);
-    wait(0.0000001);
-    E.write(0);
-        
-                                    // second page
-    GotoXY(Coord.x, Coord.y+8);
+        // second page
+        GotoXY(Coord.x, Coord.y+8);
+        displayData = ReadData();
+        displayData = ReadData();
+        GotoXY(Coord.x, Coord.y);
+    
+        DI.write(1);                            // D/I = 1
+        RW.write(0);                              // R/W = 0    
+        DB.output();                // data port is output
+
+        displayData |= data >> (8-yOffset);
+
+        DB.write(displayData);        // write data
         
-    displayData = ReadData();
-
-    DI.write(1);                            // D/I = 1
-    RW.write(0);                              // R/W = 0    
-    SelectSide(chip);
+        wait(0.0000003);             // 300ns
+        E.write(1);
+        wait(0.0000001);
+        E.write(0);
 
-    DB.output();                // data port is output
-        
-    displayData |= data >> (8-yOffset);
-    
-        if(Inverted)
-            displayData = ~displayData;
-            DB.write(displayData);        // write data
-           
-            wait(0.0000003);             // 300ns
-            E.write(1);
-            wait(0.0000001);
-            E.write(0);
-        
         GotoXY(Coord.x+1, Coord.y-8);
     }else    {
+        DI.write(1);                    // D/I = 1
+        RW.write(0);                    // R/W = 0    
+        DB.output();                    // data port is output
 
-        // just this code gets executed if the write is on a single page
-        if(Inverted)
-            data = ~data;      
-        wait(0.0000003);                 // 300nsEN_DELAY();
         DB.write(data);                    // write data
         wait(0.0000003);                 // 300ns
-           E = 1;
-           wait(0.0000001);
-        E = 0;
+        E.write(1);
+        wait(0.0000001);
+        E.write(0);
         Coord.x++; 
     }
+    return;
 }
 
 
@@ -129,40 +118,39 @@
     page    = page%8;
 
     if(col<(SCREEN_WIDTH/2)){
-    SelectSide(LEFT);           
-    WriteInstruction(LCD_SET_PAGE|page,LEFT);     
-    WriteInstruction(LCD_SET_ADD|col,LEFT);          //set page and column position
-    WriteData(data,LEFT);                            //output data to D0-D7
+        SelectSide(LEFT);           
+        WriteInstruction(LCD_SET_PAGE|page,LEFT);     
+        WriteInstruction(LCD_SET_ADD|col,LEFT);          //set page and column position
+        WriteData(data,LEFT);                            //output data to D0-D7
     }else{
-    
-    SelectSide(RIGHT);
-    col -= (SCREEN_WIDTH/2);     
-    WriteInstruction(LCD_SET_PAGE|page,RIGHT);     
-    WriteInstruction(LCD_SET_ADD|col,RIGHT);        //set page and column position
-    WriteData(data,RIGHT);                          //output data to D0-D7
+        SelectSide(RIGHT);
+        col -= (SCREEN_WIDTH/2);     
+        WriteInstruction(LCD_SET_PAGE|page,RIGHT);     
+        WriteInstruction(LCD_SET_ADD|col,RIGHT);        //set page and column position
+        WriteData(data,RIGHT);                          //output data to D0-D7
     }    
-                           
+
     SelectSide(NONE);
 }
 
 
 
 unsigned int KS0108::ReadData(){
-   unsigned int data;
-   DB.input();
+    unsigned int data = 0;
+    DB.input();
 
-   DI.write(1);     
-   RW.write(1);
+    DI.write(1);     
+    RW.write(1);
 
-   E.write(1);
-   wait(0.00000045);
-      
-   data = DB.read();
-   wait(0.0000001);
-   E.write(0);   
-   DB.output();
-   
-   return data;     
+    E.write(1);
+    wait(0.00000045);
+
+    data = DB.read();
+    E.write(0);   
+    wait(0.0000001);
+    DB.output();
+
+    return data;     
 }
 
 unsigned int KS0108::ReadStatus(){
@@ -204,6 +192,7 @@
             WriteDataColPag(page,col,WHITE);
         }
     }
+    GotoXY(Coord.x, Coord.y);
 }     
 
 
@@ -264,15 +253,6 @@
             SetPixel(i,j,color);
         } 
     }   
-}
-
-void KS0108::ReverseFullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color){
-           
-    for(unsigned int i=Xaxis2;i<=Xaxis1;i--){
-        for(unsigned int j=Yaxis2;j<=Yaxis1;j--){
-            SetPixel(i,j,color);
-        } 
-    }   
 }  
 
 
@@ -686,23 +666,20 @@
 void KS0108::GotoXY(unsigned int x, unsigned int y) {
   unsigned int chip, cmd;
     
-  if( (x > SCREEN_WIDTH-1) || (y > SCREEN_HEIGHT-1) )    // exit if coordinates are not legal
-    return;
-  Coord.x = x;                                    // save new coordinates
-  Coord.y = y;
-    
-  if(y/8 != Coord.page) {
-      Coord.page = y/8;
-    cmd = LCD_SET_PAGE | Coord.page;            // set y address on all chips    
+    Coord.x = x%SCREEN_WIDTH;                                    // save new coordinates
+    Coord.y = y%SCREEN_HEIGHT;
+
+    Coord.page = y/8;
+    cmd = LCD_SET_PAGE | Coord.page;                // set y address on all chips    
     for(chip=0; chip < 2; chip++){
-       WriteInstruction(cmd, chip);    
+        WriteInstruction(cmd, chip);    
     }
-  }
-  chip = Coord.x/64;
-  x = x % 64;
-  cmd = LCD_SET_ADD | x;
-  WriteInstruction(cmd, chip);                    // set x address on active chip        
 
+    chip = Coord.x/64;
+    x = x % 64;
+    cmd = LCD_SET_ADD | x;
+    WriteInstruction(cmd, chip);                    // set x address on active chip        
+    return;
 }
 
 
@@ -796,8 +773,7 @@
         }
         // 1px gap between chars
         WriteData(0x00);
-        GotoXY(x,Coord.y+8);
-    
+        GotoXY(x,Coord.y+8);    
     }
     GotoXY(x+width+1, y);
     
@@ -837,7 +813,3 @@
 }
 
 
-
-            
-
-