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.

Dependents:   Display

Committer:
nkhorman
Date:
Sun Oct 19 20:55:27 2014 +0000
Revision:
9:ddb97c9850a2
Parent:
8:416b8fe451b3
Child:
10:d5aee2d2f8dd
Child:
11:86909e6db3c8
c++'ify the SPI vs I2C driver portions, instead of compile time defines.; The SSD1306 driver can now be instantiated multiple times, supporting simultaneous displays of differing dimensions.

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 0:c3dcd4c4983a 45 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 46 {
nkhorman 9:ddb97c9850a2 47 public:
nkhorman 9:ddb97c9850a2 48 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 49 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 9:ddb97c9850a2 50 , rst(RST,false)
nkhorman 9:ddb97c9850a2 51 {};
nkhorman 9:ddb97c9850a2 52
nkhorman 9:ddb97c9850a2 53 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 9:ddb97c9850a2 54 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 55 virtual void data(uint8_t c) = 0;
nkhorman 0:c3dcd4c4983a 56
nkhorman 9:ddb97c9850a2 57 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 58 virtual void invertDisplay(bool i);
nkhorman 9:ddb97c9850a2 59 void display();
nkhorman 9:ddb97c9850a2 60 virtual void splash();
nkhorman 9:ddb97c9850a2 61
nkhorman 9:ddb97c9850a2 62 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 0:c3dcd4c4983a 63
nkhorman 9:ddb97c9850a2 64 protected:
nkhorman 9:ddb97c9850a2 65 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 66 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 67
nkhorman 9:ddb97c9850a2 68 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 69 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 70 };
nkhorman 9:ddb97c9850a2 71
nkhorman 9:ddb97c9850a2 72 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 73 {
nkhorman 9:ddb97c9850a2 74 public:
nkhorman 9:ddb97c9850a2 75 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 76 : Adafruit_SSD1306(RST, rawHieght, rawWidth)
nkhorman 9:ddb97c9850a2 77 , cs(CS,true)
nkhorman 9:ddb97c9850a2 78 , dc(DC,false)
nkhorman 9:ddb97c9850a2 79 , mspi(spi)
nkhorman 9:ddb97c9850a2 80 {
nkhorman 9:ddb97c9850a2 81 begin();
nkhorman 9:ddb97c9850a2 82 splash();
nkhorman 9:ddb97c9850a2 83 display();
nkhorman 9:ddb97c9850a2 84 };
nkhorman 9:ddb97c9850a2 85
nkhorman 9:ddb97c9850a2 86 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 87 {
nkhorman 9:ddb97c9850a2 88 cs = 1;
nkhorman 9:ddb97c9850a2 89 dc = 0;
nkhorman 9:ddb97c9850a2 90 cs = 0;
nkhorman 9:ddb97c9850a2 91 mspi.write(c);
nkhorman 9:ddb97c9850a2 92 cs = 1;
nkhorman 9:ddb97c9850a2 93 };
nkhorman 9:ddb97c9850a2 94
nkhorman 9:ddb97c9850a2 95 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 96 {
nkhorman 9:ddb97c9850a2 97 cs = 1;
nkhorman 9:ddb97c9850a2 98 dc = 1;
nkhorman 9:ddb97c9850a2 99 cs = 0;
nkhorman 9:ddb97c9850a2 100 mspi.write(c);
nkhorman 9:ddb97c9850a2 101 cs = 1;
nkhorman 9:ddb97c9850a2 102 };
nkhorman 9:ddb97c9850a2 103
nkhorman 9:ddb97c9850a2 104 protected:
nkhorman 9:ddb97c9850a2 105 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 106 {
nkhorman 9:ddb97c9850a2 107 cs = 1;
nkhorman 9:ddb97c9850a2 108 dc = 1;
nkhorman 9:ddb97c9850a2 109 cs = 0;
nkhorman 9:ddb97c9850a2 110
nkhorman 9:ddb97c9850a2 111 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 9:ddb97c9850a2 112 mspi.write(buffer[i]);
nkhorman 9:ddb97c9850a2 113
nkhorman 9:ddb97c9850a2 114 cs = 1;
nkhorman 9:ddb97c9850a2 115 };
nkhorman 9:ddb97c9850a2 116
nkhorman 9:ddb97c9850a2 117 DigitalOut2 cs, dc;
nkhorman 9:ddb97c9850a2 118 SPI &mspi;
nkhorman 9:ddb97c9850a2 119 };
nkhorman 9:ddb97c9850a2 120
nkhorman 9:ddb97c9850a2 121 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 122 {
nkhorman 9:ddb97c9850a2 123 public:
nkhorman 9:ddb97c9850a2 124 #define SSD_I2C_ADDRESS 0x78
nkhorman 9:ddb97c9850a2 125 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 126 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
nkhorman 9:ddb97c9850a2 127 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 128 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 129 {
nkhorman 9:ddb97c9850a2 130 begin();
nkhorman 9:ddb97c9850a2 131 splash();
nkhorman 9:ddb97c9850a2 132 display();
nkhorman 9:ddb97c9850a2 133 };
nkhorman 9:ddb97c9850a2 134
nkhorman 9:ddb97c9850a2 135 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 136 {
nkhorman 9:ddb97c9850a2 137 char buff[2];
nkhorman 9:ddb97c9850a2 138 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 139 buff[1] = c;
nkhorman 9:ddb97c9850a2 140 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 141 }
nkhorman 9:ddb97c9850a2 142
nkhorman 9:ddb97c9850a2 143 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 144 {
nkhorman 9:ddb97c9850a2 145 char buff[2];
nkhorman 9:ddb97c9850a2 146 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 147 buff[1] = c;
nkhorman 9:ddb97c9850a2 148 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 149 };
nkhorman 9:ddb97c9850a2 150
nkhorman 9:ddb97c9850a2 151 protected:
nkhorman 9:ddb97c9850a2 152 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 153 {
nkhorman 9:ddb97c9850a2 154 char buff[17];
nkhorman 9:ddb97c9850a2 155 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 156
nkhorman 9:ddb97c9850a2 157 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 158 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 159 { uint8_t x ;
nkhorman 9:ddb97c9850a2 160
nkhorman 9:ddb97c9850a2 161 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 162 for(x=1; x<sizeof(buff); x++)
nkhorman 9:ddb97c9850a2 163 buff[x] = buffer[i+x];
nkhorman 9:ddb97c9850a2 164 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 165 }
nkhorman 9:ddb97c9850a2 166 };
nkhorman 9:ddb97c9850a2 167
nkhorman 9:ddb97c9850a2 168 I2C &mi2c;
nkhorman 9:ddb97c9850a2 169 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 170 };
nkhorman 0:c3dcd4c4983a 171
nkhorman 0:c3dcd4c4983a 172 #endif