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

Revision:
20:04822be0efcd
Parent:
18:6aa925f254d6
--- a/Adafruit_SSD1306.cpp	Sun Nov 06 15:31:50 2016 +0000
+++ b/Adafruit_SSD1306.cpp	Mon Jul 24 11:09:33 2017 +0000
@@ -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));
+	}
+}