Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Oled-SSD1331 PJ12_device
ssd1331.h
00001 00002 #ifndef __ssd1331_H__ 00003 #define __ssd1331_H__ 00004 00005 #include "mbed.h" 00006 00007 // Screen Settings 00008 #define width 96-1 // Max X axial direction in screen 00009 #define height 64-1 // Max Y axial direction in screen 00010 #define Set_Column_Address 0x15 00011 #define Set_Row_Address 0x75 00012 #define contrastA 0x81 00013 #define contrastB 0x82 00014 #define contrastC 0x83 00015 #define display_on 0xAF 00016 #define display_off 0xAE 00017 00018 // Internal Font size settings 00019 #define NORMAL 0 00020 #define WIDE 1 00021 #define HIGH 2 00022 #define WH 3 00023 #define WHx36 4 00024 #define X_width 6 00025 #define Y_height 8 00026 00027 00028 // GAC hardware acceleration commands 00029 #define GAC_DRAW_LINE 0x21 // Draw Line 00030 #define GAC_DRAW_RECTANGLE 0x22 // Rectangle 00031 #define GAC_COPY_AREA 0x23 // Copy Area 00032 #define GAC_DIM_WINDOW 0x24 // Dim Window 00033 #define GAC_CLEAR_WINDOW 0x25 // Clear Window 00034 #define GAC_FILL_ENABLE_DISABLE 0x26 // Enable Fill 00035 #define SCROLL_SETUP 0x27 // Setup scroll 00036 #define SCROLL_STOP 0x2E // Scroll Stop 00037 #define SCROLL_START 0x2F // Scroll Start 00038 00039 // Basic RGB color definitions RED GREEN BLUE values 00040 00041 #define Black 0x0000 // 0, 0, 0 00042 #define LightGrey 0xC618 // 192, 192, 192 00043 #define DarkGrey 0x7BEF // 128, 128, 128 00044 #define Red 0xF800 // 255, 0, 0 00045 #define Green 0x07E0 // 0, 255, 0 00046 #define Cyan 0x07FF // 0, 255, 255 00047 #define Blue 0x001F // 0, 0, 255 00048 #define Magenta 0xF81F // 255, 0, 255 00049 #define Yellow 0xFFE0 // 255, 255, 0 00050 #define White 0xFFFF // 255, 255, 255 00051 00052 // example code 00053 /* 00054 #include "mbed.h" 00055 #include "ssd1331.h" 00056 00057 ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z) 00058 00059 char Time[50],Date[50]; 00060 void gettime(); 00061 00062 uint8_t main() { 00063 00064 while(1){ 00065 00066 oled.Fill_Screen(oled.toRGB(255,0,0)); //red 00067 wait_ms(500); 00068 oled.Fill_Screen(oled.toRGB(0,255,0)); //green 00069 wait_ms(500); 00070 oled.Fill_Screen(oled.toRGB(0,0,255)); //blue 00071 wait_ms(500); 00072 oled.Fill_Screen(oled.toRGB(255,255,255)); //white 00073 wait_ms(500); 00074 00075 oled.cls(); // clear screen to black 00076 00077 oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle 00078 oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle 00079 oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0); //circle 00080 oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line 00081 oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line 00082 oled.rectangle(10,10,90,60,oled.toRGB(255,255,0)); //rectangle 00083 oled.fillrectangle(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle 00084 00085 for(uint8_t y = 9; y >= 0; y--) { 00086 oled.contrast(y); // set contrast level 00087 oled.foreground(oled.toRGB(255,255,255)); // set text colour 00088 oled.locate(1, 10); // set text start location 00089 oled.printf("%d",y); // std printf 00090 wait_ms(300); 00091 } 00092 00093 wait_ms(1000); 00094 oled.contrast(9); // set contrast to maximum 00095 wait_ms(2000); 00096 oled.cls(); 00097 00098 oled.SetFontSize(HIGH); // set tall font 00099 oled.foreground(oled.toRGB(0,255,0)); // set text colour 00100 oled.locate(0, 10); 00101 oled.printf( "HIGH 12345"); 00102 00103 oled.SetFontSize(WIDE); // set text to wide 00104 oled.foreground(oled.toRGB(0,0,255)); 00105 oled.locate(0, 28); 00106 oled.printf( "WIDE 123"); 00107 00108 oled.SetFontSize(WH); // set text to wide and tall 00109 oled.foreground(oled.toRGB(255,0,0)); 00110 oled.locate(0, 40); 00111 oled.printf( "WH 123"); 00112 00113 oled.SetFontSize(NORMAL); // set text to normal 00114 oled.foreground(oled.toRGB(255,255,255)); 00115 00116 oled.ScrollSet(0,8,18,1,0); // set scroll function 00117 oled.Scrollstart(); // start scroll 00118 00119 gettime();wait(1);gettime();wait(1);gettime();wait(1); 00120 oled.ScrollSet(0,8,18,-2,0); 00121 oled.Scrollstart(); 00122 gettime();wait(1);gettime();wait(1);gettime();wait(1); 00123 00124 oled.ScrollSet(0,8,18,3,0); 00125 oled.Scrollstart(); 00126 00127 gettime();wait(1);gettime();wait(1);gettime();wait(1); 00128 00129 oled.ScrollSet(0,8,18,-4,0); 00130 oled.Scrollstart(); 00131 00132 gettime();wait(1);gettime();wait(1);gettime();wait(1); 00133 00134 oled.Scrollstop(); // stop scroll 00135 wait(1); 00136 } 00137 } 00138 void gettime() 00139 { 00140 time_t seconds = time(NULL); 00141 strftime(Time,40,"%H:%M:%S %a", localtime(&seconds)); 00142 strftime(Date,40,"%d-%b-%Y", localtime(&seconds)); 00143 oled.locate(0, 0); 00144 oled.printf(Time); 00145 } 00146 */ 00147 00148 class ssd1331 : public Stream { 00149 public: 00150 // constructor 00151 ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin); 00152 00153 void Init(void); 00154 void pixel(uint8_t x,uint8_t y, uint16_t color); // place a pixel x,y coordinates, color 00155 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 00156 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. 00157 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 00158 void circle (uint8_t radius, uint8_t x, uint8_t y,uint16_t color,uint16_t fill); // draw circle radius, position x,y, color 00159 void Fill_Screen(uint16_t color); // fill screen with any colour 00160 void foreground(uint16_t color); // set text color 00161 void background(uint16_t color); // set background color 00162 void SetFontSize(uint8_t); // set internal font size NORMAL, HIGH, WIDE, WH (high and wide), WHx36 (large 36x36 pixel size) 00163 void on(); // display on 00164 void off(); // display off 00165 void cls(); // clear screen to black screen 00166 void dim(); // flip dim/normal on each call 00167 void contrast(char value); //0~9 low~high 00168 void locate(uint8_t column, uint8_t row); // text start position x,y 00169 uint16_t toRGB(uint16_t R,uint16_t G,uint16_t B); // get color from RGB values 00~FF(0~255) 00170 uint8_t row(); // set row position (in pixels) 00171 uint8_t column(); // set column position (in pixels) 00172 void ScrollSet(int8_t horizontal, int8_t startline, int8_t linecount, int8_t vertical , int8_t frame_interval); // set up scroll function 00173 void Scrollstart(); // start scrolling 00174 void Scrollstop(); // stop scrolling 00175 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 00176 void character(uint8_t x, uint8_t y, uint16_t c); // Print single character, x & y pixel co-ords. 00177 void set_font(unsigned char* f); // set external font. Use oled.set_font(NULL) to restore default font 00178 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 00179 void Bitmap16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap); // load bitmap from flash 00180 00181 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 00182 int Bitmap16FS(uint8_t x, uint8_t y, unsigned char *Name_BMP); // load from fielsystem, uses 96b RAM, slower, will work on any MCU 00183 // use GIMP to generate images, open image, select image-scale image, set width/height(max 96x64), select File-Export As 00184 // select Windows BMP image, Name it with .BMP extension, select export, select Compatability Options- check 'Do Not write colour space' 00185 // select Advanced Options- check '16bit R5 G6 B5', select export. Copy the file to a SD card or to the local file system. 00186 // image maximum size 96x64, smaller images can be placed anywhere on screen setting x,y co-ords but must fit within boundaries 00187 // example: 00188 // oled.Bitmap16FS(0,0,(unsigned char *)"/sd/image.bmp"); full size 96x64 image 00189 // oled.Bitmap16RAM(20,20,(unsigned char *)"/sd/image.bmp"); place smaller image starting x-20, y-20 00190 00191 protected: 00192 // Stream implementation functions 00193 virtual int _putc(int c); 00194 virtual int _getc(); 00195 private: 00196 void RegWrite(unsigned char Command); 00197 void RegWriteM(unsigned char *Command, uint8_t count); 00198 void DataWrite(unsigned char c); 00199 void DataWrite_to(uint16_t Dat); 00200 void FontSizeConvert(int *lpx, int *lpy); 00201 void PutChar(uint8_t x,uint8_t y,int a); 00202 void PutCharInt(uint8_t x,uint8_t y,uint16_t a); 00203 void putp(int colour); 00204 unsigned char* font; 00205 uint16_t Char_Color; // text color 00206 uint16_t BGround_Color; // background color 00207 void window(uint8_t x, uint8_t y, uint8_t w, uint8_t h); 00208 void Maxwindow(); // reset display window to full size 00209 // pixel location 00210 uint8_t _x; 00211 uint8_t _y; 00212 00213 // window location 00214 uint8_t _x1; 00215 uint8_t _x2; 00216 uint8_t _y1; 00217 uint8_t _y2; 00218 uint8_t char_x; 00219 uint8_t char_y; 00220 uint8_t chr_size; 00221 uint8_t cwidth; // character's width 00222 uint8_t cvert; // character's height 00223 uint8_t externalfont; 00224 DigitalOut CS, RES, DC; 00225 SPI spi; // mosi, miso, sclk 00226 }; 00227 00228 #endif 00229
Generated on Tue Jul 12 2022 22:39:30 by
1.7.2
Display Module .95" 96x64 Oled with SPI interface