Demonstration of SSD1308 OLED driver library

Dependencies:   mbed SSD1308_128x64_I2C

Revision:
3:1337e3d65ed0
Parent:
2:d86478c0f5da
--- a/SSD1308.h	Mon Jul 09 20:46:27 2012 +0000
+++ b/SSD1308.h	Wed Jul 18 13:59:03 2012 +0000
@@ -3,8 +3,11 @@
 //   The SSD1308 is used for example in the Seeed 128x64 OLED Display
 //   http://www.seeedstudio.com/depot/grove-oled-display-12864-p-781.html?cPath=163_167
 //
-// The original code is using (and has been submitted as a part of) Jeff Rowberg's I2Cdevlib library,
+// The original code by Andrew Schamp is using (and has been submitted as a part of) Jeff Rowberg's I2Cdevlib library,
 // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
+// Some parts also mashed up from Graphic Library for driving monochrome displays based on the PCD8544,
+// Copyright (c) 2011, Wim De Roeve, who in turn did partial port of code found on
+// http://serdisplib.sourceforge.net/ser/pcd8544.html#links and by Petras Saduikis <petras@petras.co.uk>
 //
 // Changelog:
 //   2011-08-25 - Initial release by Andrew Schamp <schamp@gmail.com>
@@ -45,36 +48,57 @@
 #define SSD1308_SA1                0x7A
 #define SSD1308_DEF_SA             SSD1308_SA0
 
+// Display dimensions
 #define ROWS                       64
 #define COLUMNS                    128
 #define PAGES                      (ROWS / 8)
 #define MAX_PAGE                   (PAGES - 1)
 #define MAX_COL                    (COLUMNS - 1)
 
-#define FONT_WIDTH                 8
-#define CHARS                      (COLUMNS / FONT_WIDTH)
+// Character dimensions 8x8 font
+#define CHARS                      (COLUMNS / FONT8x8_WIDTH)
 
+// Command and Datamode 
+#define COMMAND_MODE               0x80 // continuation bit is set!
+#define DATA_MODE                  0x40
+
+// Commands and Parameter defines
 #define HORIZONTAL_ADDRESSING_MODE 0x00
 #define VERTICAL_ADDRESSING_MODE   0x01
 #define PAGE_ADDRESSING_MODE       0x02
-
-#define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte
+#define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte as given above
 
 #define SET_COLUMN_ADDRESS         0x21 // takes two bytes, start address and end address of display data RAM
 #define SET_PAGE_ADDRESS           0x22 // takes two bytes, start address and end address of display data RAM
 
+// Command maybe unsupported by SSD1308
+#define FADE_INTERVAL_8_FRAMES     0x00
+#define FADE_INTERVAL_16_FRAMES    0x01
+#define FADE_INTERVAL_24_FRAMES    0x02
+#define FADE_INTERVAL_32_FRAMES    0x03
+#define FADE_INTERVAL_64_FRAMES    0x07
+#define FADE_INTERVAL_128_FRAMES   0x0F
+#define FADE_BLINK_DISABLE         0x00
+#define FADE_OUT_ENABLE            0x20
+#define BLINK_ENABLE               0x30
+#define SET_FADE_BLINK             0x23 // takes one byte
+                                        //  bit5-4 = 0, fade/blink mode
+                                        //  bit3-0 = Time interval in frames 
+
+#define SET_DISPLAY_START_LINE     0x40 // | with a row number 0-63 to set start row. Reset = 0
+
 #define SET_CONTRAST               0x81 // takes one byte, 0x00 - 0xFF
 
 #define SET_SEGMENT_REMAP_0        0xA0 // column address 0 is mapped to SEG0 (Reset)
 #define SET_SEGMENT_REMAP_127      0xA1 // column address 127 is mapped to SEG0
 
+#define SET_DISPLAY_GDDRAM         0xA4 // restores display to contents of RAM
 #define SET_ENTIRE_DISPLAY_ON      0xA5 // turns all pixels on, does not affect RAM
-#define SET_DISPLAY_GDDRAM         0xA4 // restores display to contents of RAM
 
-#define SET_NORMAL_DISPLAY         0xA6 // a data of 1 indicates 'ON'
-#define SET_INVERSE_DISPLAY        0xA7 // a data of 0 indicates 'ON'
+#define SET_NORMAL_DISPLAY         0xA6 // a databit of 1 indicates pixel 'ON'
+#define SET_INVERSE_DISPLAY        0xA7 // a databit of 1 indicates pixel 'OFF'
 
-#define SET_MULTIPLEX_RATIO        0xA8 // takes one byte, from 16 to 63 (0x
+#define SET_MULTIPLEX_RATIO        0xA8 // takes one byte, from 16xMUX to 64xMUX (MUX Ratio = byte+1; Default 64)
 
 #define EXTERNAL_IREF              0x10
 #define INTERNAL_IREF              0x00
@@ -83,9 +107,6 @@
 #define SET_DISPLAY_POWER_OFF      0xAE
 #define SET_DISPLAY_POWER_ON       0xAF
 
