C12832 LCD library

Dependents:   app-board-demo

Fork of C12832_lcd by Peter Drescher

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Thu Jun 05 04:54:25 2014 +0000
Parent:
3:468cdccff7af
Commit message:
Constructor changed

Changed in this revision

C12832_lcd.cpp Show annotated file Show diff for this revision Revisions of this file
C12832_lcd.h Show annotated file Show diff for this revision Revisions of this file
diff -r 468cdccff7af -r 55d53324e7fa C12832_lcd.cpp
--- a/C12832_lcd.cpp	Thu Oct 25 17:34:34 2012 +0000
+++ b/C12832_lcd.cpp	Thu Jun 05 04:54:25 2014 +0000
@@ -14,7 +14,8 @@
 
 // 13.10.12    initial design
 // 25.10.12    add autorefresh of screen
-// 25.10.12    add standart font 
+// 25.10.12    add standart font
+// 20.12.12    add bitmap graphics
 
 // optional defines :
 // #define debug_lcd  1
@@ -26,8 +27,9 @@
 
 #define BPP    1       // Bits per pixel
 
-C12832_LCD::C12832_LCD(const char* name)
-    : _spi(p5,NC,p7),_reset(p6),_A0(p8),_CS(p11),GraphicsDisplay(name)
+
+C12832::C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name)
+    : _spi(mosi,NC,sck),_reset(reset),_A0(a0),_CS(ncs),GraphicsDisplay(name)
 {
     orientation = 1;
     draw_mode = NORMAL;
@@ -35,59 +37,35 @@
     lcd_reset();
 }
 
-int C12832_LCD::width()
+
+int C12832::width()
 {
     if (orientation == 0 || orientation == 2) return 32;
     else return 128;
 }
 
-int C12832_LCD::height()
+int C12832::height()
 {
     if (orientation == 0 || orientation == 2) return 128;
     else return 32;
 }
 
 
-/*void C12832_LCD::set_orientation(unsigned int o)
-{
-    orientation = o;
-    switch (o) {
-        case (0):
-            wr_cmd(0xA0);
-            wr_cmd(0xC0);
-            break;
-        case (1):
-            wr_cmd(0xA0);
-            wr_cmd(0xC8);
-            break;
-        case (2):
-            wr_cmd(0xA1);
-            wr_cmd(0xC8);
-            break;
-        case (3):
-            wr_cmd(0xA1);
-            wr_cmd(0xC0);
-            break;
-    }
-}
-
-*/
-
-void C12832_LCD::invert(unsigned int o)
+void C12832::invert(unsigned int o)
 {
     if(o == 0) wr_cmd(0xA6);
     else wr_cmd(0xA7);
 }
 
 
-void C12832_LCD::set_contrast(unsigned int o)
+void C12832::set_contrast(unsigned int o)
 {
     contrast = o;
     wr_cmd(0x81);      //  set volume
     wr_cmd(o & 0x3F);
 }
 
-unsigned int C12832_LCD::get_contrast(void)
+unsigned int C12832::get_contrast(void)
 {
     return(contrast);
 }
@@ -95,44 +73,31 @@
 
 // write command to lcd controller
 
-void C12832_LCD::wr_cmd(unsigned char cmd)
+void C12832::wr_cmd(unsigned char cmd)
 {
     _A0 = 0;
     _CS = 0;
-#if defined TARGET_LPC1768     // fast without mbed lib
-    LPC_SSP1->DR = cmd;
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
-#else
     _spi.write(cmd);
-#endif
-_CS = 1;
+    _CS = 1;
 }
 
 // write data to lcd controller
 
-void C12832_LCD::wr_dat(unsigned char dat)
+void C12832::wr_dat(unsigned char dat)
 {
     _A0 = 1;
     _CS = 0;
-#if defined TARGET_LPC1768     // fast without mbed lib
-    LPC_SSP1->DR = dat;
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
-#else
     _spi.write(dat);
-#endif
     _CS = 1;
 }
 
 // reset and init the lcd controller
 
