- code cleaned up in many points for a better readability - removed SPIPreinit and I2CPreinit classes - moved various method implementations from .h files to the corresponding .cpp ones - the splash() and clearDisplay() methods now directly update the display; no more need to call the display() method after them

Dependencies:   Adafruit_GFX mbed

Fork of Adafruit_GFX by Neal Horman

Committer:
frada
Date:
Fri Sep 04 10:08:31 2015 +0000
Revision:
18:7a3182a3fa21
Parent:
17:396d9b7eb7d5
-

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
frada 17:396d9b7eb7d5 21 * Edited by Francesco Adamo 2015/09/03:
frada 17:396d9b7eb7d5 22 * - code cleaned up in many points for a better readability
frada 17:396d9b7eb7d5 23 * - removed SPIPreinit and I2CPreinit classes
frada 17:396d9b7eb7d5 24 * - moved various method implementations from .h files to the corresponding .cpp ones
frada 17:396d9b7eb7d5 25 * - the splash() and clearDisplay() methods now directly update the display; no more need to call the display() method after them
frada 17:396d9b7eb7d5 26 *
nkhorman 0:c3dcd4c4983a 27 */
nkhorman 0:c3dcd4c4983a 28
nkhorman 0:c3dcd4c4983a 29 #ifndef _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 30 #define _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 31
nkhorman 0:c3dcd4c4983a 32 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 33 #include "Adafruit_GFX.h"
nkhorman 0:c3dcd4c4983a 34
nkhorman 9:ddb97c9850a2 35 #include <vector>
nkhorman 9:ddb97c9850a2 36 #include <algorithm>
nkhorman 0:c3dcd4c4983a 37
frada 17:396d9b7eb7d5 38 #define SSD1306_SETCONTRAST 0x81
frada 17:396d9b7eb7d5 39 #define SSD1306_DISPLAYALLON_RESUME 0xA4
frada 17:396d9b7eb7d5 40 #define SSD1306_DISPLAYALLON 0xA5
frada 17:396d9b7eb7d5 41 #define SSD1306_NORMALDISPLAY 0xA6
frada 17:396d9b7eb7d5 42 #define SSD1306_INVERTDISPLAY 0xA7
frada 17:396d9b7eb7d5 43 #define SSD1306_DISPLAYOFF 0xAE
frada 17:396d9b7eb7d5 44 #define SSD1306_DISPLAYON 0xAF
frada 17:396d9b7eb7d5 45 #define SSD1306_SETDISPLAYOFFSET 0xD3
frada 17:396d9b7eb7d5 46 #define SSD1306_SETCOMPINS 0xDA
frada 17:396d9b7eb7d5 47 #define SSD1306_SETVCOMDETECT 0xDB
frada 17:396d9b7eb7d5 48 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
frada 17:396d9b7eb7d5 49 #define SSD1306_SETPRECHARGE 0xD9
frada 17:396d9b7eb7d5 50 #define SSD1306_SETMULTIPLEX 0xA8
frada 17:396d9b7eb7d5 51 #define SSD1306_SETLOWCOLUMN 0x00
frada 17:396d9b7eb7d5 52 #define SSD1306_SETHIGHCOLUMN 0x10
frada 17:396d9b7eb7d5 53 #define SSD1306_SETSTARTLINE 0x40
frada 17:396d9b7eb7d5 54 #define SSD1306_MEMORYMODE 0x20
frada 17:396d9b7eb7d5 55 #define SSD1306_COMSCANINC 0xC0
frada 17:396d9b7eb7d5 56 #define SSD1306_COMSCANDEC 0xC8
frada 17:396d9b7eb7d5 57 #define SSD1306_SEGREMAP 0xA0
frada 17:396d9b7eb7d5 58 #define SSD1306_CHARGEPUMP 0x8D
frada 17:396d9b7eb7d5 59
frada 17:396d9b7eb7d5 60 #define SSD1306_EXTERNALVCC 0x1
frada 17:396d9b7eb7d5 61 #define SSD1306_SWITCHCAPVCC 0x2
frada 17:396d9b7eb7d5 62
nkhorman 9:ddb97c9850a2 63 // A DigitalOut sub-class that provides a constructed default state
nkhorman 9:ddb97c9850a2 64 class DigitalOut2 : public DigitalOut
nkhorman 9:ddb97c9850a2 65 {
nkhorman 9:ddb97c9850a2 66 public:
nkhorman 9:ddb97c9850a2 67 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
nkhorman 9:ddb97c9850a2 68 DigitalOut2& operator= (int value) { write(value); return *this; };
nkhorman 9:ddb97c9850a2 69 DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
nkhorman 9:ddb97c9850a2 70 operator int() { return read(); };
nkhorman 9:ddb97c9850a2 71 };
Neal Horman 6:1be3e3b46eb7 72
nkhorman 0:c3dcd4c4983a 73
nkhorman 11:86909e6db3c8 74 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 75 *
nkhorman 11:86909e6db3c8 76 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 77 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 78 */
nkhorman 0:c3dcd4c4983a 79 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 80 {
nkhorman 9:ddb97c9850a2 81 protected:
nkhorman 9:ddb97c9850a2 82 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 83 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 84
nkhorman 9:ddb97c9850a2 85 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 86 std::vector<uint8_t> buffer;
frada 17:396d9b7eb7d5 87
frada 17:396d9b7eb7d5 88 public:
frada 17:396d9b7eb7d5 89 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
frada 17:396d9b7eb7d5 90 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
frada 17:396d9b7eb7d5 91
frada 17:396d9b7eb7d5 92 // These must be implemented in the derived transport driver
frada 17:396d9b7eb7d5 93 virtual uint8_t command(uint8_t c) = 0;
frada 17:396d9b7eb7d5 94 virtual void data(uint8_t c) = 0;
frada 17:396d9b7eb7d5 95 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
frada 17:396d9b7eb7d5 96
frada 17:396d9b7eb7d5 97 // Clear the display buffer
frada 17:396d9b7eb7d5 98 void clearDisplay(void);
frada 17:396d9b7eb7d5 99 virtual void invertDisplay(bool i);
frada 17:396d9b7eb7d5 100
frada 17:396d9b7eb7d5 101 // Cause the display to be updated with the buffer content.
frada 17:396d9b7eb7d5 102 void display();
frada 17:396d9b7eb7d5 103
frada 17:396d9b7eb7d5 104 // Fill the buffer with the AdaFruit splash screen.
frada 17:396d9b7eb7d5 105 virtual void splash();
nkhorman 9:ddb97c9850a2 106 };
nkhorman 9:ddb97c9850a2 107
nkhorman 11:86909e6db3c8 108
nkhorman 11:86909e6db3c8 109 /** This is the SPI SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 110 *
nkhorman 11:86909e6db3c8 111 */
frada 17:396d9b7eb7d5 112 class Adafruit_SSD1306_SPI : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 113 {
nkhorman 9:ddb97c9850a2 114 public:
frada 17:396d9b7eb7d5 115 /*
frada 17:396d9b7eb7d5 116 * Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimensions
nkhorman 11:86909e6db3c8 117 *
nkhorman 11:86909e6db3c8 118 * Required parameters
frada 17:396d9b7eb7d5 119 * @param MOSI (SPI MOSI pin name)
frada 17:396d9b7eb7d5 120 * @param CLK (SPI clock pin name)
frada 17:396d9b7eb7d5 121 * @param CS (Chip Select) pin name
nkhorman 11:86909e6db3c8 122 * @param DC (Data/Command) pin name
nkhorman 11:86909e6db3c8 123 * @param RST (Reset) pin name
nkhorman 11:86909e6db3c8 124 *
nkhorman 11:86909e6db3c8 125 * Optional parameters
nkhorman 11:86909e6db3c8 126 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
frada 17:396d9b7eb7d5 127 * @param rawWidth - the horizontal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 128 */
frada 17:396d9b7eb7d5 129 Adafruit_SSD1306_SPI(PinName MOSI, PinName CLK, PinName CS, PinName DC, PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
frada 17:396d9b7eb7d5 130 virtual uint8_t command(uint8_t);
frada 17:396d9b7eb7d5 131 virtual void data(uint8_t);
frada 17:396d9b7eb7d5 132 virtual void sendDisplayBuffer(void);
frada 17:396d9b7eb7d5 133
nkhorman 9:ddb97c9850a2 134 protected:
nkhorman 9:ddb97c9850a2 135 DigitalOut2 cs, dc;
frada 17:396d9b7eb7d5 136 SPI mspi;
nkhorman 9:ddb97c9850a2 137 };
nkhorman 9:ddb97c9850a2 138
frada 17:396d9b7eb7d5 139
frada 17:396d9b7eb7d5 140 /*
frada 17:396d9b7eb7d5 141 * This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 142 *
nkhorman 11:86909e6db3c8 143 */
frada 17:396d9b7eb7d5 144 class Adafruit_SSD1306_I2C : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 145 {
nkhorman 9:ddb97c9850a2 146 public:
nkhorman 9:ddb97c9850a2 147 #define SSD_I2C_ADDRESS 0x78
nkhorman 11:86909e6db3c8 148 /** 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 149 *
nkhorman 11:86909e6db3c8 150 * Required parameters
nkhorman 11:86909e6db3c8 151 * @param i2c - A reference to an initialized I2C object
nkhorman 11:86909e6db3c8 152 * @param RST - The Reset pin name
nkhorman 11:86909e6db3c8 153 *
nkhorman 11:86909e6db3c8 154 * Optional parameters
nkhorman 11:86909e6db3c8 155 * @param i2cAddress - The i2c address of the display
nkhorman 11:86909e6db3c8 156 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 157 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 158 */
frada 17:396d9b7eb7d5 159 Adafruit_SSD1306_I2C(PinName SDA, PinName SCL, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
frada 17:396d9b7eb7d5 160 virtual uint8_t command(uint8_t c);
frada 17:396d9b7eb7d5 161 virtual void data(uint8_t c);
frada 17:396d9b7eb7d5 162 virtual void sendDisplayBuffer();
nkhorman 9:ddb97c9850a2 163
nkhorman 9:ddb97c9850a2 164 protected:
frada 17:396d9b7eb7d5 165 I2C mi2c;
nkhorman 9:ddb97c9850a2 166 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 167 };
nkhorman 0:c3dcd4c4983a 168
nkhorman 0:c3dcd4c4983a 169 #endif