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:
Neal Horman
Date:
Sat Oct 18 11:47:17 2014 -0500
Revision:
6:1be3e3b46eb7
Parent:
3:969ca9fe5e2b
Child:
7:779204e24db4
hand merge Alo?s Wolff's i2c additions with a few #define tweaks

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 0:c3dcd4c4983a 20 * Modified by Neal Horman 7/14/2012 for use in LPC1768
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 0:c3dcd4c4983a 29 /*=========================================================================
nkhorman 0:c3dcd4c4983a 30 SSD1306 Displays
nkhorman 0:c3dcd4c4983a 31 -----------------------------------------------------------------------
nkhorman 0:c3dcd4c4983a 32 The driver is used in multiple displays (128x64, 128x32, etc.).
nkhorman 0:c3dcd4c4983a 33 Select the appropriate display below to create an appropriately
nkhorman 0:c3dcd4c4983a 34 sized framebuffer, etc.
nkhorman 0:c3dcd4c4983a 35
nkhorman 0:c3dcd4c4983a 36 SSD1306_128_64 128x64 pixel display
nkhorman 0:c3dcd4c4983a 37
nkhorman 0:c3dcd4c4983a 38 SSD1306_128_32 128x32 pixel display
nkhorman 0:c3dcd4c4983a 39
nkhorman 0:c3dcd4c4983a 40 You also need to set the LCDWIDTH and LCDHEIGHT defines to an
nkhorman 0:c3dcd4c4983a 41 appropriate size
nkhorman 0:c3dcd4c4983a 42
nkhorman 0:c3dcd4c4983a 43 -----------------------------------------------------------------------*/
nkhorman 0:c3dcd4c4983a 44 //#define SSD1306_128_64
nkhorman 0:c3dcd4c4983a 45 #define SSD1306_128_32
Neal Horman 6:1be3e3b46eb7 46
Neal Horman 6:1be3e3b46eb7 47 #define SSD_USES_SPI
Neal Horman 6:1be3e3b46eb7 48 //#define SSD_USES_I2C
nkhorman 0:c3dcd4c4983a 49 /*=========================================================================*/
nkhorman 0:c3dcd4c4983a 50
nkhorman 0:c3dcd4c4983a 51 #if defined SSD1306_128_64 && defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 52 #error "Only one SSD1306 display can be specified at once in SSD1306.h"
nkhorman 0:c3dcd4c4983a 53 #endif
nkhorman 0:c3dcd4c4983a 54 #if !defined SSD1306_128_64 && !defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 55 #error "At least one SSD1306 display must be specified in SSD1306.h"
nkhorman 0:c3dcd4c4983a 56 #endif
nkhorman 0:c3dcd4c4983a 57
nkhorman 0:c3dcd4c4983a 58 #if defined SSD1306_128_64
nkhorman 0:c3dcd4c4983a 59 #define SSD1306_LCDWIDTH 128
nkhorman 0:c3dcd4c4983a 60 #define SSD1306_LCDHEIGHT 64
nkhorman 0:c3dcd4c4983a 61 #endif
nkhorman 0:c3dcd4c4983a 62 #if defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 63 #define SSD1306_LCDWIDTH 128
nkhorman 0:c3dcd4c4983a 64 #define SSD1306_LCDHEIGHT 32
nkhorman 0:c3dcd4c4983a 65 #endif
nkhorman 0:c3dcd4c4983a 66
Neal Horman 6:1be3e3b46eb7 67 #if defined SSD_USES_SPI && defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 68 #error "Only one communication mode can be specified at once in SSD1306.h"
Neal Horman 6:1be3e3b46eb7 69 #endif
Neal Horman 6:1be3e3b46eb7 70 #if !defined SSD_USES_SPI && !defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 71 #error "At least one communication mode must be specified in SSD1306.h"
Neal Horman 6:1be3e3b46eb7 72 #endif
Neal Horman 6:1be3e3b46eb7 73
Neal Horman 6:1be3e3b46eb7 74 //#define WITHOUT_SPLASH
Neal Horman 6:1be3e3b46eb7 75 #define I2C_ADDRESS 0x78
Neal Horman 6:1be3e3b46eb7 76
Neal Horman 6:1be3e3b46eb7 77 #define SSD_Command_Mode 0x00
Neal Horman 6:1be3e3b46eb7 78 #define SSD_Data_Mode 0x40
Neal Horman 6:1be3e3b46eb7 79
nkhorman 0:c3dcd4c4983a 80 #define SSD1306_SETCONTRAST 0x81
nkhorman 0:c3dcd4c4983a 81 #define SSD1306_DISPLAYALLON_RESUME 0xA4
nkhorman 0:c3dcd4c4983a 82 #define SSD1306_DISPLAYALLON 0xA5
nkhorman 0:c3dcd4c4983a 83 #define SSD1306_NORMALDISPLAY 0xA6
nkhorman 0:c3dcd4c4983a 84 #define SSD1306_INVERTDISPLAY 0xA7
nkhorman 0:c3dcd4c4983a 85 #define SSD1306_DISPLAYOFF 0xAE
nkhorman 0:c3dcd4c4983a 86 #define SSD1306_DISPLAYON 0xAF
nkhorman 0:c3dcd4c4983a 87 #define SSD1306_SETDISPLAYOFFSET 0xD3
nkhorman 0:c3dcd4c4983a 88 #define SSD1306_SETCOMPINS 0xDA
nkhorman 0:c3dcd4c4983a 89 #define SSD1306_SETVCOMDETECT 0xDB
nkhorman 0:c3dcd4c4983a 90 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
nkhorman 0:c3dcd4c4983a 91 #define SSD1306_SETPRECHARGE 0xD9
nkhorman 0:c3dcd4c4983a 92 #define SSD1306_SETMULTIPLEX 0xA8
nkhorman 0:c3dcd4c4983a 93 #define SSD1306_SETLOWCOLUMN 0x00
nkhorman 0:c3dcd4c4983a 94 #define SSD1306_SETHIGHCOLUMN 0x10
nkhorman 0:c3dcd4c4983a 95 #define SSD1306_SETSTARTLINE 0x40
nkhorman 0:c3dcd4c4983a 96 #define SSD1306_MEMORYMODE 0x20
nkhorman 0:c3dcd4c4983a 97 #define SSD1306_COMSCANINC 0xC0
nkhorman 0:c3dcd4c4983a 98 #define SSD1306_COMSCANDEC 0xC8
nkhorman 0:c3dcd4c4983a 99 #define SSD1306_SEGREMAP 0xA0
nkhorman 0:c3dcd4c4983a 100 #define SSD1306_CHARGEPUMP 0x8D
nkhorman 0:c3dcd4c4983a 101 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 102 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 103
nkhorman 0:c3dcd4c4983a 104 // an DigitalOut sub-class that provides a constructed default state
nkhorman 0:c3dcd4c4983a 105 class DigitalOut2 : public DigitalOut
nkhorman 0:c3dcd4c4983a 106 {
nkhorman 0:c3dcd4c4983a 107 public:
nkhorman 3:969ca9fe5e2b 108 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
nkhorman 0:c3dcd4c4983a 109 DigitalOut2& operator= (int value) { write(value); return *this; };
nkhorman 0:c3dcd4c4983a 110 DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
nkhorman 0:c3dcd4c4983a 111 operator int() { return read(); };
nkhorman 0:c3dcd4c4983a 112 };
nkhorman 0:c3dcd4c4983a 113
nkhorman 0:c3dcd4c4983a 114 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 115 {
nkhorman 0:c3dcd4c4983a 116 public:
Neal Horman 6:1be3e3b46eb7 117 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 118 Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS);
Neal Horman 6:1be3e3b46eb7 119 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 120 Adafruit_SSD1306(I2C &i2c, PinName RST);
Neal Horman 6:1be3e3b46eb7 121 #endif
nkhorman 0:c3dcd4c4983a 122 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 0:c3dcd4c4983a 123 void ssd1306_command(uint8_t c);
nkhorman 0:c3dcd4c4983a 124 void ssd1306_data(uint8_t c);
nkhorman 0:c3dcd4c4983a 125
nkhorman 0:c3dcd4c4983a 126 void clearDisplay(void);
nkhorman 0:c3dcd4c4983a 127 virtual void invertDisplay(bool i);
nkhorman 0:c3dcd4c4983a 128 void display();
nkhorman 0:c3dcd4c4983a 129
nkhorman 0:c3dcd4c4983a 130 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 0:c3dcd4c4983a 131
nkhorman 0:c3dcd4c4983a 132 private:
Neal Horman 6:1be3e3b46eb7 133
Neal Horman 6:1be3e3b46eb7 134 Serial pc;
Neal Horman 6:1be3e3b46eb7 135 DigitalOut2 rst;
Neal Horman 6:1be3e3b46eb7 136 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 137 SPI &mspi;
Neal Horman 6:1be3e3b46eb7 138 DigitalOut2 cs,dc;
Neal Horman 6:1be3e3b46eb7 139 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 140 I2C &mi2c;
Neal Horman 6:1be3e3b46eb7 141 #endif
nkhorman 0:c3dcd4c4983a 142 // the memory buffer for the LCD
nkhorman 0:c3dcd4c4983a 143 uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8];
nkhorman 0:c3dcd4c4983a 144 };
nkhorman 0:c3dcd4c4983a 145
nkhorman 0:c3dcd4c4983a 146 #endif