-void C12832_LCD::lcd_reset()
+void C12832::lcd_reset()
 {
 
     _spi.format(8,3);                 // 8 bit spi mode 3
-    _spi.frequency(20000000);          // 19,2 Mhz SPI clock
-    DigitalOut _reset(p6);
+    _spi.frequency(18000000);          // 19,2 Mhz SPI clock
     _A0 = 0;
     _CS = 1;
     _reset = 0;                        // display reset
@@ -159,26 +124,20 @@
 
     wr_cmd(0xA6);     // display normal
 
-    
-#if defined TARGET_LPC1768          //setup DMA channel 0       
-    LPC_SC->PCONP |= (1UL << 29);   // Power up the GPDMA
-    LPC_GPDMA->DMACConfig = 1;      // enable DMA controller
-    LPC_GPDMA->DMACIntTCClear = 0x1;
-    LPC_GPDMA->DMACIntErrClr = 0x1;
-    LPC_GPDMACH0->DMACCLLI = 0;
-#endif
+
     // clear and update LCD
     memset(buffer,0x00,512);  // clear display buffer
     copy_to_lcd();
     auto_up = 1;              // switch on auto update
-    claim(stdout);           // redirekt printf to lcd
+    // dont do this by default. Make the user call
+    //claim(stdout);           // redirekt printf to lcd
     locate(0,0);
-    set_font((unsigned char*)Small_7);  // standart font 
+    set_font((unsigned char*)Small_7);  // standart font
 }
 
 // set one pixel in buffer
 
-void C12832_LCD::pixel(int x, int y, int color)
+void C12832::pixel(int x, int y, int color)
 {
     // first check parameter
     if(x > 128 || y > 32 || x < 0 || y < 0) return;
@@ -196,123 +155,60 @@
 
 // update lcd
 
-void C12832_LCD::copy_to_lcd(void)
+void C12832::copy_to_lcd(void)
 {
-#ifndef TARGET_LPC1768 
-    int i;
-#endif
+    
+    int i=0;
+    
     //page 0
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB0);      // set page address  0
     _A0 = 1;
-#if defined TARGET_LPC1768     
-    _CS = 0;
-    // start 128 byte DMA transfer to SPI1
-    LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
-    LPC_SSP1->DMACR = 0x2;  // Enable SSP1 for DMA.
-    LPC_GPDMA->DMACIntTCClear = 0x1;
-    LPC_GPDMA->DMACIntErrClr = 0x1;
-    LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer);
-    LPC_GPDMACH0->DMACCControl = 128 | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
-    LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
-    LPC_GPDMA->DMACSoftSReq = 0x1;
-    do {
-    } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
-    _CS = 1;
-#else  // no DMA
-    for(i=0;i<128;i++){
+    for(i=0; i<128; i++) {
         wr_dat(buffer[i]);
-        }
-#endif 
+    }
 
     // page 1
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB1);      // set page address  1
     _A0 = 1;
-#if defined TARGET_LPC1768         
-    _CS = 0;
-    // start 128 byte DMA transfer to SPI1
-    LPC_GPDMA->DMACIntTCClear = 0x1;
-    LPC_GPDMA->DMACIntErrClr = 0x1;
-    LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 128);
-    LPC_GPDMACH0->DMACCControl = 128 | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
-    LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
-    LPC_GPDMA->DMACSoftSReq = 0x1;
-    do {
-    } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
-    _CS = 1;
-#else // no DMA
-    for(i=128;i<256;i++){
+    for(i=128; i<256; i++) {
         wr_dat(buffer[i]);
-        }
-#endif     
+    }
 
     //page 2
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB2);      // set page address  2
     _A0 = 1;
-#if defined TARGET_LPC1768      
-    _CS = 0;
-    // start 128 byte DMA transfer to SPI1
-    LPC_GPDMA->DMACIntTCClear = 0x1;
-    LPC_GPDMA->DMACIntErrClr = 0x1;
-    LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 256);
-    LPC_GPDMACH0->DMACCControl = 128 | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
-    LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX ;
-    LPC_GPDMA->DMACSoftSReq = 0x1;
-    do {
-    } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
-    _CS = 1;
-#else // no DMA
-    for(i=256;i<384;i++){
+    for(i=256; i<384; i++) {
         wr_dat(buffer[i]);
-        }
-#endif         
+    }
 
     //page 3
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB3);      // set page address  3
     _A0 = 1;
