SSD1331 Oled driver library for 96x64 colour Oled display. Demo included in .h file
Dependents: Oled-SSD1331 PJ12_device
ssd1331.h@3:a4caac512e13, 2016-05-12 (annotated)
- Committer:
- star297
- Date:
- Thu May 12 21:36:11 2016 +0000
- Revision:
- 3:a4caac512e13
- Parent:
- 2:1204274fad8f
- Child:
- 4:062b4708eb35
Added slower load from SD card support + some clean up.
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 | 0:3d7d1aec706b | 18 | // Font size |
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 | 0:3d7d1aec706b | 24 | #define X_width 6 // character's width |
star297 | 1:f3f6624f45d4 | 25 | #define Y_height 8 // character's height |
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 | 2:1204274fad8f | 39 | // some RGB color definitions RED GREEN BLUE |
star297 | 2:1204274fad8f | 40 | |
star297 | 2:1204274fad8f | 41 | #define Black 0x0000 // 0, 0, 0 |
star297 | 2:1204274fad8f | 42 | #define Navy 0x8000 // 0, 0, 128 |
star297 | 2:1204274fad8f | 43 | #define DarkGreen 0x0400 // 0, 128, 0 |
star297 | 2:1204274fad8f | 44 | #define DarkCyan 0x8400 // 0, 128, 128 |
star297 | 2:1204274fad8f | 45 | #define Maroon 0x0010 // 128, 0, 0 |
star297 | 2:1204274fad8f | 46 | #define Purple 0x8010 // 128, 0, 128 |
star297 | 2:1204274fad8f | 47 | #define Olive 0x0410 // 128, 128, 0 |
star297 | 2:1204274fad8f | 48 | #define LightGrey 0xC618 // 192, 192, 192 |
star297 | 2:1204274fad8f | 49 | #define DarkGrey 0x7BEF // 128, 128, 128 |
star297 | 2:1204274fad8f | 50 | #define Red 0xF800 // 255, 0, 0 |
star297 | 2:1204274fad8f | 51 | #define Green 0x07E0 // 0, 255, 0 |
star297 | 2:1204274fad8f | 52 | #define Yellow 0x07FF // 255, 255, 0 |
star297 | 2:1204274fad8f | 53 | #define Blue 0x001F // 0, 0, 255 |
star297 | 2:1204274fad8f | 54 | #define Magenta 0xF81F // 255, 0, 255 |
star297 | 2:1204274fad8f | 55 | #define Cyan 0xFFE0 // 0, 255, 255 |
star297 | 2:1204274fad8f | 56 | #define White 0xFFFF // 255, 255, 255 |
star297 | 2:1204274fad8f | 57 | #define Orange 0x053F // 255, 165, 0 |
star297 | 2:1204274fad8f | 58 | #define GreenYellow 0x2FF5 // 173, 255, 47 |
star297 | 2:1204274fad8f | 59 | |
star297 | 0:3d7d1aec706b | 60 | // example code |
star297 | 0:3d7d1aec706b | 61 | /* |
star297 | 0:3d7d1aec706b | 62 | #include "mbed.h" |
star297 | 0:3d7d1aec706b | 63 | #include "ssd1331.h" |
star297 | 0:3d7d1aec706b | 64 | |
star297 | 0:3d7d1aec706b | 65 | ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z) |
star297 | 0:3d7d1aec706b | 66 | |
star297 | 0:3d7d1aec706b | 67 | char Time[50],Date[50]; |
star297 | 0:3d7d1aec706b | 68 | void gettime(); |
star297 | 0:3d7d1aec706b | 69 | |
star297 | 0:3d7d1aec706b | 70 | int main() { |
star297 | 0:3d7d1aec706b | 71 | |
star297 | 0:3d7d1aec706b | 72 | while(1){ |
star297 | 0:3d7d1aec706b | 73 | |
star297 | 0:3d7d1aec706b | 74 | oled.Fill_Screen(oled.toRGB(255,0,0)); //red |
star297 | 0:3d7d1aec706b | 75 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 76 | oled.Fill_Screen(oled.toRGB(0,255,0)); //green |
star297 | 0:3d7d1aec706b | 77 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 78 | oled.Fill_Screen(oled.toRGB(0,0,255)); //blue |
star297 | 0:3d7d1aec706b | 79 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 80 | oled.Fill_Screen(oled.toRGB(255,255,255)); //white |
star297 | 0:3d7d1aec706b | 81 | wait_ms(500); |
star297 | 0:3d7d1aec706b | 82 | |
star297 | 0:3d7d1aec706b | 83 | oled.cls(); // clear screen to black |
star297 | 0:3d7d1aec706b | 84 | |
star297 | 0:3d7d1aec706b | 85 | oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle |
star297 | 0:3d7d1aec706b | 86 | oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle |
star297 | 0:3d7d1aec706b | 87 | oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0); //circle |
star297 | 0:3d7d1aec706b | 88 | oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line |
star297 | 0:3d7d1aec706b | 89 | oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line |
star297 | 3:a4caac512e13 | 90 | oled.rect(10,10,90,60,oled.toRGB(255,255,0)); //rectangle |
star297 | 3:a4caac512e13 | 91 | oled.fillrect(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle |
star297 | 0:3d7d1aec706b | 92 | |
star297 | 0:3d7d1aec706b | 93 | for(int y = 9; y >= 0; y--) { |
star297 | 0:3d7d1aec706b | 94 | oled.contrast(y); // set contrast level |
star297 | 0:3d7d1aec706b | 95 | oled.foreground(oled.toRGB(255,255,255)); // set text colour |
star297 | 0:3d7d1aec706b | 96 | oled.locate(1, 10); // set text start location |
star297 | 0:3d7d1aec706b | 97 | oled.printf("%d",y); // std printf |
star297 | 0:3d7d1aec706b | 98 | wait_ms(300); |
star297 | 0:3d7d1aec706b | 99 | } |
star297 | 0:3d7d1aec706b | 100 | |
star297 | 0:3d7d1aec706b | 101 | wait_ms(1000); |
star297 | 0:3d7d1aec706b | 102 | oled.contrast(9); // set contrast to maximum |
star297 | 0:3d7d1aec706b | 103 | wait_ms(2000); |
star297 | 0:3d7d1aec706b | 104 | oled.cls(); |
star297 | 0:3d7d1aec706b | 105 | |
star297 | 0:3d7d1aec706b | 106 | oled.SetFontSize(HIGH); // set tall font |
star297 | 0:3d7d1aec706b | 107 | oled.foreground(oled.toRGB(0,255,0)); // set text colour |
star297 | 0:3d7d1aec706b | 108 | oled.locate(0, 10); |
star297 | 0:3d7d1aec706b | 109 | oled.printf( "HIGH 12345"); |
star297 | 0:3d7d1aec706b | 110 | |
star297 | 0:3d7d1aec706b | 111 | oled.SetFontSize(WIDE); // set text to wide |
star297 | 0:3d7d1aec706b | 112 | oled.foreground(oled.toRGB(0,0,255)); |
star297 | 0:3d7d1aec706b | 113 | oled.locate(0, 28); |
star297 | 0:3d7d1aec706b | 114 | oled.printf( "WIDE 123"); |
star297 | 0:3d7d1aec706b | 115 | |
star297 | 0:3d7d1aec706b | 116 | oled.SetFontSize(WH); // set text to wide and tall |
star297 | 0:3d7d1aec706b | 117 | oled.foreground(oled.toRGB(255,0,0)); |
star297 | 0:3d7d1aec706b | 118 | oled.locate(0, 40); |
star297 | 0:3d7d1aec706b | 119 | oled.printf( "WH 123"); |
star297 | 0:3d7d1aec706b | 120 | |
star297 | 0:3d7d1aec706b | 121 | oled.SetFontSize(NORMAL); // set text to normal |
star297 | 0:3d7d1aec706b | 122 | oled.foreground(oled.toRGB(255,255,255)); |
star297 | 0:3d7d1aec706b | 123 | |
star297 | 0:3d7d1aec706b | 124 | oled.ScrollSet(0,8,18,1,0); // set scroll function |
star297 | 0:3d7d1aec706b | 125 | oled.Scrollstart(); // start scroll |
star297 | 0:3d7d1aec706b | 126 | |
star297 | 0:3d7d1aec706b | 127 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 128 | oled.ScrollSet(0,8,18,-2,0); |
star297 | 0:3d7d1aec706b | 129 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 130 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 131 | |
star297 | 0:3d7d1aec706b | 132 | oled.ScrollSet(0,8,18,3,0); |
star297 | 0:3d7d1aec706b | 133 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 134 | |
star297 | 0:3d7d1aec706b | 135 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 136 | |
star297 | 0:3d7d1aec706b | 137 | oled.ScrollSet(0,8,18,-4,0); |
star297 | 0:3d7d1aec706b | 138 | oled.Scrollstart(); |
star297 | 0:3d7d1aec706b | 139 | |
star297 | 0:3d7d1aec706b | 140 | gettime();wait(1);gettime();wait(1);gettime();wait(1); |
star297 | 0:3d7d1aec706b | 141 | |
star297 | 0:3d7d1aec706b | 142 | oled.Scrollstop(); // stop scroll |
star297 | 0:3d7d1aec706b | 143 | wait(1); |
star297 | 0:3d7d1aec706b | 144 | } |
star297 | 0:3d7d1aec706b | 145 | } |
star297 | 0:3d7d1aec706b | 146 | void gettime() |
star297 | 0:3d7d1aec706b | 147 | { |
star297 | 0:3d7d1aec706b | 148 | time_t seconds = time(NULL); |
star297 | 0:3d7d1aec706b | 149 | strftime(Time,40,"%H:%M:%S %a", localtime(&seconds)); |
star297 | 0:3d7d1aec706b | 150 | strftime(Date,40,"%d-%b-%Y", localtime(&seconds)); |
star297 | 0:3d7d1aec706b | 151 | oled.locate(0, 0); |
star297 | 0:3d7d1aec706b | 152 | oled.printf(Time); |
star297 | 0:3d7d1aec706b | 153 | } |
star297 | 0:3d7d1aec706b | 154 | */ |
star297 | 0:3d7d1aec706b | 155 | |
star297 | 0:3d7d1aec706b | 156 | class ssd1331 : public Stream { |
star297 | 0:3d7d1aec706b | 157 | public: |
star297 | 0:3d7d1aec706b | 158 | // constructor |
star297 | 0:3d7d1aec706b | 159 | ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin); |
star297 | 0:3d7d1aec706b | 160 | |
star297 | 3:a4caac512e13 | 161 | void pixel(int16_t x,int16_t y,unsigned int color); |
star297 | 3:a4caac512e13 | 162 | void rect(int16_t x1,int16_t y1,int16_t x2,int16_t y2,unsigned int colorline); |
star297 | 2:1204274fad8f | 163 | void fillrect(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill); |
star297 | 0:3d7d1aec706b | 164 | void line( int x1,int y1,int x2,int y2,unsigned int color); |
star297 | 0:3d7d1aec706b | 165 | void circle (int radius, int x, int y,unsigned int color,int fill); |
star297 | 0:3d7d1aec706b | 166 | void Fill_Screen(unsigned int color); // fill screen with any colour |
star297 | 0:3d7d1aec706b | 167 | void foreground(unsigned int color); // text color |
star297 | 0:3d7d1aec706b | 168 | void background(unsigned int color); // background color |
star297 | 0:3d7d1aec706b | 169 | void SetFontSize(int); |
star297 | 0:3d7d1aec706b | 170 | void on(); // display on |
star297 | 0:3d7d1aec706b | 171 | void off(); // display off |
star297 | 0:3d7d1aec706b | 172 | void cls(); // clear screen to black |
star297 | 0:3d7d1aec706b | 173 | void dim(); // flip dim/normal |
star297 | 0:3d7d1aec706b | 174 | void contrast(char value); //0~9 low~high |
star297 | 3:a4caac512e13 | 175 | void locate(int16_t column, int16_t row); // text start position |
star297 | 3:a4caac512e13 | 176 | int toRGB(int16_t R,int16_t G,int16_t B); // get color from RGB values 00~FF(0~255) |
star297 | 0:3d7d1aec706b | 177 | int row(); |
star297 | 0:3d7d1aec706b | 178 | int column(); |
star297 | 0:3d7d1aec706b | 179 | void ScrollSet(int horizontal, int startline, int linecount, int vertical , int frame_interval); |
star297 | 0:3d7d1aec706b | 180 | void Scrollstart(); |
star297 | 0:3d7d1aec706b | 181 | void Scrollstop(); |
star297 | 0:3d7d1aec706b | 182 | void Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y); |
star297 | 3:a4caac512e13 | 183 | void character(int x, int y, int c); // Print single character, x & y pixel co-ords. |
star297 | 3:a4caac512e13 | 184 | void set_font(unsigned char* f); // set external font. Use oled.set_font(NULL) to restore default font |
star297 | 3:a4caac512e13 | 185 | 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 |
star297 | 3:a4caac512e13 | 186 | void Bitmap16(int16_t x, int16_t y, int16_t w, int16_t h,unsigned char *bitmap); // load bitmap from flash |
star297 | 3:a4caac512e13 | 187 | int Bitmap16SD( int16_t x, int16_t y, unsigned char *Name_BMP); // uses up to 12.288k RAM, Fast |
star297 | 3:a4caac512e13 | 188 | int Bitmap16SD2(int16_t x, int16_t y, unsigned char *Name_BMP); // uses 96b RAM, slower |
star297 | 3:a4caac512e13 | 189 | |
star297 | 0:3d7d1aec706b | 190 | protected: |
star297 | 0:3d7d1aec706b | 191 | // Stream implementation functions |
star297 | 0:3d7d1aec706b | 192 | virtual int _putc(int c); |
star297 | 0:3d7d1aec706b | 193 | virtual int _getc(); |
star297 | 0:3d7d1aec706b | 194 | private: |
star297 | 0:3d7d1aec706b | 195 | void Init(void); |
star297 | 0:3d7d1aec706b | 196 | void RegWrite(unsigned char Command); |
star297 | 0:3d7d1aec706b | 197 | void RegWriteM(unsigned char *Command, int count); |
star297 | 0:3d7d1aec706b | 198 | void DataWrite(unsigned char c); |
star297 | 0:3d7d1aec706b | 199 | void DataWrite_to(unsigned int Dat); |
star297 | 0:3d7d1aec706b | 200 | void FontSizeConvert(int *lpx, int *lpy); |
star297 | 0:3d7d1aec706b | 201 | void PutChar(int x,int y,unsigned int a); |
star297 | 1:f3f6624f45d4 | 202 | void PutCharInt(int x,int y,unsigned int a); |
star297 | 1:f3f6624f45d4 | 203 | void putp(int colour); |
star297 | 1:f3f6624f45d4 | 204 | unsigned char* font; |
star297 | 0:3d7d1aec706b | 205 | unsigned int Char_Color; // text color |
star297 | 0:3d7d1aec706b | 206 | unsigned int BGround_Color; // background color |
star297 | 3:a4caac512e13 | 207 | void window(int16_t x, int16_t y, int16_t w, int16_t h); |
star297 | 2:1204274fad8f | 208 | void Maxwindow(); // reset display window to full size |
star297 | 1:f3f6624f45d4 | 209 | // pixel location |
star297 | 1:f3f6624f45d4 | 210 | short _x; |
star297 | 1:f3f6624f45d4 | 211 | short _y; |
star297 | 1:f3f6624f45d4 | 212 | |
star297 | 1:f3f6624f45d4 | 213 | // window location |
star297 | 1:f3f6624f45d4 | 214 | short _x1; |
star297 | 1:f3f6624f45d4 | 215 | short _x2; |
star297 | 1:f3f6624f45d4 | 216 | short _y1; |
star297 | 1:f3f6624f45d4 | 217 | short _y2; |
star297 | 1:f3f6624f45d4 | 218 | int char_x; |
star297 | 1:f3f6624f45d4 | 219 | int char_y; |
star297 | 0:3d7d1aec706b | 220 | int chr_size; |
star297 | 1:f3f6624f45d4 | 221 | int cwidth; // character's width |
star297 | 1:f3f6624f45d4 | 222 | int cvert; // character's height |
star297 | 1:f3f6624f45d4 | 223 | int externalfont; |
star297 | 0:3d7d1aec706b | 224 | DigitalOut CS, RES, DC; |
star297 | 0:3d7d1aec706b | 225 | SPI spi; // mosi, miso, sclk |
star297 | 2:1204274fad8f | 226 | |
star297 | 0:3d7d1aec706b | 227 | }; |
star297 | 0:3d7d1aec706b | 228 | |
star297 | 1:f3f6624f45d4 | 229 | #endif |
star297 | 1:f3f6624f45d4 | 230 |