Test for STM32F4

Dependents:   Nucleo_SSD1331

Fork of ssd1331 by Paul Staron

Committer:
star297
Date:
Sun May 01 12:37:44 2016 +0000
Revision:
1:f3f6624f45d4
Parent:
0:3d7d1aec706b
Child:
2:1204274fad8f
Added external font and bitmap handling.

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 // 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 0:3d7d1aec706b 39 // example code
star297 0:3d7d1aec706b 40 /*
star297 0:3d7d1aec706b 41 #include "mbed.h"
star297 0:3d7d1aec706b 42 #include "ssd1331.h"
star297 0:3d7d1aec706b 43
star297 0:3d7d1aec706b 44 ssd1331 oled(D8, D9, D10, D11, NC, D13); // cs, res, dc, miso(nc), sck (KL25z)
star297 0:3d7d1aec706b 45
star297 0:3d7d1aec706b 46 char Time[50],Date[50];
star297 0:3d7d1aec706b 47 void gettime();
star297 0:3d7d1aec706b 48
star297 0:3d7d1aec706b 49 int main() {
star297 0:3d7d1aec706b 50
star297 0:3d7d1aec706b 51 while(1){
star297 0:3d7d1aec706b 52
star297 0:3d7d1aec706b 53 oled.Fill_Screen(oled.toRGB(255,0,0)); //red
star297 0:3d7d1aec706b 54 wait_ms(500);
star297 0:3d7d1aec706b 55 oled.Fill_Screen(oled.toRGB(0,255,0)); //green
star297 0:3d7d1aec706b 56 wait_ms(500);
star297 0:3d7d1aec706b 57 oled.Fill_Screen(oled.toRGB(0,0,255)); //blue
star297 0:3d7d1aec706b 58 wait_ms(500);
star297 0:3d7d1aec706b 59 oled.Fill_Screen(oled.toRGB(255,255,255)); //white
star297 0:3d7d1aec706b 60 wait_ms(500);
star297 0:3d7d1aec706b 61
star297 0:3d7d1aec706b 62 oled.cls(); // clear screen to black
star297 0:3d7d1aec706b 63
star297 0:3d7d1aec706b 64 oled.circle (20, 40, 30 ,oled.toRGB(0,0,255) , 1); //fill circle
star297 0:3d7d1aec706b 65 oled.circle (20, 40, 30 ,oled.toRGB(255,255,255) , 0); //circle
star297 0:3d7d1aec706b 66 oled.circle (20, 60, 40 ,oled.toRGB(255,0,0) , 0); //circle
star297 0:3d7d1aec706b 67 oled.line( 0, 0, width, height, oled.toRGB(0,255,255)); //line
star297 0:3d7d1aec706b 68 oled.line( width, 0, 0, height, oled.toRGB(255,0,255)); //line
star297 0:3d7d1aec706b 69 oled.rectangle(10,10,90,60,oled.toRGB(255,255,0)); //rectangle
star297 0:3d7d1aec706b 70 oled.fillrectangle(20,20,40,40,oled.toRGB(255,255,255),oled.toRGB(0,255,0)); //fillrectangle
star297 0:3d7d1aec706b 71
star297 0:3d7d1aec706b 72 for(int y = 9; y >= 0; y--) {
star297 0:3d7d1aec706b 73 oled.contrast(y); // set contrast level
star297 0:3d7d1aec706b 74 oled.foreground(oled.toRGB(255,255,255)); // set text colour
star297 0:3d7d1aec706b 75 oled.locate(1, 10); // set text start location
star297 0:3d7d1aec706b 76 oled.printf("%d",y); // std printf
star297 0:3d7d1aec706b 77 wait_ms(300);
star297 0:3d7d1aec706b 78 }
star297 0:3d7d1aec706b 79
star297 0:3d7d1aec706b 80 wait_ms(1000);
star297 0:3d7d1aec706b 81 oled.contrast(9); // set contrast to maximum
star297 0:3d7d1aec706b 82 wait_ms(2000);
star297 0:3d7d1aec706b 83 oled.cls();
star297 0:3d7d1aec706b 84
star297 0:3d7d1aec706b 85 oled.SetFontSize(HIGH); // set tall font
star297 0:3d7d1aec706b 86 oled.foreground(oled.toRGB(0,255,0)); // set text colour
star297 0:3d7d1aec706b 87 oled.locate(0, 10);
star297 0:3d7d1aec706b 88 oled.printf( "HIGH 12345");
star297 0:3d7d1aec706b 89
star297 0:3d7d1aec706b 90 oled.SetFontSize(WIDE); // set text to wide
star297 0:3d7d1aec706b 91 oled.foreground(oled.toRGB(0,0,255));
star297 0:3d7d1aec706b 92 oled.locate(0, 28);
star297 0:3d7d1aec706b 93 oled.printf( "WIDE 123");
star297 0:3d7d1aec706b 94
star297 0:3d7d1aec706b 95 oled.SetFontSize(WH); // set text to wide and tall
star297 0:3d7d1aec706b 96 oled.foreground(oled.toRGB(255,0,0));
star297 0:3d7d1aec706b 97 oled.locate(0, 40);
star297 0:3d7d1aec706b 98 oled.printf( "WH 123");
star297 0:3d7d1aec706b 99
star297 0:3d7d1aec706b 100 oled.SetFontSize(NORMAL); // set text to normal
star297 0:3d7d1aec706b 101 oled.foreground(oled.toRGB(255,255,255));
star297 0:3d7d1aec706b 102
star297 0:3d7d1aec706b 103 oled.ScrollSet(0,8,18,1,0); // set scroll function
star297 0:3d7d1aec706b 104 oled.Scrollstart(); // start scroll
star297 0:3d7d1aec706b 105
star297 0:3d7d1aec706b 106 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 107 oled.ScrollSet(0,8,18,-2,0);
star297 0:3d7d1aec706b 108 oled.Scrollstart();
star297 0:3d7d1aec706b 109 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 110
star297 0:3d7d1aec706b 111 oled.ScrollSet(0,8,18,3,0);
star297 0:3d7d1aec706b 112 oled.Scrollstart();
star297 0:3d7d1aec706b 113
star297 0:3d7d1aec706b 114 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 115
star297 0:3d7d1aec706b 116 oled.ScrollSet(0,8,18,-4,0);
star297 0:3d7d1aec706b 117 oled.Scrollstart();
star297 0:3d7d1aec706b 118
star297 0:3d7d1aec706b 119 gettime();wait(1);gettime();wait(1);gettime();wait(1);
star297 0:3d7d1aec706b 120
star297 0:3d7d1aec706b 121 oled.Scrollstop(); // stop scroll
star297 0:3d7d1aec706b 122 wait(1);
star297 0:3d7d1aec706b 123 }
star297 0:3d7d1aec706b 124 }
star297 0:3d7d1aec706b 125 void gettime()
star297 0:3d7d1aec706b 126 {
star297 0:3d7d1aec706b 127 time_t seconds = time(NULL);
star297 0:3d7d1aec706b 128 strftime(Time,40,"%H:%M:%S %a", localtime(&seconds));
star297 0:3d7d1aec706b 129 strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
star297 0:3d7d1aec706b 130 oled.locate(0, 0);
star297 0:3d7d1aec706b 131 oled.printf(Time);
star297 0:3d7d1aec706b 132 }
star297 0:3d7d1aec706b 133 */
star297 0:3d7d1aec706b 134
star297 0:3d7d1aec706b 135 class ssd1331 : public Stream {
star297 0:3d7d1aec706b 136 public:
star297 0:3d7d1aec706b 137 // constructor
star297 0:3d7d1aec706b 138 ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
star297 0:3d7d1aec706b 139
star297 0:3d7d1aec706b 140 void pixel(int x,int y,unsigned int color);
star297 0:3d7d1aec706b 141 void rectangle(int x1,int y1,int x2,int y2,unsigned int colorline);
star297 0:3d7d1aec706b 142 void fillrectangle(int x1,int y1,int x2,int y2,unsigned int colorline,unsigned int colorfill);
star297 0:3d7d1aec706b 143 void line( int x1,int y1,int x2,int y2,unsigned int color);
star297 0:3d7d1aec706b 144 void circle (int radius, int x, int y,unsigned int color,int fill);
star297 0:3d7d1aec706b 145 void Fill_Screen(unsigned int color); // fill screen with any colour
star297 0:3d7d1aec706b 146 void foreground(unsigned int color); // text color
star297 0:3d7d1aec706b 147 void background(unsigned int color); // background color
star297 0:3d7d1aec706b 148 void SetFontSize(int);
star297 0:3d7d1aec706b 149 void on(); // display on
star297 0:3d7d1aec706b 150 void off(); // display off
star297 0:3d7d1aec706b 151 void cls(); // clear screen to black
star297 0:3d7d1aec706b 152 void dim(); // flip dim/normal
star297 0:3d7d1aec706b 153 void contrast(char value); //0~9 low~high
star297 0:3d7d1aec706b 154 void locate(int column, int row); // text start position
star297 0:3d7d1aec706b 155 int toRGB(int R,int G,int B); // get color from RGB values 00~FF(0~255)
star297 0:3d7d1aec706b 156 int row();
star297 0:3d7d1aec706b 157 int column();
star297 0:3d7d1aec706b 158 void ScrollSet(int horizontal, int startline, int linecount, int vertical , int frame_interval);
star297 0:3d7d1aec706b 159 void Scrollstart();
star297 0:3d7d1aec706b 160 void Scrollstop();
star297 0:3d7d1aec706b 161 void Copy(int src_x1,int src_y1,int src_x2,int src_y2, int dst_x,int dst_y);
star297 1:f3f6624f45d4 162 void character(int x, int y, int c);
star297 1:f3f6624f45d4 163 void set_font(unsigned char* f);
star297 1:f3f6624f45d4 164 void drawBitmap(const uint8_t *bitmap, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
star297 1:f3f6624f45d4 165 int BMP_16(const char *Name_BMP, unsigned int x, unsigned int y);
star297 1:f3f6624f45d4 166
star297 1:f3f6624f45d4 167 int DrawBitmapFile(const char *Name_BMP);
star297 1:f3f6624f45d4 168
star297 0:3d7d1aec706b 169
star297 0:3d7d1aec706b 170 protected:
star297 0:3d7d1aec706b 171 // Stream implementation functions
star297 0:3d7d1aec706b 172 virtual int _putc(int c);
star297 0:3d7d1aec706b 173 virtual int _getc();
star297 0:3d7d1aec706b 174 private:
star297 0:3d7d1aec706b 175 void Init(void);
star297 0:3d7d1aec706b 176 void RegWrite(unsigned char Command);
star297 0:3d7d1aec706b 177 void RegWriteM(unsigned char *Command, int count);
star297 0:3d7d1aec706b 178 void DataWrite(unsigned char c);
star297 0:3d7d1aec706b 179 void DataWrite_to(unsigned int Dat);
star297 0:3d7d1aec706b 180 void FontSizeConvert(int *lpx, int *lpy);
star297 0:3d7d1aec706b 181 void PutChar(int x,int y,unsigned int a);
star297 1:f3f6624f45d4 182 void PutCharInt(int x,int y,unsigned int a);
star297 1:f3f6624f45d4 183 void putp(int colour);
star297 1:f3f6624f45d4 184 unsigned char* font;
star297 0:3d7d1aec706b 185 unsigned int Char_Color; // text color
star297 0:3d7d1aec706b 186 unsigned int BGround_Color; // background color
star297 1:f3f6624f45d4 187 void window(int x, int y, int w, int h);
star297 1:f3f6624f45d4 188 // pixel location
star297 1:f3f6624f45d4 189 short _x;
star297 1:f3f6624f45d4 190 short _y;
star297 1:f3f6624f45d4 191
star297 1:f3f6624f45d4 192 // window location
star297 1:f3f6624f45d4 193 short _x1;
star297 1:f3f6624f45d4 194 short _x2;
star297 1:f3f6624f45d4 195 short _y1;
star297 1:f3f6624f45d4 196 short _y2;
star297 1:f3f6624f45d4 197 int char_x;
star297 1:f3f6624f45d4 198 int char_y;
star297 0:3d7d1aec706b 199 int chr_size;
star297 1:f3f6624f45d4 200 int cwidth; // character's width
star297 1:f3f6624f45d4 201 int cvert; // character's height
star297 1:f3f6624f45d4 202 int externalfont;
star297 0:3d7d1aec706b 203 DigitalOut CS, RES, DC;
star297 0:3d7d1aec706b 204 SPI spi; // mosi, miso, sclk
star297 0:3d7d1aec706b 205 };
star297 0:3d7d1aec706b 206
star297 1:f3f6624f45d4 207 #endif
star297 1:f3f6624f45d4 208