Hexiwear OLED Display Driver

Dependents:   Hexi_OLED_TextImage_Example Hexi_OLED_Text_Example Hexi_OLED_Image_Example security-console-app ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hexi_OLED_SSD1351.h Source File

Hexi_OLED_SSD1351.h

00001 /** OLED Display Driver for Hexiwear 
00002  *  This file contains OLED driver functionality for drawing images and text
00003  *
00004  * Redistribution and use in source and binary forms, with or without modification,
00005  * are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this list
00008  * of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice, this
00011  * list of conditions and the following disclaimer in the documentation and/or
00012  * other materials provided with the distribution.
00013  *
00014  * Neither the name of NXP, nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  * visit: http://www.mikroe.com and http://www.nxp.com
00030  *
00031  * get support at: http://www.mikroe.com/forum and https://community.nxp.com
00032  *
00033  * Project HEXIWEAR, 2015
00034  */
00035 
00036 #ifndef HG_HEXI_OLED_SSD1351
00037 #define HG_HEXI_OLED_SSD1351
00038 
00039 #include "mbed.h"
00040 #include "OLED_types.h" 
00041 #include "OLED_info.h"
00042 
00043 
00044 /** OLED Display Driver for Hexiwear
00045 */
00046 class SSD1351{
00047 
00048 public:
00049 
00050  /**
00051   * Create a Hexiwear OLED Driver connected to the specified pins
00052   *
00053   * @param mosiPin SPI Master Out, Slave In Pin
00054   * @param sclkPin SPI CLock Pin
00055   * @param pwrPin  OLED Power Pin
00056   * @param csPin   OLED Chip Select Pin
00057   * @param rstPin  OLED Reset Pin
00058   * @param dcPin   OLED DC Pin
00059   *
00060   * @note Default TextProperties
00061   *
00062   *       .font       = OpenSans_10x15_Regular,    
00063   *       .fontColor  = COLOR_WHITE,
00064   *       .alignParam = OLED_TEXT_ALIGN_CENTER, 
00065   *       .background = NULL
00066   *
00067   */
00068   SSD1351(PinName mosiPin,PinName sclkPin,PinName pwrPin, PinName csPin,PinName rstPin, PinName dcPin);
00069 
00070   /**
00071   * Destroy the Hexiwear instance
00072   */   
00073   ~SSD1351(); 
00074 
00075   /**
00076    * Send the command to OLED
00077    * @param  cmd     OLED command from the datasheet
00078    * @param  isFirst designate if this is the first byte in the command
00079    */
00080   void SendCmd(uint32_t cmd,
00081                      uint8_t isFirst);
00082 
00083 
00084   /**
00085    * Send data to OLED
00086    * @param  dataToSend data to send to OLED
00087    * @param  dataSize   data-size
00088    */
00089   void SendData ( const uint8_t* dataToSend, 
00090                              uint32_t dataSize);
00091 
00092   /**
00093    * draw box on OLED
00094    * @param  xCrd   x-coordinate for box's uper left corner
00095    * @param  yCrd   y-coordinate for box's uper left corner
00096    * @param  width  box's width
00097    * @param  height box's height
00098    * @param  color  color of the box
00099    * @return        status flag
00100    */
00101   oled_status_t DrawBox (
00102                               int8_t xCrd,
00103                               int8_t yCrd,
00104                               uint8_t width,
00105                               uint8_t height,
00106                               uint16_t color
00107                             );
00108 
00109   /**
00110    * Fill the entire screen with specified color
00111    * @param  color color to fill with
00112    */
00113   void FillScreen( uint16_t color );
00114 
00115 
00116 
00117   /**
00118    * Draw a single pixel
00119    * @param  xCrd  pixel's x coordinate
00120    * @param  yCrd  pixel's y coordinate
00121    * @param  color pixel's color
00122    * @return       status flag
00123    */
00124   oled_status_t DrawPixel (
00125                                  int8_t xCrd,
00126                                  int8_t yCrd,
00127                                 uint16_t color
00128                               );
00129 
00130 
00131   /**
00132    * Draw the whole screen
00133    * @param  image      image to draw
00134    * @param  xCrd       image x-coordinate
00135    * @param  yCrd       image y-coordinate
00136    * @param  width      image width
00137    * @param  height     image height
00138    * @param  transition transition style for the new image arrival
00139    * @return            status flag
00140    */
00141   oled_status_t DrawScreen (
00142                                      const uint8_t* image,
00143                                             int8_t xCrd,
00144                                             int8_t yCrd,
00145                                             uint8_t width,
00146                                             uint8_t height,
00147                                   oled_transition_t transition
00148                                 );
00149 
00150 
00151   /**
00152    * Set the font to use
00153    * @param newFont  desired font
00154    * @param newColor desired color
00155    * @return         status flag
00156    */
00157   oled_status_t SetFont(
00158                               const uint8_t* newFont,
00159                                     uint16_t newColor
00160                             );
00161 
00162 
00163   /**
00164    * Set OLED dynamic area
00165    * @param dynamic_area data-structure with desired values
00166    */
00167   void SetDynamicArea(oled_dynamic_area_t *dynamic_area);
00168 
00169   /**
00170    * Destroy current OLED dynamic area
00171    */
00172   void DestroyDynamicArea();
00173 
00174 
00175   /**
00176    * Set OLED class text properties from parameter 
00177    * @param textProperties data-structure with desired properties
00178    */
00179   void SetTextProperties(oled_text_properties_t *textProperties);
00180 
00181   /**
00182    * Copy OLED class text properties to parameter
00183    * @param textProperties destination data-structure
00184    */
00185   void GetTextProperties(oled_text_properties_t *textProperties);
00186 
00187   /**
00188    * Return the width in [px] required for the given string to be displayed
00189    * @param  text desired string
00190    * @return      required text width in [px]
00191    */
00192   uint8_t GetTextWidth(const uint8_t* text);
00193 
00194 
00195   /**
00196    * Count the characters
00197    * @param  width  text width
00198    * @param  font   text font
00199    * @param  text   given text string
00200    * @param  length text length
00201    * @return        character count
00202    */
00203   uint8_t CharCount(uint8_t width, const uint8_t* font, const uint8_t* text, uint8_t length);
00204 
00205   /**
00206    * Add text to the main screen buffer at position x,y. 
00207    * @param text    text to add
00208    * @param xCrd    x-coordinate for the given text
00209    * @param yCrd    y-coordinate for the given text
00210    * @return        status flag
00211    */
00212   oled_status_t AddText( const uint8_t* text,int8_t xCrd, int8_t yCrd );
00213     /**
00214    * Add text to the main screen buffer. Used with SetDynamicArea() Function.
00215    * @param  text text to add
00216    * @return      status flag
00217    */
00218   oled_status_t AddText( const uint8_t* text);
00219  
00220   /**
00221    * Write text on OLED at position set in Dynamic Area Field. Used with SetDynamicArea() Function.
00222    * @param text desired text
00223    * 
00224    */
00225   oled_status_t DrawText ( const uint8_t* text);
00226 
00227   /**
00228    * Return the dimensions of image 
00229    * @param width  given image's width
00230    * @param height given image's height
00231    * @param image  desired image
00232    */
00233   void GetImageDimensions(uint8_t *width, uint8_t *height, const uint8_t* image);
00234 
00235   /**
00236    * Add image to the main screen buffer.Used with SetDynamicArea() Function.
00237    * @param  image      desired image
00238    * @return            status flag
00239    */
00240   oled_status_t AddImage ( const uint8_t* image );
00241 
00242     /**
00243    * Add image to the main screen buffer at position x,y 
00244    * @param  image      desired image
00245    * @param  xCrd       image x-coordinate
00246    * @param  yCrd       image y-coordinate
00247    * @return            status flag
00248    */
00249   oled_status_t AddImage ( const uint8_t* image, int8_t xCrd, int8_t yCrd );
00250 
00251   /**
00252    * Send image to OLED GRAM.Used with SetDynamicArea() Function for positioning image. 
00253    * @param  image      desired image
00254    * @return            status flag
00255    */
00256   oled_status_t DrawImage ( const uint8_t* image );
00257     /**
00258    * Send image to OLED GRAM at position x,y.
00259    * @param  image      desired image
00260    * @param  xCrd       image x-coordinate
00261    * @param  yCrd       image y-coordinate
00262    * @return            status flag
00263    */
00264   oled_status_t DrawImage ( const uint8_t* image, int8_t xCrd, int8_t yCrd );
00265 
00266   /**
00267    * Dim OLED screen on
00268    */
00269   void DimScreenON();
00270 
00271   /**
00272    * Return OLED back to full contrast
00273    */
00274   void DimScreenOFF();
00275 
00276   /**
00277    * Swap image's bytes per pixel to obtain the correct color format
00278    * @param imgDst  desired image
00279    * @param imgSrc  original image
00280    * @param imgSize image's size
00281    */
00282   void Swap(
00283                       oled_pixel_t imgDst,
00284                     const uint8_t* imgSrc,
00285                           uint16_t imgSize
00286                   );
00287     
00288 
00289   /**
00290    * Turn on Power for OLED Display
00291    */              
00292   void PowerON();
00293 
00294   /**
00295    * Turn off Power for OLED Display
00296    */ 
00297   void PowerOFF(); 
00298 
00299   /**
00300    * update the main screen buffer
00301    * with the given image
00302 
00303    * @param  xCrd       image x-coordinate
00304    * @param  yCrd       image y-coordinate
00305    * @param  width      image width
00306    * @param  height     image height
00307    * @param  image      image for buffer
00308    */
00309   void UpdateBuffer (
00310                                 int8_t xCrd,
00311                                 int8_t yCrd,
00312                                 uint8_t width,
00313                                 uint8_t height,
00314                                 const uint8_t* image
00315                               );
00316 
00317 
00318     /**
00319    * Write text on OLED at position x,y. Recommended for Static Text. 
00320    * @param text    desired text
00321    * @param xCrd    x-coordinate for the given text
00322    * @param yCrd    y-coordinate for the given text
00323    */
00324 
00325   oled_status_t Label(const uint8_t* text,
00326                             int8_t xCrd, 
00327                             int8_t yCrd );
00328 
00329   /**
00330   * Create a text box of width,height at position x,y. Recommended for Dynamic Text.
00331   * Text is aligned in textbox accordingly to the align parameter set by SetTextProperties(). 
00332   * @param text    desired text
00333   * @param xCrd    x-coordinate for the textbox
00334   * @param yCrd    y-coordinate for the textbox
00335   * @param width   width of the textbox
00336   * @param height  height of the textbox 
00337   */
00338   
00339   oled_status_t TextBox(const uint8_t* text,
00340                               int8_t xCrd, 
00341                               int8_t yCrd,
00342                               uint8_t width,
00343                               uint8_t height);
00344 
00345 
00346 private:
00347 
00348   SPI spi;
00349   DigitalOut power;
00350   DigitalOut cs;
00351   DigitalOut rst; 
00352   DigitalOut dc;
00353 
00354  
00355   const uint8_t* selectedFont;
00356 
00357   uint8_t
00358     currentChar_width,
00359     currentChar_height,
00360     screenBuf[OLED_GRAM_SIZE];
00361 
00362   uint16_t
00363     selectedFont_color,
00364     selectedFont_firstChar, /* first character in the font table */
00365     selectedFont_lastChar,  /* last character in the font table */
00366     selectedFont_height,
00367     colorMask;
00368 
00369   oled_dynamic_area_t oled_dynamic_area;
00370   oled_text_properties_t oled_text_properties;
00371 
00372   
00373   /* Internal Functions */
00374   void Transpose( oled_pixel_t transImage, const oled_pixel_t image, uint8_t width, uint8_t height );
00375   oled_status_t TopDown   ( const uint8_t* image, int8_t xCrd, int8_t yCrd, uint8_t width, uint8_t height );
00376   oled_status_t DownTop   ( const uint8_t* image, int8_t xCrd, int8_t yCrd, uint8_t width, uint8_t height );
00377   oled_status_t LeftRight ( const uint8_t* image, int8_t xCrd, int8_t yCrd, uint8_t width, uint8_t height );
00378   oled_status_t RightLeft ( const uint8_t* image, int8_t xCrd, int8_t yCrd, uint8_t width, uint8_t height );
00379   void SetBorders( int8_t xCrd, int8_t yCrd, uint8_t width, uint8_t height );
00380   oled_status_t CreateTextBackground();
00381   void WriteCharToBuf( uint16_t charToWrite, oled_pixel_t* chrBuf );
00382   oled_status_t AddCharToTextArea( oled_pixel_t chrPtr, uint8_t chrWidth, uint8_t chrHeight, oled_pixel_t copyAddr, uint8_t imgWidth );
00383   void* AllocateDynamicArea( uint32_t area );
00384   oled_status_t DestroyDynamicArea(void * ptr);
00385 
00386 };
00387 
00388 #endif