Test for STM32F4

Dependents:   Nucleo_SSD1331

Fork of ssd1331 by Paul Staron

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Wed May 18 22:16:21 2016 +0000
Parent:
3:a4caac512e13
Child:
5:4385fd242db0
Commit message:
Updated BMP 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	Thu May 12 21:36:11 2016 +0000
+++ b/ssd1331.cpp	Wed May 18 22:16:21 2016 +0000
@@ -198,10 +198,10 @@
     }
 }
 
-void ssd1331::PutChar(int column,int row,unsigned int value)
+void ssd1331::PutChar(uint8_t column,uint8_t row, int value)
 {
     if(externalfont) { // external font
-        unsigned int hor,vert,offset,bpl,j,i,b;
+        uint8_t hor,vert,offset,bpl,j,i,b;
         const unsigned char* sign;
         unsigned char z,w;
         // read font parameter from start of array
@@ -298,7 +298,7 @@
     RegWriteM(cmd, 6);
 }
 
-int ssd1331::toRGB(int16_t R,int16_t G,int16_t B)
+uint16_t ssd1331::toRGB(uint16_t R,uint16_t G,uint16_t B)
 {  
     uint16_t c;
     c = R >> 3;
@@ -309,16 +309,12 @@
     return c;
 }
 
-void ssd1331::rect(int16_t x1,int16_t y1,int16_t x2,int16_t y2,unsigned int colorline)
+void ssd1331::rect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline)
 {
-    if  ( x1 < 0 ) x1 = 0;
-    else if  ( x1 > width ) x1 = width;
-    if  ( y1 < 0 ) y1 = 0;
-    else if  ( y1 > height ) y1 = height;
-    if  ( x2 < 0 ) x2 = 0;
-    else if  ( x2 > width ) x2 = width;
-    if  ( y2 < 0 ) y2 = 0;
-    else if  ( y2 > height ) y2 = height;
+    if  ( x1 > width ) x1 = width;
+    if  ( y1 > height ) y1 = height;
+    if  ( x2 > width ) x2 = width;
+    if  ( y2 > height ) y2 = height;
 
     unsigned char cmd[11]= { 0 };
     cmd[0] = GAC_FILL_ENABLE_DISABLE;
@@ -339,16 +335,12 @@
     wait_us(500);
 }
 
-void ssd1331::fillrect(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill)
+void ssd1331::fillrect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline,uint16_t colorfill)
 {
-    if  ( x1 < 0 ) x1 = 0;
-    else if  ( x1 > width ) x1 = width;
-    if  ( y1 < 0 ) y1 = 0;
-    else if  ( y1 > height ) y1 = height;
-    if  ( x2 < 0 ) x2 = 0;
-    else if  ( x2 > width ) x2 = width;
-    if  ( y2 < 0 ) y2 = 0;
-    else if  ( y2 > height ) y2 = height;
+    if  ( x1 > width ) x1 = width;
+    if  ( y1 > height ) y1 = height;
+    if  ( x2 > width ) x2 = width;
+    if  ( y2 > height ) y2 = height;
 
     unsigned char cmd[11]= { 0 };
     cmd[0] = GAC_FILL_ENABLE_DISABLE;
@@ -369,16 +361,12 @@
     wait_us(500);
 }
 
-void ssd1331::line(int x1,int y1,int x2,int y2,unsigned int color)
+void ssd1331::line(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t color)
 {
-    if  ( x1 < 0 ) x1 = 0;
-    else if  ( x1 > width ) x1 = width;
-    if  ( y1 < 0 ) y1 = 0;
-    else if  ( y1 > height ) y1 = height;
-    if  ( x2 < 0 ) x2 = 0;
-    else if  ( x2 > width ) x2 = width;
-    if  ( y2 < 0 ) y2 = 0;
-    else if  ( y2 > height ) y2 = height;
+    if  ( x1 > width ) x1 = width;
+    if  ( y1 > height ) y1 = height;
+    if  ( x2 > width ) x2 = width;
+    if  ( y2 > height ) y2 = height;
 
     unsigned char cmd[11]= { 0 };
     cmd[0] = GAC_FILL_ENABLE_DISABLE;
@@ -396,9 +384,12 @@
     wait_us(500);
 }
 
-void ssd1331::circle (int radius, int x, int y , unsigned int col, int fill)
+void ssd1331::circle (uint8_t radius, uint8_t x, uint8_t y , uint16_t col, uint16_t fill)
 {
-    int  cx, cy, d;
+    if  ( x > width ) x = width;
+    if  ( y > height ) y = height;
+    
+    int16_t  cx, cy, d;
     d = 3 - 2 * radius;
     cy = radius;
     pixel(x, radius+y, col);
@@ -438,7 +429,7 @@
     }
 }
 
