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:   Low_Power_Long_Distance_IR_Vision_Robot Low_Power_Long_Distance_IR_Vision_Robot

Fork of Adafruit_GFX by Neal Horman

Committer:
dev_alexander
Date:
Wed Aug 01 01:45:24 2018 +0000
Revision:
17:4b1d8d813d7f
Parent:
15:77feec1c0684
Made it function with this demo as the RST pin is not PIN that is defined in the MAX326XX to the extent that I saw.

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
dev_alexander 17:4b1d8d813d7f 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
dev_alexander 17:4b1d8d813d7f 10 Adafruit invests time and resources providing this open source code,
dev_alexander 17:4b1d8d813d7f 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
dev_alexander 17:4b1d8d813d7f 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
dev_alexander 17:4b1d8d813d7f 21 * Modified by Greg Steiert 2/7/2017 for FeatherOLED
nkhorman 0:c3dcd4c4983a 22 */
nkhorman 0:c3dcd4c4983a 23
nkhorman 0:c3dcd4c4983a 24 #ifndef _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 25 #define _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 26
nkhorman 0:c3dcd4c4983a 27 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 28 #include "Adafruit_GFX.h"
nkhorman 0:c3dcd4c4983a 29
nkhorman 9:ddb97c9850a2 30 #include <vector>
nkhorman 9:ddb97c9850a2 31 #include <algorithm>
nkhorman 0:c3dcd4c4983a 32
nkhorman 0:c3dcd4c4983a 33 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 34 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 35
dev_alexander 17:4b1d8d813d7f 36 #define SSD1306_LCDWIDTH 128
dev_alexander 17:4b1d8d813d7f 37 #define SSD1306_LCDHEIGHT 32
dev_alexander 17:4b1d8d813d7f 38
dev_alexander 17:4b1d8d813d7f 39 // Scrolling #defines
dev_alexander 17:4b1d8d813d7f 40 #define SSD1306_ACTIVATE_SCROLL 0x2F
dev_alexander 17:4b1d8d813d7f 41 #define SSD1306_DEACTIVATE_SCROLL 0x2E
dev_alexander 17:4b1d8d813d7f 42 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
dev_alexander 17:4b1d8d813d7f 43 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
dev_alexander 17:4b1d8d813d7f 44 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
dev_alexander 17:4b1d8d813d7f 45 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
dev_alexander 17:4b1d8d813d7f 46 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
dev_alexander 17:4b1d8d813d7f 47
nkhorman 11:86909e6db3c8 48 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 49 *
nkhorman 11:86909e6db3c8 50 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 51 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 52 */
nkhorman 0:c3dcd4c4983a 53 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 54 {
nkhorman 9:ddb97c9850a2 55 public:
dev_alexander 17:4b1d8d813d7f 56 Adafruit_SSD1306(uint8_t rawHeight = 32, uint8_t rawWidth = 128)
dev_alexander 17:4b1d8d813d7f 57 : Adafruit_GFX(rawWidth,rawHeight) {
dev_alexander 17:4b1d8d813d7f 58 buffer.resize(rawHeight * rawWidth / 8);
dev_alexander 17:4b1d8d813d7f 59 };
nkhorman 9:ddb97c9850a2 60
dev_alexander 17:4b1d8d813d7f 61 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
dev_alexander 17:4b1d8d813d7f 62
dev_alexander 17:4b1d8d813d7f 63 // These must be implemented in the derived transport driver
dev_alexander 17:4b1d8d813d7f 64 virtual void command(uint8_t c) = 0;
dev_alexander 17:4b1d8d813d7f 65 virtual void data(uint8_t c) = 0;
dev_alexander 17:4b1d8d813d7f 66 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 11:86909e6db3c8 67
dev_alexander 17:4b1d8d813d7f 68 /// Clear the display buffer
dev_alexander 17:4b1d8d813d7f 69 void clearDisplay(void);
dev_alexander 17:4b1d8d813d7f 70 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 71
dev_alexander 17:4b1d8d813d7f 72 /// Cause the display to be updated with the buffer content.
dev_alexander 17:4b1d8d813d7f 73 void display();
dev_alexander 17:4b1d8d813d7f 74 /// Fill the buffer with the AdaFruit splash screen.
dev_alexander 17:4b1d8d813d7f 75 virtual void splash();
nkhorman 9:ddb97c9850a2 76
dev_alexander 17:4b1d8d813d7f 77 //Scrolling Options
dev_alexander 17:4b1d8d813d7f 78 void startscrollright(uint8_t start, uint8_t stop);
dev_alexander 17:4b1d8d813d7f 79 void startscrollleft(uint8_t start, uint8_t stop);
dev_alexander 17:4b1d8d813d7f 80
dev_alexander 17:4b1d8d813d7f 81 void startscrolldiagright(uint8_t start, uint8_t stop);
dev_alexander 17:4b1d8d813d7f 82 void startscrolldiagleft(uint8_t start, uint8_t stop);
dev_alexander 17:4b1d8d813d7f 83 void stopscroll(void);
nkhorman 9:ddb97c9850a2 84
nkhorman 11:86909e6db3c8 85
nkhorman 9:ddb97c9850a2 86
nkhorman 9:ddb97c9850a2 87 protected:
dev_alexander 17:4b1d8d813d7f 88 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 89
dev_alexander 17:4b1d8d813d7f 90 // the memory buffer for the LCD
dev_alexander 17:4b1d8d813d7f 91 std::vector<uint8_t> buffer;
dev_alexander 17:4b1d8d813d7f 92 };
nkhorman 11:86909e6db3c8 93
nkhorman 9:ddb97c9850a2 94
nkhorman 11:86909e6db3c8 95 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 96 *
nkhorman 11:86909e6db3c8 97 */
nkhorman 9:ddb97c9850a2 98 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 99 {
nkhorman 9:ddb97c9850a2 100 public:
dev_alexander 17:4b1d8d813d7f 101 #define SSD_I2C_ADDRESS 0x78
dev_alexander 17:4b1d8d813d7f 102 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
dev_alexander 17:4b1d8d813d7f 103 *
dev_alexander 17:4b1d8d813d7f 104 * Required parameters
dev_alexander 17:4b1d8d813d7f 105 * @param i2c - A reference to an initialized I2C object
dev_alexander 17:4b1d8d813d7f 106 * @param RST - The Reset pin name
dev_alexander 17:4b1d8d813d7f 107 *
dev_alexander 17:4b1d8d813d7f 108 * Optional parameters
dev_alexander 17:4b1d8d813d7f 109 * @param i2cAddress - The i2c address of the display
dev_alexander 17:4b1d8d813d7f 110 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
dev_alexander 17:4b1d8d813d7f 111 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
dev_alexander 17:4b1d8d813d7f 112 */
dev_alexander 17:4b1d8d813d7f 113 Adafruit_SSD1306_I2c(I2C &i2c, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
dev_alexander 17:4b1d8d813d7f 114 : Adafruit_SSD1306(rawHeight, rawWidth)
dev_alexander 17:4b1d8d813d7f 115 , mi2c(i2c)
dev_alexander 17:4b1d8d813d7f 116 , mi2cAddress(i2cAddress) {
dev_alexander 17:4b1d8d813d7f 117 begin();
dev_alexander 17:4b1d8d813d7f 118 splash();
dev_alexander 17:4b1d8d813d7f 119 display();
dev_alexander 17:4b1d8d813d7f 120 };
nkhorman 9:ddb97c9850a2 121
dev_alexander 17:4b1d8d813d7f 122 virtual void command(uint8_t c) {
dev_alexander 17:4b1d8d813d7f 123 char buff[2];
dev_alexander 17:4b1d8d813d7f 124 buff[0] = 0; // Command Mode
dev_alexander 17:4b1d8d813d7f 125 buff[1] = c;
dev_alexander 17:4b1d8d813d7f 126 mi2c.write(mi2cAddress, buff, sizeof(buff));
dev_alexander 17:4b1d8d813d7f 127 }
nkhorman 9:ddb97c9850a2 128
dev_alexander 17:4b1d8d813d7f 129 virtual void data(uint8_t c) {
dev_alexander 17:4b1d8d813d7f 130 char buff[2];
dev_alexander 17:4b1d8d813d7f 131 buff[0] = 0x40; // Data Mode
dev_alexander 17:4b1d8d813d7f 132 buff[1] = c;
dev_alexander 17:4b1d8d813d7f 133 mi2c.write(mi2cAddress, buff, sizeof(buff));
dev_alexander 17:4b1d8d813d7f 134 };
nkhorman 9:ddb97c9850a2 135
nkhorman 9:ddb97c9850a2 136 protected:
dev_alexander 17:4b1d8d813d7f 137 virtual void sendDisplayBuffer() {
dev_alexander 17:4b1d8d813d7f 138 char buff[17];
dev_alexander 17:4b1d8d813d7f 139 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 140
dev_alexander 17:4b1d8d813d7f 141 // send display buffer in 16 byte chunks
dev_alexander 17:4b1d8d813d7f 142 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) {
dev_alexander 17:4b1d8d813d7f 143 uint8_t x ;
nkhorman 9:ddb97c9850a2 144
dev_alexander 17:4b1d8d813d7f 145 // TODO - this will segfault if buffer.size() % 16 != 0
dev_alexander 17:4b1d8d813d7f 146 for(x=1; x<sizeof(buff); x++)
dev_alexander 17:4b1d8d813d7f 147 buff[x] = buffer[i+x-1];
dev_alexander 17:4b1d8d813d7f 148 mi2c.write(mi2cAddress, buff, sizeof(buff));
dev_alexander 17:4b1d8d813d7f 149 }
dev_alexander 17:4b1d8d813d7f 150 };
nkhorman 9:ddb97c9850a2 151
dev_alexander 17:4b1d8d813d7f 152 I2C &mi2c;
dev_alexander 17:4b1d8d813d7f 153 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 154 };
nkhorman 0:c3dcd4c4983a 155
nkhorman 0:c3dcd4c4983a 156 #endif