Stepan Oslejsek / Mbed 2 deprecated OLEDMag

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSD1306_mini.h Source File

SSD1306_mini.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  *  Modified by Jiri Stepanovsky 6/4/2018 for LPE compatibility
00022  */
00023 
00024 #ifndef _SSD1306_MINI_H_
00025 #define _SSD1306_MINI_H_
00026 
00027 #include "mbed.h"
00028 #include "Adafruit_GFX.h"
00029 
00030 #define OLED_WIDTH 128
00031 #define OLED_HEIGHT 64
00032 #define OLED_BUFFER_SIZE ((OLED_WIDTH*OLED_HEIGHT)/8)
00033 
00034 // A DigitalOut sub-class that provides a constructed default state
00035 class DigitalOut2 : public DigitalOut
00036 {
00037 public:
00038     DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
00039     DigitalOut2& operator= (int value) { write(value); return *this; };
00040     DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
00041     operator int() { return read(); };
00042 };
00043 
00044 #define SSD1306_EXTERNALVCC 0x1
00045 #define SSD1306_SWITCHCAPVCC 0x2
00046 
00047 /** The pure base class for the SSD1306 display driver.
00048  *
00049  * You should derive from this for a new transport interface type,
00050  * such as the SPI and I2C drivers.
00051  */
00052 class SSD1306_mini : public Adafruit_GFX
00053 {
00054 public:
00055     SSD1306_mini(PinName RST)
00056         : Adafruit_GFX(OLED_WIDTH,OLED_HEIGHT)
00057         , rst(RST,false)
00058     {
00059     };
00060 
00061     void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
00062     
00063     // These must be implemented in the derived transport driver
00064     virtual void command(uint8_t c) = 0;
00065     virtual void data(uint8_t c) = 0;
00066     virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
00067 
00068     /// Clear the display buffer    
00069     void clearDisplay(void);
00070     virtual void invertDisplay(bool i);
00071 
00072     /// Cause the display to be updated with the buffer content.
00073     void display();
00074     
00075 protected:
00076     virtual void sendDisplayBuffer() = 0;
00077     DigitalOut2 rst;
00078 
00079     // the memory buffer for the LCD
00080     uint8_t buffer[OLED_BUFFER_SIZE];
00081 };
00082 
00083 
00084 /** This is the SW SPI SSD1306 display driver transport class
00085  *
00086  */
00087 class SSD1306_mini_swspi : public SSD1306_mini
00088 {
00089 public:
00090     /** Create a SSD1306 SW SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00091      *
00092      * Required parameters
00093      * @param D0 - (CLK) pin name
00094      * @param D1 - (MOSI) pin name
00095      * @param DC (Data/Command) pin name
00096      * @param RST (Reset) pin name
00097      * @param CS (Chip Select) pin name
00098      */
00099     SSD1306_mini_swspi(PinName D0, PinName D1, PinName DC, PinName RST, PinName CS)
00100         : SSD1306_mini(RST)
00101         , cs(CS,true)
00102         , dc(DC,false)
00103         , clk(D0,true)
00104         , mosi(D1,false)
00105         {
00106             begin();
00107             display();
00108         };
00109         
00110     void swspi_write(uint8_t v)
00111     {
00112         for (int bit = 7; bit >= 0; --bit) {
00113             mosi = ((v >> bit) & 0x01);
00114             clk = 0;
00115             clk = 1;
00116         }
00117     }
00118 
00119     virtual void command(uint8_t c)
00120     {
00121         cs = 1;
00122         dc = 0;
00123         cs = 0;
00124         swspi_write(c);
00125         cs = 1;
00126     };
00127 
00128     virtual void data(uint8_t c)
00129     {
00130         cs = 1;
00131         dc = 1;
00132         cs = 0;
00133         swspi_write(c);
00134         cs = 1;
00135     };
00136 
00137 protected:
00138     virtual void sendDisplayBuffer()
00139     {
00140         cs = 1;
00141         dc = 1;
00142         cs = 0;
00143 
00144         for(uint16_t i=0, q=OLED_BUFFER_SIZE; i<q; i++)
00145             swspi_write(buffer[i]);
00146 
00147         if(OLED_HEIGHT == 32)
00148         {
00149             for(uint16_t i=0, q=OLED_BUFFER_SIZE; i<q; i++)
00150                 swspi_write(0);
00151         }
00152 
00153         cs = 1;
00154     };
00155 
00156     DigitalOut2 cs, dc, clk, mosi;
00157 };
00158 
00159 
00160 /** This is the SPI SSD1306 display driver transport class
00161  *
00162  */
00163 class SSD1306_mini_spi : public SSD1306_mini
00164 {
00165 public:
00166     /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00167      *
00168      * Required parameters
00169      * @param spi - a reference to an initialized SPI object
00170      * @param DC (Data/Command) pin name
00171      * @param RST (Reset) pin name
00172      * @param CS (Chip Select) pin name
00173      */
00174     SSD1306_mini_spi(SPI &spi, PinName DC, PinName RST, PinName CS)
00175         : SSD1306_mini(RST)
00176         , cs(CS,true)
00177         , dc(DC,false)
00178         , mspi(spi)
00179         {
00180             begin();
00181             display();
00182         };
00183 
00184     virtual void command(uint8_t c)
00185     {
00186         cs = 1;
00187         dc = 0;
00188         cs = 0;
00189         mspi.write(c);
00190         cs = 1;
00191     };
00192 
00193     virtual void data(uint8_t c)
00194     {
00195         cs = 1;
00196         dc = 1;
00197         cs = 0;
00198         mspi.write(c);
00199         cs = 1;
00200     };
00201 
00202 protected:
00203     virtual void sendDisplayBuffer()
00204     {
00205         cs = 1;
00206         dc = 1;
00207         cs = 0;
00208 
00209         for(uint16_t i=0, q=OLED_BUFFER_SIZE; i<q; i++)
00210             mspi.write(buffer[i]);
00211 
00212         if(OLED_HEIGHT == 32)
00213         {
00214             for(uint16_t i=0, q=OLED_BUFFER_SIZE; i<q; i++)
00215                 mspi.write(0);
00216         }
00217 
00218         cs = 1;
00219     };
00220 
00221     DigitalOut2 cs, dc;
00222     SPI &mspi;
00223 };
00224 
00225 
00226 /** This is the I2C SSD1306 display driver transport class
00227  *
00228  */
00229 class SSD1306_mini_i2c : public SSD1306_mini
00230 {
00231 public:
00232     #define SSD_I2C_ADDRESS     0x78
00233     /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
00234      *
00235      * Required parameters
00236      * @param i2c - A reference to an initialized I2C object
00237      * @param RST - The Reset pin name
00238      *
00239      * Optional parameters
00240      * @param i2cAddress - The i2c address of the display
00241      */
00242     SSD1306_mini_i2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS)
00243         : SSD1306_mini(RST)
00244         , mi2c(i2c)
00245         , mi2cAddress(i2cAddress)
00246         {
00247             begin();
00248             display();
00249         };
00250 
00251     virtual void command(uint8_t c)
00252     {
00253         char buff[2];
00254         buff[0] = 0; // Command Mode
00255         buff[1] = c;
00256         mi2c.write(mi2cAddress, buff, sizeof(buff));
00257     }
00258 
00259     virtual void data(uint8_t c)
00260     {
00261         char buff[2];
00262         buff[0] = 0x40; // Data Mode
00263         buff[1] = c;
00264         mi2c.write(mi2cAddress, buff, sizeof(buff));
00265     };
00266 
00267 protected:
00268     virtual void sendDisplayBuffer()
00269     {
00270         char buff[17];
00271         buff[0] = 0x40; // Data Mode
00272 
00273         // send display buffer in 16 byte chunks
00274         for(uint16_t i=0, q=OLED_BUFFER_SIZE; i<q; i+=16 ) 
00275         {   uint8_t x ;
00276 
00277             // TODO - this will segfault if buffer.size() % 16 != 0
00278             for(x=1; x<sizeof(buff); x++) 
00279                 buff[x] = buffer[i+x-1];
00280             mi2c.write(mi2cAddress, buff, sizeof(buff));
00281         }
00282     };
00283 
00284     I2C &mi2c;
00285     uint8_t mi2cAddress;
00286 };
00287 
00288 #endif