-void ssd1331::ScrollSet(int horizontal, int startline, int linecount, int vertical , int frame_interval)
+void ssd1331::ScrollSet(int8_t horizontal, int8_t startline, int8_t linecount, int8_t vertical , int8_t frame_interval)
 {
     unsigned char cmd[7];
     if((startline>height+1)||((startline+linecount)>height+1)) return ;
@@ -452,7 +443,7 @@
     RegWriteM(cmd,6);
 }
 
-void ssd1331::Bitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, unsigned char color) {
+void ssd1331::Bitmap(const uint8_t *bitmap, uint8_t x, uint8_t y, uint8_t w, uint8_t h, unsigned char color) {
  
     window(x, y, w, h);
     int16_t i, j, byteWidth = (w + 7) / 8; 
@@ -466,7 +457,7 @@
     Maxwindow();
 }
 
-void ssd1331::Bitmap16( int16_t x, int16_t y, int16_t w, int16_t h, unsigned char *bitmap)
+void ssd1331::Bitmap16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t  *bitmap)
 {
     unsigned int  i,j;
     int padd;
@@ -478,7 +469,9 @@
     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
@@ -492,7 +485,7 @@
     Maxwindow();
 }
 
-int ssd1331::Bitmap16SD( int16_t x, int16_t y, unsigned char *Name_BMP)
+int ssd1331::Bitmap16RAM(uint8_t x, uint8_t y, unsigned char *Name_BMP)
 {   
     #define OffsetPixelWidth    18
     #define OffsetPixelHeigh    22
@@ -580,7 +573,7 @@
     return (fileSize);
 }
 
-int ssd1331::Bitmap16SD2(int16_t x, int16_t y, unsigned char *Name_BMP) {
+int ssd1331::Bitmap16FS(uint8_t x, uint8_t y, unsigned char *Name_BMP) {
 #define OffsetPixelWidth    18
 #define OffsetPixelHeigh    22
 #define OffsetFileSize      34
@@ -650,7 +643,7 @@
     return(PixelWidth);
 }
 
-void ssd1331::Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y)
+void ssd1331::Copy(uint8_t src_x1,uint8_t src_y1,uint8_t src_x2,uint8_t src_y2,uint8_t dst_x,uint8_t dst_y)
 {
     unsigned char cmd[8]= { 0 };
     if ((src_x1>width)||(src_y1>height)||(src_x2>width)||(src_y2>height)) return;
@@ -665,28 +658,28 @@
     RegWriteM(cmd,7);
 }
 
-void ssd1331::Fill_Screen(unsigned int color)
+void ssd1331::Fill_Screen(uint16_t color)
 {
     BGround_Color = color;
     fillrect(0,0,width,height,color,color);
 }
 
-void ssd1331::locate(int16_t column, int16_t row)
+void ssd1331::locate(uint8_t column, uint8_t row)
 {
     char_x  = column;
     char_y = row;
 }
 
-void ssd1331::foreground(unsigned int color)
+void ssd1331::foreground(uint16_t color)
 {
     Char_Color = color;
 }
-void ssd1331::background(unsigned int color)
+void ssd1331::background(uint16_t color)
 {
     BGround_Color = color;
 }
 
-void ssd1331::SetFontSize(int Csize)
+void ssd1331::SetFontSize(uint8_t Csize)
 {
     chr_size = Csize;
 }
