.edited to work on 128x64 display. .added preamble per page address (character rows)

Committer:
switches
Date:
Tue Feb 07 23:52:12 2017 +0000
Revision:
18:a267f00528be
Parent:
15:77feec1c0684
Child:
19:fdb58b689175
Library for Adafruit OLED Feather Wing

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
switches 18:a267f00528be 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
nkhorman 11:86909e6db3c8 36 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 37 *
nkhorman 11:86909e6db3c8 38 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 39 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 40 */
nkhorman 0:c3dcd4c4983a 41 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 42 {
nkhorman 9:ddb97c9850a2 43 public:
switches 18:a267f00528be 44 Adafruit_SSD1306(uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 45 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 11:86909e6db3c8 46 {
nkhorman 11:86909e6db3c8 47 buffer.resize(rawHeight * rawWidth / 8);
nkhorman 11:86909e6db3c8 48 };
nkhorman 9:ddb97c9850a2 49
nkhorman 9:ddb97c9850a2 50 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 11:86909e6db3c8 51
nkhorman 11:86909e6db3c8 52 // These must be implemented in the derived transport driver
nkhorman 9:ddb97c9850a2 53 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 54 virtual void data(uint8_t c) = 0;
nkhorman 11:86909e6db3c8 55 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 11:86909e6db3c8 56
nkhorman 11:86909e6db3c8 57 /// Clear the display buffer
nkhorman 9:ddb97c9850a2 58 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 59 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 60
nkhorman 11:86909e6db3c8 61 /// Cause the display to be updated with the buffer content.
nkhorman 9:ddb97c9850a2 62 void display();
nkhorman 11:86909e6db3c8 63 /// Fill the buffer with the AdaFruit splash screen.
nkhorman 9:ddb97c9850a2 64 virtual void splash();
nkhorman 9:ddb97c9850a2 65
nkhorman 9:ddb97c9850a2 66 protected:
nkhorman 9:ddb97c9850a2 67 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 68
nkhorman 9:ddb97c9850a2 69 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 70 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 71 };
nkhorman 9:ddb97c9850a2 72
nkhorman 11:86909e6db3c8 73
nkhorman 11:86909e6db3c8 74 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 75 *
nkhorman 11:86909e6db3c8 76 */
nkhorman 9:ddb97c9850a2 77 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 78 {
nkhorman 9:ddb97c9850a2 79 public:
nkhorman 9:ddb97c9850a2 80 #define SSD_I2C_ADDRESS 0x78
nkhorman 11:86909e6db3c8 81 /** 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 82 *
nkhorman 11:86909e6db3c8 83 * Required parameters
nkhorman 11:86909e6db3c8 84 * @param i2c - A reference to an initialized I2C object
nkhorman 11:86909e6db3c8 85 * @param RST - The Reset pin name
nkhorman 11:86909e6db3c8 86 *
nkhorman 11:86909e6db3c8 87 * Optional parameters
nkhorman 11:86909e6db3c8 88 * @param i2cAddress - The i2c address of the display
nkhorman 11:86909e6db3c8 89 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 90 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 91 */
switches 18:a267f00528be 92 Adafruit_SSD1306_I2c(I2C &i2c, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
switches 18:a267f00528be 93 : Adafruit_SSD1306(rawHeight, rawWidth)
nkhorman 9:ddb97c9850a2 94 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 95 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 96 {
nkhorman 9:ddb97c9850a2 97 begin();
nkhorman 9:ddb97c9850a2 98 splash();
nkhorman 9:ddb97c9850a2 99 display();
nkhorman 9:ddb97c9850a2 100 };
nkhorman 9:ddb97c9850a2 101
nkhorman 9:ddb97c9850a2 102 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 103 {
nkhorman 9:ddb97c9850a2 104 char buff[2];
nkhorman 9:ddb97c9850a2 105 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 106 buff[1] = c;
nkhorman 9:ddb97c9850a2 107 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 108 }
nkhorman 9:ddb97c9850a2 109
nkhorman 9:ddb97c9850a2 110 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 111 {
nkhorman 9:ddb97c9850a2 112 char buff[2];
nkhorman 9:ddb97c9850a2 113 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 114 buff[1] = c;
nkhorman 9:ddb97c9850a2 115 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 116 };
nkhorman 9:ddb97c9850a2 117
nkhorman 9:ddb97c9850a2 118 protected:
nkhorman 9:ddb97c9850a2 119 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 120 {
nkhorman 9:ddb97c9850a2 121 char buff[17];
nkhorman 9:ddb97c9850a2 122 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 123
nkhorman 9:ddb97c9850a2 124 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 125 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 126 { uint8_t x ;
nkhorman 9:ddb97c9850a2 127
nkhorman 9:ddb97c9850a2 128 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 129 for(x=1; x<sizeof(buff); x++)
JojoS 15:77feec1c0684 130 buff[x] = buffer[i+x-1];
nkhorman 9:ddb97c9850a2 131 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 132 }
nkhorman 9:ddb97c9850a2 133 };
nkhorman 9:ddb97c9850a2 134
nkhorman 9:ddb97c9850a2 135 I2C &mi2c;
nkhorman 9:ddb97c9850a2 136 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 137 };
nkhorman 0:c3dcd4c4983a 138
nkhorman 0:c3dcd4c4983a 139 #endif