Kevin Kadooka / SSD1351

Dependencies:   BurstSPI

Committer:
kkado
Date:
Mon Jul 03 08:05:00 2017 +0000
Revision:
2:fb159c293541
Parent:
1:ae4fe66e9c0e
Child:
3:7dd40c4c2ef3
First public commit

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