SSD1331 Oled driver library for 96x64 colour Oled display. Demo included in .h file
Dependents: Oled-SSD1331 PJ12_device
ssd1331.h@5:4385fd242db0, 2016-05-29 (annotated)
- Committer:
- star297
- Date:
- Sun May 29 17:59:51 2016 +0000
- Revision:
- 5:4385fd242db0
- Parent:
- 4:062b4708eb35
Minor update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:3d7d1aec706b | 1 | |
star297 | 0:3d7d1aec706b | 2 | #ifndef __ssd1331_H__ |
star297 | 0:3d7d1aec706b | 3 | #define __ssd1331_H__ |
star297 | 0:3d7d1aec706b | 4 | |
star297 | 0:3d7d1aec706b | 5 | #include "mbed.h" |
star297 | 0:3d7d1aec706b | 6 | |
star297 | 0:3d7d1aec706b | 7 | // Screen Settings |
star297 | 0:3d7d1aec706b | 8 | #define width 96-1 // Max X axial direction in screen |
star297 | 0:3d7d1aec706b | 9 | #define height 64-1 // Max Y axial direction in screen |
star297 | 0:3d7d1aec706b | 10 | #define Set_Column_Address 0x15 |
star297 | 0:3d7d1aec706b | 11 | #define Set_Row_Address 0x75 |
star297 | 0:3d7d1aec706b | 12 | #define contrastA 0x81 |
star297 | 0:3d7d1aec706b | 13 | #define contrastB 0x82 |
star297 | 0:3d7d1aec706b | 14 | #define contrastC 0x83 |
star297 | 0:3d7d1aec706b | 15 | #define display_on 0xAF |
star297 | 0:3d7d1aec706b | 16 | #define display_off 0xAE |
star297 | 0:3d7d1aec706b | 17 | |
star297 | 4:062b4708eb35 | 18 | // Internal Font size settings |
star297 | 0:3d7d1aec706b | 19 | #define NORMAL 0 |
star297 | 0:3d7d1aec706b | 20 | #define WIDE 1 |
star297 | 0:3d7d1aec706b | 21 | #define HIGH 2 |
star297 | 0:3d7d1aec706b | 22 | #define WH 3 |
star297 | 0:3d7d1aec706b | 23 | #define WHx36 4 |
star297 | 4:062b4708eb35 | 24 | #define X_width 6 |
star297 | 4:062b4708eb35 | 25 | #define Y_height 8 |
star297 | 0:3d7d1aec706b | 26 | |
star297 | 0:3d7d1aec706b | 27 | |
star297 | 0:3d7d1aec706b | 28 | // GAC hardware acceleration commands |
star297 | 0:3d7d1aec706b | 29 | #define GAC_DRAW_LINE 0x21 // Draw Line |
star297 | 0:3d7d1aec706b | 30 | #define GAC_DRAW_RECTANGLE 0x22 // Rectangle |
star297 | 0:3d7d1aec706b | 31 | #define GAC_COPY_AREA 0x23 // Copy Area |
star297 | 0:3d7d1aec706b | 32 | #define GAC_DIM_WINDOW 0x24 // Dim Window |
star297 | 0:3d7d1aec706b | 33 | #define GAC_CLEAR_WINDOW 0x25 // Clear Window |
star297 | 0:3d7d1aec706b | 34 | #define GAC_FILL_ENABLE_DISABLE 0x26 // Enable Fill |
star297 | 0:3d7d1aec706b | 35 | #define SCROLL_SETUP 0x27 // Setup scroll |
star297 | 0:3d7d1aec706b | 36 | #define SCROLL_STOP 0x2E // Scroll Stop |
star297 | 0:3d7d1aec706b | 37 | #define SCROLL_START 0x2F // Scroll Start |
star297 | 0:3d7d1aec706b | 38 | |
star297 | 4:062b4708eb35 | 39 | // Basic RGB color definitions RED GREEN BLUE values |
star297 | 2:1204274fad8f | 40 | |
star297 | 2:1204274fad8f | 41 | #define Black 0x0000 // 0, 0, 0 |
star297 | 2:1204274fad8f | 42 | #define LightGrey 0xC618 // 192, 192, 192 |
star297 | 2:1204274fad8f | 43 | #define DarkGrey 0x7BEF // 128, 128, 128 |
star297 | 2:1204274fad8f | 44 | #define Red 0xF800 // 255, 0, 0 |
star297 | 2:1204274fad8f | 45 | #define Green 0x07E0 // 0, 255, 0 |
star297 | 4:062b4708eb35 | 46 | #define Cyan 0x07FF // 0, 255, 255 |
star297 | 2:1204274fad8f | 47 | #define Blue 0x001F // 0, 0, 255 |
star297 | 2:1204274fad8f | 48 | #define Magenta 0xF81F // 255, 0, 255 |
star297 | 4:062b4708eb35 | 49 | #define Yellow 0xFFE0 // 255, 255, 0 |
star297 | 2:1204274fad8f | 50 | #define White 0xFFFF // 255, 255, 255 |
star297 | 2:1204274fad8f | 51 | |
star297 | 0:3d7d1aec706b | 52 | // example code |
star297 | 0:3d7d1aec706b | 53 | /* |
star297 | 0:3d7d1aec706b | 54 | #include "mbed.h" |
star297 | 0:3d7d1aec706b | 55 | #include "ssd1331.h" |
star297 | 0:3d7d1aec706b | 56 | |
star297 | 0:3d7d1aec706b | 57 | ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z) |
star297 | 0:3d7d1aec706b | 58 | |
star297 | 0:3d7d1aec706b | 59 | char Time[50],Date[50]; |
star297 | 0:3d7d1aec706b | 60 | void gettime(); |
star297 | 0:3d7d1aec706b | 61 | |
star297 | 4:062b4708eb35 | 62 | uint8_t main() { |
star297 | 0:3d7d1aec706b | 63 | |
star297 | 0:3d7d1aec706b | 64 | while(1){ |
star297 | 0:3d7d1aec706b | 65 | |
star297 | 0:3d7d1aec706b | 66 | oled.Fill_Screen(oled.toRGB(255,0,0)); //red |
star297 | 0:3d7d1aec706b | 67 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 68 | oled.Fill_Screen(oled.toRGB(0,255,0)); //green |
star297 | 0:3d7d1aec706b | 69 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 70 | oled.Fill_Screen(oled.toRGB(0,0,255)); //blue |
star297 | 0:3d7d1aec706b | 71 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 72 | oled.Fill_Screen(oled.toRGB(255,255,255)); //white |
star297 | 0:3d7d1aec706b | 73 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 74 | |
star297 | 0:3d7d1aec706b | 75 | oled.cls(); // clear screen to black |
star297 | 0:3d7d1aec706b | 76 | |
star297 | 0:3d7d1aec706b | 77 | oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle |
star297 | 0:3d7d1aec706b | 78 | oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle |
star297 | 0:3d7d1aec706b | 79 | oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0); //circle |
star297 | 0:3d7d1aec706b | 80 | oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line |
star297 | 0:3d7d1aec706b | 81 | oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line |
star297 | 4:062b4708eb35 | 82 | oled.rectangle(10,10,90,60,oled.toRGB(255,255,0)); //rectangle |
star297 | 4:062b4708eb35 | 83 | oled.fillrectangle(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle |
star297 | 0:3d7d1aec706b | 84 | |
star297 | 4:062b4708eb35 | 85 | for(uint8_t y = 9; y >= 0; y--) { |
star297 | 0:3d7d1aec706b | 86 | oled.contrast(y); // set contrast level |
star297 | 0:3d7d1aec706b | 87 | oled.foreground(oled.toRGB(255,255,255)); // set text colour |
star297 | 0:3d7d1aec706b | 88 | oled.locate(1, 10); // set text start location |
star297 | 0:3d7d1aec706b | 89 | oled.printf("%d",y); // std printf |
star297 | 0:3d7d1aec706b | 90 | wait_ms(300); |
star297 | 0:3d7d1aec706b | 91 | } |
star297 | 0:3d7d1aec706b | 92 | |
star297 | 0:3d7d1aec706b | 93 | wait_ms(1000); |
star297 | 0:3d7d1aec706b | 94 | oled.contrast(9); // set contrast to maximum |
star297 | 0:3d7d1aec706b | 95 | wait_ms(2000); |
star297 | 0:3d7d1aec706b | 96 | oled.cls(); |
star297 | 0:3d7d1aec706b | 97 | |
star297 | 0:3d7d1aec706b | 98 | oled.SetFontSize(HIGH); // set tall font |
star297 | 0:3d7d1aec706b | 99 | oled.foreground(oled.toRGB(0,255,0)); // set text colour |
star297 | 0:3d7d1aec706b | 100 | oled.locate(0, 10); |
star297 | 0:3d7d1aec706b | 101 | oled.printf( "HIGH 12345"); |
star297 | 0:3d7d1aec706b | 102 | |
star297 | 0:3d7d1aec706b | 103 | oled.SetFontSize(WIDE); // set text to wide |
star297 | 0:3d7d1aec706b | 104 | oled.foreground(oled.toRGB(0,0,255)); |
star297 | 0:3d7d1aec706b | 105 | oled.locate(0, 28); |
star297 | 0:3d7d1aec706b | 106 | oled.printf( "WIDE 123"); |
star297 | 0:3d7d1aec706b | 107 | |
star297 | 0:3d7d1aec706b | 108 | oled.SetFontSize(WH); // set text to wide and tall |
star297 | 0:3d7d1aec706b | 109 | oled.foreground(oled.toRGB(255,0,0)); |
star297 | 0:3d7d1aec706b | 110 | oled.locate(0, 40); |
star297 | 0:3d7d1aec706b | 111 | oled.printf( "WH 123"); |
star297 | 0:3d7d1aec706b | 112 | |
star297 | 0:3d7d1aec706b | 113 | oled.SetFontSize(NORMAL); // set text to normal |
star297 | 0:3d7d1aec706b | 114 | oled.foreground(oled.toRGB(255,255,255)); |
star297 | 0:3d7d1aec706b | 115 | |
star297 | 0:3d7d1aec706b | 116 | oled.ScrollSet(0,8,18,1,0); // set scroll function |
star297 | 0:3d7d1aec706b | 117 | oled.Scrollstart(); // start scroll |
star297 | 0:3d7d1aec706b | 118 | |
star297 | 0:3d7d1aec706b | 119 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 120 | oled.ScrollSet(0,8,18,-2,0); |
star297 | 0:3d7d1aec706b | 121 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 122 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 123 | |
star297 | 0:3d7d1aec706b | 124 | oled.ScrollSet(0,8,18,3,0); |
star297 | 0:3d7d1aec706b | 125 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 126 | |
star297 | 0:3d7d1aec706b | 127 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 128 | |
star297 | 0:3d7d1aec706b | 129 | oled.ScrollSet(0,8,18,-4,0); |
star297 | 0:3d7d1aec706b | 130 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 131 | |
star297 | 0:3d7d1aec706b | 132 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 133 | |
star297 | 0:3d7d1aec706b | 134 | oled.Scrollstop(); // stop scroll |
star297 | 0:3d7d1aec706b | 135 | wait(1); |
star297 | 0:3d7d1aec706b | 136 | } |
star297 | 0:3d7d1aec706b | 137 | } |
star297 | 0:3d7d1aec706b | 138 | void gettime() |
star297 | 0:3d7d1aec706b | 139 | { |
star297 | 0:3d7d1aec706b | 140 | time_t seconds = time(NULL); |
star297 | 0:3d7d1aec706b | 141 | strftime(Time,40,"%H:%M:%S %a", localtime(&seconds)); |
star297 | 0:3d7d1aec706b | 142 | strftime(Date,40,"%d-%b-%Y", localtime(&seconds)); |
star297 | 0:3d7d1aec706b | 143 | oled.locate(0, 0); |
star297 | 0:3d7d1aec706b | 144 | oled.printf(Time); |
star297 | 0:3d7d1aec706b | 145 | } |
star297 | 0:3d7d1aec706b | 146 | */ |
star297 | 0:3d7d1aec706b | 147 | |
star297 | 0:3d7d1aec706b | 148 | class ssd1331 : public Stream { |
star297 | 0:3d7d1aec706b | 149 | public: |
star297 | 0:3d7d1aec706b | 150 | // constructor |
star297 | 0:3d7d1aec706b | 151 | ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin); |
star297 | 5:4385fd242db0 | 152 | |
star297 | 5:4385fd242db0 | 153 | void Init(void); |
star297 | 4:062b4708eb35 | 154 | void pixel(uint8_t x,uint8_t y, uint16_t color); // place a pixel x,y coordinates, color |
star297 | 4:062b4708eb35 | 155 | 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 |
star297 | 4:062b4708eb35 | 156 | 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. |
star297 | 4:062b4708eb35 | 157 | 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 |
star297 | 4:062b4708eb35 | 158 | void circle (uint8_t radius, uint8_t x, uint8_t y,uint16_t color,uint16_t fill); // draw circle radius, position x,y, color |
star297 | 4:062b4708eb35 | 159 | void Fill_Screen(uint16_t color); // fill screen with any colour |
star297 | 4:062b4708eb35 | 160 | void foreground(uint16_t color); // set text color |
star297 | 4:062b4708eb35 | 161 | void background(uint16_t color); // set background color |
star297 | 4:062b4708eb35 | 162 | void SetFontSize(uint8_t); // set internal font size NORMAL, HIGH, WIDE, WH (high and wide), WHx36 (large 36x36 pixel size) |
star297 | 0:3d7d1aec706b | 163 | void on(); // display on |
star297 | 0:3d7d1aec706b | 164 | void off(); // display off |
star297 | 4:062b4708eb35 | 165 | void cls(); // clear screen to black screen |
star297 | 4:062b4708eb35 | 166 | void dim(); // flip dim/normal on each call |
star297 | 0:3d7d1aec706b | 167 | void contrast(char value); //0~9 low~high |
star297 | 4:062b4708eb35 | 168 | void locate(uint8_t column, uint8_t row); // text start position x,y |
star297 | 4:062b4708eb35 | 169 | uint16_t toRGB(uint16_t R,uint16_t G,uint16_t B); // get color from RGB values 00~FF(0~255) |
star297 | 4:062b4708eb35 | 170 | uint8_t row(); // set row position (in pixels) |
star297 | 4:062b4708eb35 | 171 | uint8_t column(); // set column position (in pixels) |
star297 | 4:062b4708eb35 | 172 | void ScrollSet(int8_t horizontal, int8_t startline, int8_t linecount, int8_t vertical , int8_t frame_interval); // set up scroll function |
star297 | 4:062b4708eb35 | 173 | void Scrollstart(); // start scrolling |
star297 | 4:062b4708eb35 | 174 | void Scrollstop(); // stop scrolling |
star297 | 4:062b4708eb35 | 175 | 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 |
star297 | 4:062b4708eb35 | 176 | void character(uint8_t x, uint8_t y, uint16_t c); // Print single character, x & y pixel co-ords. |
star297 | 3:a4caac512e13 | 177 | void set_font(unsigned char* f); // set external font. Use oled.set_font(NULL) to restore default font |
star297 | 4:062b4708eb35 | 178 | 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 |
star297 | 4:062b4708eb35 | 179 | void Bitmap16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap); // load bitmap from flash |
star297 | 4:062b4708eb35 | 180 | |
star297 | 4:062b4708eb35 | 181 | 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 |
star297 | 4:062b4708eb35 | 182 | int Bitmap16FS(uint8_t x, uint8_t y, unsigned char *Name_BMP); // load from fielsystem, uses 96b RAM, slower, will work on any MCU |
star297 | 4:062b4708eb35 | 183 | // use GIMP to generate images, open image, select image-scale image, set width/height(max 96x64), select File-Export As |
star297 | 4:062b4708eb35 | 184 | // select Windows BMP image, Name it with .BMP extension, select export, select Compatability Options- check 'Do Not write colour space' |
star297 | 4:062b4708eb35 | 185 | // select Advanced Options- check '16bit R5 G6 B5', select export. Copy the file to a SD card or to the local file system. |
star297 | 4:062b4708eb35 | 186 | // image maximum size 96x64, smaller images can be placed anywhere on screen setting x,y co-ords but must fit within boundaries |
star297 | 4:062b4708eb35 | 187 | // example: |
star297 | 4:062b4708eb35 | 188 | // oled.Bitmap16FS(0,0,(unsigned char *)"/sd/image.bmp"); full size 96x64 image |
star297 | 4:062b4708eb35 | 189 | // oled.Bitmap16RAM(20,20,(unsigned char *)"/sd/image.bmp"); place smaller image starting x-20, y-20 |
star297 | 3:a4caac512e13 | 190 | |
star297 | 0:3d7d1aec706b | 191 | protected: |
star297 | 0:3d7d1aec706b | 192 | // Stream implementation functions |
star297 | 0:3d7d1aec706b | 193 | virtual int _putc(int c); |
star297 | 0:3d7d1aec706b | 194 | virtual int _getc(); |
star297 | 0:3d7d1aec706b | 195 | private: |
star297 | 0:3d7d1aec706b | 196 | void RegWrite(unsigned char Command); |
star297 | 4:062b4708eb35 | 197 | void RegWriteM(unsigned char *Command, uint8_t count); |
star297 | 0:3d7d1aec706b | 198 | void DataWrite(unsigned char c); |
star297 | 4:062b4708eb35 | 199 | void DataWrite_to(uint16_t Dat); |
star297 | 0:3d7d1aec706b | 200 | void FontSizeConvert(int *lpx, int *lpy); |
star297 | 4:062b4708eb35 | 201 | void PutChar(uint8_t x,uint8_t y,int a); |
star297 | 4:062b4708eb35 | 202 | void PutCharInt(uint8_t x,uint8_t y,uint16_t a); |
star297 | 1:f3f6624f45d4 | 203 | void putp(int colour); |
star297 | 1:f3f6624f45d4 | 204 | unsigned char* font; |
star297 | 4:062b4708eb35 | 205 | uint16_t Char_Color; // text color |
star297 | 4:062b4708eb35 | 206 | uint16_t BGround_Color; // background color |
star297 | 4:062b4708eb35 | 207 | void window(uint8_t x, uint8_t y, uint8_t w, uint8_t h); |
star297 | 2:1204274fad8f | 208 | void Maxwindow(); // reset display window to full size |
star297 | 1:f3f6624f45d4 | 209 | // pixel location |
star297 | 4:062b4708eb35 | 210 | uint8_t _x; |
star297 | 4:062b4708eb35 | 211 | uint8_t _y; |
star297 | 1:f3f6624f45d4 | 212 | |
star297 | 1:f3f6624f45d4 | 213 | // window location |
star297 | 4:062b4708eb35 | 214 | uint8_t _x1; |
star297 | 4:062b4708eb35 | 215 | uint8_t _x2; |
star297 | 4:062b4708eb35 | 216 | uint8_t _y1; |
star297 | 4:062b4708eb35 | 217 | uint8_t _y2; |
star297 | 4:062b4708eb35 | 218 | uint8_t char_x; |
star297 | 4:062b4708eb35 | 219 | uint8_t char_y; |
star297 | 4:062b4708eb35 | 220 | uint8_t chr_size; |
star297 | 4:062b4708eb35 | 221 | uint8_t cwidth; // character's width |
star297 | 4:062b4708eb35 | 222 | uint8_t cvert; // character's height |
star297 | 4:062b4708eb35 | 223 | uint8_t externalfont; |
star297 | 0:3d7d1aec706b | 224 | DigitalOut CS, RES, DC; |
star297 | 4:062b4708eb35 | 225 | SPI spi; // mosi, miso, sclk |
star297 | 0:3d7d1aec706b | 226 | }; |
star297 | 0:3d7d1aec706b | 227 | |
star297 | 1:f3f6624f45d4 | 228 | #endif |
star297 | 1:f3f6624f45d4 | 229 |