Driver and Graphics library for MicroOLED 0.66'' (64 x 48 pixel) display with SSD1306 controller. Display can be obtained from Sparkfun or from diverse Chinese sellers (similar displays) via Aliexpress. Display is driven via SPI. The library can be very easily adapted to other OLED displays (up to 128 x 64 pixel) with SSD1306 controller by setting defines and changing controller init commands accordingly.

Dependents:   MicroOLED_Test

Small test-program that briefly shows the basic usage of the library:

#include "mbed.h"
#include "SFE_MicroOLED.h"
 
DigitalOut myled(LED1);
SPI my_spi(p5, p6, p7);
MicroOLED my_oled(my_spi, p11, p10, p9);
 
int main() {
    my_oled.init(0, 8000000);
    my_oled.clear(ALL);
    my_oled.puts("Hello all!");
    my_oled.circle(16, 31, 16);
    my_oled.line(0, 9, 63, 47);
    my_oled.rectFill(33, 32, 8, 8);
    my_oled.display();
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

SFE_MicroOLED.h

Committer:
synvox
Date:
2015-03-19
Revision:
0:b7fc78d2b795

File content as of revision 0:b7fc78d2b795:

/****************************************************************************** 
SFE_MicroOLED.h
Header file for the MicroOLED mbed Library

Jim Lindblom @ SparkFun Electronics
October 26, 2014
https://github.com/sparkfun/Micro_OLED_Breakout/tree/master/Firmware/Arduino/libraries/SFE_MicroOLED

Adapted for mbed by Nenad Milosevic
March, 2015

This file defines the hardware SPI interface for the Micro OLED Breakout.

Development environment specifics:
Various suitable mbed platforms
Micro OLED Breakout v1.0

This code was heavily based around the MicroView library, written by GeekAmmo
(https://github.com/geekammo/MicroView-Arduino-Library), and released under 
the terms of the GNU General Public License as published by the Free Software 
Foundation, either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#ifndef SFE_MICROOLED_H
#define SFE_MICROOLED_H

static inline void swap(uint8_t &a, uint8_t &b)
{
    uint8_t t = a;
    
    a = b;
    b = t;
}
 
#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif

#define BLACK 0
#define WHITE 1

#define LCDWIDTH			64
#define LCDHEIGHT			48
#define LCDCOLUMNOFFSET   32 // Visible start column within SSD1306 controller memory
#define FONTHEADERSIZE    6

#define LCDTOTALWIDTH   128  // Full width of SSD1306 controller memory
#define LCDTOTALHEIGHT   64  // Full height of SSD1306 controller memory

#define NORM				0
#define XOR					1

#define PAGE				0
#define ALL					1

#define SETCONTRAST 		0x81
#define DISPLAYALLONRESUME 	0xA4
#define DISPLAYALLON 		0xA5
#define NORMALDISPLAY 		0xA6
#define INVERTDISPLAY 		0xA7
#define DISPLAYOFF 			0xAE
#define DISPLAYON 			0xAF
#define SETDISPLAYOFFSET 	0xD3
#define SETCOMPINS 			0xDA
#define SETVCOMDESELECT		0xDB
#define SETDISPLAYCLOCKDIV 	0xD5
#define SETPRECHARGE 		0xD9
#define SETMULTIPLEX 		0xA8
#define SETLOWCOLUMN 		0x00
#define SETHIGHCOLUMN 		0x10
#define SETSTARTLINE 		0x40
#define MEMORYMODE 			0x20
#define SETCOLUMNBOUNDS		0x21
#define SETPAGEBOUNDS		0x22
#define COMSCANINC 			0xC0
#define COMSCANDEC 			0xC8
#define SEGREMAP 			0xA0
#define CHARGEPUMP 			0x8D
#define EXTERNALVCC 		0x01
#define SWITCHCAPVCC 		0x02

// Scroll
#define ACTIVATESCROLL 					0x2F
#define DEACTIVATESCROLL 				0x2E
#define SETVERTICALSCROLLAREA 			0xA3
#define RIGHTHORIZONTALSCROLL 			0x26
#define LEFTHORIZONTALSCROLL 			0x27
#define VERTICALRIGHTHORIZONTALSCROLL	0x29
#define VERTICALLEFTHORIZONTALSCROLL	0x2A

typedef bool boolean;

class MicroOLED {
public:
	// Constructor
	MicroOLED(SPI &spi, PinName rst, PinName dc, PinName cs) : miol_spi(spi), rstPin(rst), dcPin(dc), csPin(cs)
	{
		// Set default states for the DigitalOut pins.
		rstPin = 1;
		dcPin = 0;
		csPin = 1;
	};
	
	// Initialize SPI mode and frequency and SSD1306 for particular display
	void init(int spi_mode, int spi_freq);
	
	// Standard text output functions
	void putc(char c);
	void puts(const char *cstring);
	void printf(const char *format, ...);

	// RAW LCD functions
	void command(uint8_t c);
	void command(uint8_t c1, uint8_t c2);
	void command(uint8_t c1, uint8_t c2, uint8_t c3);
	void command(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4, uint8_t c5, uint8_t c6, uint8_t c7, uint8_t c8);
	
	// LCD Draw functions
	void clear(uint8_t mode);
	void clear(uint8_t mode, uint8_t c);
	void invert(boolean inv);
	void contrast(uint8_t contrast);
	void display(void);
	void setCursor(uint8_t x, uint8_t y);
	void pixel(uint8_t x, uint8_t y);
	void pixel(uint8_t x, uint8_t y, uint8_t color, uint8_t mode);
	void line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
	void line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color, uint8_t mode);
	void lineH(uint8_t x, uint8_t y, uint8_t width);
	void lineH(uint8_t x, uint8_t y, uint8_t width, uint8_t color, uint8_t mode);
	void lineV(uint8_t x, uint8_t y, uint8_t height);
	void lineV(uint8_t x, uint8_t y, uint8_t height, uint8_t color, uint8_t mode);
	void rect(uint8_t x, uint8_t y, uint8_t width, uint8_t height);
	void rect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color , uint8_t mode);
	void rectFill(uint8_t x, uint8_t y, uint8_t width, uint8_t height);
	void rectFill(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color , uint8_t mode);
	void circle(uint8_t x, uint8_t y, uint8_t radius);
	void circle(uint8_t x, uint8_t y, uint8_t radius, uint8_t color, uint8_t mode);
	void circleFill(uint8_t x0, uint8_t y0, uint8_t radius);
	void circleFill(uint8_t x0, uint8_t y0, uint8_t radius, uint8_t color, uint8_t mode);
	void drawChar(uint8_t x, uint8_t y, uint8_t c);
	void drawChar(uint8_t x, uint8_t y, uint8_t c, uint8_t color, uint8_t mode);
	void drawBitmap(const uint8_t * bitArray);
	uint8_t getLCDWidth(void);
	uint8_t getLCDHeight(void);
	void setColor(uint8_t color);
	void setDrawMode(uint8_t mode);
	uint8_t *getScreenBuffer(void);

	// Font functions
	uint8_t getFontWidth(void);
	uint8_t getFontHeight(void);
	uint8_t getTotalFonts(void);
	uint8_t getFontType(void);
	uint8_t setFontType(uint8_t type);
	uint8_t getFontStartChar(void);
	uint8_t getFontTotalChar(void);

	// LCD Rotate Scroll functions	
	void scrollRight(uint8_t start, uint8_t stop);
	void scrollLeft(uint8_t start, uint8_t stop);
	void scrollStop(void);
	void flipVertical(boolean flip);
	void flipHorizontal(boolean flip);
	
private:
	SPI &miol_spi;
	DigitalOut rstPin, dcPin, csPin;
	uint8_t foreColor, drawMode, fontWidth, fontHeight, fontType, fontStartChar, fontTotalChar, cursorX, cursorY;
	uint16_t fontMapWidth;
	static const unsigned char *fontsPointer[];
};
#endif