SSD1331 Oled driver library for 96x64 colour Oled display. Demo included in .h file

Dependents:   Oled-SSD1331 PJ12_device

Revision:
3:a4caac512e13
Parent:
2:1204274fad8f
Child:
4:062b4708eb35
--- a/ssd1331.cpp	Fri May 06 21:40:50 2016 +0000
+++ b/ssd1331.cpp	Thu May 12 21:36:11 2016 +0000
@@ -165,8 +165,6 @@
     RegWrite(0x80);
     RegWrite(0xAF);    //display ON
 
-    char_x  = 0;
-    char_y  = 0;
     chr_size = NORMAL;
     cls();
 }
@@ -183,7 +181,7 @@
 
 void ssd1331::cls()
 {
-    unsigned char cmd[5]= {GAC_CLEAR_WINDOW,0,0,width,height};
+    unsigned char cmd[6]= {GAC_CLEAR_WINDOW,0,0,width,height};
     RegWriteM(cmd,5);
     wait_us(500);
     Maxwindow();
@@ -300,7 +298,7 @@
     RegWriteM(cmd, 6);
 }
 
-int ssd1331::toRGB(int R,int G,int B)
+int ssd1331::toRGB(int16_t R,int16_t G,int16_t B)
 {  
     uint16_t c;
     c = R >> 3;
@@ -311,7 +309,7 @@
     return c;
 }
 
-void ssd1331::rect(int x1,int y1,int x2,int y2,unsigned int colorline)
+void ssd1331::rect(int16_t x1,int16_t y1,int16_t x2,int16_t y2,unsigned int colorline)
 {
     if  ( x1 < 0 ) x1 = 0;
     else if  ( x1 > width ) x1 = width;
@@ -454,19 +452,21 @@
     RegWriteM(cmd,6);
 }
 
-void ssd1331::Bitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
+void ssd1331::Bitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, unsigned char color) {
  
-  int16_t i, j, byteWidth = (w + 7) / 8; 
-  for(j=0; j<h; j++) {
-    for(i=0; i<w; i++ ) {
-        if(bitmap[ j * byteWidth + i / 8] & (128 >> (i & 7))) {
-            pixel(x+i, y+j, color);
+    window(x, y, w, h);
+    int16_t i, j, byteWidth = (w + 7) / 8; 
+    for(j=0; j<h; j++) {
+        for(i=0; i<w; i++ ) {
+            if(bitmap[ j * byteWidth + i / 8] & (128 >> (i & 7))) {
+                pixel(x+i, y+j, color);
+            }
         }
     }
-  }
+    Maxwindow();
 }
 
-void ssd1331::Bitmap16( unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned char *bitmap)
+void ssd1331::Bitmap16( int16_t x, int16_t y, int16_t w, int16_t h, unsigned char *bitmap)
 {
     unsigned int  i,j;
     int padd;
@@ -489,9 +489,10 @@
         bitmap_ptr -= 2*w;
         bitmap_ptr -= padd;
     }
+    Maxwindow();
 }
 
-int ssd1331::Bitmap16SD( unsigned int x, unsigned int y, unsigned char *Name_BMP)
+int ssd1331::Bitmap16SD( int16_t x, int16_t y, unsigned char *Name_BMP)
 {   
     #define OffsetPixelWidth    18
     #define OffsetPixelHeigh    22
@@ -535,8 +536,8 @@
         fclose(Image);
         return(-3);      // to big
     }
+    
     start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
-
     
     // obtain file size:
     fseek (Image , 0 , SEEK_END);
@@ -544,25 +545,26 @@
     rewind (Image);
     
     // allocate memory to contain the whole file:
-    buffer = (char*) malloc (sizeof(char)*fileSize);
-    if (buffer == NULL) {return (-4);}
+    buffer = (char*) malloc (sizeof(char)*fileSize/2);
+    if (buffer == NULL) {return (fileSize/2);}
     
     // copy the file into the buffer:
     fseek (Image, start_data , SEEK_SET );  // set SD file data start position    
     result = fread (buffer,1,fileSize,Image);
     fclose (Image);
-    if (result != fileSize) {return (-5);}
+    if (result != fileSize) {return (-5);}   
     
-    unsigned short *bitmap_ptr = (unsigned short *)buffer;
-        
-    window(x, y, PixelWidth, PixelHeigh);    
+    unsigned short *bitmap_ptr = (unsigned short *)buffer;    
+          
     // the lines are padded to multiple of 4 bytes in a bitmap
     padd = -1;
     do {
         padd ++;
     } while (2*(PixelWidth + padd)%4 != 0);
-    
+     
     bitmap_ptr += ((PixelHeigh - 1)* (PixelWidth + padd));
+   
+    window(x, y, PixelWidth, PixelHeigh);
     
     for (j = 0; j < PixelHeigh; j++) {         //Lines
         for (i = 0; i < PixelWidth; i++) {     // one line
@@ -574,9 +576,80 @@
     }
     // terminate
     free (buffer);
+    Maxwindow();
     return (fileSize);
 }
 
+int ssd1331::Bitmap16SD2(int16_t x, int16_t y, unsigned char *Name_BMP) {
+#define OffsetPixelWidth    18
+#define OffsetPixelHeigh    22
+#define OffsetFileSize      34
+#define OffsetPixData       10
+#define OffsetBPP           28
+    char filename[50];
+    unsigned char BMP_Header[54];
+    unsigned short BPP_t;
+    unsigned int PixelWidth,PixelHeigh,start_data;
+    unsigned int    i,off;
+    int padd,j;
+    unsigned short *line;
+    
+    i=0;
+    while (*Name_BMP!='\0') {
+        filename[i++]=*Name_BMP++;
+    } 
+    
+    filename[i] = 0; 
+    FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
+    if (!Image) {
+        return(0);      // error file not found !
+    }
+    
+    fread(&BMP_Header[0],1,54,Image);      // get the BMP Header
+
+    if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
+        fclose(Image);
+        return(-1);     // error no BMP file
+    }
+    BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
+    if (BPP_t != 0x0010) {
+        fclose(Image);
+        return(-2);     // error no 16 bit BMP
+    }
+    PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
+    PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
+    if (PixelHeigh > height+1 + y || PixelWidth > width+1 + x) {
+        fclose(Image);
+        return(-3);      // to big
+    }
+    start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
+
+    line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line
+    if (line == NULL) {
+        return(-4);         // error no memory
+    }
+    // the lines are padded to multiple of 4 bytes
+    padd = -1;
+    do {
+        padd ++;
+    } while ((PixelWidth * 2 + padd)%4 != 0);
+    
+    window(x, y,PixelWidth,PixelHeigh);
+   
+    for (j = PixelHeigh - 1; j >= 0; j--) {               //Lines bottom up
+        off = j * (PixelWidth * 2 + padd) + start_data;   // start of line
+        fseek(Image, off ,SEEK_SET);
+        fread(line,1,PixelWidth * 2,Image);       // read a line - slow !
+        for (i = 0; i < PixelWidth; i++) {        // copy pixel data to TFT
+             DataWrite_to(line[i]);
+        } 
+    }
+    free (line);
+    fclose(Image);
+    Maxwindow();
+    return(PixelWidth);
+}
+
 void ssd1331::Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y)
 {
     unsigned char cmd[8]= { 0 };
@@ -598,7 +671,7 @@
     fillrect(0,0,width,height,color,color);
 }
 
