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: servo_sensor ArchPro_TFT BLE_Display SSD1306_demo ... more
Import libraryAdafruit_GFX
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.
This is an SPI or I2C driver, font, and graphics drawing library as initially provided by Adafruit which has been modified for use in the mbed envionment.
Example
/* * Copyright (c) 2012 Neal Horman - http://www.wanlink.com * * License: MIT open source (http://opensource.org/licenses/MIT) * Summary; * Use / modify / distribute / publish it how you want and * if you use it, or don't, you can't hold me liable for how * it does or doesn't work. * If it doesn't work how you want, don't use it, or change * it so that it does work. */ #include "mbed.h" #include "Adafruit_SSD1306.h" DigitalOut myled(LED1); // an SPI sub-class that provides a constructed default class SPIPreInit : public SPI { public: SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) { format(8,3); frequency(2000000); }; }; // an I2C sub-class that provides a constructed default class I2CPreInit : public I2C { public: I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) { frequency(400000); start(); }; }; SPIPreInit gSpi(p5,NC,p7); Adafruit_SSD1306_Spi gOled1(gSpi,p26,p25,p24); I2CPreInit gI2C(p9,p10); Adafruit_SSD1306_I2c gOled2(gI2C,p27); int main() { uint16_t x=0; gOled1.printf("%ux%u OLED Display\r\n", gOled1.width(), gOled1.height()); gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height()); while(1) { myled = !myled; gOled1.printf("%u\r",x); gOled1.display(); gOled2.printf("%u\r",x); gOled2.display(); x++; wait(1.0); } }
Adafruit_SSD1306.h
- Committer:
- nkhorman
- Date:
- 2014-10-19
- Revision:
- 9:ddb97c9850a2
- Parent:
- 8:416b8fe451b3
- Child:
- 10:d5aee2d2f8dd
- Child:
- 11:86909e6db3c8
File content as of revision 9:ddb97c9850a2:
/********************************************************************* This is a library for our Monochrome OLEDs based on SSD1306 drivers Pick one up today in the adafruit shop! ------> http://www.adafruit.com/category/63_98 These displays use SPI to communicate, 4 or 5 pins are required to interface Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, check license.txt for more information All text above, and the splash screen must be included in any redistribution *********************************************************************/ /* * Modified by Neal Horman 7/14/2012 for use in mbed */ #ifndef _ADAFRUIT_SSD1306_H_ #define _ADAFRUIT_SSD1306_H_ #include "mbed.h" #include "Adafruit_GFX.h" #include <vector> #include <algorithm> // A DigitalOut sub-class that provides a constructed default state class DigitalOut2 : public DigitalOut { public: DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); }; DigitalOut2& operator= (int value) { write(value); return *this; }; DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; }; operator int() { return read(); }; }; #define SSD1306_EXTERNALVCC 0x1 #define SSD1306_SWITCHCAPVCC 0x2 class Adafruit_SSD1306 : public Adafruit_GFX { public: Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128) : Adafruit_GFX(rawWidth,rawHeight) , rst(RST,false) {}; void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); virtual void command(uint8_t c) = 0; virtual void data(uint8_t c) = 0; void clearDisplay(void); virtual void invertDisplay(bool i); void display(); virtual void splash(); virtual void drawPixel(int16_t x, int16_t y, uint16_t color); protected: virtual void sendDisplayBuffer() = 0; DigitalOut2 rst; // the memory buffer for the LCD std::vector<uint8_t> buffer; }; class Adafruit_SSD1306_Spi : public Adafruit_SSD1306 { public: Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128) : Adafruit_SSD1306(RST, rawHieght, rawWidth) , cs(CS,true) , dc(DC,false) , mspi(spi) { begin(); splash(); display(); }; virtual void command(uint8_t c) { cs = 1; dc = 0; cs = 0; mspi.write(c); cs = 1; }; virtual void data(uint8_t c) { cs = 1; dc = 1; cs = 0; mspi.write(c); cs = 1; }; protected: virtual void sendDisplayBuffer() { cs = 1; dc = 1; cs = 0; for(uint16_t i=0, q=buffer.size(); i<q; i++) mspi.write(buffer[i]); cs = 1; }; DigitalOut2 cs, dc; SPI &mspi; }; class Adafruit_SSD1306_I2c : public Adafruit_SSD1306 { public: #define SSD_I2C_ADDRESS 0x78 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128) : Adafruit_SSD1306(RST, rawHeight, rawWidth) , mi2c(i2c) , mi2cAddress(i2cAddress) { begin(); splash(); display(); }; virtual void command(uint8_t c) { char buff[2]; buff[0] = 0; // Command Mode buff[1] = c; mi2c.write(mi2cAddress, buff, sizeof(buff)); } virtual void data(uint8_t c) { char buff[2]; buff[0] = 0x40; // Data Mode buff[1] = c; mi2c.write(mi2cAddress, buff, sizeof(buff)); }; protected: virtual void sendDisplayBuffer() { char buff[17]; buff[0] = 0x40; // Data Mode // send display buffer in 16 byte chunks for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) { uint8_t x ; // TODO - this will segfault if buffer.size() % 16 != 0 for(x=1; x<sizeof(buff); x++) buff[x] = buffer[i+x]; mi2c.write(mi2cAddress, buff, sizeof(buff)); } }; I2C &mi2c; uint8_t mi2cAddress; }; #endif