Hexiwear OLED

Dependents:   Hexi

Fork of Hexi_OLED_SSD1351 by Hexiwear

Committer:
khuang
Date:
Thu Aug 18 23:01:06 2016 +0000
Revision:
1:3b5be0ee5f0c
Child:
2:fc06b5b5bf6a
Hexiwear OLED Display Driver enabled.; Allow images and text to be displayed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khuang 1:3b5be0ee5f0c 1 #ifndef HG_OLED_DRIVER
khuang 1:3b5be0ee5f0c 2 #define HG_OLED_DRIVER
khuang 1:3b5be0ee5f0c 3
khuang 1:3b5be0ee5f0c 4 #include "mbed.h"
khuang 1:3b5be0ee5f0c 5 #include "OLED_types.h"
khuang 1:3b5be0ee5f0c 6 #include "OLED_info.h"
khuang 1:3b5be0ee5f0c 7
khuang 1:3b5be0ee5f0c 8
khuang 1:3b5be0ee5f0c 9
khuang 1:3b5be0ee5f0c 10 class SSD1351{
khuang 1:3b5be0ee5f0c 11
khuang 1:3b5be0ee5f0c 12 public:
khuang 1:3b5be0ee5f0c 13
khuang 1:3b5be0ee5f0c 14
khuang 1:3b5be0ee5f0c 15 SSD1351(PinName mosi,PinName sclk,PinName pwr, PinName cs,PinName rst, PinName dc);
khuang 1:3b5be0ee5f0c 16
khuang 1:3b5be0ee5f0c 17 ~SSD1351();
khuang 1:3b5be0ee5f0c 18
khuang 1:3b5be0ee5f0c 19 /**
khuang 1:3b5be0ee5f0c 20 * Send the command to OLED
khuang 1:3b5be0ee5f0c 21 * @param self OLED handle
khuang 1:3b5be0ee5f0c 22 * @param cmd OLED command from the datasheet
khuang 1:3b5be0ee5f0c 23 * @param isFirst designate if this is the first byte in the command
khuang 1:3b5be0ee5f0c 24 */
khuang 1:3b5be0ee5f0c 25 void SendCmd(uint32_t cmd,
khuang 1:3b5be0ee5f0c 26 uint8_t isFirst);
khuang 1:3b5be0ee5f0c 27
khuang 1:3b5be0ee5f0c 28
khuang 1:3b5be0ee5f0c 29 /**
khuang 1:3b5be0ee5f0c 30 * Send data to OLED
khuang 1:3b5be0ee5f0c 31 * @param dataToSend data to send to OLED
khuang 1:3b5be0ee5f0c 32 * @param dataSize data-size
khuang 1:3b5be0ee5f0c 33 */
khuang 1:3b5be0ee5f0c 34 void SendData ( const uint8_t* dataToSend,
khuang 1:3b5be0ee5f0c 35 uint32_t dataSize);
khuang 1:3b5be0ee5f0c 36
khuang 1:3b5be0ee5f0c 37 /**
khuang 1:3b5be0ee5f0c 38 * draw box on OLED
khuang 1:3b5be0ee5f0c 39 * @param xCrd x-coordinate for box's uper left corner
khuang 1:3b5be0ee5f0c 40 * @param yCrd y-coordinate for box's uper left corner
khuang 1:3b5be0ee5f0c 41 * @param width box's width
khuang 1:3b5be0ee5f0c 42 * @param height box's height
khuang 1:3b5be0ee5f0c 43 * @param color color of the box
khuang 1:3b5be0ee5f0c 44 * @return status flag
khuang 1:3b5be0ee5f0c 45 */
khuang 1:3b5be0ee5f0c 46 oled_status_t DrawBox (
khuang 1:3b5be0ee5f0c 47 uint16_t xCrd,
khuang 1:3b5be0ee5f0c 48 uint16_t yCrd,
khuang 1:3b5be0ee5f0c 49 uint16_t width,
khuang 1:3b5be0ee5f0c 50 uint16_t height,
khuang 1:3b5be0ee5f0c 51 uint16_t color
khuang 1:3b5be0ee5f0c 52 );
khuang 1:3b5be0ee5f0c 53
khuang 1:3b5be0ee5f0c 54 /**
khuang 1:3b5be0ee5f0c 55 * Fill the entire screen with specified color
khuang 1:3b5be0ee5f0c 56 * @param color color to fill with
khuang 1:3b5be0ee5f0c 57 */
khuang 1:3b5be0ee5f0c 58 void FillScreen( uint16_t color );
khuang 1:3b5be0ee5f0c 59
khuang 1:3b5be0ee5f0c 60
khuang 1:3b5be0ee5f0c 61
khuang 1:3b5be0ee5f0c 62 /**
khuang 1:3b5be0ee5f0c 63 * Draw a single pixel
khuang 1:3b5be0ee5f0c 64 * @param xCrd pixel's x coordinate
khuang 1:3b5be0ee5f0c 65 * @param yCrd pixel's y coordinate
khuang 1:3b5be0ee5f0c 66 * @param color pixel's color
khuang 1:3b5be0ee5f0c 67 * @return status flag
khuang 1:3b5be0ee5f0c 68 */
khuang 1:3b5be0ee5f0c 69 oled_status_t DrawPixel (
khuang 1:3b5be0ee5f0c 70 int16_t xCrd,
khuang 1:3b5be0ee5f0c 71 int16_t yCrd,
khuang 1:3b5be0ee5f0c 72 uint16_t color
khuang 1:3b5be0ee5f0c 73 );
khuang 1:3b5be0ee5f0c 74
khuang 1:3b5be0ee5f0c 75
khuang 1:3b5be0ee5f0c 76 /**
khuang 1:3b5be0ee5f0c 77 * Draw the whole screen
khuang 1:3b5be0ee5f0c 78 * @param image image to draw
khuang 1:3b5be0ee5f0c 79 * @param xCrd image x-coordinate
khuang 1:3b5be0ee5f0c 80 * @param yCrd image y-coordinate
khuang 1:3b5be0ee5f0c 81 * @param width image width
khuang 1:3b5be0ee5f0c 82 * @param height image height
khuang 1:3b5be0ee5f0c 83 * @param transition transition style for the new image arrival
khuang 1:3b5be0ee5f0c 84 * @return status flag
khuang 1:3b5be0ee5f0c 85 */
khuang 1:3b5be0ee5f0c 86 oled_status_t DrawScreen (
khuang 1:3b5be0ee5f0c 87 const uint8_t* image,
khuang 1:3b5be0ee5f0c 88 uint8_t xCrd,
khuang 1:3b5be0ee5f0c 89 uint8_t yCrd,
khuang 1:3b5be0ee5f0c 90 uint8_t width,
khuang 1:3b5be0ee5f0c 91 uint8_t height,
khuang 1:3b5be0ee5f0c 92 oled_transition_t transition
khuang 1:3b5be0ee5f0c 93 );
khuang 1:3b5be0ee5f0c 94
khuang 1:3b5be0ee5f0c 95
khuang 1:3b5be0ee5f0c 96 /**
khuang 1:3b5be0ee5f0c 97 * Set the font to use
khuang 1:3b5be0ee5f0c 98 * @param newFont desired font
khuang 1:3b5be0ee5f0c 99 * @param newColor desired color
khuang 1:3b5be0ee5f0c 100 * @return status flag
khuang 1:3b5be0ee5f0c 101 */
khuang 1:3b5be0ee5f0c 102 oled_status_t SetFont(
khuang 1:3b5be0ee5f0c 103 const uint8_t* newFont,
khuang 1:3b5be0ee5f0c 104 uint16_t newColor
khuang 1:3b5be0ee5f0c 105 );
khuang 1:3b5be0ee5f0c 106
khuang 1:3b5be0ee5f0c 107
khuang 1:3b5be0ee5f0c 108 /**
khuang 1:3b5be0ee5f0c 109 * Set OLED dynamic area
khuang 1:3b5be0ee5f0c 110 * @param dynamic_area data-structure with desired values
khuang 1:3b5be0ee5f0c 111 */
khuang 1:3b5be0ee5f0c 112 void SetDynamicArea(oled_dynamic_area_t *dynamic_area);
khuang 1:3b5be0ee5f0c 113
khuang 1:3b5be0ee5f0c 114 /**
khuang 1:3b5be0ee5f0c 115 * Destroy current OLED dynamic area
khuang 1:3b5be0ee5f0c 116 */
khuang 1:3b5be0ee5f0c 117 void DestroyDynamicArea();
khuang 1:3b5be0ee5f0c 118
khuang 1:3b5be0ee5f0c 119
khuang 1:3b5be0ee5f0c 120 /**
khuang 1:3b5be0ee5f0c 121 * Set OLED text properties
khuang 1:3b5be0ee5f0c 122 * @param textProperties data-structure with desired properties
khuang 1:3b5be0ee5f0c 123 */
khuang 1:3b5be0ee5f0c 124 void SetTextProperties(oled_text_properties_t *textProperties);
khuang 1:3b5be0ee5f0c 125
khuang 1:3b5be0ee5f0c 126
khuang 1:3b5be0ee5f0c 127 /**
khuang 1:3b5be0ee5f0c 128 * Return the width in [px] required for the given string to be displayed
khuang 1:3b5be0ee5f0c 129 * @param text desired string
khuang 1:3b5be0ee5f0c 130 * @return required text width in [px]
khuang 1:3b5be0ee5f0c 131 */
khuang 1:3b5be0ee5f0c 132 uint8_t GetTextWidth(const uint8_t* text);
khuang 1:3b5be0ee5f0c 133
khuang 1:3b5be0ee5f0c 134
khuang 1:3b5be0ee5f0c 135 /**
khuang 1:3b5be0ee5f0c 136 * Count the characters
khuang 1:3b5be0ee5f0c 137 * @param width text width
khuang 1:3b5be0ee5f0c 138 * @param font text font
khuang 1:3b5be0ee5f0c 139 * @param text given text string
khuang 1:3b5be0ee5f0c 140 * @param length text length
khuang 1:3b5be0ee5f0c 141 * @return character count
khuang 1:3b5be0ee5f0c 142 */
khuang 1:3b5be0ee5f0c 143 uint8_t CharCount(uint8_t width, const uint8_t* font, const uint8_t* text, uint8_t length);
khuang 1:3b5be0ee5f0c 144
khuang 1:3b5be0ee5f0c 145 /**
khuang 1:3b5be0ee5f0c 146 * Add text to the main screen buffer at position x,y.
khuang 1:3b5be0ee5f0c 147 * @param text text to add
khuang 1:3b5be0ee5f0c 148 * @param xCrd x-coordinate for the given text
khuang 1:3b5be0ee5f0c 149 * @param yCrd y-coordinate for the given text
khuang 1:3b5be0ee5f0c 150 * @return status flag
khuang 1:3b5be0ee5f0c 151 */
khuang 1:3b5be0ee5f0c 152 oled_status_t AddText( const uint8_t* text,uint8_t xCrd, uint8_t yCrd );
khuang 1:3b5be0ee5f0c 153 /**
khuang 1:3b5be0ee5f0c 154 * Add text to the main screen buffer. Used with SetDynamicArea() Function.
khuang 1:3b5be0ee5f0c 155 * @param text text to add
khuang 1:3b5be0ee5f0c 156 * @return status flag
khuang 1:3b5be0ee5f0c 157 */
khuang 1:3b5be0ee5f0c 158 oled_status_t AddText( const uint8_t* text);
khuang 1:3b5be0ee5f0c 159
khuang 1:3b5be0ee5f0c 160 /**
khuang 1:3b5be0ee5f0c 161 * Write text on OLED at position x,y.
khuang 1:3b5be0ee5f0c 162 * @param text desired text
khuang 1:3b5be0ee5f0c 163 * @param xCrd x-coordinate for the given text
khuang 1:3b5be0ee5f0c 164 * @param yCrd y-coordinate for the given text
khuang 1:3b5be0ee5f0c 165 */
khuang 1:3b5be0ee5f0c 166
khuang 1:3b5be0ee5f0c 167 oled_status_t DrawText ( const uint8_t* text,uint8_t xCrd, uint8_t yCrd );
khuang 1:3b5be0ee5f0c 168
khuang 1:3b5be0ee5f0c 169 /**
khuang 1:3b5be0ee5f0c 170 * Write text on OLED at position set in Dynamic Area Field. Used with SetDynamicArea() Function.
khuang 1:3b5be0ee5f0c 171 * @param text desired text
khuang 1:3b5be0ee5f0c 172 * @param x x-coordinate for the given text, set through dynamicArea_t variable
khuang 1:3b5be0ee5f0c 173 * @param y y-coordinate for the given text, set through dynamicArea_t variable
khuang 1:3b5be0ee5f0c 174 *
khuang 1:3b5be0ee5f0c 175 */
khuang 1:3b5be0ee5f0c 176 oled_status_t DrawText ( const uint8_t* text);
khuang 1:3b5be0ee5f0c 177
khuang 1:3b5be0ee5f0c 178 /**
khuang 1:3b5be0ee5f0c 179 * Return the dimensions of image
khuang 1:3b5be0ee5f0c 180 * @param width given image's width
khuang 1:3b5be0ee5f0c 181 * @param height given image's height
khuang 1:3b5be0ee5f0c 182 * @param image desired image
khuang 1:3b5be0ee5f0c 183 */
khuang 1:3b5be0ee5f0c 184 void GetImageDimensions(uint8_t *width, uint8_t *height, const uint8_t* image);
khuang 1:3b5be0ee5f0c 185
khuang 1:3b5be0ee5f0c 186
khuang 1:3b5be0ee5f0c 187
khuang 1:3b5be0ee5f0c 188 /**
khuang 1:3b5be0ee5f0c 189 * Add image to the main screen buffer.Used with SetDynamicArea() Function.
khuang 1:3b5be0ee5f0c 190 * @param image desired image
khuang 1:3b5be0ee5f0c 191 * @return status flag
khuang 1:3b5be0ee5f0c 192 */
khuang 1:3b5be0ee5f0c 193 oled_status_t AddImage ( const uint8_t* image );
khuang 1:3b5be0ee5f0c 194
khuang 1:3b5be0ee5f0c 195 /**
khuang 1:3b5be0ee5f0c 196 * Add image to the main screen buffer at position x,y
khuang 1:3b5be0ee5f0c 197 * @param image desired image
khuang 1:3b5be0ee5f0c 198 * @return status flag
khuang 1:3b5be0ee5f0c 199 */
khuang 1:3b5be0ee5f0c 200 oled_status_t AddImage ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd );
khuang 1:3b5be0ee5f0c 201
khuang 1:3b5be0ee5f0c 202 /**
khuang 1:3b5be0ee5f0c 203 * Draw image, i.e. send image to OLED GRAM.Used with SetDynamicArea() Function.
khuang 1:3b5be0ee5f0c 204 * @param image desired image
khuang 1:3b5be0ee5f0c 205 * @param transition desired transition style for the new image
khuang 1:3b5be0ee5f0c 206 * @return status flag
khuang 1:3b5be0ee5f0c 207 */
khuang 1:3b5be0ee5f0c 208 oled_status_t DrawImage ( const uint8_t* image );
khuang 1:3b5be0ee5f0c 209 /**
khuang 1:3b5be0ee5f0c 210 * Draw image, i.e. send image to OLED GRAM at position x,y.
khuang 1:3b5be0ee5f0c 211 * @param image desired image
khuang 1:3b5be0ee5f0c 212 * @param transition desired transition style for the new image
khuang 1:3b5be0ee5f0c 213 * @return status flag
khuang 1:3b5be0ee5f0c 214 */
khuang 1:3b5be0ee5f0c 215 oled_status_t DrawImage ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd );
khuang 1:3b5be0ee5f0c 216
khuang 1:3b5be0ee5f0c 217 /**
khuang 1:3b5be0ee5f0c 218 * Dim OLED screen on
khuang 1:3b5be0ee5f0c 219 */
khuang 1:3b5be0ee5f0c 220 void DimScreenON();
khuang 1:3b5be0ee5f0c 221
khuang 1:3b5be0ee5f0c 222 /**
khuang 1:3b5be0ee5f0c 223 * Return OLED back to full contrast
khuang 1:3b5be0ee5f0c 224 */
khuang 1:3b5be0ee5f0c 225 void DimScreenOFF();
khuang 1:3b5be0ee5f0c 226
khuang 1:3b5be0ee5f0c 227 /**
khuang 1:3b5be0ee5f0c 228 * Swap image's bytes per pixel to obtain the correct color format
khuang 1:3b5be0ee5f0c 229 * @param imgDst desired image
khuang 1:3b5be0ee5f0c 230 * @param imgSrc original image
khuang 1:3b5be0ee5f0c 231 * @param imgSize image's size
khuang 1:3b5be0ee5f0c 232 */
khuang 1:3b5be0ee5f0c 233 void Swap(
khuang 1:3b5be0ee5f0c 234 oled_pixel_t imgDst,
khuang 1:3b5be0ee5f0c 235 const uint8_t* imgSrc,
khuang 1:3b5be0ee5f0c 236 uint16_t imgSize
khuang 1:3b5be0ee5f0c 237 );
khuang 1:3b5be0ee5f0c 238
khuang 1:3b5be0ee5f0c 239
khuang 1:3b5be0ee5f0c 240 /**
khuang 1:3b5be0ee5f0c 241 * Turn on Power for OLED Display
khuang 1:3b5be0ee5f0c 242 */
khuang 1:3b5be0ee5f0c 243 void PowerON();
khuang 1:3b5be0ee5f0c 244
khuang 1:3b5be0ee5f0c 245 /**
khuang 1:3b5be0ee5f0c 246 * Turn off Power for OLED Display
khuang 1:3b5be0ee5f0c 247 */
khuang 1:3b5be0ee5f0c 248 void PowerOFF();
khuang 1:3b5be0ee5f0c 249
khuang 1:3b5be0ee5f0c 250 /**
khuang 1:3b5be0ee5f0c 251 * update the main screen buffer
khuang 1:3b5be0ee5f0c 252 * with the given image
khuang 1:3b5be0ee5f0c 253
khuang 1:3b5be0ee5f0c 254 * @param xCrd image x-coordinate
khuang 1:3b5be0ee5f0c 255 * @param yCrd image y-coordinate
khuang 1:3b5be0ee5f0c 256 * @param width image width
khuang 1:3b5be0ee5f0c 257 * @param height image height
khuang 1:3b5be0ee5f0c 258 * @param image image for buffer
khuang 1:3b5be0ee5f0c 259 */
khuang 1:3b5be0ee5f0c 260 void UpdateBuffer (
khuang 1:3b5be0ee5f0c 261 uint8_t xCrd,
khuang 1:3b5be0ee5f0c 262 uint8_t yCrd,
khuang 1:3b5be0ee5f0c 263 uint8_t width,
khuang 1:3b5be0ee5f0c 264 uint8_t height,
khuang 1:3b5be0ee5f0c 265 const uint8_t* image
khuang 1:3b5be0ee5f0c 266 );
khuang 1:3b5be0ee5f0c 267
khuang 1:3b5be0ee5f0c 268
khuang 1:3b5be0ee5f0c 269
khuang 1:3b5be0ee5f0c 270 private:
khuang 1:3b5be0ee5f0c 271
khuang 1:3b5be0ee5f0c 272 SPI spi;
khuang 1:3b5be0ee5f0c 273 DigitalOut power;
khuang 1:3b5be0ee5f0c 274 DigitalOut cs;
khuang 1:3b5be0ee5f0c 275 DigitalOut rst;
khuang 1:3b5be0ee5f0c 276 DigitalOut dc;
khuang 1:3b5be0ee5f0c 277
khuang 1:3b5be0ee5f0c 278
khuang 1:3b5be0ee5f0c 279 const uint8_t* selectedFont;
khuang 1:3b5be0ee5f0c 280
khuang 1:3b5be0ee5f0c 281 uint8_t
khuang 1:3b5be0ee5f0c 282 isFontInitialized,
khuang 1:3b5be0ee5f0c 283 currentChar_width,
khuang 1:3b5be0ee5f0c 284 currentChar_height,
khuang 1:3b5be0ee5f0c 285 screenBuf[OLED_GRAM_SIZE];
khuang 1:3b5be0ee5f0c 286
khuang 1:3b5be0ee5f0c 287 uint16_t
khuang 1:3b5be0ee5f0c 288 selectedFont_color,
khuang 1:3b5be0ee5f0c 289 selectedFont_firstChar, // first character in the font table
khuang 1:3b5be0ee5f0c 290 selectedFont_lastChar, // last character in the font table
khuang 1:3b5be0ee5f0c 291 selectedFont_height,
khuang 1:3b5be0ee5f0c 292 colorMask;
khuang 1:3b5be0ee5f0c 293
khuang 1:3b5be0ee5f0c 294 oled_dynamic_area_t oled_dynamic_area;
khuang 1:3b5be0ee5f0c 295 oled_text_properties_t oled_text_properties;
khuang 1:3b5be0ee5f0c 296
khuang 1:3b5be0ee5f0c 297
khuang 1:3b5be0ee5f0c 298 //Internal Functions
khuang 1:3b5be0ee5f0c 299 void Transpose( oled_pixel_t transImage, const oled_pixel_t image, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 300 oled_status_t TopDown ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 301 oled_status_t DownTop ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 302 oled_status_t LeftRight ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 303 oled_status_t RightLeft ( const uint8_t* image, uint8_t xCrd, uint8_t yCrd, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 304 void SetBorders( uint8_t xCrd, uint8_t yCrd, uint8_t width, uint8_t height );
khuang 1:3b5be0ee5f0c 305 oled_status_t CreateTextBackground();
khuang 1:3b5be0ee5f0c 306 void WriteCharToBuf( uint16_t charToWrite, oled_pixel_t* chrBuf );
khuang 1:3b5be0ee5f0c 307 oled_status_t AddCharToTextArea( oled_pixel_t chrPtr, uint8_t chrWidth, uint8_t chrHeight, oled_pixel_t copyAddr, uint8_t imgWidth );
khuang 1:3b5be0ee5f0c 308 void* AllocateDynamicArea( uint32_t area );
khuang 1:3b5be0ee5f0c 309 oled_status_t DestroyDynamicArea(void * ptr);
khuang 1:3b5be0ee5f0c 310
khuang 1:3b5be0ee5f0c 311 };
khuang 1:3b5be0ee5f0c 312
khuang 1:3b5be0ee5f0c 313 #endif