- code cleaned up in many points for a better readability - removed SPIPreinit and I2CPreinit classes - moved various method implementations from .h files to the corresponding .cpp ones - the splash() and clearDisplay() methods now directly update the display; no more need to call the display() method after them

Dependencies:   Adafruit_GFX mbed

Fork of Adafruit_GFX by Neal Horman

Revision:
17:396d9b7eb7d5
Parent:
15:77feec1c0684
--- a/Adafruit_SSD1306.h	Tue Nov 11 22:08:20 2014 +0000
+++ b/Adafruit_SSD1306.h	Thu Sep 03 20:27:40 2015 +0000
@@ -18,6 +18,12 @@
 
 /*
  *  Modified by Neal Horman 7/14/2012 for use in mbed
+ *  Edited by Francesco Adamo 2015/09/03: 
+ *	 - code cleaned up in many points for a better readability
+ *	 - removed SPIPreinit and I2CPreinit classes
+ *	 - moved various method implementations from .h files to the corresponding .cpp ones
+ *	 - the splash() and clearDisplay() methods now directly update the display; no more need to call the display() method after them
+ *	
  */
 
 #ifndef _ADAFRUIT_SSD1306_H_
@@ -29,6 +35,31 @@
 #include <vector>
 #include <algorithm>
 
+#define SSD1306_SETCONTRAST 0x81
+#define SSD1306_DISPLAYALLON_RESUME 0xA4
+#define SSD1306_DISPLAYALLON 0xA5
+#define SSD1306_NORMALDISPLAY 0xA6
+#define SSD1306_INVERTDISPLAY 0xA7
+#define SSD1306_DISPLAYOFF 0xAE
+#define SSD1306_DISPLAYON 0xAF
+#define SSD1306_SETDISPLAYOFFSET 0xD3
+#define SSD1306_SETCOMPINS 0xDA
+#define SSD1306_SETVCOMDETECT 0xDB
+#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
+#define SSD1306_SETPRECHARGE 0xD9
+#define SSD1306_SETMULTIPLEX 0xA8
+#define SSD1306_SETLOWCOLUMN 0x00
+#define SSD1306_SETHIGHCOLUMN 0x10
+#define SSD1306_SETSTARTLINE 0x40
+#define SSD1306_MEMORYMODE 0x20
+#define SSD1306_COMSCANINC 0xC0
+#define SSD1306_COMSCANDEC 0xC8
+#define SSD1306_SEGREMAP 0xA0
+#define SSD1306_CHARGEPUMP 0x8D
+
+#define SSD1306_EXTERNALVCC 0x1
+#define SSD1306_SWITCHCAPVCC 0x2
+
 // A DigitalOut sub-class that provides a constructed default state
 class DigitalOut2 : public DigitalOut
 {
@@ -39,8 +70,6 @@
 	operator int() { return read(); };
 };
 
-#define SSD1306_EXTERNALVCC 0x1
-#define SSD1306_SWITCHCAPVCC 0x2
 
 /** The pure base class for the SSD1306 display driver.
  *
@@ -49,113 +78,70 @@
  */
 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)
-	{
-		buffer.resize(rawHeight * rawWidth / 8);
-	};
-
-	void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
-	
-	// These must be implemented in the derived transport driver
-	virtual void command(uint8_t c) = 0;
-	virtual void data(uint8_t c) = 0;
-	virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
-
-	/// Clear the display buffer    
-	void clearDisplay(void);
-	virtual void invertDisplay(bool i);
-
-	/// Cause the display to be updated with the buffer content.
-	void display();
-	/// Fill the buffer with the AdaFruit splash screen.
-	virtual void splash();
-    
 protected:
 	virtual void sendDisplayBuffer() = 0;
 	DigitalOut2 rst;
 
 	// the memory buffer for the LCD
 	std::vector<uint8_t> buffer;
+
+public:
+	Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
+	void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
+	
+	// These must be implemented in the derived transport driver
+	virtual uint8_t command(uint8_t c) = 0;
+	virtual void data(uint8_t c) = 0;
+	virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
+
+	// Clear the display buffer    
+	void clearDisplay(void);
+	virtual void invertDisplay(bool i);
+
+	// Cause the display to be updated with the buffer content.
+	void display();
+
+	// Fill the buffer with the AdaFruit splash screen.
+	virtual void splash();
 };
 
 
 /** This is the SPI SSD1306 display driver transport class
  *
  */
-class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
+class Adafruit_SSD1306_SPI : public Adafruit_SSD1306
 {
 public:
-	/** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
+	/*
+	 * Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimensions
 	 *
 	 * Required parameters
-	 * @param spi - a reference to an initialized SPI object
+	 * @param MOSI (SPI MOSI pin name)
+	 * @param CLK (SPI clock pin name)
+	 * @param CS (Chip Select) pin name
 	 * @param DC (Data/Command) pin name
 	 * @param RST (Reset) pin name
-	 * @param CS (Chip Select) pin name
 	 *
 	 * Optional parameters
 	 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
-	 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
+	 * @param rawWidth - the horizontal number of pixels for the display, defaults to 128
 	 */
-	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;
-	};
-
+	Adafruit_SSD1306_SPI(PinName MOSI, PinName CLK, PinName CS, PinName DC, PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
+	virtual uint8_t command(uint8_t);
+	virtual void data(uint8_t);
+	virtual void sendDisplayBuffer(void);
+	
 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]);
-
-		if(height() == 32)
-		{
-			for(uint16_t i=0, q=buffer.size(); i<q; i++)
-				mspi.write(0);
-		}
-
-		cs = 1;
-	};
-
 	DigitalOut2 cs, dc;
-	SPI &mspi;
+	SPI mspi;
 };
 
-/** This is the I2C SSD1306 display driver transport class
+
+/*
+ * This is the I2C SSD1306 display driver transport class
  *
  */
-class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
+class Adafruit_SSD1306_I2C : public Adafruit_SSD1306
 {
 public:
 	#define SSD_I2C_ADDRESS     0x78
@@ -170,50 +156,13 @@
 	 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
 	 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
 	 */
-	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));
-	};
+	Adafruit_SSD1306_I2C(PinName SDA, PinName SCL, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128);
+	virtual uint8_t command(uint8_t c);
+	virtual void data(uint8_t c);
+	virtual void sendDisplayBuffer();
 
 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-1];
-			mi2c.write(mi2cAddress, buff, sizeof(buff));
-		}
-	};
-
-	I2C &mi2c;
+	I2C mi2c;
 	uint8_t mi2cAddress;
 };