i2c version has an offset due to wrong copy of temp buffer to display buffer, fixed in Adafruit_SSD1306.h

Dependents:   ezSBC_MPU9250 Test_OLED_Display untodoenuno OledI2CDisplay ... more

Fork of Adafruit_GFX by Neal Horman

Committer:
JojoS
Date:
Wed Mar 04 20:19:52 2015 +0000
Revision:
18:6aa925f254d6
Parent:
17:f0cf0e1210be
Child:
20:04822be0efcd
added flipVertical Option to constructor; use of reset signal is now optional (NC maybe used for rst pin); added command for clearing page start address (important for usage without hardware reset pin)

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 0:c3dcd4c4983a 32 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 33 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 34
nkhorman 11:86909e6db3c8 35 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 36 *
nkhorman 11:86909e6db3c8 37 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 38 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 39 */
nkhorman 0:c3dcd4c4983a 40 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 41 {
nkhorman 9:ddb97c9850a2 42 public:
JojoS 18:6aa925f254d6 43 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128, bool _flipVertical=false)
nkhorman 9:ddb97c9850a2 44 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 9:ddb97c9850a2 45 , rst(RST,false)
JojoS 18:6aa925f254d6 46 , flipVertical(_flipVertical)
nkhorman 11:86909e6db3c8 47 {
nkhorman 11:86909e6db3c8 48 buffer.resize(rawHeight * rawWidth / 8);
nkhorman 11:86909e6db3c8 49 };
nkhorman 9:ddb97c9850a2 50
nkhorman 9:ddb97c9850a2 51 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 11:86909e6db3c8 52
nkhorman 11:86909e6db3c8 53 // These must be implemented in the derived transport driver
nkhorman 9:ddb97c9850a2 54 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 55 virtual void data(uint8_t c) = 0;
nkhorman 11:86909e6db3c8 56 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 11:86909e6db3c8 57
nkhorman 11:86909e6db3c8 58 /// Clear the display buffer
nkhorman 9:ddb97c9850a2 59 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 60 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 61
nkhorman 11:86909e6db3c8 62 /// Cause the display to be updated with the buffer content.
nkhorman 9:ddb97c9850a2 63 void display();
nkhorman 11:86909e6db3c8 64 /// Fill the buffer with the AdaFruit splash screen.
nkhorman 9:ddb97c9850a2 65 virtual void splash();
nkhorman 9:ddb97c9850a2 66
nkhorman 9:ddb97c9850a2 67 protected:
nkhorman 9:ddb97c9850a2 68 virtual void sendDisplayBuffer() = 0;
JojoS 17:f0cf0e1210be 69 DigitalOut rst;
JojoS 18:6aa925f254d6 70 bool flipVertical;
nkhorman 9:ddb97c9850a2 71
nkhorman 9:ddb97c9850a2 72 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 73 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 74 };
nkhorman 9:ddb97c9850a2 75
nkhorman 11:86909e6db3c8 76
nkhorman 11:86909e6db3c8 77 /** This is the SPI SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 78 *
nkhorman 11:86909e6db3c8 79 */
nkhorman 9:ddb97c9850a2 80 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 81 {
nkhorman 9:ddb97c9850a2 82 public:
nkhorman 11:86909e6db3c8 83 /** 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 84 *
nkhorman 11:86909e6db3c8 85 * Required parameters
nkhorman 11:86909e6db3c8 86 * @param spi - a reference to an initialized SPI object
nkhorman 11:86909e6db3c8 87 * @param DC (Data/Command) pin name
nkhorman 11:86909e6db3c8 88 * @param RST (Reset) pin name
nkhorman 11:86909e6db3c8 89 * @param CS (Chip Select) pin name
nkhorman 11:86909e6db3c8 90 *
nkhorman 11:86909e6db3c8 91 * Optional parameters
nkhorman 11:86909e6db3c8 92 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 93 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 94 */
JojoS 18:6aa925f254d6 95 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128, bool flipVertical = false)
JojoS 18:6aa925f254d6 96 : Adafruit_SSD1306(RST, rawHieght, rawWidth, flipVertical)
nkhorman 9:ddb97c9850a2 97 , cs(CS,true)
nkhorman 9:ddb97c9850a2 98 , dc(DC,false)
nkhorman 9:ddb97c9850a2 99 , mspi(spi)
nkhorman 9:ddb97c9850a2 100 {
nkhorman 9:ddb97c9850a2 101 begin();
nkhorman 9:ddb97c9850a2 102 splash();
nkhorman 9:ddb97c9850a2 103 display();
nkhorman 9:ddb97c9850a2 104 };
nkhorman 9:ddb97c9850a2 105
nkhorman 9:ddb97c9850a2 106 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 107 {
nkhorman 9:ddb97c9850a2 108 cs = 1;
nkhorman 9:ddb97c9850a2 109 dc = 0;
nkhorman 9:ddb97c9850a2 110 cs = 0;
nkhorman 9:ddb97c9850a2 111 mspi.write(c);
nkhorman 9:ddb97c9850a2 112 cs = 1;
nkhorman 9:ddb97c9850a2 113 };
nkhorman 9:ddb97c9850a2 114
nkhorman 9:ddb97c9850a2 115 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 116 {
nkhorman 9:ddb97c9850a2 117 cs = 1;
nkhorman 9:ddb97c9850a2 118 dc = 1;
nkhorman 9:ddb97c9850a2 119 cs = 0;
nkhorman 9:ddb97c9850a2 120 mspi.write(c);
nkhorman 9:ddb97c9850a2 121 cs = 1;
nkhorman 9:ddb97c9850a2 122 };
nkhorman 9:ddb97c9850a2 123
nkhorman 9:ddb97c9850a2 124 protected:
nkhorman 9:ddb97c9850a2 125 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 126 {
nkhorman 9:ddb97c9850a2 127 cs = 1;
nkhorman 9:ddb97c9850a2 128 dc = 1;
nkhorman 9:ddb97c9850a2 129 cs = 0;
nkhorman 9:ddb97c9850a2 130
nkhorman 9:ddb97c9850a2 131 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 9:ddb97c9850a2 132 mspi.write(buffer[i]);
nkhorman 9:ddb97c9850a2 133
nkhorman 11:86909e6db3c8 134 if(height() == 32)
nkhorman 11:86909e6db3c8 135 {
nkhorman 11:86909e6db3c8 136 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 11:86909e6db3c8 137 mspi.write(0);
nkhorman 11:86909e6db3c8 138 }
nkhorman 11:86909e6db3c8 139
nkhorman 9:ddb97c9850a2 140 cs = 1;
nkhorman 9:ddb97c9850a2 141 };
nkhorman 9:ddb97c9850a2 142
JojoS 17:f0cf0e1210be 143 DigitalOut cs, dc;
nkhorman 9:ddb97c9850a2 144 SPI &mspi;
nkhorman 9:ddb97c9850a2 145 };
nkhorman 9:ddb97c9850a2 146
nkhorman 11:86909e6db3c8 147 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 148 *
nkhorman 11:86909e6db3c8 149 */
nkhorman 9:ddb97c9850a2 150 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 151 {
nkhorman 9:ddb97c9850a2 152 public:
nkhorman 9:ddb97c9850a2 153 #define SSD_I2C_ADDRESS 0x78
nkhorman 11:86909e6db3c8 154 /** 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 155 *
nkhorman 11:86909e6db3c8 156 * Required parameters
nkhorman 11:86909e6db3c8 157 * @param i2c - A reference to an initialized I2C object
nkhorman 11:86909e6db3c8 158 * @param RST - The Reset pin name
nkhorman 11:86909e6db3c8 159 *
nkhorman 11:86909e6db3c8 160 * Optional parameters
nkhorman 11:86909e6db3c8 161 * @param i2cAddress - The i2c address of the display
nkhorman 11:86909e6db3c8 162 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 163 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 164 */
JojoS 18:6aa925f254d6 165 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128, bool flipVertical = false)
JojoS 18:6aa925f254d6 166 : Adafruit_SSD1306(RST, rawHeight, rawWidth, flipVertical)
nkhorman 9:ddb97c9850a2 167 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 168 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 169 {
nkhorman 9:ddb97c9850a2 170 begin();
nkhorman 9:ddb97c9850a2 171 splash();
nkhorman 9:ddb97c9850a2 172 display();
nkhorman 9:ddb97c9850a2 173 };
nkhorman 9:ddb97c9850a2 174
nkhorman 9:ddb97c9850a2 175 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 176 {
nkhorman 9:ddb97c9850a2 177 char buff[2];
nkhorman 9:ddb97c9850a2 178 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 179 buff[1] = c;
nkhorman 9:ddb97c9850a2 180 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 181 }
nkhorman 9:ddb97c9850a2 182
nkhorman 9:ddb97c9850a2 183 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 184 {
nkhorman 9:ddb97c9850a2 185 char buff[2];
nkhorman 9:ddb97c9850a2 186 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 187 buff[1] = c;
nkhorman 9:ddb97c9850a2 188 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 189 };
nkhorman 9:ddb97c9850a2 190
nkhorman 9:ddb97c9850a2 191 protected:
nkhorman 9:ddb97c9850a2 192 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 193 {
nkhorman 9:ddb97c9850a2 194 char buff[17];
nkhorman 9:ddb97c9850a2 195 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 196
nkhorman 9:ddb97c9850a2 197 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 198 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 199 { uint8_t x ;
nkhorman 9:ddb97c9850a2 200
nkhorman 9:ddb97c9850a2 201 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 202 for(x=1; x<sizeof(buff); x++)
JojoS 15:77feec1c0684 203 buff[x] = buffer[i+x-1];
nkhorman 9:ddb97c9850a2 204 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 205 }
nkhorman 9:ddb97c9850a2 206 };
nkhorman 9:ddb97c9850a2 207
nkhorman 9:ddb97c9850a2 208 I2C &mi2c;
nkhorman 9:ddb97c9850a2 209 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 210 };
nkhorman 0:c3dcd4c4983a 211
nkhorman 0:c3dcd4c4983a 212 #endif