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
- Committer:
- star297
- Date:
- 2016-05-12
- Revision:
- 3:a4caac512e13
- Parent:
- 2:1204274fad8f
- Child:
- 4:062b4708eb35
File content as of revision 3:a4caac512e13:
#ifndef __ssd1331_H__
#define __ssd1331_H__
#include "mbed.h"
// Screen Settings
#define width 96-1 // Max X axial direction in screen
#define height 64-1 // Max Y axial direction in screen
#define Set_Column_Address 0x15
#define Set_Row_Address 0x75
#define contrastA 0x81
#define contrastB 0x82
#define contrastC 0x83
#define display_on 0xAF
#define display_off 0xAE
// Font size
#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
// GAC hardware acceleration commands
#define GAC_DRAW_LINE 0x21 // Draw Line
#define GAC_DRAW_RECTANGLE 0x22 // Rectangle
#define GAC_COPY_AREA 0x23 // Copy Area
#define GAC_DIM_WINDOW 0x24 // Dim Window
#define GAC_CLEAR_WINDOW 0x25 // Clear Window
#define GAC_FILL_ENABLE_DISABLE 0x26 // Enable Fill
#define SCROLL_SETUP 0x27 // Setup scroll
#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"
#include "ssd1331.h"
ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z)
char Time[50],Date[50];
void gettime();
int main() {
while(1){
oled.Fill_Screen(oled.toRGB(255,0,0)); //red
wait_ms(500);
oled.Fill_Screen(oled.toRGB(0,255,0)); //green
wait_ms(500);
oled.Fill_Screen(oled.toRGB(0,0,255)); //blue
wait_ms(500);
oled.Fill_Screen(oled.toRGB(255,255,255)); //white
wait_ms(500);
oled.cls(); // clear screen to black
oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle
oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle
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
for(int 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
oled.printf("%d",y); // std printf
wait_ms(300);
}
wait_ms(1000);
oled.contrast(9); // set contrast to maximum
wait_ms(2000);
oled.cls();
oled.SetFontSize(HIGH); // set tall font
oled.foreground(oled.toRGB(0,255,0)); // set text colour
oled.locate(0, 10);
oled.printf( "HIGH 12345");
oled.SetFontSize(WIDE); // set text to wide
oled.foreground(oled.toRGB(0,0,255));
oled.locate(0, 28);
oled.printf( "WIDE 123");
oled.SetFontSize(WH); // set text to wide and tall
oled.foreground(oled.toRGB(255,0,0));
oled.locate(0, 40);
oled.printf( "WH 123");
oled.SetFontSize(NORMAL); // set text to normal
oled.foreground(oled.toRGB(255,255,255));
oled.ScrollSet(0,8,18,1,0); // set scroll function
oled.Scrollstart(); // start scroll
gettime();wait(1);gettime();wait(1);gettime();wait(1);
oled.ScrollSet(0,8,18,-2,0);
oled.Scrollstart();
gettime();wait(1);gettime();wait(1);gettime();wait(1);
oled.ScrollSet(0,8,18,3,0);
oled.Scrollstart();
gettime();wait(1);gettime();wait(1);gettime();wait(1);
oled.ScrollSet(0,8,18,-4,0);
oled.Scrollstart();
gettime();wait(1);gettime();wait(1);gettime();wait(1);
oled.Scrollstop(); // stop scroll
wait(1);
}
}
void gettime()
{
time_t seconds = time(NULL);
strftime(Time,40,"%H:%M:%S %a", localtime(&seconds));
strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
oled.locate(0, 0);
oled.printf(Time);
}
*/
class ssd1331 : public Stream {
public:
// 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 on(); // display on
void off(); // display off
void cls(); // clear screen to black
void dim(); // flip dim/normal
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 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
protected:
// Stream implementation functions
virtual int _putc(int c);
virtual int _getc();
private:
void Init(void);
void RegWrite(unsigned char Command);
void RegWriteM(unsigned char *Command, int count);
void DataWrite(unsigned char c);
void DataWrite_to(unsigned int 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 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);
void Maxwindow(); // reset display window to full size
// pixel location
short _x;
short _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;
DigitalOut CS, RES, DC;
SPI spi; // mosi, miso, sclk
};
#endif
Display Module .95" 96x64 Oled with SPI interface