SSD1351 library for the STM32F401RE. Uses BurstSPI and a couple of tweaks to improve throughput.

Dependencies:   BurstSPI

Library for the SSD1351 128 x 128 OLED display, specifically for the STM32F401RE Nucleo/STMstation development boards.

/media/uploads/kkado/imgp1229.jpg

See API documentation for more details and code.

Committer:
kkado
Date:
Sat Jul 15 04:41:23 2017 +0000
Revision:
4:d65b0a5d58f0
Parent:
3:7dd40c4c2ef3
Fixed length of vlines (one too short)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkado 0:5115e0080bd5 1 #ifndef SSD1351_h
kkado 0:5115e0080bd5 2 #define SSD1351_h
kkado 0:5115e0080bd5 3
kkado 0:5115e0080bd5 4 #include "mbed.h"
kkado 0:5115e0080bd5 5 #include "BurstSPI.h"
kkado 0:5115e0080bd5 6
kkado 1:ae4fe66e9c0e 7 //Pinouts for OLED SPI interface
kkado 1:ae4fe66e9c0e 8 #define OLED_MOSI PA_7
kkado 1:ae4fe66e9c0e 9 #define OLED_SCLK PA_5
kkado 1:ae4fe66e9c0e 10 #define OLED_CS PA_6
kkado 1:ae4fe66e9c0e 11 #define OLED_DC PC_5
kkado 1:ae4fe66e9c0e 12 #define OLED_RST PA_4
kkado 2:fb159c293541 13
kkado 1:ae4fe66e9c0e 14 //SSD1351 Regs
kkado 0:5115e0080bd5 15 #define SSD1351_CMD_SETCOLUMN 0x15
kkado 0:5115e0080bd5 16 #define SSD1351_CMD_SETROW 0x75
kkado 0:5115e0080bd5 17 #define SSD1351_CMD_WRITERAM 0x5C
kkado 0:5115e0080bd5 18 #define SSD1351_CMD_READRAM 0x5D
kkado 0:5115e0080bd5 19 #define SSD1351_CMD_SETREMAP 0xA0
kkado 0:5115e0080bd5 20 #define SSD1351_CMD_STARTLINE 0xA1
kkado 0:5115e0080bd5 21 #define SSD1351_CMD_DISPLAYOFFSET 0xA2
kkado 0:5115e0080bd5 22 #define SSD1351_CMD_DISPLAYALLOFF 0xA4
kkado 0:5115e0080bd5 23 #define SSD1351_CMD_DISPLAYALLON 0xA5
kkado 0:5115e0080bd5 24 #define SSD1351_CMD_NORMALDISPLAY 0xA6
kkado 0:5115e0080bd5 25 #define SSD1351_CMD_INVERTDISPLAY 0xA7
kkado 0:5115e0080bd5 26 #define SSD1351_CMD_FUNCTIONSELECT 0xAB
kkado 0:5115e0080bd5 27 #define SSD1351_CMD_DISPLAYOFF 0xAE
kkado 0:5115e0080bd5 28 #define SSD1351_CMD_DISPLAYON 0xAF
kkado 0:5115e0080bd5 29 #define SSD1351_CMD_PRECHARGE 0xB1
kkado 0:5115e0080bd5 30 #define SSD1351_CMD_DISPLAYENHANCE 0xB2
kkado 0:5115e0080bd5 31 #define SSD1351_CMD_CLOCKDIV 0xB3
kkado 0:5115e0080bd5 32 #define SSD1351_CMD_SETVSL 0xB4
kkado 0:5115e0080bd5 33 #define SSD1351_CMD_SETGPIO 0xB5
kkado 0:5115e0080bd5 34 #define SSD1351_CMD_PRECHARGE2 0xB6
kkado 0:5115e0080bd5 35 #define SSD1351_CMD_SETGRAY 0xB8
kkado 0:5115e0080bd5 36 #define SSD1351_CMD_USELUT 0xB9
kkado 0:5115e0080bd5 37 #define SSD1351_CMD_PRECHARGELEVEL 0xBB
kkado 0:5115e0080bd5 38 #define SSD1351_CMD_VCOMH 0xBE
kkado 0:5115e0080bd5 39 #define SSD1351_CMD_CONTRASTABC 0xC1
kkado 0:5115e0080bd5 40 #define SSD1351_CMD_CONTRASTMASTER 0xC7
kkado 0:5115e0080bd5 41 #define SSD1351_CMD_MUXRATIO 0xCA
kkado 0:5115e0080bd5 42 #define SSD1351_CMD_COMMANDLOCK 0xFD
kkado 0:5115e0080bd5 43 #define SSD1351_CMD_HORIZSCROLL 0x96
kkado 0:5115e0080bd5 44 #define SSD1351_CMD_STOPSCROLL 0x9E
kkado 0:5115e0080bd5 45 #define SSD1351_CMD_STARTSCROLL 0x9F
kkado 0:5115e0080bd5 46
kkado 3:7dd40c4c2ef3 47 /** SSD1351 Library for STM32F401RE Nucleo or STMstation P.1 development boards - may work with
kkado 3:7dd40c4c2ef3 48 * other targets, but not tested yet.
kkado 3:7dd40c4c2ef3 49 *
kkado 3:7dd40c4c2ef3 50 * Standard mbed SPI library is VERY slow, limiting frame rate. Using EricWieser's BurstSPI (which
kkado 3:7dd40c4c2ef3 51 * fixes compilation errors on the STM32F4XX improves throughput significantly.
kkado 3:7dd40c4c2ef3 52 *
kkado 3:7dd40c4c2ef3 53 * Tested on SSD1351 P/N UG-2828GDEDF11. May work with other SSD1351 panels but this is untested.
kkado 3:7dd40c4c2ef3 54 *
kkado 3:7dd40c4c2ef3 55 * Example:
kkado 3:7dd40c4c2ef3 56 * @code
kkado 3:7dd40c4c2ef3 57 *
kkado 3:7dd40c4c2ef3 58 * #include "mbed.h"
kkado 3:7dd40c4c2ef3 59 * #include "SSD1351.h"
kkado 3:7dd40c4c2ef3 60 *
kkado 3:7dd40c4c2ef3 61 * uint8_t buffer[128*128*2];
kkado 3:7dd40c4c2ef3 62 *
kkado 3:7dd40c4c2ef3 63 * SSD1351 oled;
kkado 3:7dd40c4c2ef3 64 * //SSD1351 oled(PA_7,PA_5,PA_6,PC_5,PA_4);
kkado 3:7dd40c4c2ef3 65 *
kkado 3:7dd40c4c2ef3 66 *
kkado 3:7dd40c4c2ef3 67 * int main(){
kkado 3:7dd40c4c2ef3 68 * oled.enableWrite();
kkado 3:7dd40c4c2ef3 69 * oled.setBuf(buffer);
kkado 3:7dd40c4c2ef3 70 * oled.fillBuf(0x0000);
kkado 3:7dd40c4c2ef3 71 * oled.printText("Hello World!",0,0,0x07E0,1)
kkado 3:7dd40c4c2ef3 72 * oled.writeBuf();
kkado 3:7dd40c4c2ef3 73 * }
kkado 3:7dd40c4c2ef3 74 *
kkado 3:7dd40c4c2ef3 75 * @endcode
kkado 3:7dd40c4c2ef3 76 */
kkado 0:5115e0080bd5 77 class SSD1351{
kkado 0:5115e0080bd5 78 public:
kkado 2:fb159c293541 79 /** Connect to an SSD1351 on specified pins
kkado 2:fb159c293541 80 * Connect to: (MOSI, SCLK) <-- Native SPI pins
kkado 2:fb159c293541 81 * (DC, CS, RST) <-- Any digital pins
kkado 2:fb159c293541 82 */
kkado 0:5115e0080bd5 83 SSD1351(PinName mosi_pin, PinName sclk_pin, PinName dc_pin, PinName cs_pin, PinName rst_pin);
kkado 2:fb159c293541 84 /** Connect to SSD1351 on STMstation P.1, or default pins specified in defines in SSD1351.h
kkado 2:fb159c293541 85 * Default pins are: MOSI PA_7
kkado 2:fb159c293541 86 * SCLK PA_5
kkado 2:fb159c293541 87 * CS PA_6
kkado 2:fb159c293541 88 * DC PC_5
kkado 2:fb159c293541 89 * RST PA_4
kkado 2:fb159c293541 90 */
kkado 1:ae4fe66e9c0e 91 SSD1351();
kkado 2:fb159c293541 92
kkado 2:fb159c293541 93 //Rectangle fill without buffer
kkado 0:5115e0080bd5 94 //void fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor);
kkado 0:5115e0080bd5 95
kkado 2:fb159c293541 96 /** Enable writing directly to the VRAM. This must be called at least once before calling
kkado 2:fb159c293541 97 * writeBuf().
kkado 2:fb159c293541 98 */
kkado 0:5115e0080bd5 99 void enableWrite();
kkado 2:fb159c293541 100 /** Fill the buffer with a single color
kkado 2:fb159c293541 101 * @param fillcolor Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 102 */
kkado 0:5115e0080bd5 103 void fillBuf(uint16_t fillcolor);
kkado 2:fb159c293541 104 /** Write the buffer to the VRAM. Make sure you call enableWrte() before doing this!
kkado 2:fb159c293541 105 */
kkado 0:5115e0080bd5 106 void writeBuf();
kkado 2:fb159c293541 107 /** Draw a sprite from flash memory, into the buffer
kkado 2:fb159c293541 108 * @param s[] Sprite containing unsigned 16-bit 565 RGB values (1D vector)
kkado 2:fb159c293541 109 * @param x x-coordinate of sprite
kkado 2:fb159c293541 110 * @param y y-coordinate of sprite
kkado 2:fb159c293541 111 * @param w Width of sprite
kkado 2:fb159c293541 112 * @param h Height of sprite
kkado 2:fb159c293541 113 * @param mask This value in the sprite is "skipped" - transparancy value
kkado 2:fb159c293541 114 */
kkado 0:5115e0080bd5 115 void drawSpritePtr(const uint16_t s[] ,int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask);
kkado 2:fb159c293541 116 /** Fill the collision mask with a single value
kkado 2:fb159c293541 117 * @param state Fill value
kkado 2:fb159c293541 118 */
kkado 0:5115e0080bd5 119 void fillCMask(uint8_t state);
kkado 2:fb159c293541 120 /** Draw a sprite from flash memory, into the collision map
kkado 2:fb159c293541 121 * @param s[] Sprite containing unsigned 16-bit 565 RGB values (1D vector)
kkado 2:fb159c293541 122 * @param x x-coordinate of sprite
kkado 2:fb159c293541 123 * @param y y-coordinate of sprite
kkado 2:fb159c293541 124 * @param w Width of sprite
kkado 2:fb159c293541 125 * @param h Height of sprite
kkado 2:fb159c293541 126 * @param mask This value in the sprite is "skipped" - transparancy value
kkado 2:fb159c293541 127 * @param state Value written to the collision map
kkado 2:fb159c293541 128 */
kkado 0:5115e0080bd5 129 void drawCMask(const uint16_t s[], int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask, uint8_t state);
kkado 2:fb159c293541 130 /** Check if a sprite (not yet written to collision map) is going to collide with anything
kkado 2:fb159c293541 131 * @param s[] Sprite containing unsigned 16-bit 565 RGB values (1D vector)
kkado 2:fb159c293541 132 * @param x x-coordinate of sprite
kkado 2:fb159c293541 133 * @param y y-coordinate of sprite
kkado 2:fb159c293541 134 * @param w Width of sprite
kkado 2:fb159c293541 135 * @param h Height of sprite
kkado 2:fb159c293541 136 * @param mask This value in the sprite is "skipped" - transparancy value
kkado 2:fb159c293541 137 */
kkado 0:5115e0080bd5 138 uint8_t checkCollision(const uint16_t s[], int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t mask);
kkado 2:fb159c293541 139 /** Draw a single character
kkado 2:fb159c293541 140 * @param c ASCII character
kkado 2:fb159c293541 141 * @param x x-coordinate of character
kkado 2:fb159c293541 142 * @param y y-coordinate of character
kkado 2:fb159c293541 143 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 144 * @param zoom Scaling factor
kkado 2:fb159c293541 145 */
kkado 0:5115e0080bd5 146 void drawChar(char c, int16_t x, int16_t y, uint16_t color, uint8_t zoom);
kkado 2:fb159c293541 147 /** Draw a single character
kkado 2:fb159c293541 148 * @param c Char array
kkado 2:fb159c293541 149 * @param x x-coordinate of character
kkado 2:fb159c293541 150 * @param y y-coordinate of character
kkado 2:fb159c293541 151 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 152 * @param zoom Scaling factor
kkado 2:fb159c293541 153 */
kkado 0:5115e0080bd5 154 void printText(const char c[], int16_t x, int16_t y, uint16_t color, uint8_t zoom);
kkado 2:fb159c293541 155 /** Set the display buffer.
kkado 2:fb159c293541 156 * @param _buf Buffer, must be uint8_t name[32768]
kkado 2:fb159c293541 157 */
kkado 0:5115e0080bd5 158 void setBuf(uint8_t* _buf);
kkado 2:fb159c293541 159 /** Set the collision map.
kkado 2:fb159c293541 160 * @param _cmask Collision map, must be uint8_t name[16384]
kkado 2:fb159c293541 161 */
kkado 0:5115e0080bd5 162 void setCMask(uint8_t* _cmask);
kkado 0:5115e0080bd5 163
kkado 2:fb159c293541 164 //Drawing primitives
kkado 2:fb159c293541 165
kkado 2:fb159c293541 166 /** Draw a filled rectangle to buffer
kkado 2:fb159c293541 167 * @param x x-coordinate
kkado 2:fb159c293541 168 * @param y y-coordinate
kkado 2:fb159c293541 169 * @param w Width
kkado 2:fb159c293541 170 * @param h Height
kkado 2:fb159c293541 171 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 172 */
kkado 0:5115e0080bd5 173 void fillRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color);
kkado 2:fb159c293541 174 /** Draw a open rectangle to buffer
kkado 2:fb159c293541 175 * @param x x-coordinate
kkado 2:fb159c293541 176 * @param y y-coordinate
kkado 2:fb159c293541 177 * @param w Width
kkado 2:fb159c293541 178 * @param h Height
kkado 2:fb159c293541 179 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 180 */
kkado 0:5115e0080bd5 181 void openRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color);
kkado 2:fb159c293541 182 /** Draw a horizontal line without position calculations
kkado 2:fb159c293541 183 * @param x x-coordinate
kkado 2:fb159c293541 184 * @param y y-coordinate
kkado 2:fb159c293541 185 * @param length Length, can be positive or negative
kkado 2:fb159c293541 186 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 187 */
kkado 0:5115e0080bd5 188 void drawHLine(int16_t x, int16_t y, int16_t length, uint16_t color);
kkado 2:fb159c293541 189 /** Draw a vertical line without position calculations
kkado 2:fb159c293541 190 * @param x x-coordinate
kkado 2:fb159c293541 191 * @param y y-coordinate
kkado 2:fb159c293541 192 * @param length Length, can be positive or negative
kkado 2:fb159c293541 193 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 194 */
kkado 0:5115e0080bd5 195 void drawVLine(int16_t x, int16_t y, int16_t length, uint16_t color);
kkado 2:fb159c293541 196 /** Draw a line using Bresenham algorithm
kkado 2:fb159c293541 197 * @param x1 Start x-coordinate
kkado 2:fb159c293541 198 * @param y1 Start y-coordinate
kkado 2:fb159c293541 199 * @param x2 End x-coordinate
kkado 2:fb159c293541 200 * @param y2 End y-coordinate
kkado 2:fb159c293541 201 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 202 */
kkado 0:5115e0080bd5 203 void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
kkado 2:fb159c293541 204 /** Draw open circle
kkado 2:fb159c293541 205 * @param x0 Center x-coordinate
kkado 2:fb159c293541 206 * @param y0 Center y-coordinate
kkado 2:fb159c293541 207 * @param radius Circle radius
kkado 2:fb159c293541 208 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 209 */
kkado 0:5115e0080bd5 210 void openCircle(int16_t x0, int16_t y0, uint16_t radius, uint16_t color);
kkado 2:fb159c293541 211 /** Draw filled circle
kkado 2:fb159c293541 212 * @param x0 Center x-coordinate
kkado 2:fb159c293541 213 * @param y0 Center y-coordinate
kkado 2:fb159c293541 214 * @param radius Circle radius
kkado 2:fb159c293541 215 * @param color Unsigned 16-bit 565 RGB
kkado 2:fb159c293541 216 */
kkado 0:5115e0080bd5 217 void fillCircle(int16_t x0, int16_t y0, uint16_t radius, uint16_t color);
kkado 0:5115e0080bd5 218
kkado 0:5115e0080bd5 219 private:
kkado 2:fb159c293541 220 void begin();
kkado 0:5115e0080bd5 221 void spiwrite(uint8_t c);
kkado 0:5115e0080bd5 222 void writeCommand(uint8_t c);
kkado 0:5115e0080bd5 223 void writeData(uint8_t c);
kkado 0:5115e0080bd5 224 DigitalOut cs, dc, rst;
kkado 0:5115e0080bd5 225 BurstSPI spi;
kkado 0:5115e0080bd5 226 uint8_t *buf, *collisionmask;
kkado 0:5115e0080bd5 227 };
kkado 0:5115e0080bd5 228
kkado 0:5115e0080bd5 229 #endif