Fork of nkhorman's Adafruit mbed port with added I2C support

Fork of Adafruit_GFX by Neal Horman

Files at this revision

API Documentation at this revision

Comitter:
pommzorz
Date:
Thu Mar 13 10:47:03 2014 +0000
Parent:
3:969ca9fe5e2b
Child:
5:b8313591b561
Commit message:
Added I2C support with precompiler defines

Changed in this revision

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_SSD1306.cpp	Wed Jan 23 02:43:38 2013 +0000
+++ b/Adafruit_SSD1306.cpp	Thu Mar 13 10:47:03 2014 +0000
@@ -22,8 +22,8 @@
 
 #include "mbed.h"
 #include "Adafruit_SSD1306.h"
-
-uint8_t splashScreen[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = { 
+#ifdef WITH_SPLASH
+uint8_t splashScreen[512] = { //SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 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,
@@ -55,51 +55,38 @@
 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
-0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-#if (SSD1306_LCDHEIGHT == 64)
-0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
-0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
-0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
-0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
-0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
-0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
-0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
-0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
-0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
-0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
-0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
-0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
-0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
-0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
-0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
-0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
-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,
-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,
-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
+0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
+};
 #endif
-};
 
+#ifdef IS_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)
 {
-    memcpy(buffer,splashScreen,sizeof(buffer));
+    //memcpy(buffer,splashScreen,sizeof(buffer));
     begin();
     display();
 };
+#elif defined IS_I2C
+Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST)
+    : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
+    , rst(RST,false),mi2c(i2c),pc(USBTX, USBRX)
+{
+    
+    pc.baud(921600);
+    mi2c.frequency(100000);
+    pc.printf("\r\n\ninit.");
+    mi2c.start();
+    //memcpy(buffer,splashScreen,sizeof(buffer));
+    pc.printf("cp.");
+    begin();
+    pc.printf("bg.");
+    display();
+    pc.printf("dp\r\n");
+};
+#endif
+
 
 void Adafruit_SSD1306::begin(uint8_t vccstate)
 {
@@ -228,20 +215,40 @@
 
 void Adafruit_SSD1306::ssd1306_command(uint8_t c)
 {
+#ifdef IS_SPI
     cs = 1;
     dc = 0;
     cs = 0;
     mspi.write(c);
     cs = 1;
+    
+#elif defined IS_I2C
+    char buff[2] ;
+    buff[0] = SSD_Command_Mode ; 
+    buff[1] = c;
+    if(mi2c.write(I2C_ADDRESS,buff,sizeof(buff)))pc.printf("!");
+    
+#endif
+
 }
 
 void Adafruit_SSD1306::ssd1306_data(uint8_t c)
 {
+#ifdef IS_SPI
     cs = 1;
     dc = 1;
     cs = 0;
     mspi.write(c);
     cs = 1;
+#elif defined IS_I2C
+    char buff[2] ;
+    // Setup D/C to switch to data mode
+    buff[0] = SSD_Data_Mode; 
+    buff[1] = c;
+    // Write on i2c
+    if(mi2c.write(I2C_ADDRESS,buff,sizeof(buff)))pc.printf("?");
+#endif
+
 }
 
 void Adafruit_SSD1306::display(void)
@@ -249,7 +256,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 IS_SPI
     cs = 1;
     dc = 1;
     cs = 0;
@@ -265,6 +272,22 @@
     }
     
     cs = 1;
+    
+#elif defined  IS_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];
+        if(mi2c.write(I2C_ADDRESS, buff, 17))pc.printf("?");
+    }
+#endif
 }
 
 // clear everything
--- a/Adafruit_SSD1306.h	Wed Jan 23 02:43:38 2013 +0000
+++ b/Adafruit_SSD1306.h	Thu Mar 13 10:47:03 2014 +0000
@@ -43,6 +43,9 @@
     -----------------------------------------------------------------------*/
 //#define SSD1306_128_64
 #define SSD1306_128_32
+
+//#define IS_SPI
+#define IS_I2C
 /*=========================================================================*/
 
 #if defined SSD1306_128_64 && defined SSD1306_128_32
@@ -61,6 +64,19 @@
     #define SSD1306_LCDHEIGHT 32
 #endif
 
+#if defined IS_SPI && defined IS_I2C
+    #error "Only one communication mode can be specified at once in SSD1306.h"
+#endif
+#if !defined IS_SPI && !defined IS_I2C
+    #error "At least one communication mode must be specified in SSD1306.h"
+#endif
+
+//#define WITH_SPLASH
+#define I2C_ADDRESS     0x78
+
+#define SSD_Command_Mode    0x00
+#define SSD_Data_Mode       0x40
+
 #define SSD1306_SETCONTRAST 0x81
 #define SSD1306_DISPLAYALLON_RESUME 0xA4
 #define SSD1306_DISPLAYALLON 0xA5
@@ -98,7 +114,11 @@
 class Adafruit_SSD1306 : public Adafruit_GFX
 {
  public:
+#ifdef IS_SPI
     Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS);
+#elif defined IS_I2C
+    Adafruit_SSD1306(I2C &i2c, PinName RST);
+#endif
     void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
     void ssd1306_command(uint8_t c);
     void ssd1306_data(uint8_t c);
@@ -110,8 +130,15 @@
     virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
     
 private:
-    DigitalOut2 rst,cs,dc;
+    
+    Serial pc;
+    DigitalOut2 rst;
+#ifdef IS_SPI
     SPI &mspi;
+    DigitalOut2 cs,dc;
+#elif defined IS_I2C
+    I2C &mi2c;
+#endif
     // the memory buffer for the LCD
     uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8];
 };