Test for STM32F4

Dependents:   Nucleo_SSD1331

Fork of ssd1331 by Paul Staron

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Fri May 06 21:40:50 2016 +0000
Parent:
1:f3f6624f45d4
Child:
3:a4caac512e13
Commit message:
Added bitmap functions

Changed in this revision

ssd1331.cpp Show annotated file Show diff for this revision Revisions of this file
ssd1331.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/ssd1331.h	Sun May 01 12:37:44 2016 +0000
+++ b/ssd1331.h	Fri May 06 21:40:50 2016 +0000
@@ -36,6 +36,27 @@
 #define SCROLL_STOP             0x2E    // Scroll Stop
 #define SCROLL_START            0x2F    // Scroll Start
 
+// some RGB color definitions         RED GREEN BLUE                          
+
+#define Black           0x0000      //   0,   0,   0 
+#define Navy            0x8000      //   0,   0, 128 
+#define DarkGreen       0x0400      //   0, 128,   0 
+#define DarkCyan        0x8400      //   0, 128, 128 
+#define Maroon          0x0010      // 128,   0,   0 
+#define Purple          0x8010      // 128,   0, 128 
+#define Olive           0x0410      // 128, 128,   0 
+#define LightGrey       0xC618      // 192, 192, 192 
+#define DarkGrey        0x7BEF      // 128, 128, 128 
+#define Red             0xF800      // 255,   0,   0 
+#define Green           0x07E0      //   0, 255,   0 
+#define Yellow          0x07FF      // 255, 255,   0 
+#define Blue            0x001F      //   0,   0, 255 
+#define Magenta         0xF81F      // 255,   0, 255 
+#define Cyan            0xFFE0      //   0, 255, 255 
+#define White           0xFFFF      // 255, 255, 255 
+#define Orange          0x053F      // 255, 165,   0 
+#define GreenYellow     0x2FF5      // 173, 255,  47 
+
 // example code
 /*
 #include "mbed.h"
@@ -138,8 +159,8 @@
     ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
 
     void pixel(int x,int y,unsigned int color);
-    void rectangle(int x1,int y1,int x2,int y2,unsigned int colorline);
-    void fillrectangle(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill);
+    void rect(int x1,int y1,int x2,int y2,unsigned int colorline);
+    void fillrect(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill);
     void line( int x1,int y1,int x2,int y2,unsigned int color);
     void circle (int radius, int x, int y,unsigned int color,int fill);
     void Fill_Screen(unsigned int color); // fill screen with any colour
@@ -161,10 +182,9 @@
     void Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y);
     void character(int x, int y, int c);
     void set_font(unsigned char* f);
-    void drawBitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
-    int BMP_16(const char *Name_BMP, unsigned int x, unsigned int y);
-
-    int DrawBitmapFile(const char *Name_BMP);
+    void Bitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
+    void Bitmap16(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
+    int Bitmap16SD( unsigned int x, unsigned int y, unsigned char *Name_BMP); // 12.288k
 
     
 protected:
@@ -185,6 +205,7 @@
     unsigned int Char_Color;    // text color
     unsigned int BGround_Color; // background color
     void window(int x, int y, int w, int h);
+    void Maxwindow(); // reset display window to full size
     // pixel location
     short _x;
     short _y;
@@ -202,6 +223,7 @@
     int externalfont;
     DigitalOut  CS,  RES,  DC;
     SPI spi; // mosi, miso, sclk
+    
 };
 
 #endif