-void ssd1331::locate(int column, int row)
+void ssd1331::locate(int16_t column, int16_t row)
 {
     char_x  = column;
     char_y = row;
@@ -638,7 +711,7 @@
     return c;
 }
 
-void ssd1331::pixel(int x,int y,unsigned int Color)
+void ssd1331::pixel(int16_t x,int16_t y,unsigned int Color)
 {
     unsigned char cmd[7]= {Set_Column_Address,0x00,0x00,Set_Row_Address,0x00,0x00};
     if ((x<0)||(y<0)||(x>width)||(y>height)) return ;
@@ -652,31 +725,34 @@
 
 void ssd1331::Maxwindow()
 {
-    RegWrite(0x75);    /* Set Row Address */
-    RegWrite(0);    /* Start = 0 */
-    RegWrite(0x3F);    /* End = 63 */
-    RegWrite(0x15);    /* Set Column Address */
-    RegWrite(0);    /* Start = 0 */
-    RegWrite(0x5F);    /* End = 95 */
+    
+    unsigned char cmd[7]= {Set_Column_Address,0x00,0x5F,Set_Row_Address,0x00,0x3F};
+    RegWriteM(cmd, 6);
+ /*   
+    RegWrite(0x75);    // Set Row Address 
+    RegWrite(0);    // Start = 0 
+    RegWrite(0x3F);    // End = 63 
+    RegWrite(0x15);    // Set Column Address 
+    RegWrite(0);    // Start = 0 
+    RegWrite(0x5F);    // End = 95
     char_x  = 0;
     char_y  = 0;
+    */
 }
 
-void ssd1331::window(int x, int y, int w, int h)
+void ssd1331::window(int16_t x, int16_t y, int16_t w, int16_t h)
 {
     _x = x;
     _y = y;
     // window settings
-    _x1 = x;
-    _x2 = x + w - 1;
-    _y1 = y;
-    _y2 = y + h - 1;
-    RegWrite(0x75); // Set Row Address 
-    RegWrite(_y);   // Start y
-    RegWrite(_y2);  // End y
-    RegWrite(0x15); // Set Column Address 
-    RegWrite(_x);   // Start = x
-    RegWrite(_x2);  // End = x    
+    _x1 = x;            // start y
+    _x2 = x + w - 1;    // end x
+    _y1 = y;            // start y
+    _y2 = y + h - 1;    // end y
+    unsigned char cmd[7]= {Set_Column_Address,0x00,0x00,Set_Row_Address,0x00,0x00};
+    RegWriteM(cmd, 6);
+    unsigned char cmd2[7]= {Set_Column_Address,_x1,_x2,Set_Row_Address,_y1,_y2};
+    RegWriteM(cmd2, 6);
 }
 
 void ssd1331::Scrollstart()
@@ -765,3 +841,4 @@
     spi.write((unsigned char)(Dat));
     CS = 1;    // CS dissable
 }
+