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.
Fork of Adafruit_GFX by
Revision 6:1be3e3b46eb7, committed 2014-10-18
- Comitter:
- Neal Horman
- Date:
- Sat Oct 18 11:47:17 2014 -0500
- Parent:
- 5:315de3647c9f
- Child:
- 7:779204e24db4
- Commit message:
- hand merge Alo?s Wolff's i2c additions with a few #define tweaks
Changed in this revision
Adafruit_SSD1306.cpp | Show annotated file Show diff for this revision Revisions of this file |
Adafruit_SSD1306.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Adafruit_SSD1306.cpp Thu Oct 16 23:18:25 2014 -0500 +++ b/Adafruit_SSD1306.cpp Sat Oct 18 11:47:17 2014 -0500 @@ -94,6 +94,7 @@ }; #endif +#ifdef SSD_USES_SPI Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi) @@ -104,6 +105,22 @@ begin(); display(); }; +#elif defined SSD_USES_I2C +Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST) + : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) + , rst(RST,false),mi2c(i2c) +{ + + mi2c.frequency(400000); + mi2c.start(); +#ifndef WITHOUT_SPLASH + memcpy(buffer,splashScreen,sizeof(buffer)); +#endif + begin(); + display(); +}; +#endif + void Adafruit_SSD1306::begin(uint8_t vccstate) { @@ -198,20 +215,39 @@ void Adafruit_SSD1306::ssd1306_command(uint8_t c) { +#ifdef SSD_USES_SPI cs = 1; dc = 0; cs = 0; mspi.write(c); cs = 1; + +#elif defined SSD_USES_I2C + char buff[2] ; + buff[0] = SSD_Command_Mode ; + buff[1] = c; + mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff)); +#endif + } void Adafruit_SSD1306::ssd1306_data(uint8_t c) { +#ifdef SSD_USES_SPI cs = 1; dc = 1; cs = 0; mspi.write(c); cs = 1; +#elif defined SSD_USES_I2C + char buff[2] ; + // Setup D/C to switch to data mode + buff[0] = SSD_Data_Mode; + buff[1] = c; + // Write on i2c + mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff)); +#endif + } void Adafruit_SSD1306::display(void) @@ -219,7 +255,7 @@ ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 - +#ifdef SSD_USES_SPI cs = 1; dc = 1; cs = 0; @@ -235,6 +271,22 @@ } cs = 1; + +#elif defined SSD_USES_I2C + char buff[17] ; + uint8_t x ; + // Setup D/C to switch to data mode + buff[0] = SSD_Data_Mode; + + // loop trough all OLED buffer and + // send a bunch of 16 data byte in one xmission + for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 ) + { + for (x=1; x<=16; x++) + buff[x] = buffer[i+x]; + mi2c.write(SSD_I2C_ADDRESS, buff, 17); + } +#endif } // clear everything
--- a/Adafruit_SSD1306.h Thu Oct 16 23:18:25 2014 -0500 +++ b/Adafruit_SSD1306.h Sat Oct 18 11:47:17 2014 -0500 @@ -43,6 +43,9 @@ -----------------------------------------------------------------------*/ //#define SSD1306_128_64 #define SSD1306_128_32 + +#define SSD_USES_SPI +//#define SSD_USES_I2C /*=========================================================================*/ #if defined SSD1306_128_64 && defined SSD1306_128_32 @@ -61,6 +64,19 @@ #define SSD1306_LCDHEIGHT 32 #endif +#if defined SSD_USES_SPI && defined SSD_USES_I2C + #error "Only one communication mode can be specified at once in SSD1306.h" +#endif +#if !defined SSD_USES_SPI && !defined SSD_USES_I2C + #error "At least one communication mode must be specified in SSD1306.h" +#endif + +//#define WITHOUT_SPLASH +#define I2C_ADDRESS 0x78 + +#define SSD_Command_Mode 0x00 +#define SSD_Data_Mode 0x40 + #define SSD1306_SETCONTRAST 0x81 #define SSD1306_DISPLAYALLON_RESUME 0xA4 #define SSD1306_DISPLAYALLON 0xA5 @@ -98,7 +114,11 @@ class Adafruit_SSD1306 : public Adafruit_GFX { public: +#ifdef SSD_USES_SPI Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS); +#elif defined SSD_USES_I2C + Adafruit_SSD1306(I2C &i2c, PinName RST); +#endif void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); void ssd1306_command(uint8_t c); void ssd1306_data(uint8_t c); @@ -110,8 +130,15 @@ virtual void drawPixel(int16_t x, int16_t y, uint16_t color); private: - DigitalOut2 rst,cs,dc; + + Serial pc; + DigitalOut2 rst; +#ifdef SSD_USES_SPI SPI &mspi; + DigitalOut2 cs,dc; +#elif defined SSD_USES_I2C + I2C &mi2c; +#endif // the memory buffer for the LCD uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8]; };