i2c version has an offset due to wrong copy of temp buffer to display buffer, fixed in Adafruit_SSD1306.h

Dependents:   ezSBC_MPU9250 Test_OLED_Display untodoenuno OledI2CDisplay ... more

Fork of Adafruit_GFX by Neal Horman

Files at this revision

API Documentation at this revision

Comitter:
JojoS
Date:
Mon Jul 24 21:30:02 2017 +0200
Parent:
19:73007a24ddaa
Child:
22:818c271c30fa
Commit message:
made deriving from Stream optional

Changed in this revision

Adafruit_GFX.h Show annotated file Show diff for this revision Revisions of this file
Adafruit_GFX_Config.h Show annotated file Show diff for this revision Revisions of this file
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_GFX.h	Sun Nov 06 15:31:50 2016 +0000
+++ b/Adafruit_GFX.h	Mon Jul 24 21:30:02 2017 +0200
@@ -46,7 +46,11 @@
  * hardware based on application control.
  *
  */
+#if defined(USE_IOSTREAM)
 class Adafruit_GFX : public Stream
+#else
+class Adafruit_GFX
+#endif
 {
  public:
     Adafruit_GFX(int16_t w, int16_t h)
--- a/Adafruit_GFX_Config.h	Sun Nov 06 15:31:50 2016 +0000
+++ b/Adafruit_GFX_Config.h	Mon Jul 24 21:30:02 2017 +0200
@@ -2,13 +2,18 @@
 #define _ADAFRUIT_GFX_CONFIG_H_
 
 // Uncomment this to turn off the builtin splash
-//#define NO_SPLASH_ADAFRUIT
+#define NO_SPLASH_ADAFRUIT
 
 // Uncomment this to enable all functionality
+// saves about 600 Bytes
 #define GFX_WANT_ABSTRACTS
 
+// USE_IOSTREAM : GFX is derived from class stream
+// adds printf, but costs about 10 kB
+//#define USE_IOSTREAM
+
 // Uncomment this to enable only runtime font scaling, without all the rest of the Abstracts
 //#define GFX_SIZEABLE_TEXT
 
 
-#endif
\ No newline at end of file
+#endif
--- a/Adafruit_SSD1306.cpp	Sun Nov 06 15:31:50 2016 +0000
+++ b/Adafruit_SSD1306.cpp	Mon Jul 24 21:30:02 2017 +0200
@@ -50,18 +50,26 @@
 #define SSD1306_CHARGEPUMP 0x8D
 #define SSD1306_SETPAGESTARTADDRESS 0xB0
 
+Adafruit_SSD1306::Adafruit_SSD1306(PinName reset, uint8_t rawHeight, uint8_t rawWidth, bool flipVertical)
+	: Adafruit_GFX(rawWidth,rawHeight)
+	, _reset(reset,false)
+	, _flipVertical(flipVertical)
+{
+	buffer.resize(rawHeight * rawWidth / 8);
+}
+
 void Adafruit_SSD1306::begin(uint8_t vccstate)
 {
-    if (rst.is_connected()) {		// reset input is not present on every SSD1306 display board, so usage is optional
-        rst = 1;
+    if (_reset.is_connected()) {		// reset input is not present on every SSD1306 display board, so usage is optional
+        _reset = 1;
         // VDD (3.3V) goes high at start, lets just chill for a ms
         wait_ms(1);
         // bring reset low
-        rst = 0;
+        _reset = 0;
         // wait 10ms
         wait_ms(10);
         // bring out of reset
-        rst = 1;
+        _reset = 1;
         // turn on VCC (9V?)
     }
 
@@ -85,7 +93,7 @@
     command(0x00);                                  // 0x0 act like ks0108
 
 
-    if (flipVertical) {
+    if (_flipVertical) {
         command(SSD1306_SEGREMAP | 0x0);
         command(SSD1306_COMSCANINC);
     } else {
@@ -152,6 +160,19 @@
     command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
 }
 
+void Adafruit_SSD1306::flipVertical(bool flip)
+{
+	_flipVertical = flip;
+
+	if (_flipVertical) {
+        command(SSD1306_SEGREMAP | 0x0);
+        command(SSD1306_COMSCANINC);
+    } else {
+        command(SSD1306_SEGREMAP | 0x1);
+        command(SSD1306_COMSCANDEC);					// flip vertically
+    }
+}
+
 // Send the display buffer out to the display
 void Adafruit_SSD1306::display(void)
 {
@@ -170,7 +191,7 @@
 void Adafruit_SSD1306::splash(void)
 {
 #ifndef NO_SPLASH_ADAFRUIT
-    uint8_t adaFruitLogo[64 * 128 / 8] = {
+    const uint8_t adaFruitLogo[64 * 128 / 8] = {
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -245,3 +266,104 @@
     );
 #endif
 }
+
+/*
+ *
+ *   SPI specific implementation
+ *
+ */
+
+Adafruit_SSD1306_Spi::Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght, uint8_t rawWidth, bool flipVertical)
+: Adafruit_SSD1306(RST, rawHieght, rawWidth, flipVertical)
+, cs(CS,true)
+, dc(DC,false)
+, mspi(spi)
+{
+	begin();
+	splash();
+	display();
+}
+
+void Adafruit_SSD1306_Spi::command(uint8_t c)
+{
+    cs = 1;
+    dc = 0;
+    cs = 0;
+    mspi.write(c);
+    cs = 1;
+}
+
+void Adafruit_SSD1306_Spi::data(uint8_t c)
+{
+    cs = 1;
+    dc = 1;
+    cs = 0;
+    mspi.write(c);
+    cs = 1;
+};
+
+void Adafruit_SSD1306_Spi::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;
+}
+
+/*
+ *
+ *   I2C specific implementation
+ *
+ */
+
+Adafruit_SSD1306_I2c::Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress, uint8_t rawHeight, uint8_t rawWidth, bool flipVertical)
+	    : Adafruit_SSD1306(RST, rawHeight, rawWidth, flipVertical)
+	    , mi2c(i2c)
+	    , mi2cAddress(i2cAddress)
+{
+	begin();
+	splash();
+	display();
+}
+
+void Adafruit_SSD1306_I2c::command(uint8_t c)
+{
+	char buff[2];
+	buff[0] = 0; // Command Mode
+	buff[1] = c;
+	mi2c.write(mi2cAddress, buff, sizeof(buff));
+}
+
+void Adafruit_SSD1306_I2c::data(uint8_t c)
+{
+	char buff[2];
+	buff[0] = 0x40; // Data Mode
+	buff[1] = c;
+	mi2c.write(mi2cAddress, buff, sizeof(buff));
+}
+
+void Adafruit_SSD1306_I2c::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));
+	}
+}
--- a/Adafruit_SSD1306.h	Sun Nov 06 15:31:50 2016 +0000
+++ b/Adafruit_SSD1306.h	Mon Jul 24 21:30:02 2017 +0200
@@ -1,212 +1,133 @@
-/*********************************************************************
-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>
-
-#define SSD1306_EXTERNALVCC 0x1
-#define SSD1306_SWITCHCAPVCC 0x2
-
-/** The pure base class for the SSD1306 display driver.
- *
- * You should derive from this for a new transport interface type,
- * such as the SPI and I2C drivers.
- */
-class Adafruit_SSD1306 : public Adafruit_GFX
-{
-public:
-	Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128, bool _flipVertical=false)
-		: Adafruit_GFX(rawWidth,rawHeight)
-		, rst(RST,false)
-		, flipVertical(_flipVertical)
-	{
-		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;
-	DigitalOut rst;
-	bool flipVertical;
-
-	// the memory buffer for the LCD
-	std::vector<uint8_t> buffer;
-};
-
-
-/** This is the SPI SSD1306 display driver transport class
- *
- */
-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
-	 *
-	 * Required parameters
-	 * @param spi - a reference to an initialized SPI object
-	 * @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
-	 */
-	Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128, bool flipVertical = false)
-	    : Adafruit_SSD1306(RST, rawHieght, rawWidth, flipVertical)
-	    , 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]);
-
-		if(height() == 32)
-		{
-			for(uint16_t i=0, q=buffer.size(); i<q; i++)
-				mspi.write(0);
-		}
-
-		cs = 1;
-	};
-
-	DigitalOut cs, dc;
-	SPI &mspi;
-};
-
-/** This is the I2C SSD1306 display driver transport class
- *
- */
-class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
-{
-public:
-	#define SSD_I2C_ADDRESS     0x78
-	/** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
-	 *
-	 * Required parameters
-	 * @param i2c - A reference to an initialized I2C object
-	 * @param RST - The Reset pin name
-	 *
-	 * Optional parameters
-	 * @param i2cAddress - The i2c address of the display
-	 * @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, bool flipVertical = false)
-	    : Adafruit_SSD1306(RST, rawHeight, rawWidth, flipVertical)
-	    , 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-1];
-			mi2c.write(mi2cAddress, buff, sizeof(buff));
-		}
-	};
-
-	I2C &mi2c;
-	uint8_t mi2cAddress;
-};
-
-#endif
\ No newline at end of file
+/*********************************************************************
+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>
+
+#define SSD1306_EXTERNALVCC 0x1
+#define SSD1306_SWITCHCAPVCC 0x2
+
+/** The pure base class for the SSD1306 display driver.
+ *
+ * You should derive from this for a new transport interface type,
+ * such as the SPI and I2C drivers.
+ */
+class Adafruit_SSD1306 : public Adafruit_GFX
+{
+public:
+	Adafruit_SSD1306(PinName reset, uint8_t rawHeight = 32, uint8_t rawWidth = 128, bool flipVertical=false);
+
+	// start display sequence
+	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);
+	void flipVertical(bool flip);
+
+	/// 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;
+	DigitalOut _reset;
+	bool _flipVertical;
+
+	// the memory buffer for the LCD
+	std::vector<uint8_t> buffer;
+};
+
+
+/** This is the SPI SSD1306 display driver transport class
+ *
+ */
+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
+	 *
+	 * Required parameters
+	 * @param spi - a reference to an initialized SPI object
+	 * @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
+	 */
+	Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128, bool flipVertical = false);
+
+	virtual void command(uint8_t c);
+	virtual void data(uint8_t c);
+
+protected:
+	virtual void sendDisplayBuffer();
+
+	DigitalOut cs, dc;
+	SPI &mspi;
+};
+
+/** This is the I2C SSD1306 display driver transport class
+ *
+ */
+class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
+{
+public:
+	#define SSD_I2C_ADDRESS     0x78
+	/** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
+	 *
+	 * Required parameters
+	 * @param i2c - A reference to an initialized I2C object
+	 * @param RST - The Reset pin name
+	 *
+	 * Optional parameters
+	 * @param i2cAddress - The i2c address of the display
+	 * @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, bool flipVertical = false);
+
+	virtual void command(uint8_t c);
+	virtual void data(uint8_t c);
+
+protected:
+	virtual void sendDisplayBuffer();
+
+	I2C &mi2c;
+	uint8_t mi2cAddress;
+};
+
+#endif