Kevin Kadooka / SSD1351

Dependencies:   BurstSPI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSD1351.h Source File

SSD1351.h

00001 #ifndef SSD1351_h
00002 #define SSD1351_h
00003 
00004 #include "mbed.h"
00005 #include "BurstSPI.h"
00006 
00007 //Pinouts for OLED SPI interface
00008 #define OLED_MOSI  PA_7
00009 #define OLED_SCLK  PA_5
00010 #define OLED_CS    PA_6
00011 #define OLED_DC    PC_5
00012 #define OLED_RST   PA_4
00013 
00014 //SSD1351 Regs
00015 #define SSD1351_CMD_SETCOLUMN       0x15
00016 #define SSD1351_CMD_SETROW          0x75
00017 #define SSD1351_CMD_WRITERAM        0x5C
00018 #define SSD1351_CMD_READRAM         0x5D
00019 #define SSD1351_CMD_SETREMAP        0xA0
00020 #define SSD1351_CMD_STARTLINE       0xA1
00021 #define SSD1351_CMD_DISPLAYOFFSET   0xA2
00022 #define SSD1351_CMD_DISPLAYALLOFF   0xA4
00023 #define SSD1351_CMD_DISPLAYALLON    0xA5
00024 #define SSD1351_CMD_NORMALDISPLAY   0xA6
00025 #define SSD1351_CMD_INVERTDISPLAY   0xA7
00026 #define SSD1351_CMD_FUNCTIONSELECT  0xAB
00027 #define SSD1351_CMD_DISPLAYOFF      0xAE
00028 #define SSD1351_CMD_DISPLAYON       0xAF
00029 #define SSD1351_CMD_PRECHARGE       0xB1
00030 #define SSD1351_CMD_DISPLAYENHANCE  0xB2
00031 #define SSD1351_CMD_CLOCKDIV        0xB3
00032 #define SSD1351_CMD_SETVSL          0xB4
00033 #define SSD1351_CMD_SETGPIO         0xB5
00034 #define SSD1351_CMD_PRECHARGE2      0xB6
00035 #define SSD1351_CMD_SETGRAY         0xB8
00036 #define SSD1351_CMD_USELUT          0xB9
00037 #define SSD1351_CMD_PRECHARGELEVEL  0xBB
00038 #define SSD1351_CMD_VCOMH           0xBE
00039 #define SSD1351_CMD_CONTRASTABC     0xC1
00040 #define SSD1351_CMD_CONTRASTMASTER  0xC7
00041 #define SSD1351_CMD_MUXRATIO        0xCA
00042 #define SSD1351_CMD_COMMANDLOCK     0xFD
00043 #define SSD1351_CMD_HORIZSCROLL     0x96
00044 #define SSD1351_CMD_STOPSCROLL      0x9E
00045 #define SSD1351_CMD_STARTSCROLL     0x9F
00046 
00047 /** SSD1351 Library for STM32F401RE Nucleo or STMstation P.1 development boards - may work with
00048  *  other targets, but not tested yet.
00049  *  
00050  *  Standard mbed SPI library is VERY slow, limiting frame rate. Using EricWieser's BurstSPI (which
00051  *  fixes compilation errors on the STM32F4XX improves throughput significantly.
00052  *  
00053  *  Tested on SSD1351 P/N UG-2828GDEDF11. May work with other SSD1351 panels but this is untested.
00054  *
00055  *  Example:
00056  *  @code
00057  *  
00058  *  #include "mbed.h"
00059  *  #include "SSD1351.h"
00060  *
00061  *  uint8_t buffer[128*128*2];
00062  *
00063  *  SSD1351 oled;
00064  *  //SSD1351 oled(PA_7,PA_5,PA_6,PC_5,PA_4);
00065  *  
00066  *
00067  *  int main(){
00068  *      oled.enableWrite();
00069  *      oled.setBuf(buffer);
00070  *      oled.fillBuf(0x0000);
00071  *      oled.printText("Hello World!",0,0,0x07E0,1)
00072  *      oled.writeBuf();
00073  *  }
00074  *
00075  *  @endcode
00076  */
00077 class SSD1351{
00078     public:
00079         /** Connect to an SSD1351 on specified pins
00080          *  Connect to: (MOSI, SCLK)  <-- Native SPI pins
00081          *              (DC, CS, RST) <-- Any digital pins
00082          */
00083         SSD1351(PinName mosi_pin, PinName sclk_pin, PinName dc_pin, PinName cs_pin, PinName rst_pin);
00084         /** Connect to SSD1351 on STMstation P.1, or default pins specified in defines in SSD1351.h
00085          *  Default pins are:   MOSI    PA_7
00086          *                      SCLK    PA_5
00087          *                      CS      PA_6
00088          *                      DC      PC_5
00089          *                      RST     PA_4
00090          */
00091         SSD1351();
00092                
00093         //Rectangle fill without buffer
00094         //void fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor);
00095         
00096         /** Enable writing directly to the VRAM. This must be called at least once before calling 
00097          *  writeBuf().
00098          */
00099         void enableWrite();
00100         /** Fill the buffer with a single color
00101          *  @param fillcolor Unsigned 16-bit 565 RGB
00102          */
00103         void fillBuf(uint16_t fillcolor);
00104         /** Write the buffer to the VRAM. Make sure you call enableWrte() before doing this!
00105          */
00106         void writeBuf();
00107         /** Draw a sprite from flash memory, into the buffer
00108          *  @param s[]  Sprite containing unsigned 16-bit 565 RGB values (1D vector)
00109          *  @param x    x-coordinate of sprite
00110          *  @param y    y-coordinate of sprite
00111          *  @param w    Width of sprite
00112          *  @param h    Height of sprite
00113          *  @param mask This value in the sprite is "skipped" - transparancy value
00114          */
00115         void drawSpritePtr(const uint16_t s[] ,int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask);
00116         /** Fill the collision mask with a single value
00117          *  @param state Fill value
00118          */
00119         void fillCMask(uint8_t state);
00120         /** Draw a sprite from flash memory, into the collision map
00121          *  @param s[]  Sprite containing unsigned 16-bit 565 RGB values (1D vector)
00122          *  @param x     x-coordinate of sprite
00123          *  @param y     y-coordinate of sprite
00124          *  @param w     Width of sprite
00125          *  @param h     Height of sprite
00126          *  @param mask  This value in the sprite is "skipped" - transparancy value
00127          *  @param state Value written to the collision map
00128          */
00129         void drawCMask(const uint16_t s[], int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask, uint8_t state);
00130         /** Check if a sprite (not yet written to collision map) is going to collide with anything
00131          *  @param s[]  Sprite containing unsigned 16-bit 565 RGB values (1D vector)
00132          *  @param x    x-coordinate of sprite
00133          *  @param y    y-coordinate of sprite
00134          *  @param w    Width of sprite
00135          *  @param h    Height of sprite
00136          *  @param mask This value in the sprite is "skipped" - transparancy value
00137          */
00138         uint8_t checkCollision(const uint16_t s[], int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask);
00139         /** Draw a single character
00140          *  @param c     ASCII character 
00141          *  @param x     x-coordinate of character   
00142          *  @param y     y-coordinate of character   
00143          *  @param color Unsigned 16-bit 565 RGB   
00144          *  @param zoom  Scaling factor   
00145          */
00146         void drawChar(char c, int16_t x, int16_t y, uint16_t color, uint8_t zoom);
00147         /** Draw a single character
00148          *  @param c     Char array
00149          *  @param x     x-coordinate of character   
00150          *  @param y     y-coordinate of character   
00151          *  @param color Unsigned 16-bit 565 RGB   
00152          *  @param zoom  Scaling factor   
00153          */
00154         void printText(const char c[], int16_t x, int16_t y, uint16_t color, uint8_t zoom);
00155         /** Set the display buffer.
00156          *  @param _buf Buffer, must be uint8_t name[32768]
00157          */
00158         void setBuf(uint8_t* _buf);
00159         /** Set the collision map.
00160          *  @param _cmask Collision map, must be uint8_t name[16384]
00161          */
00162         void setCMask(uint8_t* _cmask);
00163         
00164         //Drawing primitives
00165         
00166         /** Draw a filled rectangle to buffer
00167          *  @param x     x-coordinate
00168          *  @param y     y-coordinate
00169          *  @param w     Width
00170          *  @param h     Height
00171          *  @param color Unsigned 16-bit 565 RGB
00172          */
00173         void fillRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color);
00174         /** Draw a open rectangle to buffer
00175          *  @param x     x-coordinate
00176          *  @param y     y-coordinate
00177          *  @param w     Width
00178          *  @param h     Height
00179          *  @param color Unsigned 16-bit 565 RGB
00180          */
00181         void openRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color);
00182         /** Draw a horizontal line without position calculations
00183          *  @param x      x-coordinate
00184          *  @param y      y-coordinate
00185          *  @param length Length, can be positive or negative
00186          *  @param color  Unsigned 16-bit 565 RGB
00187          */
00188         void drawHLine(int16_t x, int16_t y, int16_t length, uint16_t color);
00189         /** Draw a vertical line without position calculations
00190          *  @param x      x-coordinate
00191          *  @param y      y-coordinate
00192          *  @param length Length, can be positive or negative
00193          *  @param color  Unsigned 16-bit 565 RGB
00194          */
00195         void drawVLine(int16_t x, int16_t y, int16_t length, uint16_t color);
00196         /** Draw a line using Bresenham algorithm
00197          *  @param x1     Start x-coordinate
00198          *  @param y1     Start y-coordinate
00199          *  @param x2     End x-coordinate
00200          *  @param y2     End y-coordinate
00201          *  @param color  Unsigned 16-bit 565 RGB
00202          */
00203         void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
00204         /** Draw open circle
00205          *  @param x0     Center x-coordinate
00206          *  @param y0     Center y-coordinate
00207          *  @param radius Circle radius
00208          *  @param color  Unsigned 16-bit 565 RGB
00209          */
00210         void openCircle(int16_t x0, int16_t y0, uint16_t radius, uint16_t color);
00211         /** Draw filled circle
00212          *  @param x0     Center x-coordinate
00213          *  @param y0     Center y-coordinate
00214          *  @param radius Circle radius
00215          *  @param color  Unsigned 16-bit 565 RGB
00216          */
00217         void fillCircle(int16_t x0, int16_t y0, uint16_t radius, uint16_t color);
00218         
00219     private:
00220         void begin();
00221         void spiwrite(uint8_t c);
00222         void writeCommand(uint8_t c);
00223         void writeData(uint8_t c);
00224         DigitalOut cs, dc, rst;
00225         BurstSPI spi;
00226         uint8_t *buf, *collisionmask;
00227 };
00228 
00229 #endif