Forked from Neal Horman: Adafruit_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. Now it is adopted also for the SH1106 I2C 128x64 display as well...

Dependents:   Lab06_oled_i2c Lab06_BME280_oled Lab06_oled_clock I2C_SSD1306andSH1106_nucleo_F446RE

Revision:
17:00a1379bd18a
Parent:
15:77feec1c0684
Child:
19:1b773847a04b
--- a/Adafruit_SSD1306.h	Tue Nov 11 22:08:20 2014 +0000
+++ b/Adafruit_SSD1306.h	Mon Jan 31 13:07:52 2022 +0000
@@ -4,14 +4,14 @@
   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  
+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 
+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.  
+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
 *********************************************************************/
@@ -33,10 +33,24 @@
 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(); };
+    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
@@ -50,35 +64,35 @@
 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);
-	};
+    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);
+    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);
+    /// 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();
-    
+    /// 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;
+    virtual void sendDisplayBuffer() = 0;
+    DigitalOut2 rst;
 
-	// the memory buffer for the LCD
-	std::vector<uint8_t> buffer;
+    // the memory buffer for the LCD
+    std::vector<uint8_t> buffer;
 };
 
 
@@ -88,68 +102,67 @@
 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)
-	    : Adafruit_SSD1306(RST, rawHieght, rawWidth)
-	    , cs(CS,true)
-	    , dc(DC,false)
-	    , mspi(spi)
-	    {
-		    begin();
-		    splash();
-		    display();
-	    };
+    /** 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)
+        : 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 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;
-	};
+    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;
+    virtual void sendDisplayBuffer()
+    {
+        cs = 1;
+        dc = 1;
+        cs = 0;
 
-		for(uint16_t i=0, q=buffer.size(); i<q; i++)
-			mspi.write(buffer[i]);
+        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);
-		}
+        if(height() == 32) {
+            for(uint16_t i=0, q=buffer.size(); i<q; i++)
+                mspi.write(0);
+        }
 
-		cs = 1;
-	};
+        cs = 1;
+    };
 
-	DigitalOut2 cs, dc;
-	SPI &mspi;
+    DigitalOut2 cs, dc;
+    SPI &mspi;
 };
 
 /** This is the I2C SSD1306 display driver transport class
@@ -158,63 +171,130 @@
 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)
-	    : Adafruit_SSD1306(RST, rawHeight, rawWidth)
-	    , mi2c(i2c)
-	    , mi2cAddress(i2cAddress)
-	    {
-		    begin();
-		    splash();
-		    display();
-	    };
+#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)
+        : 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 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));
-	};
+    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 ;
+    virtual void sendDisplayBuffer()
+    {
 
-			// 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));
-		}
-	};
+        char buff[256];
+        char cmd[4] = {0, 0xB0, 0x00, 0x10};
+        for (uint8_t m = 0; m < 8; m++) {
+            buff[0] = 0x40;
+            cmd[1] = 0xB0 + m;
+            for(int i=0; i<128; i++) {
+                buff[i+1]= buffer[m*128+i];
+            }
+            mi2c.write(mi2cAddress, cmd, 4);
+            mi2c.write(mi2cAddress, buff, 129);
+        }
+    };
 
-	I2C &mi2c;
-	uint8_t mi2cAddress;
+    I2C &mi2c;
+    uint8_t mi2cAddress;
 };
 
+/** This is the I2C SH1106 display driver transport class
+ *
+ */
+class Adafruit_SH1106_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_SH1106_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 64, 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[256];
+        char cmd[4] = {0, 0xB0, 0x02, 0x10};
+        for (uint8_t m = 0; m < 8; m++) {
+            buff[0] = 0x40;
+            cmd[1] = 0xB0 + m;
+            for(int i=0; i<128; i++) {
+                buff[i+1]= buffer[m*128+i];
+            }
+            mi2c.write(mi2cAddress, cmd, 4);
+            mi2c.write(mi2cAddress, buff, 129);
+        }
+    };
+
+    I2C &mi2c;
+    uint8_t mi2cAddress;
+};
+
+
+
 #endif
\ No newline at end of file