-#define COMMAND_MODE               0x80 // continuation bit is set!
-#define DATA_MODE                  0x40
-
 #define PAGE0                      0x00
 #define PAGE1                      0x01
 #define PAGE2                      0x02
@@ -99,54 +120,97 @@
 #define SET_COMMON_REMAP_0         0xC0 // row address  0 is mapped to COM0 (Reset)
 #define SET_COMMON_REMAP_63        0xC8 // row address 63 is mapped to COM0
 
+#define SET_DISPLAY_OFFSET         0xD3 // takes one byte from 0-63 for vertical shift, Reset = 0
 
-#define SET_DISPLAY_OFFSET         0xD3
+#define SET_DISPLAY_CLOCK          0xD5 // takes one byte
+                                        //  bit7-4 = Osc Freq DCLK (Reset = 1000b) 
+                                        //  bit3-0 = Divide ration (Reset = oooob, Ratio = 1)   
 
-#define SET_DISPLAY_CLOCK          0xD5
+#define SET_PRECHARGE_TIME         0xD9 // takes one byte
+                                        //  bit7-4 = Phase2, upto 15 DCLKs (Reset = 0010b) 
+                                        //  bit3-0 = Phase1, upto 15 DCLKs (Reset = 0010b)   
 
+                                       
 #define COMMON_BASE                0x02 // 
 #define COMMON_SEQUENTIAL          0x00 // Sequential common pins config
 #define COMMON_ALTERNATIVE         0x10 // Odd/Even common pins config (Reset)
 #define COMMON_LEFTRIGHT_NORMAL    0x00 // LeftRight Normal (Reset)
 #define COMMON_LEFTRIGHT_FLIP      0x20 // LeftRight Flip 
-#define SET_COMMON_CONF            0xDA
+#define SET_COMMON_CONF            0xDA // takes one byte as given above
+
 
 #define VCOMH_DESELECT_0_65_CODE   0x00
 #define VCOMH_DESELECT_0_77_CODE   0x20
 #define VCOMH_DESELECT_0_83_CODE   0x30
-#define SET_VCOMH_DESELECT_LEVEL   0xDB
+#define SET_VCOMH_DESELECT_LEVEL   0xDB // takes one byte as given above
 
 #define NOP                        0xE3
 
-#define SET_RIGHT_HORIZONTAL_SCROLL 0x26
-#define SET_LEFT_HORIZONTAL_SCROLL  0x27
-#define SET_VERTICAL_RIGHT_HORIZONTAL_SCROLL 0x29
-#define SET_VERTICAL_LEFT_HORIZONTAL_SCROLL  0x2A
+#define SCROLL_INTERVAL_5_FRAMES   0x00
+#define SCROLL_INTERVAL_64_FRAMES  0x01
+#define SCROLL_INTERVAL_128_FRAMES 0x02
+#define SCROLL_INTERVAL_256_FRAMES 0x03
+#define SCROLL_INTERVAL_3_FRAMES   0x04
+#define SCROLL_INTERVAL_4_FRAMES   0x05
+#define SCROLL_INTERVAL_25_FRAMES  0x06
+#define SCROLL_INTERVAL_2_FRAMES   0x07
+
+#define SET_RIGHT_HOR_SCROLL       0x26 // takes 6 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, 0x00, 0xFF
+#define SET_LEFT_HOR_SCROLL        0x27 // takes 6 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, 0x00, 0xFF
+
+#define SET_VERT_RIGHT_HOR_SCROLL  0x29 // takes 5 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, VertOffset
+#define SET_VERT_LEFT_HOR_SCROLL   0x2A // takes 5 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, VertOffset
 
 #define SET_DEACTIVATE_SCROLL      0x2E
 #define SET_ACTIVATE_SCROLL        0x2F
 
-#define SET_VERTICAL_SCROLL_AREA   0xA3
+#define SET_VERTICAL_SCROLL_AREA   0xA3 // takes 2 bytes: Rows in Top Area (Reset=0), Rows in Scroll Area (Reset=64)
 
