I2Cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs

SSD1308/SSD1308.h
00001 // I2Cdev library collection - SSD1308 I2C device class header file
00002 // Based on Solomon Systech SSD1308 datasheet, rev. 1, 10/2008
00003 // 8/25/2011 by Andrew Schamp <schamp@gmail.com>
00004 //
00005 // This I2C device library is using (and submitted as a part of) Jeff Rowberg's I2Cdevlib library,
00006 // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
00007 //
00008 // Changelog:
00009 //     2011-08-25 - initial release
00010         
00011 /* ============================================
00012 I2Cdev device library code is placed under the MIT license
00013 Copyright (c) 2011 Andrew Schamp
00014 
00015 Permission is hereby granted, free of charge, to any person obtaining a copy
00016 of this software and associated documentation files (the "Software"), to deal
00017 in the Software without restriction, including without limitation the rights
00018 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00019 copies of the Software, and to permit persons to whom the Software is
00020 furnished to do so, subject to the following conditions:
00021 
00022 The above copyright notice and this permission notice shall be included in
00023 all copies or substantial portions of the Software.
00024 
00025 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00026 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00027 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00028 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00029 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00030 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 THE SOFTWARE.
00032 ===============================================
00033 */
00034 
00035 #ifndef _SSD1308_h_
00036 #define _SSD1308_h_
00037 
00038 #include <inttypes.h>
00039 
00040 // this is the 7-bit I2C address
00041 // which wone is used is determined by the D/C# pin.
00042 // with D/C# (pin 13) grounded, address is 0x3C
00043 // tied high, 0x3D
00044 // assume grounded by default.
00045 #define SSD1308_ADDRESS_1 0x3C
00046 #define SSD1308_ADDRESS_2 0x3D
00047 #define SSD1308_DEFAULT_ADDRESS SSD1308_ADDRESS_1
00048 
00049 #define ROWS 64
00050 #define COLUMNS 128
00051 #define PAGES 8
00052 #define PAGE_WIDTH (ROWS / 8)
00053 #define FONT_WIDTH 8
00054 #define CHARS (COLUMNS / FONT_WIDTH)
00055 #define MAX_PAGE (PAGES - 1)
00056 #define MAX_COL (COLUMNS - 1)
00057 
00058 #define HORIZONTAL_ADDRESSING_MODE 0x00
00059 #define VERTICAL_ADDRESSING_MODE   0x01
00060 #define PAGE_ADDRESSING_MODE       0x02
00061 
00062 #define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte
00063 
00064 #define SET_COLUMN_ADDRESS 0x21 // takes two bytes, start address and end address of display data RAM
00065 #define SET_PAGE_ADDRESS   0x22 // takes two bytes, start address and end address of display data RAM
00066 
00067 #define SET_CONTRAST 0x81 // takes one byte, 0x00 - 0xFF
00068 
00069 #define SET_SEGMENT_REMAP_0   0xA0 // column address 0 is mapped to SEG0
00070 #define SET_SEGMENT_REMAP_127 0xA1 // column address 127 is mapped to SEG0
00071 
00072 #define SET_ENTIRE_DISPLAY_ON 0xA5 // turns all pixels on, does not affect RAM
00073 #define SET_DISPLAY_GDDRAM    0xA4 // restores display to contents of RAM
00074 
00075 #define SET_NORMAL_DISPLAY  0xA6 // a data of 1 indicates 'ON'
00076 #define SET_INVERSE_DISPLAY 0xA7 // a data of 0 indicates 'ON'
00077 
00078 #define SET_MULTIPLEX_RATIO 0xA8 // takes one byte, from 16 to 63 (0x
00079 
00080 #define EXTERNAL_IREF 0x10
00081 #define INTERNAL_IREF 0x00
00082 #define SET_IREF_SELECTION 0xAD // sets internal or external Iref
00083 
00084 #define SET_DISPLAY_POWER_OFF 0xAE
00085 #define SET_DISPLAY_POWER_ON  0xAF
00086 
00087 #define COMMAND_MODE 0x80
00088 #define DATA_MODE 0x40
00089 
00090 #define PAGE0 0x00
00091 #define PAGE1 0x01
00092 #define PAGE2 0x02
00093 #define PAGE3 0x03
00094 #define PAGE4 0x04
00095 #define PAGE5 0x05
00096 #define PAGE6 0x06
00097 #define PAGE7 0x07
00098 #define SET_PAGE_START_ADDRESS 0xB0 // | with a page number to get start address
00099 
00100 #define SET_DISPLAY_OFFSET 0xD3
00101 
00102 #define SET_DISPLAY_CLOCK 0xD5
00103 
00104 #define VCOMH_DESELECT_0_65_CODE 0x00
00105 #define VCOMH_DESELECT_0_77_CODE 0x20
00106 #define VCOMH_DESELECT_0_83_CODE 0x30
00107 #define SET_VCOMH_DESELECT_LEVEL 0xDB
00108 
00109 #define NOP 0xE3
00110 
00111 #define SET_RIGHT_HORIZONTAL_SCROLL 0x26
00112 #define SET_LEFT_HORIZONTAL_SCROLL  0x27
00113 #define SET_VERTICAL_RIGHT_HORIZONTAL_SCROLL 0x29
00114 #define SET_VERTICAL_LEFT_HORIZONTAL_SCROLL  0x2A
00115 
00116 #define SET_DEACTIVATE_SCROLL 0x2E
00117 #define SET_ACTIVATE_SCROLL 0x2F
00118 
00119 #define SET_VERTICAL_SCROLL_AREA 0xA3
00120 
00121 class SSD1308
00122 {
00123   public:
00124     
00125       // constructor
00126     // takes a 7-b I2C address to use (0x3C by default, assumes D/C# (pin 13) grounded)
00127     SSD1308(uint8_t address = SSD1308_DEFAULT_ADDRESS);
00128 
00129     void initialize();
00130     void clearDisplay();
00131     void fillDisplay(); // crosshatches    
00132     
00133     // x, y is position (x is row (i.e., page), y is character (0-15), starting at top-left)
00134     // text will wrap around until it is done.
00135     void writeString(uint8_t row, uint8_t col, uint16_t len, const char* txt);
00136     
00137     //void setXY(uint8_t, uint8_t y);
00138 
00139     void setHorizontalAddressingMode();
00140     void setVerticalAddressingMode();
00141     void setPageAddressingMode();
00142     
00143     void setMemoryAddressingMode(uint8_t mode);
00144     
00145     // takes one byte, 0x00-0x0F
00146     void setLowerColumnStartAddressForPageAddressingMode(uint8_t address);
00147     
00148     // takes one byte, 0x10-0x1F
00149     void setHigherColumnStartAddressForPageAddressingMode(uint8_t address);
00150     
00151     // takes two bytes, start address and end address of display data RAM
00152     void setColumnAddress(uint8_t start, uint8_t end);
00153     
00154     // takes two bytes, start address and end address of display data RAM
00155     void setPageAddress(uint8_t start, uint8_t end);
00156     
00157     // takes one byte, PAGE0 - PAGE7
00158     void setPageStartForPageAddressingMode(uint8_t page);
00159     
00160     // takes one byte, 0x40-0x7F
00161     void setDisplayStartLine(uint8_t line);
00162     
00163     // takes one byte, 0x00 (lowest) - 0xFF (highest)
00164     void setContrastControl(uint8_t contrast);
00165     
00166     void setEntireDisplayOn();
00167     void setEntireDisplayRAM();
00168     void setEntireDisplay(bool on);
00169     void setNormalDisplay();
00170     void setInverseDisplay();
00171     
00172     // setMultiplexRatio
00173     
00174     void setInternalIref();
00175     void setExternalIref();
00176     
00177     void setDisplayOn();
00178     void setDisplayOff();
00179     void setDisplayPower(bool on);
00180     
00181     // Set vertical shift by COM from 0 - 63 (0x00 - 0x3F)
00182     // set to 0x00 after RESET
00183     void setDisplayOffset(uint8_t offset);
00184     
00185     // divide ratio 0x00-0x0F, value +1 (reset 0x00)
00186     // oscillator freq 0x00-0x0F (reset 0x08)
00187     void setDisplayClock(uint8_t divideRatio, uint8_t oscFreq);
00188     
00189     // phase1 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
00190     // phase2 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
00191     void setPrechargePeriod(uint8_t phase1, uint8_t phase2);
00192     
00193     #define VCOM_DESELECT_0_65 0x00
00194     #define VCOM_DESELECT_0_77 0x02
00195     #define VCOM_DESELECT_0_83 0x03
00196     void setVcomhDeselectLevel(uint8_t level);
00197     
00198     // command for no operation
00199     void nop();
00200     
00201     #define SCROLL_INTERVAL_5_FRAMES   0x00
00202     #define SCROLL_INTERVAL_64_FRAMES  0x01
00203     #define SCROLL_INTERVAL_128_FRAMES 0x02
00204     #define SCROLL_INTERVAL_256_FRAMES 0x03
00205     #define SCROLL_INTERVAL_3_FRAMES   0x04
00206     #define SCROLL_INTERVAL_4_FRAMES   0x05
00207     #define SCROLL_INTERVAL_25_FRAMES  0x06
00208     #define SCROLL_INTERVAL_2_FRAMES   0x07
00209     // end_page must not be less than start_page
00210     void setContinuousHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page);
00211     // horizontal scroll by one column per interval
00212     // offset = 1 (0x01) to 63 (0x3F)
00213     void setContinuousVerticalAndHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page, uint8_t offset);
00214     
00215     // note, after deactivating scrolling, the RAM data needs to be rewritten
00216     void deactivateScroll();
00217     void activateScroll();
00218     
00219     void setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows);
00220 
00221     void sendData(uint8_t data);
00222     void sendData(uint8_t len, uint8_t* data);
00223     // write the configuration registers in accordance with the datasheet and app note 3944
00224 //    void initialize();
00225     
00226     // returns true if the device is responding on the I2C bus
00227 //    bool testConnection();
00228 
00229     // getTouchStatus returns the touch status for the given channel (0 - 11)
00230 //    bool getTouchStatus(uint8_t channel);
00231     // when not given a channel, returns a bitfield of all touch channels.
00232 //    uint16_t getTouchStatus();
00233 
00234   private:
00235     // sends a single-byte command (given) to device
00236     void sendCommand(uint8_t command);
00237     void sendCommands(uint8_t len, uint8_t* buf);
00238 
00239     void writeChar(char chr);
00240     
00241     uint8_t m_devAddr; // contains the I2C address of the device
00242 };
00243 
00244 #endif
00245 
 All Data Structures Functions Variables