Leon Wehmeier / Mbed OS fiasco_max32630

Dependencies:   SoftSerial MAX14690 Buffer

Fork of rtos_threading_with_callback by mbed_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_SSD1306.h Source File

Adafruit_SSD1306.h

00001 /*********************************************************************
00002 This is a library for our Monochrome OLEDs based on SSD1306 drivers
00003 
00004   Pick one up today in the adafruit shop!
00005   ------> http://www.adafruit.com/category/63_98
00006 
00007 These displays use SPI to communicate, 4 or 5 pins are required to  
00008 interface
00009 
00010 Adafruit invests time and resources providing this open source code, 
00011 please support Adafruit and open-source hardware by purchasing 
00012 products from Adafruit!
00013 
00014 Written by Limor Fried/Ladyada  for Adafruit Industries.  
00015 BSD license, check license.txt for more information
00016 All text above, and the splash screen must be included in any redistribution
00017 *********************************************************************/
00018 
00019 /*
00020  *  Modified by Neal Horman 7/14/2012 for use in mbed
00021  */
00022 
00023 #ifndef _ADAFRUIT_SSD1306_H_
00024 #define _ADAFRUIT_SSD1306_H_
00025 
00026 #include "mbed.h"
00027 #include "Adafruit_GFX.h"
00028 
00029 #include <vector>
00030 #include <algorithm>
00031 
00032 #define SSD1306_EXTERNALVCC 0x1
00033 #define SSD1306_SWITCHCAPVCC 0x2
00034 
00035 /** The pure base class for the SSD1306 display driver.
00036  *
00037  * You should derive from this for a new transport interface type,
00038  * such as the SPI and I2C drivers.
00039  */
00040 class Adafruit_SSD1306 : public Adafruit_GFX
00041 {
00042 public:
00043     Adafruit_SSD1306(uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00044         : Adafruit_GFX(rawWidth,rawHeight)
00045     {
00046         buffer.resize(rawHeight * rawWidth / 8);
00047     };
00048 
00049     void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
00050     
00051     // These must be implemented in the derived transport driver
00052     virtual void command(uint8_t c) = 0;
00053     virtual void data(uint8_t c) = 0;
00054     virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
00055 
00056     /// Clear the display buffer    
00057     void clearDisplay(void);
00058     virtual void invertDisplay(bool i);
00059 
00060     /// Cause the display to be updated with the buffer content.
00061     void display();
00062     /// Fill the buffer with the AdaFruit splash screen.
00063     virtual void splash();
00064     
00065 protected:
00066     virtual void sendDisplayBuffer() = 0;
00067 
00068     // the memory buffer for the LCD
00069     std::vector<uint8_t> buffer;
00070 };
00071 
00072 
00073 /** This is the I2C SSD1306 display driver transport class
00074  *
00075  */
00076 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
00077 {
00078 public:
00079     #define SSD_I2C_ADDRESS     0x78
00080     /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
00081      *
00082      * Required parameters
00083      * @param i2c - A reference to an initialized I2C object
00084      * @param RST - The Reset pin name
00085      *
00086      * Optional parameters
00087      * @param i2cAddress - The i2c address of the display
00088      * @param rawHeight - The vertical number of pixels for the display, defaults to 32
00089      * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
00090      */
00091     Adafruit_SSD1306_I2c(I2C *i2c, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00092         : Adafruit_SSD1306(rawHeight, rawWidth)
00093         , mi2c(i2c)
00094         , mi2cAddress(i2cAddress)
00095         {
00096             begin();
00097             splash();
00098             display();
00099         };
00100 
00101     virtual void command(uint8_t c)
00102     {
00103         char buff[2];
00104         buff[0] = 0; // Command Mode
00105         buff[1] = c;
00106         mi2c->write(mi2cAddress, buff, sizeof(buff));
00107     }
00108 
00109     virtual void data(uint8_t c)
00110     {
00111         char buff[2];
00112         buff[0] = 0x40; // Data Mode
00113         buff[1] = c;
00114         mi2c->write(mi2cAddress, buff, sizeof(buff));
00115     };
00116 
00117 protected:
00118     virtual void sendDisplayBuffer()
00119     {
00120         char buff[17];
00121         buff[0] = 0x40; // Data Mode
00122 
00123         // send display buffer in 16 byte chunks
00124         for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) 
00125         {   uint8_t x ;
00126 
00127             // TODO - this will segfault if buffer.size() % 16 != 0
00128             for(x=1; x<sizeof(buff); x++) 
00129                 buff[x] = buffer[i+x-1];
00130             mi2c->write(mi2cAddress, buff, sizeof(buff));
00131         }
00132     };
00133 
00134     I2C *mi2c;
00135     uint8_t mi2cAddress;
00136 };
00137 
00138 #endif