-class SSD1308 {
+class SSD1308 : public Stream {
   public:
     
-    // constructor
-    // takes a 8bit I2C address to use (0x78 by default, assumes D/C# (pin 13) grounded)
+// Constructor
+    // takes 8bit I2C address to use for the controller (0x78 by default, assumes D/C# (pin 13) grounded)
     SSD1308(I2C &i2c, uint8_t address = SSD1308_DEF_SA);
 
+// High Level methods
     void initialize();
+
     void clearDisplay();
-    void fillDisplay(uint8_t pattern); // pattern     
+//    void fillDisplay(uint8_t pattern); // pattern     
+    void fillDisplay(uint8_t pattern = 0x00,
+                     uint8_t start_page=0, uint8_t end_page=MAX_PAGE,
+                     uint8_t start_col=0, uint8_t end_col=MAX_COL);
+    
+    void writeBitmap(uint8_t* data,
+                     uint8_t start_page=0, uint8_t end_page=MAX_PAGE,
+                     uint8_t start_col=0, uint8_t end_col=MAX_COL);
     
-    void writeBitmap(int len, uint8_t* data);
+    //void setXY(uint8_t, uint8_t y);
+    
+    // Select inverted or normal text    
+    void setInverted(bool inverted) { _inverted = inverted; };    
+        
+    // write char at current cursor location
+    void writeChar(char chr);  
+    
+    // write large char at cursor location    
+    void writeBigChar(uint8_t row, uint8_t col, char chr);      
     
     // x, y is position (x is row (i.e., page), y is character (0-15), starting at top-left)
     // text will wrap around until it is done.
-    void writeString(uint8_t row, uint8_t col, uint16_t len, const char* txt, bool inverted = false);
+    void writeString(uint8_t row, uint8_t col, uint16_t len, const char* txt);
+
+    // Stream implementation - provides printf() interface
+    // You would otherwise be forced to use writeChar() or writeString()
+    virtual int _putc(int value) { writeChar(value); return 1; };
+    virtual int _getc() { return -1; };
+   
+// Future extension with graphics features
+    // this must be defined by the subclass
+//    virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
     
-    //void setXY(uint8_t, uint8_t y);
-
+// Medium Level methods
     void setHorizontalAddressingMode();
     void setVerticalAddressingMode();
     void setPageAddressingMode();
@@ -190,73 +254,64 @@
     void setDisplayNormal();
     void setDisplayInverse();
     
-
+    void setDisplayBlink(bool on);       
+    void setDisplayFade(bool on);    
+    
     // Display Flip (Up/Down, Left/Right)
     // 
     void setDisplayFlip(bool left, bool down);
 
-
     // Set vertical shift by COM from 0 - 63 (0x00 - 0x3F)
     // set to 0x00 after RESET
     void setDisplayOffset(uint8_t offset);
     
-    // divide ratio 0x00-0x0F, value +1 (reset 0x00)
-    // oscillator freq 0x00-0x0F (reset 0x08)
+    // Divide ratio 0x00-0x0F, value +1 (reset 0x00)
+    // Oscillator freq 0x00-0x0F (reset 0x08)
     void setDisplayClock(uint8_t divideRatio, uint8_t oscFreq);
     
-    // phase1 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
-    // phase2 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
+    // Phase1 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
+    // Phase2 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
     void setPrechargePeriod(uint8_t phase1, uint8_t phase2);
     
-    #define VCOM_DESELECT_0_65 0x00
-    #define VCOM_DESELECT_0_77 0x02
-    #define VCOM_DESELECT_0_83 0x03
     void setVcomhDeselectLevel(uint8_t level);
     
-    // command for no-operation
+    // Command for no-operation
     void nop();
     
-    #define SCROLL_INTERVAL_5_FRAMES   0x00
-    #define SCROLL_INTERVAL_64_FRAMES  0x01
-    #define SCROLL_INTERVAL_128_FRAMES 0x02
-    #define SCROLL_INTERVAL_256_FRAMES 0x03
-    #define SCROLL_INTERVAL_3_FRAMES   0x04
-    #define SCROLL_INTERVAL_4_FRAMES   0x05
-    #define SCROLL_INTERVAL_25_FRAMES  0x06
-    #define SCROLL_INTERVAL_2_FRAMES   0x07
-    // end_page must not be less than start_page
+    // End_page must not be less than start_page
     void setContinuousHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page);
-    // horizontal scroll by one column per interval
-    // offset = 1 (0x01) to 63 (0x3F)
+    // Horizontal scroll by one column per interval
+    // Offset = 1 (0x01) to 63 (0x3F)
     void setContinuousVerticalAndHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page, uint8_t offset);
     
-    // note, after deactivating scrolling, the RAM data needs to be rewritten
+    // Note, after deactivating scrolling, the RAM data needs to be rewritten
     void deactivateScroll();
     void activateScroll();
     
-    void setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows);
-
-    void sendData(uint8_t data);
-    void sendData(uint8_t len, uint8_t* data);
-    
+    void setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows); 
   
   private:
-    // sends a command and optional params to device
-    void sendCommand(uint8_t command);  
-    void sendCommand(uint8_t command, uint8_t param1);
-    void sendCommand(uint8_t command, uint8_t param1, uint8_t param2);    
-            
-    void sendCommands(uint8_t len, uint8_t* buf);
-    void writeChar(char chr, bool inverted);
+
+// Low Level methods
+    // Sends a command and optional params to device
+    void _sendCommand(uint8_t command);  
+    void _sendCommand(uint8_t command, uint8_t param1);
+    void _sendCommand(uint8_t command, uint8_t param1, uint8_t param2);              
+//    void sendCommands(uint8_t len, uint8_t* buf);
+    
+    // Sends data to device
+    void _sendData(uint8_t data);
+    void _sendData(uint8_t len, uint8_t* data);
 
     // Init the configuration registers in accordance with the datasheet
     void _init();
-    
-    I2C _i2c;          // I2C bus
+
+    I2C _i2c;          // I2C bus reference
 //    uint8_t m_devAddr; // contains the I2C address of the device
     uint8_t _readOpcode;
     uint8_t _writeOpcode; 
-   
+    
+    bool _inverted;        // inverted or normal text   
 };
 
 #endif