+
+    _CS = 0;
     
-     _CS = 0;
-#if defined TARGET_LPC1768     
-    // start 128 byte DMA transfer to SPI1
-    LPC_GPDMA->DMACIntTCClear = 0x1;
-    LPC_GPDMA->DMACIntErrClr = 0x1;
-    LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 384);
-    LPC_GPDMACH0->DMACCControl = 128  | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
-    LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
-    LPC_GPDMA->DMACSoftSReq = 0x1;
-    do {
-    } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
-    do {
-    } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
-    _CS = 1;
-#else // no DMA
-    for(i=384;i<512;i++){
+    for(i=384; i<512; i++) {
         wr_dat(buffer[i]);
-        }
-#endif   
+    }
+
 }
 
-void C12832_LCD::cls(void)
+void C12832::cls(void)
 {
     memset(buffer,0x00,512);  // clear display buffer
     copy_to_lcd();
 }
 
 
-void C12832_LCD::line(int x0, int y0, int x1, int y1, int color)
+void C12832::line(int x0, int y0, int x1, int y1, int color)
 {
     int   dx = 0, dy = 0;
     int   dx_sym = 0, dy_sym = 0;
@@ -382,7 +278,7 @@
     if(auto_up) copy_to_lcd();
 }
 
-void C12832_LCD::rect(int x0, int y0, int x1, int y1, int color)
+void C12832::rect(int x0, int y0, int x1, int y1, int color)
 {
 
     if (x1 > x0) line(x0,y0,x1,y0,color);
@@ -400,7 +296,7 @@
     if(auto_up) copy_to_lcd();
 }
 
