Forked from Neal Horman: Adafruit_GFX, 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. Now it is adopted also for the SH1106 I2C 128x64 display as well...

Dependents:   Lab06_oled_i2c Lab06_BME280_oled Lab06_oled_clock I2C_SSD1306andSH1106_nucleo_F446RE

Committer:
cspista
Date:
Mon Jan 31 13:07:52 2022 +0000
Revision:
17:00a1379bd18a
Parent:
15:77feec1c0684
Child:
19:1b773847a04b
Adopted for SH1106_I2C

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
cspista 17:00a1379bd18a 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
cspista 17:00a1379bd18a 10 Adafruit invests time and resources providing this open source code,
cspista 17:00a1379bd18a 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
cspista 17:00a1379bd18a 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:
cspista 17:00a1379bd18a 36 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin)
cspista 17:00a1379bd18a 37 {
cspista 17:00a1379bd18a 38 write(active);
cspista 17:00a1379bd18a 39 };
cspista 17:00a1379bd18a 40 DigitalOut2& operator= (int value)
cspista 17:00a1379bd18a 41 {
cspista 17:00a1379bd18a 42 write(value);
cspista 17:00a1379bd18a 43 return *this;
cspista 17:00a1379bd18a 44 };
cspista 17:00a1379bd18a 45 DigitalOut2& operator= (DigitalOut2& rhs)
cspista 17:00a1379bd18a 46 {
cspista 17:00a1379bd18a 47 write(rhs.read());
cspista 17:00a1379bd18a 48 return *this;
cspista 17:00a1379bd18a 49 };
cspista 17:00a1379bd18a 50 operator int()
cspista 17:00a1379bd18a 51 {
cspista 17:00a1379bd18a 52 return read();
cspista 17:00a1379bd18a 53 };
nkhorman 9:ddb97c9850a2 54 };
Neal Horman 6:1be3e3b46eb7 55
nkhorman 0:c3dcd4c4983a 56 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 57 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 58
nkhorman 11:86909e6db3c8 59 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 60 *
nkhorman 11:86909e6db3c8 61 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 62 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 63 */
nkhorman 0:c3dcd4c4983a 64 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 65 {
nkhorman 9:ddb97c9850a2 66 public:
cspista 17:00a1379bd18a 67 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
cspista 17:00a1379bd18a 68 : Adafruit_GFX(rawWidth,rawHeight)
cspista 17:00a1379bd18a 69 , rst(RST,false)
cspista 17:00a1379bd18a 70 {
cspista 17:00a1379bd18a 71 buffer.resize(rawHeight * rawWidth / 8);
cspista 17:00a1379bd18a 72 };
nkhorman 9:ddb97c9850a2 73
cspista 17:00a1379bd18a 74 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
cspista 17:00a1379bd18a 75
cspista 17:00a1379bd18a 76 // These must be implemented in the derived transport driver
cspista 17:00a1379bd18a 77 virtual void command(uint8_t c) = 0;
cspista 17:00a1379bd18a 78 virtual void data(uint8_t c) = 0;
cspista 17:00a1379bd18a 79 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 11:86909e6db3c8 80
cspista 17:00a1379bd18a 81 /// Clear the display buffer
cspista 17:00a1379bd18a 82 void clearDisplay(void);
cspista 17:00a1379bd18a 83 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 84
cspista 17:00a1379bd18a 85 /// Cause the display to be updated with the buffer content.
cspista 17:00a1379bd18a 86 void display();
cspista 17:00a1379bd18a 87 /// Fill the buffer with the AdaFruit splash screen.
cspista 17:00a1379bd18a 88 virtual void splash();
cspista 17:00a1379bd18a 89
nkhorman 9:ddb97c9850a2 90 protected:
cspista 17:00a1379bd18a 91 virtual void sendDisplayBuffer() = 0;
cspista 17:00a1379bd18a 92 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 93
cspista 17:00a1379bd18a 94 // the memory buffer for the LCD
cspista 17:00a1379bd18a 95 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 96 };
nkhorman 9:ddb97c9850a2 97
nkhorman 11:86909e6db3c8 98
nkhorman 11:86909e6db3c8 99 /** This is the SPI SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 100 *
nkhorman 11:86909e6db3c8 101 */
nkhorman 9:ddb97c9850a2 102 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 103 {
nkhorman 9:ddb97c9850a2 104 public:
cspista 17:00a1379bd18a 105 /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
cspista 17:00a1379bd18a 106 *
cspista 17:00a1379bd18a 107 * Required parameters
cspista 17:00a1379bd18a 108 * @param spi - a reference to an initialized SPI object
cspista 17:00a1379bd18a 109 * @param DC (Data/Command) pin name
cspista 17:00a1379bd18a 110 * @param RST (Reset) pin name
cspista 17:00a1379bd18a 111 * @param CS (Chip Select) pin name
cspista 17:00a1379bd18a 112 *
cspista 17:00a1379bd18a 113 * Optional parameters
cspista 17:00a1379bd18a 114 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
cspista 17:00a1379bd18a 115 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
cspista 17:00a1379bd18a 116 */
cspista 17:00a1379bd18a 117 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
cspista 17:00a1379bd18a 118 : Adafruit_SSD1306(RST, rawHieght, rawWidth)
cspista 17:00a1379bd18a 119 , cs(CS,true)
cspista 17:00a1379bd18a 120 , dc(DC,false)
cspista 17:00a1379bd18a 121 , mspi(spi)
cspista 17:00a1379bd18a 122 {
cspista 17:00a1379bd18a 123 begin();
cspista 17:00a1379bd18a 124 splash();
cspista 17:00a1379bd18a 125 display();
cspista 17:00a1379bd18a 126 };
nkhorman 9:ddb97c9850a2 127
cspista 17:00a1379bd18a 128 virtual void command(uint8_t c)
cspista 17:00a1379bd18a 129 {
cspista 17:00a1379bd18a 130 cs = 1;
cspista 17:00a1379bd18a 131 dc = 0;
cspista 17:00a1379bd18a 132 cs = 0;
cspista 17:00a1379bd18a 133 mspi.write(c);
cspista 17:00a1379bd18a 134 cs = 1;
cspista 17:00a1379bd18a 135 };
nkhorman 9:ddb97c9850a2 136
cspista 17:00a1379bd18a 137 virtual void data(uint8_t c)
cspista 17:00a1379bd18a 138 {
cspista 17:00a1379bd18a 139 cs = 1;
cspista 17:00a1379bd18a 140 dc = 1;
cspista 17:00a1379bd18a 141 cs = 0;
cspista 17:00a1379bd18a 142 mspi.write(c);
cspista 17:00a1379bd18a 143 cs = 1;
cspista 17:00a1379bd18a 144 };
nkhorman 9:ddb97c9850a2 145
nkhorman 9:ddb97c9850a2 146 protected:
cspista 17:00a1379bd18a 147 virtual void sendDisplayBuffer()
cspista 17:00a1379bd18a 148 {
cspista 17:00a1379bd18a 149 cs = 1;
cspista 17:00a1379bd18a 150 dc = 1;
cspista 17:00a1379bd18a 151 cs = 0;
nkhorman 9:ddb97c9850a2 152
cspista 17:00a1379bd18a 153 for(uint16_t i=0, q=buffer.size(); i<q; i++)
cspista 17:00a1379bd18a 154 mspi.write(buffer[i]);
nkhorman 9:ddb97c9850a2 155
cspista 17:00a1379bd18a 156 if(height() == 32) {
cspista 17:00a1379bd18a 157 for(uint16_t i=0, q=buffer.size(); i<q; i++)
cspista 17:00a1379bd18a 158 mspi.write(0);
cspista 17:00a1379bd18a 159 }
nkhorman 11:86909e6db3c8 160
cspista 17:00a1379bd18a 161 cs = 1;
cspista 17:00a1379bd18a 162 };
nkhorman 9:ddb97c9850a2 163
cspista 17:00a1379bd18a 164 DigitalOut2 cs, dc;
cspista 17:00a1379bd18a 165 SPI &mspi;
nkhorman 9:ddb97c9850a2 166 };
nkhorman 9:ddb97c9850a2 167
nkhorman 11:86909e6db3c8 168 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 169 *
nkhorman 11:86909e6db3c8 170 */
nkhorman 9:ddb97c9850a2 171 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 172 {
nkhorman 9:ddb97c9850a2 173 public:
cspista 17:00a1379bd18a 174 #define SSD_I2C_ADDRESS 0x78
cspista 17:00a1379bd18a 175 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
cspista 17:00a1379bd18a 176 *
cspista 17:00a1379bd18a 177 * Required parameters
cspista 17:00a1379bd18a 178 * @param i2c - A reference to an initialized I2C object
cspista 17:00a1379bd18a 179 * @param RST - The Reset pin name
cspista 17:00a1379bd18a 180 *
cspista 17:00a1379bd18a 181 * Optional parameters
cspista 17:00a1379bd18a 182 * @param i2cAddress - The i2c address of the display
cspista 17:00a1379bd18a 183 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
cspista 17:00a1379bd18a 184 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
cspista 17:00a1379bd18a 185 */
cspista 17:00a1379bd18a 186 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
cspista 17:00a1379bd18a 187 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
cspista 17:00a1379bd18a 188 , mi2c(i2c)
cspista 17:00a1379bd18a 189 , mi2cAddress(i2cAddress)
cspista 17:00a1379bd18a 190 {
cspista 17:00a1379bd18a 191 begin();
cspista 17:00a1379bd18a 192 splash();
cspista 17:00a1379bd18a 193 display();
cspista 17:00a1379bd18a 194 };
nkhorman 9:ddb97c9850a2 195
cspista 17:00a1379bd18a 196 virtual void command(uint8_t c)
cspista 17:00a1379bd18a 197 {
cspista 17:00a1379bd18a 198 char buff[2];
cspista 17:00a1379bd18a 199 buff[0] = 0; // Command Mode
cspista 17:00a1379bd18a 200 buff[1] = c;
cspista 17:00a1379bd18a 201 mi2c.write(mi2cAddress, buff, sizeof(buff));
cspista 17:00a1379bd18a 202 }
nkhorman 9:ddb97c9850a2 203
cspista 17:00a1379bd18a 204 virtual void data(uint8_t c)
cspista 17:00a1379bd18a 205 {
cspista 17:00a1379bd18a 206 char buff[2];
cspista 17:00a1379bd18a 207 buff[0] = 0x40; // Data Mode
cspista 17:00a1379bd18a 208 buff[1] = c;
cspista 17:00a1379bd18a 209 mi2c.write(mi2cAddress, buff, sizeof(buff));
cspista 17:00a1379bd18a 210 };
nkhorman 9:ddb97c9850a2 211
nkhorman 9:ddb97c9850a2 212 protected:
cspista 17:00a1379bd18a 213 virtual void sendDisplayBuffer()
cspista 17:00a1379bd18a 214 {
nkhorman 9:ddb97c9850a2 215
cspista 17:00a1379bd18a 216 char buff[256];
cspista 17:00a1379bd18a 217 char cmd[4] = {0, 0xB0, 0x00, 0x10};
cspista 17:00a1379bd18a 218 for (uint8_t m = 0; m < 8; m++) {
cspista 17:00a1379bd18a 219 buff[0] = 0x40;
cspista 17:00a1379bd18a 220 cmd[1] = 0xB0 + m;
cspista 17:00a1379bd18a 221 for(int i=0; i<128; i++) {
cspista 17:00a1379bd18a 222 buff[i+1]= buffer[m*128+i];
cspista 17:00a1379bd18a 223 }
cspista 17:00a1379bd18a 224 mi2c.write(mi2cAddress, cmd, 4);
cspista 17:00a1379bd18a 225 mi2c.write(mi2cAddress, buff, 129);
cspista 17:00a1379bd18a 226 }
cspista 17:00a1379bd18a 227 };
nkhorman 9:ddb97c9850a2 228
cspista 17:00a1379bd18a 229 I2C &mi2c;
cspista 17:00a1379bd18a 230 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 231 };
nkhorman 0:c3dcd4c4983a 232
cspista 17:00a1379bd18a 233 /** This is the I2C SH1106 display driver transport class
cspista 17:00a1379bd18a 234 *
cspista 17:00a1379bd18a 235 */
cspista 17:00a1379bd18a 236 class Adafruit_SH1106_I2c : public Adafruit_SSD1306
cspista 17:00a1379bd18a 237 {
cspista 17:00a1379bd18a 238 public:
cspista 17:00a1379bd18a 239 #define SSD_I2C_ADDRESS 0x78
cspista 17:00a1379bd18a 240 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
cspista 17:00a1379bd18a 241 *
cspista 17:00a1379bd18a 242 * Required parameters
cspista 17:00a1379bd18a 243 * @param i2c - A reference to an initialized I2C object
cspista 17:00a1379bd18a 244 * @param RST - The Reset pin name
cspista 17:00a1379bd18a 245 *
cspista 17:00a1379bd18a 246 * Optional parameters
cspista 17:00a1379bd18a 247 * @param i2cAddress - The i2c address of the display
cspista 17:00a1379bd18a 248 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
cspista 17:00a1379bd18a 249 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
cspista 17:00a1379bd18a 250 */
cspista 17:00a1379bd18a 251 Adafruit_SH1106_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 64, uint8_t rawWidth = 128)
cspista 17:00a1379bd18a 252 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
cspista 17:00a1379bd18a 253 , mi2c(i2c)
cspista 17:00a1379bd18a 254 , mi2cAddress(i2cAddress)
cspista 17:00a1379bd18a 255 {
cspista 17:00a1379bd18a 256 begin();
cspista 17:00a1379bd18a 257 splash();
cspista 17:00a1379bd18a 258 display();
cspista 17:00a1379bd18a 259 };
cspista 17:00a1379bd18a 260
cspista 17:00a1379bd18a 261 virtual void command(uint8_t c)
cspista 17:00a1379bd18a 262 {
cspista 17:00a1379bd18a 263 char buff[2];
cspista 17:00a1379bd18a 264 buff[0] = 0; // Command Mode
cspista 17:00a1379bd18a 265 buff[1] = c;
cspista 17:00a1379bd18a 266 mi2c.write(mi2cAddress, buff, sizeof(buff));
cspista 17:00a1379bd18a 267 }
cspista 17:00a1379bd18a 268
cspista 17:00a1379bd18a 269 virtual void data(uint8_t c)
cspista 17:00a1379bd18a 270 {
cspista 17:00a1379bd18a 271 char buff[2];
cspista 17:00a1379bd18a 272 buff[0] = 0x40; // Data Mode
cspista 17:00a1379bd18a 273 buff[1] = c;
cspista 17:00a1379bd18a 274 mi2c.write(mi2cAddress, buff, sizeof(buff));
cspista 17:00a1379bd18a 275 };
cspista 17:00a1379bd18a 276
cspista 17:00a1379bd18a 277 protected:
cspista 17:00a1379bd18a 278 virtual void sendDisplayBuffer()
cspista 17:00a1379bd18a 279 {
cspista 17:00a1379bd18a 280
cspista 17:00a1379bd18a 281 char buff[256];
cspista 17:00a1379bd18a 282 char cmd[4] = {0, 0xB0, 0x02, 0x10};
cspista 17:00a1379bd18a 283 for (uint8_t m = 0; m < 8; m++) {
cspista 17:00a1379bd18a 284 buff[0] = 0x40;
cspista 17:00a1379bd18a 285 cmd[1] = 0xB0 + m;
cspista 17:00a1379bd18a 286 for(int i=0; i<128; i++) {
cspista 17:00a1379bd18a 287 buff[i+1]= buffer[m*128+i];
cspista 17:00a1379bd18a 288 }
cspista 17:00a1379bd18a 289 mi2c.write(mi2cAddress, cmd, 4);
cspista 17:00a1379bd18a 290 mi2c.write(mi2cAddress, buff, 129);
cspista 17:00a1379bd18a 291 }
cspista 17:00a1379bd18a 292 };
cspista 17:00a1379bd18a 293
cspista 17:00a1379bd18a 294 I2C &mi2c;
cspista 17:00a1379bd18a 295 uint8_t mi2cAddress;
cspista 17:00a1379bd18a 296 };
cspista 17:00a1379bd18a 297
cspista 17:00a1379bd18a 298
cspista 17:00a1379bd18a 299
nkhorman 0:c3dcd4c4983a 300 #endif