@@ -694,7 +687,6 @@
 void ssd1331::putp(int colour)
 {
     pixel(_x, _y, colour);
-//    wait_us(1000);
     _x++;
     if(_x > _x2) {
         _x = _x1;
@@ -711,10 +703,10 @@
     return c;
 }
 
-void ssd1331::pixel(int16_t x,int16_t y,unsigned int Color)
+void ssd1331::pixel(uint8_t x,uint8_t y,uint16_t 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 ;
+    if ((x>width)||(y>height)) return ;
     cmd[1] = (unsigned char)x;
     cmd[2] = (unsigned char)x;
     cmd[4] = (unsigned char)y;
@@ -724,23 +716,12 @@
 }
 
 void ssd1331::Maxwindow()
-{
-    
+{    
     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(int16_t x, int16_t y, int16_t w, int16_t h)
+void ssd1331::window(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
 {
     _x = x;
     _y = y;
@@ -770,11 +751,11 @@
     return -1;
 }
 
-int ssd1331::row()
+uint8_t ssd1331::row()
 {
     return char_y;
 }
-int ssd1331::column()
+uint8_t ssd1331::column()
 {
     return char_x;
 }
@@ -814,7 +795,7 @@
     CS = 1;     // CS dissable
 }
 
-void  ssd1331::RegWriteM(unsigned char *Command, int count)
+void  ssd1331::RegWriteM(unsigned char *Command, uint8_t count)
 {
     int i;
     DC = 0;     // Command
@@ -833,7 +814,7 @@
     CS = 1;    // CS dissable
 }
 
-void  ssd1331::DataWrite_to(unsigned int Dat)
+void  ssd1331::DataWrite_to(uint16_t Dat)
 {
     DC = 1;    // DATA
     CS = 0;    // CS enable    
@@ -842,3 +823,7 @@
     CS = 1;    // CS dissable
 }
 
+
+
+
+
--- a/ssd1331.h	Thu May 12 21:36:11 2016 +0000
+++ b/ssd1331.h	Wed May 18 22:16:21 2016 +0000
@@ -15,14 +15,14 @@
 #define display_on          0xAF
 #define display_off         0xAE
 
-// Font size
+// Internal Font size settings
 #define NORMAL  0
 #define WIDE    1
 #define HIGH    2
 #define WH      3
 #define WHx36   4
-#define X_width 6           // character's width
-#define Y_height 8           // character's height
+#define X_width 6 
+#define Y_height 8 
 
 
 // GAC hardware acceleration commands
@@ -36,26 +36,18 @@
 #define SCROLL_STOP             0x2E    // Scroll Stop
 #define SCROLL_START            0x2F    // Scroll Start
 
-// some RGB color definitions         RED GREEN BLUE                          
+// Basic RGB color definitions         RED GREEN BLUE values                         
 
 #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 Cyan            0x07FF      //   0, 255, 255 
 #define Blue            0x001F      //   0,   0, 255 
 #define Magenta         0xF81F      // 255,   0, 255 
-#define Cyan            0xFFE0      //   0, 255, 255 
+#define Yellow          0xFFE0      // 255, 255,   0 
 #define White           0xFFFF      // 255, 255, 255 
-#define Orange          0x053F      // 255, 165,   0 
-#define GreenYellow     0x2FF5      // 173, 255,  47 
 
 // example code
 /*
@@ -67,7 +59,7 @@
 char Time[50],Date[50];
 void gettime();
 
-int main() { 
+uint8_t main() { 
 
     while(1){
         
@@ -87,10 +79,10 @@
         oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0);      //circle
         oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line
         oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line
-        oled.rect(10,10,90,60,oled.toRGB(255,255,0));      //rectangle
-        oled.fillrect(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle
+        oled.rectangle(10,10,90,60,oled.toRGB(255,255,0));      //rectangle
+        oled.fillrectangle(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle
         
-        for(int y = 9; y >= 0; y--) {
+        for(uint8_t y = 9; y >= 0; y--) {
              oled.contrast(y);  // set contrast level
              oled.foreground(oled.toRGB(255,255,255)); // set text colour
              oled.locate(1, 10); // set text start location
@@ -158,34 +150,42 @@
     // constructor
     ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
 
-    void pixel(int16_t x,int16_t y,unsigned int color);
-    void rect(int16_t x1,int16_t y1,int16_t x2,int16_t 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
-    void foreground(unsigned int color); // text color
-    void background(unsigned int color); // background color
-    void SetFontSize(int);
+    void pixel(uint8_t x,uint8_t y, uint16_t color); // place a pixel x,y coordinates, color
+    void rect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline); // draw rectangle, start x,y end x,y, color
+    void fillrect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline,uint16_t colorfill); // fill rectangle start x,y, end x,y, outline color, fill color.
+    void line(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t color); // draw line start x,y, end x,y, color
+    void circle (uint8_t radius, uint8_t x, uint8_t y,uint16_t color,uint16_t fill); // draw circle radius, position x,y, color
+    void Fill_Screen(uint16_t color); // fill screen with any colour
+    void foreground(uint16_t color); // set text color
+    void background(uint16_t color); // set background color
+    void SetFontSize(uint8_t); // set internal font size NORMAL, HIGH, WIDE, WH (high and wide), WHx36 (large 36x36 pixel size)
     void on();  // display on
     void off(); // display off
-    void cls(); // clear screen to black
-    void dim();  // flip dim/normal
+    void cls(); // clear screen to black screen
+    void dim();  // flip dim/normal on each call
     void contrast(char value); //0~9 low~high
-    void locate(int16_t column, int16_t row); // text start position
-    int toRGB(int16_t R,int16_t G,int16_t B);   // get color from RGB values 00~FF(0~255)
-    int row();
-    int column();        
-    void ScrollSet(int horizontal, int startline, int linecount, int vertical , int frame_interval);
-    void Scrollstart();
-    void Scrollstop();
-    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); // Print single character, x & y pixel co-ords.
+    void locate(uint8_t column, uint8_t row); // text start position x,y
+    uint16_t toRGB(uint16_t R,uint16_t G,uint16_t B);   // get color from RGB values 00~FF(0~255)
+    uint8_t row(); // set row position (in pixels)
+    uint8_t column(); // set column position (in pixels)
+    void ScrollSet(int8_t horizontal, int8_t startline, int8_t linecount, int8_t vertical , int8_t frame_interval); // set up scroll function
+    void Scrollstart(); // start scrolling
+    void Scrollstop(); // stop scrolling
+    void Copy(uint8_t src_x1,uint8_t src_y1,uint8_t src_x2,uint8_t src_y2, uint8_t dst_x,uint8_t dst_y); // GAC function to copy/paste area on the screen
+    void character(uint8_t x, uint8_t y, uint16_t c); // Print single character, x & y pixel co-ords.
     void set_font(unsigned char* f); // set external font. Use oled.set_font(NULL) to restore default font
-    void Bitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, unsigned char color); // load mono bitmap from flash
-    void Bitmap16(int16_t x, int16_t y, int16_t w, int16_t h,unsigned char *bitmap); // load bitmap from flash
-    int Bitmap16SD( int16_t x, int16_t y, unsigned char *Name_BMP); // uses up to 12.288k RAM, Fast
-    int Bitmap16SD2(int16_t x, int16_t y, unsigned char *Name_BMP); // uses 96b RAM, slower
+    void Bitmap(const uint8_t *bitmap, uint8_t x, uint8_t y, uint8_t w, uint8_t h, unsigned char color); // load mono bitmap from flash
+    void Bitmap16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap); // load bitmap from flash
+    
+    int Bitmap16RAM(uint8_t x, uint8_t y, unsigned char *Name_BMP); // copy image to RAM, uses up to 12.288k RAM, Fast but need plenty of RAM
+    int Bitmap16FS(uint8_t x, uint8_t y, unsigned char *Name_BMP); // load from fielsystem, uses 96b RAM, slower, will work on any MCU
+    // use GIMP to generate images, open image, select image-scale image, set width/height(max 96x64), select File-Export As
+    // select Windows BMP image, Name it with .BMP extension, select export, select Compatability Options- check 'Do Not write colour space'
+    // select Advanced Options- check '16bit R5 G6 B5', select export. Copy the file to a SD card or to the local file system. 
+    // image maximum size 96x64, smaller images can be placed anywhere on screen setting x,y co-ords but must fit within boundaries
+    // example:
+    // oled.Bitmap16FS(0,0,(unsigned char *)"/sd/image.bmp"); full size 96x64 image
+    // oled.Bitmap16RAM(20,20,(unsigned char *)"/sd/image.bmp"); place smaller image starting x-20, y-20
    
 protected:
     // Stream implementation functions
@@ -194,36 +194,35 @@
 private:
     void Init(void);
     void RegWrite(unsigned char Command);
-    void RegWriteM(unsigned char *Command, int count);
+    void RegWriteM(unsigned char *Command, uint8_t count);
     void DataWrite(unsigned char c);
-    void DataWrite_to(unsigned int Dat);
+    void DataWrite_to(uint16_t Dat);
     void FontSizeConvert(int *lpx, int *lpy);
-    void PutChar(int x,int y,unsigned int a);
-    void PutCharInt(int x,int y,unsigned int a);
+    void PutChar(uint8_t x,uint8_t y,int a);
+    void PutCharInt(uint8_t x,uint8_t y,uint16_t a);
     void putp(int colour);
     unsigned char* font;
-    unsigned int Char_Color;    // text color
-    unsigned int BGround_Color; // background color
-    void window(int16_t x, int16_t y, int16_t w, int16_t h);
+    uint16_t Char_Color;    // text color
+    uint16_t BGround_Color; // background color
+    void window(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
     void Maxwindow(); // reset display window to full size
     // pixel location
-    short _x;
-    short _y;
+    uint8_t _x;
+    uint8_t _y;
     
     // window location
-    short _x1;
-    short _x2;
-    short _y1;
-    short _y2;
-    int char_x;
-    int char_y;
-    int chr_size;
-    int cwidth;           // character's width
-    int cvert;           // character's height
-    int externalfont;
+    uint8_t _x1;
+    uint8_t _x2;
+    uint8_t _y1;
+    uint8_t _y2;
+    uint8_t char_x;
+    uint8_t char_y;
+    uint8_t chr_size;
+    uint8_t cwidth;       // character's width
+    uint8_t cvert;        // character's height
+    uint8_t externalfont;
     DigitalOut  CS,  RES,  DC;
-    SPI spi; // mosi, miso, sclk
-    
+    SPI spi; // mosi, miso, sclk    
 };
 
 #endif