-void C12832_LCD::fillrect(int x0, int y0, int x1, int y1, int color)
+void C12832::fillrect(int x0, int y0, int x1, int y1, int color)
 {
     int l,c,i;
     if(x0 > x1) {
@@ -425,7 +321,7 @@
 
 
 
-void C12832_LCD::circle(int x0, int y0, int r, int color)
+void C12832::circle(int x0, int y0, int r, int color)
 {
 
     int draw_x0, draw_y0;
@@ -533,7 +429,7 @@
     if(auto_up) copy_to_lcd();
 }
 
-void C12832_LCD::fillcircle(int x, int y, int r, int color)
+void C12832::fillcircle(int x, int y, int r, int color)
 {
     int i,up;
     up = auto_up;
@@ -541,15 +437,15 @@
     for (i = 0; i <= r; i++)
         circle(x,y,i,color);
     auto_up = up;
-    if(auto_up) copy_to_lcd();  
+    if(auto_up) copy_to_lcd();
 }
 
-void C12832_LCD::setmode(int mode)
+void C12832::setmode(int mode)
 {
     draw_mode = mode;
 }
 
-void C12832_LCD::locate(int x, int y)
+void C12832::locate(int x, int y)
 {
     char_x = x;
     char_y = y;
@@ -557,21 +453,21 @@
 
 
 
-int C12832_LCD::columns()
+int C12832::columns()
 {
     return width() / font[1];
 }
 
 
 
-int C12832_LCD::rows()
+int C12832::rows()
 {
     return height() / font[2];
 }
 
 
 
-int C12832_LCD::_putc(int value)
+int C12832::_putc(int value)
 {
     if (value == '\n') {    // new line
         char_x = 0;
@@ -586,7 +482,7 @@
     return value;
 }
 
-void C12832_LCD::character(int x, int y, int c)
+void C12832::character(int x, int y, int c)
 {
     unsigned int hor,vert,offset,bpl,j,i,b;
     unsigned char* zeichen;
@@ -628,18 +524,40 @@
 }
 
 
-void C12832_LCD::set_font(unsigned char* f)
+void C12832::set_font(unsigned char* f)
 {
     font = f;
 }
 
-void C12832_LCD::set_auto_up(unsigned int up)
+void C12832::set_auto_up(unsigned int up)
 {
     if(up ) auto_up = 1;
+    else auto_up = 0;
 }
 
-unsigned int C12832_LCD::get_auto_up(void){
+unsigned int C12832::get_auto_up(void)
+{
     return (auto_up);
 }
 
+void C12832::print_bm(Bitmap bm, int x, int y)
+{
+    int h,v,b;
+    char d;
 
+    for(v=0; v < bm.ySize; v++) {   // lines
+        for(h=0; h < bm.xSize; h++) { // pixel
+            if(h + x > 127) break;
+            if(v + y > 31) break;
+            d = bm.data[bm.Byte_in_Line * v + ((h & 0xF8) >> 3)];
+            b = 0x80 >> (h & 0x07);
+            if((d & b) == 0) {
+                pixel(x+h,y+v,0);
+            } else {
+                pixel(x+h,y+v,1);
+            }
+        }
+    }
+
+}
+
diff -r 468cdccff7af -r 55d53324e7fa C12832_lcd.h
--- a/C12832_lcd.h	Thu Oct 25 17:34:34 2012 +0000
+++ b/C12832_lcd.h	Thu Jun 05 04:54:25 2014 +0000
@@ -41,14 +41,24 @@
   */
 enum {NORMAL,XOR};
 
-class C12832_LCD : public GraphicsDisplay
+/** Bitmap
+ */
+struct Bitmap{
+    int xSize;
+    int ySize;
+    int Byte_in_Line;
+    char* data;
+    };
+
+class C12832 : public GraphicsDisplay
 {
 public:
-    /** Create a C12832_LCD object connected to SPI1
+    /** Create a C12832 object connected to SPI1
       *
       */
 
-    C12832_LCD(const char* name = "LCD");
+    C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name = "LCD");
+
 
     /** Get the width of the screen in pixel
       *
@@ -130,12 +140,6 @@
       *
       */
 
-    //void set_orientation(unsigned int o);
-
-    /** set the contrast of the screen
-      *
-      * @param o contrast 0-63
-      */
 
     void set_contrast(unsigned int o);
 
@@ -145,7 +149,6 @@
     unsigned int get_contrast(void);
 
 
-
     /** invert the screen
       *
       * @param o = 0 normal, 1 invert
@@ -164,7 +167,7 @@
 
     void setmode(int mode);
 
-    int columns(void);
+    virtual int columns(void);
 
     /** calculate the max number of columns
      *
@@ -172,7 +175,7 @@
      * depends on actual font size
      *
      */
-    int rows(void);
+    virtual int rows(void);
 
     /** put a char on the screen
      *
@@ -180,7 +183,7 @@
      * @returns printed char
      *
      */
-    int _putc(int value);
+    virtual int _putc(int value);
 
     /** draw a character on given position out of the active font to the LCD
      *
@@ -196,7 +199,7 @@
      * @param x x-position (top left)
      * @param y y-position
      */
-    void locate(int x, int y);
+    virtual void locate(int x, int y);
     
     /** setup auto update of screen 
       *
@@ -204,13 +207,13 @@
       * if switched off the program has to call copy_to_lcd() 
       * to update screen from framebuffer
       */
-    void C12832_LCD::set_auto_up(unsigned int up);
+    void set_auto_up(unsigned int up);
 
     /** get status of the auto update function
       *
       *  @returns if auto update is on
       */
-    unsigned int C12832_LCD::get_auto_up(void);
+    unsigned int get_auto_up(void);
 
     /** Vars     */
     SPI _spi;
@@ -235,7 +238,16 @@
       *
       */
     void set_font(unsigned char* f);
+    
+    /** print bitmap to buffer
+      *
+      * @param bm Bitmap in flash
+      * @param x  x start
+      * @param y  y start 
+      *
+      */
 
+    void print_bm(Bitmap bm, int x, int y);
 
 protected: