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

Dependents:   Oled-SSD1331 PJ12_device

Revision:
2:1204274fad8f
Parent:
1:f3f6624f45d4
Child:
3:a4caac512e13
--- a/ssd1331.cpp	Sun May 01 12:37:44 2016 +0000
+++ b/ssd1331.cpp	Fri May 06 21:40:50 2016 +0000
@@ -112,7 +112,7 @@
 void ssd1331::Init(void)
 {
     spi.format(8,3);
-    spi.frequency(48000000);  // 24Mhz suggested but works at 48MHz
+    spi.frequency(24000000);  // 12Mhz max for KL25z
 
     // reset
     wait_ms(200);
@@ -186,6 +186,7 @@
     unsigned char cmd[5]= {GAC_CLEAR_WINDOW,0,0,width,height};
     RegWriteM(cmd,5);
     wait_us(500);
+    Maxwindow();
     background(0);
 }
 
@@ -300,7 +301,7 @@
 }
 
 int ssd1331::toRGB(int R,int G,int B)
-{
+{  
     uint16_t c;
     c = R >> 3;
     c <<= 6;
@@ -310,7 +311,7 @@
     return c;
 }
 
-void ssd1331::rectangle(int x1,int y1,int x2,int y2,unsigned int colorline)
+void ssd1331::rect(int x1,int y1,int x2,int y2,unsigned int colorline)
 {
     if  ( x1 < 0 ) x1 = 0;
     else if  ( x1 > width ) x1 = width;
@@ -340,7 +341,7 @@
     wait_us(500);
 }
 
-void ssd1331::fillrectangle(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill)
+void ssd1331::fillrect(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill)
 {
     if  ( x1 < 0 ) x1 = 0;
     else if  ( x1 > width ) x1 = width;
@@ -452,7 +453,8 @@
     cmd[5] = frame_interval;
     RegWriteM(cmd,6);
 }
-void ssd1331::drawBitmap( 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, uint16_t color) {
  
   int16_t i, j, byteWidth = (w + 7) / 8; 
   for(j=0; j<h; j++) {
@@ -464,81 +466,115 @@
   }
 }
 
-int ssd1331::BMP_16(const char *Name_BMP, unsigned int x, unsigned int y)
-{ 
-#define OffsetPixelWidth    18
-#define OffsetPixelHeigh    22
-#define OffsetFileSize      34
-#define OffsetPixData       10
-#define OffsetBPP           28
+void ssd1331::Bitmap16( unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned char *bitmap)
+{
+    unsigned int  i,j;
+    int padd;
+    unsigned short *bitmap_ptr = (unsigned short *)bitmap;
+    
+    window(x, y, w, h);    
+    // the lines are padded to multiple of 4 bytes in a bitmap
+    padd = -1;
+    do {
+        padd ++;
+    } while (2*(w + padd)%4 != 0);
+    window(x, y, w, h);
+    bitmap_ptr += ((h - 1)* (w + padd));
+    
+    for (j = 0; j < h; j++) {         //Lines
+        for (i = 0; i < w; i++) {     // one line
+                DataWrite_to(*bitmap_ptr);
+                bitmap_ptr++;
+        }
+        bitmap_ptr -= 2*w;
+        bitmap_ptr -= padd;
+    }
+}
 
+int ssd1331::Bitmap16SD( unsigned int x, unsigned int 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;
- 
-    // get the filename
+    char * buffer;
+    size_t result;
+    int fileSize,padd,i,j;
+     
     i=0;
     while (*Name_BMP!='\0') {
         filename[i++]=*Name_BMP++;
-    }
-    filename[i] = 0;  
-
+    } 
+    
+    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
+        return(-1);     // error not 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
+        return(-2);     // error not 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 + y || PixelWidth > width + x) {
+    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 (2 * PixelHeigh); // we need a buffer for a line
-    if (line == NULL) {
-        return(-4);         // error no memory
-    }
-    // the bmp lines are padded to multiple of 4 bytes
+
+    
+    // obtain file size:
+    fseek (Image , 0 , SEEK_END);
+    fileSize = ftell (Image)-start_data;
+    rewind (Image);
+    
+    // allocate memory to contain the whole file:
+    buffer = (char*) malloc (sizeof(char)*fileSize);
+    if (buffer == NULL) {return (-4);}
+    
+    // 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);}
+    
+    unsigned short *bitmap_ptr = (unsigned short *)buffer;
+        
+    window(x, y, PixelWidth, PixelHeigh);    
+    // the lines are padded to multiple of 4 bytes in a bitmap
     padd = -1;
     do {
         padd ++;
-    } while ((PixelHeigh * 2 + padd)%4 != 0);
- 
-    window(x, y,PixelWidth ,PixelHeigh);
-        
-    for (j = PixelWidth - 1; j >= 0; j--) {               //Lines bottom up
-        off = j * (PixelHeigh  * 2 + padd) + start_data;   // start of line
-        fseek(Image, off ,SEEK_SET);
-        fread(line,1,PixelHeigh * 2,Image);       // read a line - slow 
-        for (i = 0; i < PixelHeigh; i++) {        // copy pixel data to TFT
+    } while (2*(PixelWidth + padd)%4 != 0);
+    
+    bitmap_ptr += ((PixelHeigh - 1)* (PixelWidth + padd));
     
-            DataWrite(line[i]);
+    for (j = 0; j < PixelHeigh; j++) {         //Lines
+        for (i = 0; i < PixelWidth; i++) {     // one line
+                DataWrite_to(*bitmap_ptr);
+                bitmap_ptr++;
         }
-     }
-    free (line);
-    fclose(Image);
-    window(0,0,width,height);
-    return(1);
+        bitmap_ptr -= 2*PixelWidth ;
+        bitmap_ptr -= padd;
+    }
+    // terminate
+    free (buffer);
+    return (fileSize);
 }
 
 void ssd1331::Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y)
@@ -559,7 +595,7 @@
 void ssd1331::Fill_Screen(unsigned int color)
 {
     BGround_Color = color;
-    fillrectangle(0,0,width,height,color,color);
+    fillrect(0,0,width,height,color,color);
 }
 
 void ssd1331::locate(int column, int row)
@@ -614,9 +650,20 @@
     DataWrite_to(Color);
 }
 
+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 */
+    char_x  = 0;
+    char_y  = 0;
+}
+
 void ssd1331::window(int x, int y, int w, int h)
 {
-    // current pixel location
     _x = x;
     _y = y;
     // window settings
@@ -624,6 +671,12 @@
     _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    
 }
 
 void ssd1331::Scrollstart()
@@ -707,7 +760,7 @@
 void  ssd1331::DataWrite_to(unsigned int Dat)
 {
     DC = 1;    // DATA
-    CS = 0;    // CS enable
+    CS = 0;    // CS enable    
     spi.write((unsigned char)((Dat >> 8)));
     spi.write((unsigned char)(Dat));
     CS = 1;    // CS dissable