A derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C.

Fork of Adafruit_GFX by Neal Horman

Committer:
marcpl
Date:
Sat Jun 20 09:59:00 2015 +0000
Revision:
19:8ba9f863f391
Parent:
17:cbd14e83142c
Minor fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 0:c3dcd4c4983a 1 /*********************************************************************
nkhorman 0:c3dcd4c4983a 2 This is a library for our Monochrome OLEDs based on SSD1306 drivers
nkhorman 0:c3dcd4c4983a 3
nkhorman 0:c3dcd4c4983a 4 Pick one up today in the adafruit shop!
nkhorman 0:c3dcd4c4983a 5 ------> http://www.adafruit.com/category/63_98
nkhorman 0:c3dcd4c4983a 6
nkhorman 0:c3dcd4c4983a 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
nkhorman 0:c3dcd4c4983a 10 Adafruit invests time and resources providing this open source code,
nkhorman 0:c3dcd4c4983a 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
nkhorman 0:c3dcd4c4983a 14 Written by Limor Fried/Ladyada for Adafruit Industries.
nkhorman 0:c3dcd4c4983a 15 BSD license, check license.txt for more information
nkhorman 0:c3dcd4c4983a 16 All text above, and the splash screen must be included in any redistribution
nkhorman 0:c3dcd4c4983a 17 *********************************************************************/
nkhorman 0:c3dcd4c4983a 18
nkhorman 0:c3dcd4c4983a 19 /*
nkhorman 9:ddb97c9850a2 20 * Modified by Neal Horman 7/14/2012 for use in mbed
nkhorman 0:c3dcd4c4983a 21 */
nkhorman 0:c3dcd4c4983a 22
nkhorman 0:c3dcd4c4983a 23 #ifndef _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 24 #define _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 25
nkhorman 0:c3dcd4c4983a 26 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 27 #include "Adafruit_GFX.h"
nkhorman 0:c3dcd4c4983a 28
nkhorman 9:ddb97c9850a2 29 #include <vector>
nkhorman 9:ddb97c9850a2 30 #include <algorithm>
nkhorman 0:c3dcd4c4983a 31
nkhorman 9:ddb97c9850a2 32 // A DigitalOut sub-class that provides a constructed default state
nkhorman 9:ddb97c9850a2 33 class DigitalOut2 : public DigitalOut
nkhorman 9:ddb97c9850a2 34 {
nkhorman 9:ddb97c9850a2 35 public:
nkhorman 9:ddb97c9850a2 36 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
nkhorman 9:ddb97c9850a2 37 DigitalOut2& operator= (int value) { write(value); return *this; };
nkhorman 9:ddb97c9850a2 38 DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
nkhorman 9:ddb97c9850a2 39 operator int() { return read(); };
nkhorman 9:ddb97c9850a2 40 };
Neal Horman 6:1be3e3b46eb7 41
nkhorman 0:c3dcd4c4983a 42 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 43 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 44
nkhorman 11:86909e6db3c8 45 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 46 *
nkhorman 11:86909e6db3c8 47 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 48 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 49 */
nkhorman 0:c3dcd4c4983a 50 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 51 {
nkhorman 9:ddb97c9850a2 52 public:
nkhorman 9:ddb97c9850a2 53 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 54 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 9:ddb97c9850a2 55 , rst(RST,false)
nkhorman 11:86909e6db3c8 56 {
nkhorman 11:86909e6db3c8 57 buffer.resize(rawHeight * rawWidth / 8);
nkhorman 11:86909e6db3c8 58 };
nkhorman 9:ddb97c9850a2 59
nkhorman 9:ddb97c9850a2 60 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 11:86909e6db3c8 61
nkhorman 11:86909e6db3c8 62 // These must be implemented in the derived transport driver
nkhorman 9:ddb97c9850a2 63 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 64 virtual void data(uint8_t c) = 0;
nkhorman 11:86909e6db3c8 65 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 11:86909e6db3c8 66
nkhorman 11:86909e6db3c8 67 /// Clear the display buffer
nkhorman 9:ddb97c9850a2 68 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 69 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 70
nkhorman 11:86909e6db3c8 71 /// Cause the display to be updated with the buffer content.
nkhorman 9:ddb97c9850a2 72 void display();
nkhorman 11:86909e6db3c8 73 /// Fill the buffer with the AdaFruit splash screen.
nkhorman 9:ddb97c9850a2 74 virtual void splash();
nkhorman 9:ddb97c9850a2 75
nkhorman 9:ddb97c9850a2 76 protected:
nkhorman 9:ddb97c9850a2 77 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 78 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 79
nkhorman 9:ddb97c9850a2 80 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 81 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 82 };
nkhorman 9:ddb97c9850a2 83
nkhorman 11:86909e6db3c8 84
nkhorman 11:86909e6db3c8 85 /** This is the SPI SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 86 *
nkhorman 11:86909e6db3c8 87 */
nkhorman 9:ddb97c9850a2 88 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 89 {
nkhorman 9:ddb97c9850a2 90 public:
nkhorman 11:86909e6db3c8 91 /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
nkhorman 11:86909e6db3c8 92 *
nkhorman 11:86909e6db3c8 93 * Required parameters
nkhorman 11:86909e6db3c8 94 * @param spi - a reference to an initialized SPI object
nkhorman 11:86909e6db3c8 95 * @param DC (Data/Command) pin name
nkhorman 11:86909e6db3c8 96 * @param RST (Reset) pin name
nkhorman 11:86909e6db3c8 97 * @param CS (Chip Select) pin name
nkhorman 11:86909e6db3c8 98 *
nkhorman 11:86909e6db3c8 99 * Optional parameters
nkhorman 11:86909e6db3c8 100 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 101 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 102 */
nkhorman 9:ddb97c9850a2 103 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 104 : Adafruit_SSD1306(RST, rawHieght, rawWidth)
nkhorman 9:ddb97c9850a2 105 , cs(CS,true)
nkhorman 9:ddb97c9850a2 106 , dc(DC,false)
nkhorman 9:ddb97c9850a2 107 , mspi(spi)
nkhorman 9:ddb97c9850a2 108 {
nkhorman 9:ddb97c9850a2 109 begin();
nkhorman 9:ddb97c9850a2 110 splash();
nkhorman 9:ddb97c9850a2 111 display();
nkhorman 9:ddb97c9850a2 112 };
nkhorman 9:ddb97c9850a2 113
nkhorman 9:ddb97c9850a2 114 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 115 {
nkhorman 9:ddb97c9850a2 116 cs = 1;
nkhorman 9:ddb97c9850a2 117 dc = 0;
nkhorman 9:ddb97c9850a2 118 cs = 0;
nkhorman 9:ddb97c9850a2 119 mspi.write(c);
nkhorman 9:ddb97c9850a2 120 cs = 1;
nkhorman 9:ddb97c9850a2 121 };
nkhorman 9:ddb97c9850a2 122
nkhorman 9:ddb97c9850a2 123 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 124 {
nkhorman 9:ddb97c9850a2 125 cs = 1;
nkhorman 9:ddb97c9850a2 126 dc = 1;
nkhorman 9:ddb97c9850a2 127 cs = 0;
nkhorman 9:ddb97c9850a2 128 mspi.write(c);
nkhorman 9:ddb97c9850a2 129 cs = 1;
nkhorman 9:ddb97c9850a2 130 };
nkhorman 9:ddb97c9850a2 131
nkhorman 9:ddb97c9850a2 132 protected:
marcpl 17:cbd14e83142c 133 virtual void setPageAddress(uint8_t add)
marcpl 17:cbd14e83142c 134 {
marcpl 17:cbd14e83142c 135 add=0xb0|add;
marcpl 17:cbd14e83142c 136 this->command(add);
marcpl 17:cbd14e83142c 137 }
marcpl 17:cbd14e83142c 138
marcpl 17:cbd14e83142c 139 virtual void setColumnAddress(uint8_t add)
marcpl 17:cbd14e83142c 140 {
marcpl 17:cbd14e83142c 141 this->command((0x10|(add>>4))+0x02);
marcpl 17:cbd14e83142c 142 this->command((0x0f&add));
marcpl 17:cbd14e83142c 143 }
marcpl 17:cbd14e83142c 144
nkhorman 9:ddb97c9850a2 145 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 146 {
marcpl 17:cbd14e83142c 147 if (height() == 48 && width() == 64) {
marcpl 17:cbd14e83142c 148 uint8_t i, j;
marcpl 17:cbd14e83142c 149 for (i=0; i<6; i++) {
marcpl 17:cbd14e83142c 150 this->setPageAddress(i);
marcpl 17:cbd14e83142c 151 this->setColumnAddress(0);
marcpl 17:cbd14e83142c 152 for (j=0;j<0x40;j++) {
marcpl 17:cbd14e83142c 153 this->data(buffer[i*0x40+j]);
marcpl 17:cbd14e83142c 154 }
marcpl 17:cbd14e83142c 155 }
marcpl 17:cbd14e83142c 156
marcpl 17:cbd14e83142c 157 } else {
marcpl 17:cbd14e83142c 158 cs = 1;
marcpl 17:cbd14e83142c 159 dc = 1;
marcpl 17:cbd14e83142c 160 cs = 0;
marcpl 17:cbd14e83142c 161
nkhorman 11:86909e6db3c8 162 for(uint16_t i=0, q=buffer.size(); i<q; i++)
marcpl 17:cbd14e83142c 163 mspi.write(buffer[i]);
marcpl 17:cbd14e83142c 164
marcpl 17:cbd14e83142c 165 if(height() == 32)
marcpl 17:cbd14e83142c 166 {
marcpl 17:cbd14e83142c 167 for(uint16_t i=0, q=buffer.size(); i<q; i++)
marcpl 17:cbd14e83142c 168 mspi.write(0);
marcpl 17:cbd14e83142c 169 }
marcpl 17:cbd14e83142c 170
marcpl 17:cbd14e83142c 171 cs = 1;
nkhorman 11:86909e6db3c8 172 }
nkhorman 11:86909e6db3c8 173
marcpl 17:cbd14e83142c 174
nkhorman 9:ddb97c9850a2 175 };
nkhorman 9:ddb97c9850a2 176
nkhorman 9:ddb97c9850a2 177 DigitalOut2 cs, dc;
nkhorman 9:ddb97c9850a2 178 SPI &mspi;
nkhorman 9:ddb97c9850a2 179 };
nkhorman 9:ddb97c9850a2 180
nkhorman 11:86909e6db3c8 181 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 182 *
nkhorman 11:86909e6db3c8 183 */
nkhorman 9:ddb97c9850a2 184 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 185 {
nkhorman 9:ddb97c9850a2 186 public:
nkhorman 9:ddb97c9850a2 187 #define SSD_I2C_ADDRESS 0x78
nkhorman 11:86909e6db3c8 188 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
nkhorman 11:86909e6db3c8 189 *
nkhorman 11:86909e6db3c8 190 * Required parameters
nkhorman 11:86909e6db3c8 191 * @param i2c - A reference to an initialized I2C object
nkhorman 11:86909e6db3c8 192 * @param RST - The Reset pin name
nkhorman 11:86909e6db3c8 193 *
nkhorman 11:86909e6db3c8 194 * Optional parameters
nkhorman 11:86909e6db3c8 195 * @param i2cAddress - The i2c address of the display
nkhorman 11:86909e6db3c8 196 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 197 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 198 */
nkhorman 9:ddb97c9850a2 199 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 200 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
nkhorman 9:ddb97c9850a2 201 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 202 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 203 {
nkhorman 9:ddb97c9850a2 204 begin();
nkhorman 9:ddb97c9850a2 205 splash();
nkhorman 9:ddb97c9850a2 206 display();
nkhorman 9:ddb97c9850a2 207 };
nkhorman 9:ddb97c9850a2 208
nkhorman 9:ddb97c9850a2 209 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 210 {
nkhorman 9:ddb97c9850a2 211 char buff[2];
nkhorman 9:ddb97c9850a2 212 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 213 buff[1] = c;
nkhorman 9:ddb97c9850a2 214 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 215 }
nkhorman 9:ddb97c9850a2 216
nkhorman 9:ddb97c9850a2 217 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 218 {
nkhorman 9:ddb97c9850a2 219 char buff[2];
nkhorman 9:ddb97c9850a2 220 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 221 buff[1] = c;
nkhorman 9:ddb97c9850a2 222 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 223 };
nkhorman 9:ddb97c9850a2 224
nkhorman 9:ddb97c9850a2 225 protected:
nkhorman 9:ddb97c9850a2 226 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 227 {
nkhorman 9:ddb97c9850a2 228 char buff[17];
nkhorman 9:ddb97c9850a2 229 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 230
nkhorman 9:ddb97c9850a2 231 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 232 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 233 { uint8_t x ;
nkhorman 9:ddb97c9850a2 234
nkhorman 9:ddb97c9850a2 235 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 236 for(x=1; x<sizeof(buff); x++)
JojoS 15:77feec1c0684 237 buff[x] = buffer[i+x-1];
nkhorman 9:ddb97c9850a2 238 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 239 }
nkhorman 9:ddb97c9850a2 240 };
nkhorman 9:ddb97c9850a2 241
nkhorman 9:ddb97c9850a2 242 I2C &mi2c;
nkhorman 9:ddb97c9850a2 243 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 244 };
nkhorman 0:c3dcd4c4983a 245
nkhorman 0:c3dcd4c4983a 246 #endif