SG12864A

Dependents:   SG12864A_TestProgram

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Tue Jul 20 03:59:17 2010 +0000
Parent:
0:238f2d048222
Child:
2:91c03e41c927
Commit message:

Changed in this revision

SG12864A.cpp Show annotated file Show diff for this revision Revisions of this file
SG12864A.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SG12864A.cpp	Mon Jul 19 12:43:25 2010 +0000
+++ b/SG12864A.cpp	Tue Jul 20 03:59:17 2010 +0000
@@ -7,6 +7,10 @@
 
 #include "SG12864A.h"
 
+#define setPixel(x,y) buffer[((COLUMNS * 2)* (y / 8)) + x] |= (1 << (y % 8))
+#define unsetPixel(x,y) buffer[((COLUMNS * 2)* (y / 8)) + x] &= ~(1 << (y % 8))
+#define swap(a,b) {int c=a;a=b;b=c;}
+
 SG12864A::SG12864A(PinName di,
                    PinName rw,
                    PinName en,
@@ -29,22 +33,101 @@
         ioCS1(cs1),
         ioCS2(cs2),
         ioRES(res) {
+    bufferClear();
     setDirectionToWrite();
 }
 
 SG12864A::~SG12864A() {
 }
 
+void SG12864A::bufferPush(void) {
+    for (uint8_t i = 0; i < PAGES; i++) {
+        // CS1
+        setPageAddress(SG12864A::CS1, i);
+        setColumnAddress(SG12864A::CS1, 0);
+        for (uint8_t j = 0; j < COLUMNS; j++) {
+            writeData(CS1, buffer[0 + ((COLUMNS * 2) * i) + j]);
+        }
+        // CS2
+        setPageAddress(SG12864A::CS2, i);
+        setColumnAddress(SG12864A::CS2, 0);
+        for (uint8_t j = 0; j < COLUMNS; j++) {
+            writeData(CS2, buffer[COLUMNS + ((COLUMNS * 2) * i) + j]);
+        }
+    }
+}
+
+void SG12864A::bufferPull(void) {
+}
+
+void SG12864A::bufferClear(void) {
+    for (int i = 0; i < sizeof(buffer); i++) {
+        buffer[i] = 0x00;
+    }
+}
+
+void SG12864A::bufferDrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
+    /*
+     * Bresenham's line algorithm
+     */
+    bool steep = abs(y2 - y1) > abs(x2 - x1);
+    if (steep) {
+        swap(x1, y1);
+        swap(x2, y2);
+    }
+    if (x1 > x2) {
+        swap(x1, x2);
+        swap(y1, y2);
+    }
+    int deltax = x2 - x1;
+    int deltay = abs(y2 - y1);
+    int error = deltax / 2;
+    int ystep;
+    int y = y1;
+    if (y1 < y2) {
+        ystep = 1;
+    } else {
+        ystep = -1;
+    }
+    for (int x = x1; x <= x2; x++) {
+        if (steep) {
+            setPixel(y,x);
+        } else {
+            setPixel(x,y);
+        }
+        error = error - deltay;
+        if (error < 0) {
+            y = y + ystep;
+            error = error + deltax;
+        }
+    }
+}
+
+void SG12864A::bufferDrawBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
+    for (int x = x1; x <= x2; x++) {
+        setPixel(x, y1);
+        setPixel(x, y2);
+    }
+    for (int y = y1; y <= y2; y++) {
+        setPixel(x1, y);
+        setPixel(x2, y);
+    }
+}
+
+void SG12864A::bufferFillBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
+    for (int x = x1; x <= x2; x++) {
+        for (int y = y1; y <= y2; y++) {
+            setPixel(x, y);
+        }
+    }
+}
+
 /**
  * High Level Interface.
  *
  * Reset display module.
  */
 void SG12864A::reset(void) {
-    setDisplayOnOff(SG12864A::CS1, false);
-    setDisplayOnOff(SG12864A::CS2, false);
-    clear();
-
     reset(true);
     wait_ms(200);
     reset(false);
--- a/SG12864A.h	Mon Jul 19 12:43:25 2010 +0000
+++ b/SG12864A.h	Tue Jul 20 03:59:17 2010 +0000
@@ -10,6 +10,8 @@
 
 #include "mbed.h"
 
+/**
+ */
 class SG12864A {
 public:
     SG12864A(
@@ -32,8 +34,16 @@
         CS1,
         CS2
     };
+    void bufferPush(void);
+    void bufferPull(void);
+    void bufferClear(void);
+    void bufferDrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
+    void bufferDrawBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
+    void bufferFillBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
+
     void reset(void);
     void clear(void);
+    
     void setDisplayOnOff(Target t, bool on);
     void setDisplayStartLine(Target t, uint8_t displayStartLine);
     void setPageAddress(Target t, uint8_t addr);
@@ -41,9 +51,12 @@
     void readStatus(Target t, uint8_t *c);
     void writeData(Target t, uint8_t c);
     void readData(Target t, uint8_t *c);
+    static const int PIXEL_X = 128;
+    static const int PIXEL_Y = 64;
 private:
     static const int PAGES = 8;
     static const int COLUMNS = 64;
+    uint8_t buffer[PAGES * COLUMNS * 2];
     DigitalOut ioDI;
     DigitalOut ioRW;
     DigitalOut ioEN;
--- a/main.cpp	Mon Jul 19 12:43:25 2010 +0000
+++ b/main.cpp	Tue Jul 20 03:59:17 2010 +0000
@@ -11,7 +11,8 @@
 int main() {
 
     SG12864A lcd(p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18);
-
+#if 0
+    lcd.clear();
     lcd.reset();
 
     {
@@ -37,4 +38,26 @@
         n++;
         wait_ms(100);
     }
+#else
+    lcd.clear();
+    lcd.reset();
+    while (1) {
+        //
+        lcd.bufferClear();
+        lcd.bufferDrawLine(0, 0, SG12864A::PIXEL_X - 1, SG12864A::PIXEL_Y - 1);
+        lcd.bufferDrawLine(0, SG12864A::PIXEL_Y - 1, SG12864A::PIXEL_X - 1, 0);
+        lcd.bufferPush();
+        wait_ms(1000);
+        //
+        lcd.bufferClear();
+        lcd.bufferDrawBox(0, 0, SG12864A::PIXEL_X - 1, SG12864A::PIXEL_Y - 1);
+        lcd.bufferPush();
+        wait_ms(1000);
+        //
+        lcd.bufferClear();
+        lcd.bufferFillBox(0, 0, SG12864A::PIXEL_X - 1, SG12864A::PIXEL_Y - 1);
+        lcd.bufferPush();
+        wait_ms(1000);
+    }
+#endif
 }