Test for STM32F4

Dependents:   Nucleo_SSD1331

Fork of ssd1331 by Paul Staron

Committer:
star297
Date:
Tue Apr 26 22:51:28 2016 +0000
Revision:
0:3d7d1aec706b
Child:
1:f3f6624f45d4
Initial version v1.0

Who changed what in which revision?

UserRevisionLine numberNew 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
star297 0:3d7d1aec706b 8 // Screen Settings
star297 0:3d7d1aec706b 9 #define width 96-1 // Max X axial direction in screen
star297 0:3d7d1aec706b 10 #define height 64-1 // Max Y axial direction in screen
star297 0:3d7d1aec706b 11 #define Set_Column_Address 0x15
star297 0:3d7d1aec706b 12 #define Set_Row_Address 0x75
star297 0:3d7d1aec706b 13 #define contrastA 0x81
star297 0:3d7d1aec706b 14 #define contrastB 0x82
star297 0:3d7d1aec706b 15 #define contrastC 0x83
star297 0:3d7d1aec706b 16 #define display_on 0xAF
star297 0:3d7d1aec706b 17 #define display_off 0xAE
star297 0:3d7d1aec706b 18
star297 0:3d7d1aec706b 19 // Font size
star297 0:3d7d1aec706b 20 #define NORMAL 0
star297 0:3d7d1aec706b 21 #define WIDE 1
star297 0:3d7d1aec706b 22 #define HIGH 2
star297 0:3d7d1aec706b 23 #define WH 3
star297 0:3d7d1aec706b 24 #define WHx36 4
star297 0:3d7d1aec706b 25 #define X_width 6 // character's width
star297 0:3d7d1aec706b 26 #define Y_width 8 // character's height
star297 0:3d7d1aec706b 27
star297 0:3d7d1aec706b 28
star297 0:3d7d1aec706b 29 // GAC hardware acceleration commands
star297 0:3d7d1aec706b 30 #define GAC_DRAW_LINE 0x21 // Draw Line
star297 0:3d7d1aec706b 31 #define GAC_DRAW_RECTANGLE 0x22 // Rectangle
star297 0:3d7d1aec706b 32 #define GAC_COPY_AREA 0x23 // Copy Area
star297 0:3d7d1aec706b 33 #define GAC_DIM_WINDOW 0x24 // Dim Window
star297 0:3d7d1aec706b 34 #define GAC_CLEAR_WINDOW 0x25 // Clear Window
star297 0:3d7d1aec706b 35 #define GAC_FILL_ENABLE_DISABLE 0x26 // Enable Fill
star297 0:3d7d1aec706b 36 #define SCROLL_SETUP 0x27 // Setup scroll
star297 0:3d7d1aec706b 37 #define SCROLL_STOP 0x2E // Scroll Stop
star297 0:3d7d1aec706b 38 #define SCROLL_START 0x2F // Scroll Start
star297 0:3d7d1aec706b 39
star297 0:3d7d1aec706b 40 // example code
star297 0:3d7d1aec706b 41 /*
star297 0:3d7d1aec706b 42 #include "mbed.h"
star297 0:3d7d1aec706b 43 #include "ssd1331.h"
star297 0:3d7d1aec706b 44
star297 0:3d7d1aec706b 45 ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z)
star297 0:3d7d1aec706b 46
star297 0:3d7d1aec706b 47 char Time[50],Date[50];
star297 0:3d7d1aec706b 48 void gettime();
star297 0:3d7d1aec706b 49
star297 0:3d7d1aec706b 50 int main() {
star297 0:3d7d1aec706b 51
star297 0:3d7d1aec706b 52 while(1){
star297 0:3d7d1aec706b 53
star297 0:3d7d1aec706b 54 oled.Fill_Screen(oled.toRGB(255,0,0)); //red
star297 0:3d7d1aec706b 55 wait_ms(500);
star297 0:3d7d1aec706b 56 oled.Fill_Screen(oled.toRGB(0,255,0)); //green
star297 0:3d7d1aec706b 57 wait_ms(500);
star297 0:3d7d1aec706b 58 oled.Fill_Screen(oled.toRGB(0,0,255)); //blue
star297 0:3d7d1aec706b 59 wait_ms(500);
star297 0:3d7d1aec706b 60 oled.Fill_Screen(oled.toRGB(255,255,255)); //white
star297 0:3d7d1aec706b 61 wait_ms(500);
star297 0:3d7d1aec706b 62
star297 0:3d7d1aec706b 63 oled.cls(); // clear screen to black
star297 0:3d7d1aec706b 64
star297 0:3d7d1aec706b 65 oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle
star297 0:3d7d1aec706b 66 oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle
star297 0:3d7d1aec706b 67 oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0); //circle
star297 0:3d7d1aec706b 68 oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line
star297 0:3d7d1aec706b 69 oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line
star297 0:3d7d1aec706b 70 oled.rectangle(10,10,90,60,oled.toRGB(255,255,0)); //rectangle
star297 0:3d7d1aec706b 71 oled.fillrectangle(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle
star297 0:3d7d1aec706b 72
star297 0:3d7d1aec706b 73 for(int y = 9; y >= 0; y--) {
star297 0:3d7d1aec706b 74 oled.contrast(y); // set contrast level
star297 0:3d7d1aec706b 75 oled.foreground(oled.toRGB(255,255,255)); // set text colour
star297 0:3d7d1aec706b 76 oled.locate(1, 10); // set text start location
star297 0:3d7d1aec706b 77 oled.printf("%d",y); // std printf
star297 0:3d7d1aec706b 78 wait_ms(300);
star297 0:3d7d1aec706b 79 }
star297 0:3d7d1aec706b 80
star297 0:3d7d1aec706b 81 wait_ms(1000);
star297 0:3d7d1aec706b 82 oled.contrast(9); // set contrast to maximum
star297 0:3d7d1aec706b 83 wait_ms(2000);
star297 0:3d7d1aec706b 84 oled.cls();
star297 0:3d7d1aec706b 85
star297 0:3d7d1aec706b 86 oled.SetFontSize(HIGH); // set tall font
star297 0:3d7d1aec706b 87 oled.foreground(oled.toRGB(0,255,0)); // set text colour
star297 0:3d7d1aec706b 88 oled.locate(0, 10);
star297 0:3d7d1aec706b 89 oled.printf( "HIGH 12345");
star297 0:3d7d1aec706b 90
star297 0:3d7d1aec706b 91 oled.SetFontSize(WIDE); // set text to wide
star297 0:3d7d1aec706b 92 oled.foreground(oled.toRGB(0,0,255));
star297 0:3d7d1aec706b 93 oled.locate(0, 28);
star297 0:3d7d1aec706b 94 oled.printf( "WIDE 123");
star297 0:3d7d1aec706b 95
star297 0:3d7d1aec706b 96 oled.SetFontSize(WH); // set text to wide and tall
star297 0:3d7d1aec706b 97 oled.foreground(oled.toRGB(255,0,0));
star297 0:3d7d1aec706b 98 oled.locate(0, 40);
star297 0:3d7d1aec706b 99 oled.printf( "WH 123");
star297 0:3d7d1aec706b 100
star297 0:3d7d1aec706b 101 oled.SetFontSize(NORMAL); // set text to normal
star297 0:3d7d1aec706b 102 oled.foreground(oled.toRGB(255,255,255));
star297 0:3d7d1aec706b 103
star297 0:3d7d1aec706b 104 oled.ScrollSet(0,8,18,1,0); // set scroll function
star297 0:3d7d1aec706b 105 oled.Scrollstart(); // start scroll
star297 0:3d7d1aec706b 106
star297 0:3d7d1aec706b 107 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 108 oled.ScrollSet(0,8,18,-2,0);
star297 0:3d7d1aec706b 109 oled.Scrollstart();
star297 0:3d7d1aec706b 110 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 111
star297 0:3d7d1aec706b 112 oled.ScrollSet(0,8,18,3,0);
star297 0:3d7d1aec706b 113 oled.Scrollstart();
star297 0:3d7d1aec706b 114
star297 0:3d7d1aec706b 115 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 116
star297 0:3d7d1aec706b 117 oled.ScrollSet(0,8,18,-4,0);
star297 0:3d7d1aec706b 118 oled.Scrollstart();
star297 0:3d7d1aec706b 119
star297 0:3d7d1aec706b 120 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 121
star297 0:3d7d1aec706b 122 oled.Scrollstop(); // stop scroll
star297 0:3d7d1aec706b 123 wait(1);
star297 0:3d7d1aec706b 124 }
star297 0:3d7d1aec706b 125 }
star297 0:3d7d1aec706b 126 void gettime()
star297 0:3d7d1aec706b 127 {
star297 0:3d7d1aec706b 128 time_t seconds = time(NULL);
star297 0:3d7d1aec706b 129 strftime(Time,40,"%H:%M:%S %a", localtime(&seconds));
star297 0:3d7d1aec706b 130 strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
star297 0:3d7d1aec706b 131 oled.locate(0, 0);
star297 0:3d7d1aec706b 132 oled.printf(Time);
star297 0:3d7d1aec706b 133 }
star297 0:3d7d1aec706b 134 */
star297 0:3d7d1aec706b 135
star297 0:3d7d1aec706b 136 class ssd1331 : public Stream {
star297 0:3d7d1aec706b 137 public:
star297 0:3d7d1aec706b 138 // constructor
star297 0:3d7d1aec706b 139 ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
star297 0:3d7d1aec706b 140
star297 0:3d7d1aec706b 141 void pixel(int x,int y,unsigned int color);
star297 0:3d7d1aec706b 142 void rectangle(int x1,int y1,int x2,int y2,unsigned int colorline);
star297 0:3d7d1aec706b 143 void fillrectangle(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill);
star297 0:3d7d1aec706b 144 void line( int x1,int y1,int x2,int y2,unsigned int color);
star297 0:3d7d1aec706b 145 void circle (int radius, int x, int y,unsigned int color,int fill);
star297 0:3d7d1aec706b 146 void Fill_Screen(unsigned int color); // fill screen with any colour
star297 0:3d7d1aec706b 147 void foreground(unsigned int color); // text color
star297 0:3d7d1aec706b 148 void background(unsigned int color); // background color
star297 0:3d7d1aec706b 149 void SetFontSize(int);
star297 0:3d7d1aec706b 150 void on(); // display on
star297 0:3d7d1aec706b 151 void off(); // display off
star297 0:3d7d1aec706b 152 void cls(); // clear screen to black
star297 0:3d7d1aec706b 153 void dim(); // flip dim/normal
star297 0:3d7d1aec706b 154 void contrast(char value); //0~9 low~high
star297 0:3d7d1aec706b 155 void locate(int column, int row); // text start position
star297 0:3d7d1aec706b 156 int toRGB(int R,int G,int B); // get color from RGB values 00~FF(0~255)
star297 0:3d7d1aec706b 157 int row();
star297 0:3d7d1aec706b 158 int column();
star297 0:3d7d1aec706b 159 void ScrollSet(int horizontal, int startline, int linecount, int vertical , int frame_interval);
star297 0:3d7d1aec706b 160 void Scrollstart();
star297 0:3d7d1aec706b 161 void Scrollstop();
star297 0:3d7d1aec706b 162 void Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y);
star297 0:3d7d1aec706b 163
star297 0:3d7d1aec706b 164 protected:
star297 0:3d7d1aec706b 165 // Stream implementation functions
star297 0:3d7d1aec706b 166 virtual int _putc(int c);
star297 0:3d7d1aec706b 167 virtual int _getc();
star297 0:3d7d1aec706b 168 private:
star297 0:3d7d1aec706b 169 void Init(void);
star297 0:3d7d1aec706b 170 void RegWrite(unsigned char Command);
star297 0:3d7d1aec706b 171 void RegWriteM(unsigned char *Command, int count);
star297 0:3d7d1aec706b 172 void DataWrite(unsigned char c);
star297 0:3d7d1aec706b 173 void DataWrite_to(unsigned int Dat);
star297 0:3d7d1aec706b 174 void FontSizeConvert(int *lpx, int *lpy);
star297 0:3d7d1aec706b 175 void PutChar(int x,int y,unsigned int a);
star297 0:3d7d1aec706b 176 unsigned int Char_Color; // text color
star297 0:3d7d1aec706b 177 unsigned int BGround_Color; // background color
star297 0:3d7d1aec706b 178 int x_locate;
star297 0:3d7d1aec706b 179 int y_locate;
star297 0:3d7d1aec706b 180 int chr_size;
star297 0:3d7d1aec706b 181
star297 0:3d7d1aec706b 182 DigitalOut CS, RES, DC;
star297 0:3d7d1aec706b 183 SPI spi; // mosi, miso, sclk
star297 0:3d7d1aec706b 184 };
star297 0:3d7d1aec706b 185
star297 0:3d7d1aec706b 186 #endif