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.

128x32 OLED Display

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);
    }
}
Committer:
Neal Horman
Date:
Sat Oct 18 11:47:17 2014 -0500
Revision:
6:1be3e3b46eb7
Parent:
5:315de3647c9f
Child:
9:ddb97c9850a2
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 below 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 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 24 #include "Adafruit_SSD1306.h"
nkhorman 0:c3dcd4c4983a 25
Neal Horman 5:315de3647c9f 26 #ifndef WITHOUT_SPLASH
nkhorman 0:c3dcd4c4983a 27 uint8_t splashScreen[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = {
nkhorman 0:c3dcd4c4983a 28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
nkhorman 0:c3dcd4c4983a 32 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 33 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 37 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
nkhorman 0:c3dcd4c4983a 39 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
nkhorman 0:c3dcd4c4983a 40 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 41 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
nkhorman 0:c3dcd4c4983a 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
nkhorman 0:c3dcd4c4983a 43 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 44 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
nkhorman 0:c3dcd4c4983a 45 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
nkhorman 0:c3dcd4c4983a 47 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
nkhorman 0:c3dcd4c4983a 48 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 49 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
nkhorman 0:c3dcd4c4983a 50 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
nkhorman 0:c3dcd4c4983a 51 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 52 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
nkhorman 0:c3dcd4c4983a 53 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
nkhorman 0:c3dcd4c4983a 54 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
nkhorman 0:c3dcd4c4983a 55 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
nkhorman 0:c3dcd4c4983a 56 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 57 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
nkhorman 0:c3dcd4c4983a 58 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
nkhorman 0:c3dcd4c4983a 59 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 60 #if (SSD1306_LCDHEIGHT == 64)
nkhorman 0:c3dcd4c4983a 61 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
nkhorman 0:c3dcd4c4983a 62 0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
nkhorman 0:c3dcd4c4983a 63 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 65 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
nkhorman 0:c3dcd4c4983a 67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 69 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
nkhorman 0:c3dcd4c4983a 70 0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
nkhorman 0:c3dcd4c4983a 71 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 72 0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
nkhorman 0:c3dcd4c4983a 73 0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
nkhorman 0:c3dcd4c4983a 74 0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
nkhorman 0:c3dcd4c4983a 75 0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
nkhorman 0:c3dcd4c4983a 76 0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
nkhorman 0:c3dcd4c4983a 77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 78 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
nkhorman 0:c3dcd4c4983a 79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 80 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
nkhorman 0:c3dcd4c4983a 81 0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
nkhorman 0:c3dcd4c4983a 82 0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
nkhorman 0:c3dcd4c4983a 83 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
nkhorman 0:c3dcd4c4983a 84 0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
nkhorman 0:c3dcd4c4983a 85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
nkhorman 0:c3dcd4c4983a 93 #endif
nkhorman 0:c3dcd4c4983a 94 };
Neal Horman 5:315de3647c9f 95 #endif
nkhorman 0:c3dcd4c4983a 96
Neal Horman 6:1be3e3b46eb7 97 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 98 Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS)
nkhorman 0:c3dcd4c4983a 99 : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
nkhorman 0:c3dcd4c4983a 100 , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi)
nkhorman 0:c3dcd4c4983a 101 {
Neal Horman 5:315de3647c9f 102 #ifndef WITHOUT_SPLASH
nkhorman 0:c3dcd4c4983a 103 memcpy(buffer,splashScreen,sizeof(buffer));
Neal Horman 5:315de3647c9f 104 #endif
nkhorman 0:c3dcd4c4983a 105 begin();
nkhorman 0:c3dcd4c4983a 106 display();
nkhorman 0:c3dcd4c4983a 107 };
Neal Horman 6:1be3e3b46eb7 108 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 109 Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST)
Neal Horman 6:1be3e3b46eb7 110 : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
Neal Horman 6:1be3e3b46eb7 111 , rst(RST,false),mi2c(i2c)
Neal Horman 6:1be3e3b46eb7 112 {
Neal Horman 6:1be3e3b46eb7 113
Neal Horman 6:1be3e3b46eb7 114 mi2c.frequency(400000);
Neal Horman 6:1be3e3b46eb7 115 mi2c.start();
Neal Horman 6:1be3e3b46eb7 116 #ifndef WITHOUT_SPLASH
Neal Horman 6:1be3e3b46eb7 117 memcpy(buffer,splashScreen,sizeof(buffer));
Neal Horman 6:1be3e3b46eb7 118 #endif
Neal Horman 6:1be3e3b46eb7 119 begin();
Neal Horman 6:1be3e3b46eb7 120 display();
Neal Horman 6:1be3e3b46eb7 121 };
Neal Horman 6:1be3e3b46eb7 122 #endif
Neal Horman 6:1be3e3b46eb7 123
nkhorman 0:c3dcd4c4983a 124
nkhorman 0:c3dcd4c4983a 125 void Adafruit_SSD1306::begin(uint8_t vccstate)
nkhorman 0:c3dcd4c4983a 126 {
nkhorman 0:c3dcd4c4983a 127 rst = 1;
nkhorman 0:c3dcd4c4983a 128 // VDD (3.3V) goes high at start, lets just chill for a ms
nkhorman 0:c3dcd4c4983a 129 wait_ms(1);
nkhorman 0:c3dcd4c4983a 130 // bring reset low
nkhorman 0:c3dcd4c4983a 131 rst = 0;
nkhorman 0:c3dcd4c4983a 132 // wait 10ms
nkhorman 0:c3dcd4c4983a 133 wait_ms(10);
nkhorman 0:c3dcd4c4983a 134 // bring out of reset
nkhorman 0:c3dcd4c4983a 135 rst = 1;
nkhorman 0:c3dcd4c4983a 136 // turn on VCC (9V?)
nkhorman 0:c3dcd4c4983a 137
nkhorman 0:c3dcd4c4983a 138 #if defined SSD1306_128_32
Neal Horman 5:315de3647c9f 139 #define VAL_MULTIPLEX 0x1F
Neal Horman 5:315de3647c9f 140 #define VAL_COMPINS 0x02
Neal Horman 5:315de3647c9f 141 #define VAL_CONTRAST 0x8F
chrta 4:853097cfa773 142 #elif defined SSD1306_128_64
Neal Horman 5:315de3647c9f 143 #define VAL_MULTIPLEX 0x3F
Neal Horman 5:315de3647c9f 144 #define VAL_COMPINS 0x12
Neal Horman 5:315de3647c9f 145 #define VAL_CONTRAST ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF)
chrta 4:853097cfa773 146 #else
chrta 4:853097cfa773 147 #error "Display dimensions must be defined"
chrta 4:853097cfa773 148 #endif
chrta 4:853097cfa773 149
nkhorman 0:c3dcd4c4983a 150 ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE
nkhorman 0:c3dcd4c4983a 151 ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
nkhorman 0:c3dcd4c4983a 152 ssd1306_command(0x80); // the suggested ratio 0x80
nkhorman 0:c3dcd4c4983a 153 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8
Neal Horman 5:315de3647c9f 154 ssd1306_command(VAL_MULTIPLEX);
nkhorman 0:c3dcd4c4983a 155 ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3
nkhorman 0:c3dcd4c4983a 156 ssd1306_command(0x0); // no offset
nkhorman 0:c3dcd4c4983a 157 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
nkhorman 0:c3dcd4c4983a 158 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D
Neal Horman 5:315de3647c9f 159 ssd1306_command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
nkhorman 0:c3dcd4c4983a 160 ssd1306_command(SSD1306_MEMORYMODE); // 0x20
nkhorman 0:c3dcd4c4983a 161 ssd1306_command(0x00); // 0x0 act like ks0108
nkhorman 0:c3dcd4c4983a 162 ssd1306_command(SSD1306_SEGREMAP | 0x1);
nkhorman 0:c3dcd4c4983a 163 ssd1306_command(SSD1306_COMSCANDEC);
nkhorman 0:c3dcd4c4983a 164 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
Neal Horman 5:315de3647c9f 165 ssd1306_command(VAL_COMPINS);
nkhorman 0:c3dcd4c4983a 166 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
Neal Horman 5:315de3647c9f 167 ssd1306_command(VAL_CONTRAST);
nkhorman 0:c3dcd4c4983a 168 ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9
Neal Horman 5:315de3647c9f 169 ssd1306_command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
nkhorman 0:c3dcd4c4983a 170 ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB
nkhorman 0:c3dcd4c4983a 171 ssd1306_command(0x40);
nkhorman 0:c3dcd4c4983a 172 ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
nkhorman 0:c3dcd4c4983a 173 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6
nkhorman 0:c3dcd4c4983a 174
nkhorman 0:c3dcd4c4983a 175 ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel
nkhorman 0:c3dcd4c4983a 176 }
nkhorman 0:c3dcd4c4983a 177
nkhorman 0:c3dcd4c4983a 178 // the most basic function, set a single pixel
nkhorman 0:c3dcd4c4983a 179 void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color)
nkhorman 0:c3dcd4c4983a 180 {
nkhorman 0:c3dcd4c4983a 181 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
nkhorman 0:c3dcd4c4983a 182 return;
nkhorman 0:c3dcd4c4983a 183
nkhorman 0:c3dcd4c4983a 184 // check rotation, move pixel around if necessary
nkhorman 0:c3dcd4c4983a 185 switch (getRotation())
nkhorman 0:c3dcd4c4983a 186 {
nkhorman 0:c3dcd4c4983a 187 case 1:
nkhorman 0:c3dcd4c4983a 188 swap(x, y);
nkhorman 0:c3dcd4c4983a 189 x = _rawWidth - x - 1;
nkhorman 0:c3dcd4c4983a 190 break;
nkhorman 0:c3dcd4c4983a 191 case 2:
nkhorman 0:c3dcd4c4983a 192 x = _rawWidth - x - 1;
nkhorman 0:c3dcd4c4983a 193 y = _rawHeight - y - 1;
nkhorman 0:c3dcd4c4983a 194 break;
nkhorman 0:c3dcd4c4983a 195 case 3:
nkhorman 0:c3dcd4c4983a 196 swap(x, y);
nkhorman 0:c3dcd4c4983a 197 y = _rawHeight - y - 1;
nkhorman 0:c3dcd4c4983a 198 break;
nkhorman 0:c3dcd4c4983a 199 }
nkhorman 0:c3dcd4c4983a 200
nkhorman 0:c3dcd4c4983a 201 // x is which column
nkhorman 0:c3dcd4c4983a 202 if (color == WHITE)
nkhorman 0:c3dcd4c4983a 203 buffer[x+ (y/8)*SSD1306_LCDWIDTH] |= _BV((y%8));
nkhorman 0:c3dcd4c4983a 204 else
nkhorman 0:c3dcd4c4983a 205 buffer[x+ (y/8)*SSD1306_LCDWIDTH] &= ~_BV((y%8));
nkhorman 0:c3dcd4c4983a 206 }
nkhorman 0:c3dcd4c4983a 207
nkhorman 0:c3dcd4c4983a 208 void Adafruit_SSD1306::invertDisplay(bool i)
nkhorman 0:c3dcd4c4983a 209 {
nkhorman 0:c3dcd4c4983a 210 if (i)
nkhorman 0:c3dcd4c4983a 211 ssd1306_command(SSD1306_INVERTDISPLAY);
nkhorman 0:c3dcd4c4983a 212 else
nkhorman 0:c3dcd4c4983a 213 ssd1306_command(SSD1306_NORMALDISPLAY);
nkhorman 0:c3dcd4c4983a 214 }
nkhorman 0:c3dcd4c4983a 215
nkhorman 0:c3dcd4c4983a 216 void Adafruit_SSD1306::ssd1306_command(uint8_t c)
nkhorman 0:c3dcd4c4983a 217 {
Neal Horman 6:1be3e3b46eb7 218 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 219 cs = 1;
nkhorman 0:c3dcd4c4983a 220 dc = 0;
nkhorman 0:c3dcd4c4983a 221 cs = 0;
nkhorman 0:c3dcd4c4983a 222 mspi.write(c);
nkhorman 0:c3dcd4c4983a 223 cs = 1;
Neal Horman 6:1be3e3b46eb7 224
Neal Horman 6:1be3e3b46eb7 225 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 226 char buff[2] ;
Neal Horman 6:1be3e3b46eb7 227 buff[0] = SSD_Command_Mode ;
Neal Horman 6:1be3e3b46eb7 228 buff[1] = c;
Neal Horman 6:1be3e3b46eb7 229 mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
Neal Horman 6:1be3e3b46eb7 230 #endif
Neal Horman 6:1be3e3b46eb7 231
nkhorman 0:c3dcd4c4983a 232 }
nkhorman 0:c3dcd4c4983a 233
nkhorman 0:c3dcd4c4983a 234 void Adafruit_SSD1306::ssd1306_data(uint8_t c)
nkhorman 0:c3dcd4c4983a 235 {
Neal Horman 6:1be3e3b46eb7 236 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 237 cs = 1;
nkhorman 0:c3dcd4c4983a 238 dc = 1;
nkhorman 0:c3dcd4c4983a 239 cs = 0;
nkhorman 0:c3dcd4c4983a 240 mspi.write(c);
nkhorman 0:c3dcd4c4983a 241 cs = 1;
Neal Horman 6:1be3e3b46eb7 242 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 243 char buff[2] ;
Neal Horman 6:1be3e3b46eb7 244 // Setup D/C to switch to data mode
Neal Horman 6:1be3e3b46eb7 245 buff[0] = SSD_Data_Mode;
Neal Horman 6:1be3e3b46eb7 246 buff[1] = c;
Neal Horman 6:1be3e3b46eb7 247 // Write on i2c
Neal Horman 6:1be3e3b46eb7 248 mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
Neal Horman 6:1be3e3b46eb7 249 #endif
Neal Horman 6:1be3e3b46eb7 250
nkhorman 0:c3dcd4c4983a 251 }
nkhorman 0:c3dcd4c4983a 252
nkhorman 0:c3dcd4c4983a 253 void Adafruit_SSD1306::display(void)
nkhorman 0:c3dcd4c4983a 254 {
nkhorman 0:c3dcd4c4983a 255 ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
nkhorman 0:c3dcd4c4983a 256 ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
nkhorman 0:c3dcd4c4983a 257 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
Neal Horman 6:1be3e3b46eb7 258 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 259 cs = 1;
nkhorman 0:c3dcd4c4983a 260 dc = 1;
nkhorman 0:c3dcd4c4983a 261 cs = 0;
nkhorman 0:c3dcd4c4983a 262
nkhorman 0:c3dcd4c4983a 263 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++)
nkhorman 0:c3dcd4c4983a 264 mspi.write(buffer[i]);
nkhorman 0:c3dcd4c4983a 265
nkhorman 0:c3dcd4c4983a 266 // i wonder why we have to do this (check datasheet)
nkhorman 0:c3dcd4c4983a 267 if (SSD1306_LCDHEIGHT == 32)
nkhorman 0:c3dcd4c4983a 268 {
nkhorman 0:c3dcd4c4983a 269 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++)
nkhorman 0:c3dcd4c4983a 270 mspi.write(0);
nkhorman 0:c3dcd4c4983a 271 }
nkhorman 0:c3dcd4c4983a 272
nkhorman 0:c3dcd4c4983a 273 cs = 1;
Neal Horman 6:1be3e3b46eb7 274
Neal Horman 6:1be3e3b46eb7 275 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 276 char buff[17] ;
Neal Horman 6:1be3e3b46eb7 277 uint8_t x ;
Neal Horman 6:1be3e3b46eb7 278 // Setup D/C to switch to data mode
Neal Horman 6:1be3e3b46eb7 279 buff[0] = SSD_Data_Mode;
Neal Horman 6:1be3e3b46eb7 280
Neal Horman 6:1be3e3b46eb7 281 // loop trough all OLED buffer and
Neal Horman 6:1be3e3b46eb7 282 // send a bunch of 16 data byte in one xmission
Neal Horman 6:1be3e3b46eb7 283 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 )
Neal Horman 6:1be3e3b46eb7 284 {
Neal Horman 6:1be3e3b46eb7 285 for (x=1; x<=16; x++)
Neal Horman 6:1be3e3b46eb7 286 buff[x] = buffer[i+x];
Neal Horman 6:1be3e3b46eb7 287 mi2c.write(SSD_I2C_ADDRESS, buff, 17);
Neal Horman 6:1be3e3b46eb7 288 }
Neal Horman 6:1be3e3b46eb7 289 #endif
nkhorman 0:c3dcd4c4983a 290 }
nkhorman 0:c3dcd4c4983a 291
nkhorman 0:c3dcd4c4983a 292 // clear everything
nkhorman 0:c3dcd4c4983a 293 void Adafruit_SSD1306::clearDisplay(void)
nkhorman 0:c3dcd4c4983a 294 {
nkhorman 0:c3dcd4c4983a 295 memset(buffer,0,sizeof(buffer));
nkhorman 0:c3dcd4c4983a 296 }