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:
6:1be3e3b46eb7
Parent:
5:315de3647c9f
Child:
9:ddb97c9850a2
diff -r 315de3647c9f -r 1be3e3b46eb7 Adafruit_SSD1306.cpp
--- a/Adafruit_SSD1306.cpp	Thu Oct 16 23:18:25 2014 -0500
+++ b/Adafruit_SSD1306.cpp	Sat Oct 18 11:47:17 2014 -0500
@@ -94,6 +94,7 @@
 };
 #endif
 
+#ifdef SSD_USES_SPI
 Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS)
     : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
     , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi)
@@ -104,6 +105,22 @@
     begin();
     display();
 };
+#elif defined SSD_USES_I2C
+Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST)
+    : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
+    , rst(RST,false),mi2c(i2c)
+{
+    
+    mi2c.frequency(400000);
+    mi2c.start();
+#ifndef WITHOUT_SPLASH
+    memcpy(buffer,splashScreen,sizeof(buffer));
+#endif
+    begin();
+    display();
+};
+#endif
+
 
 void Adafruit_SSD1306::begin(uint8_t vccstate)
 {
@@ -198,20 +215,39 @@
 
 void Adafruit_SSD1306::ssd1306_command(uint8_t c)
 {
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 0;
     cs = 0;
     mspi.write(c);
     cs = 1;
+    
+#elif defined SSD_USES_I2C
+    char buff[2] ;
+    buff[0] = SSD_Command_Mode ; 
+    buff[1] = c;
+    mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
+#endif
+
 }
 
 void Adafruit_SSD1306::ssd1306_data(uint8_t c)
 {
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 1;
     cs = 0;
     mspi.write(c);
     cs = 1;
+#elif defined SSD_USES_I2C
+    char buff[2] ;
+    // Setup D/C to switch to data mode
+    buff[0] = SSD_Data_Mode; 
+    buff[1] = c;
+    // Write on i2c
+    mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
+#endif
+
 }
 
 void Adafruit_SSD1306::display(void)
@@ -219,7 +255,7 @@
     ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
     ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
     ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
-
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 1;
     cs = 0;
@@ -235,6 +271,22 @@
     }
     
     cs = 1;
+    
+#elif defined  SSD_USES_I2C
+    char buff[17] ;
+    uint8_t x ;
+    // Setup D/C to switch to data mode
+    buff[0] = SSD_Data_Mode; 
+
+    // loop trough all OLED buffer and 
+    // send a bunch of 16 data byte in one xmission
+    for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 ) 
+    {
+        for (x=1; x<=16; x++) 
+            buff[x] = buffer[i+x];
+        mi2c.write(SSD_I2C_ADDRESS, buff, 17);
+    }
+#endif
 }
 
 // clear everything