Demonstration of SSD1308 OLED driver library
Dependencies: mbed SSD1308_128x64_I2C
SSD1308.h@0:2ded56b8407d, 2012-06-19 (annotated)
- Committer:
- wim
- Date:
- Tue Jun 19 20:00:10 2012 +0000
- Revision:
- 0:2ded56b8407d
- Child:
- 1:00053cb70ac5
First code port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:2ded56b8407d | 1 | // I2Cdev library collection - SSD1308 I2C device class header file |
wim | 0:2ded56b8407d | 2 | // Based on Solomon Systech SSD1308 datasheet, rev. 1, 10/2008 |
wim | 0:2ded56b8407d | 3 | // 8/25/2011 by Andrew Schamp <schamp@gmail.com> |
wim | 0:2ded56b8407d | 4 | // 19/06/2012 Ported to mbed (WH) |
wim | 0:2ded56b8407d | 5 | // |
wim | 0:2ded56b8407d | 6 | // This I2C device library is using (and submitted as a part of) Jeff Rowberg's I2Cdevlib library, |
wim | 0:2ded56b8407d | 7 | // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib |
wim | 0:2ded56b8407d | 8 | // Note WH: I2Cdevlib not used for mbed port |
wim | 0:2ded56b8407d | 9 | // |
wim | 0:2ded56b8407d | 10 | // Changelog: |
wim | 0:2ded56b8407d | 11 | // 2011-08-25 - initial release |
wim | 0:2ded56b8407d | 12 | // 2012-06-19 - Ported to mbed (WH) |
wim | 0:2ded56b8407d | 13 | |
wim | 0:2ded56b8407d | 14 | /* ============================================ |
wim | 0:2ded56b8407d | 15 | I2Cdev device library code is placed under the MIT license |
wim | 0:2ded56b8407d | 16 | Copyright (c) 2011 Andrew Schamp |
wim | 0:2ded56b8407d | 17 | Copyright (c) 2012 WH (mbed port) |
wim | 0:2ded56b8407d | 18 | |
wim | 0:2ded56b8407d | 19 | Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:2ded56b8407d | 20 | of this software and associated documentation files (the "Software"), to deal |
wim | 0:2ded56b8407d | 21 | in the Software without restriction, including without limitation the rights |
wim | 0:2ded56b8407d | 22 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:2ded56b8407d | 23 | copies of the Software, and to permit persons to whom the Software is |
wim | 0:2ded56b8407d | 24 | furnished to do so, subject to the following conditions: |
wim | 0:2ded56b8407d | 25 | |
wim | 0:2ded56b8407d | 26 | The above copyright notice and this permission notice shall be included in |
wim | 0:2ded56b8407d | 27 | all copies or substantial portions of the Software. |
wim | 0:2ded56b8407d | 28 | |
wim | 0:2ded56b8407d | 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:2ded56b8407d | 30 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:2ded56b8407d | 31 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:2ded56b8407d | 32 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:2ded56b8407d | 33 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:2ded56b8407d | 34 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:2ded56b8407d | 35 | THE SOFTWARE. |
wim | 0:2ded56b8407d | 36 | =============================================== |
wim | 0:2ded56b8407d | 37 | */ |
wim | 0:2ded56b8407d | 38 | |
wim | 0:2ded56b8407d | 39 | #ifndef SSD1308_H |
wim | 0:2ded56b8407d | 40 | #define SSD1308_H |
wim | 0:2ded56b8407d | 41 | |
wim | 0:2ded56b8407d | 42 | // This is the I2C address (8 bit) |
wim | 0:2ded56b8407d | 43 | // which one is used is determined by the D/C# pin. |
wim | 0:2ded56b8407d | 44 | // with D/C# (pin 13) grounded, address is 0x78 |
wim | 0:2ded56b8407d | 45 | // with D/C# tied high it is 0x7A |
wim | 0:2ded56b8407d | 46 | // assume grounded by default. |
wim | 0:2ded56b8407d | 47 | #define SSD1308_SA0 0x78 |
wim | 0:2ded56b8407d | 48 | #define SSD1308_SA1 0x7A |
wim | 0:2ded56b8407d | 49 | #define SSD1308_DEF_SA SSD1308_SA0 |
wim | 0:2ded56b8407d | 50 | |
wim | 0:2ded56b8407d | 51 | #define ROWS 64 |
wim | 0:2ded56b8407d | 52 | #define COLUMNS 128 |
wim | 0:2ded56b8407d | 53 | #define PAGES 8 |
wim | 0:2ded56b8407d | 54 | #define PAGE_WIDTH (ROWS / 8) |
wim | 0:2ded56b8407d | 55 | #define FONT_WIDTH 8 |
wim | 0:2ded56b8407d | 56 | #define CHARS (COLUMNS / FONT_WIDTH) |
wim | 0:2ded56b8407d | 57 | #define MAX_PAGE (PAGES - 1) |
wim | 0:2ded56b8407d | 58 | #define MAX_COL (COLUMNS - 1) |
wim | 0:2ded56b8407d | 59 | |
wim | 0:2ded56b8407d | 60 | #define HORIZONTAL_ADDRESSING_MODE 0x00 |
wim | 0:2ded56b8407d | 61 | #define VERTICAL_ADDRESSING_MODE 0x01 |
wim | 0:2ded56b8407d | 62 | #define PAGE_ADDRESSING_MODE 0x02 |
wim | 0:2ded56b8407d | 63 | |
wim | 0:2ded56b8407d | 64 | #define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte |
wim | 0:2ded56b8407d | 65 | |
wim | 0:2ded56b8407d | 66 | #define SET_COLUMN_ADDRESS 0x21 // takes two bytes, start address and end address of display data RAM |
wim | 0:2ded56b8407d | 67 | #define SET_PAGE_ADDRESS 0x22 // takes two bytes, start address and end address of display data RAM |
wim | 0:2ded56b8407d | 68 | |
wim | 0:2ded56b8407d | 69 | #define SET_CONTRAST 0x81 // takes one byte, 0x00 - 0xFF |
wim | 0:2ded56b8407d | 70 | |
wim | 0:2ded56b8407d | 71 | #define SET_SEGMENT_REMAP_0 0xA0 // column address 0 is mapped to SEG0 |
wim | 0:2ded56b8407d | 72 | #define SET_SEGMENT_REMAP_127 0xA1 // column address 127 is mapped to SEG0 |
wim | 0:2ded56b8407d | 73 | |
wim | 0:2ded56b8407d | 74 | #define SET_ENTIRE_DISPLAY_ON 0xA5 // turns all pixels on, does not affect RAM |
wim | 0:2ded56b8407d | 75 | #define SET_DISPLAY_GDDRAM 0xA4 // restores display to contents of RAM |
wim | 0:2ded56b8407d | 76 | |
wim | 0:2ded56b8407d | 77 | #define SET_NORMAL_DISPLAY 0xA6 // a data of 1 indicates 'ON' |
wim | 0:2ded56b8407d | 78 | #define SET_INVERSE_DISPLAY 0xA7 // a data of 0 indicates 'ON' |
wim | 0:2ded56b8407d | 79 | |
wim | 0:2ded56b8407d | 80 | #define SET_MULTIPLEX_RATIO 0xA8 // takes one byte, from 16 to 63 (0x |
wim | 0:2ded56b8407d | 81 | |
wim | 0:2ded56b8407d | 82 | #define EXTERNAL_IREF 0x10 |
wim | 0:2ded56b8407d | 83 | #define INTERNAL_IREF 0x00 |
wim | 0:2ded56b8407d | 84 | #define SET_IREF_SELECTION 0xAD // sets internal or external Iref |
wim | 0:2ded56b8407d | 85 | |
wim | 0:2ded56b8407d | 86 | #define SET_DISPLAY_POWER_OFF 0xAE |
wim | 0:2ded56b8407d | 87 | #define SET_DISPLAY_POWER_ON 0xAF |
wim | 0:2ded56b8407d | 88 | |
wim | 0:2ded56b8407d | 89 | #define COMMAND_MODE 0x80 |
wim | 0:2ded56b8407d | 90 | #define DATA_MODE 0x40 |
wim | 0:2ded56b8407d | 91 | |
wim | 0:2ded56b8407d | 92 | #define PAGE0 0x00 |
wim | 0:2ded56b8407d | 93 | #define PAGE1 0x01 |
wim | 0:2ded56b8407d | 94 | #define PAGE2 0x02 |
wim | 0:2ded56b8407d | 95 | #define PAGE3 0x03 |
wim | 0:2ded56b8407d | 96 | #define PAGE4 0x04 |
wim | 0:2ded56b8407d | 97 | #define PAGE5 0x05 |
wim | 0:2ded56b8407d | 98 | #define PAGE6 0x06 |
wim | 0:2ded56b8407d | 99 | #define PAGE7 0x07 |
wim | 0:2ded56b8407d | 100 | #define SET_PAGE_START_ADDRESS 0xB0 // | with a page number to get start address |
wim | 0:2ded56b8407d | 101 | |
wim | 0:2ded56b8407d | 102 | #define SET_DISPLAY_OFFSET 0xD3 |
wim | 0:2ded56b8407d | 103 | |
wim | 0:2ded56b8407d | 104 | #define SET_DISPLAY_CLOCK 0xD5 |
wim | 0:2ded56b8407d | 105 | |
wim | 0:2ded56b8407d | 106 | #define VCOMH_DESELECT_0_65_CODE 0x00 |
wim | 0:2ded56b8407d | 107 | #define VCOMH_DESELECT_0_77_CODE 0x20 |
wim | 0:2ded56b8407d | 108 | #define VCOMH_DESELECT_0_83_CODE 0x30 |
wim | 0:2ded56b8407d | 109 | #define SET_VCOMH_DESELECT_LEVEL 0xDB |
wim | 0:2ded56b8407d | 110 | |
wim | 0:2ded56b8407d | 111 | #define NOP 0xE3 |
wim | 0:2ded56b8407d | 112 | |
wim | 0:2ded56b8407d | 113 | #define SET_RIGHT_HORIZONTAL_SCROLL 0x26 |
wim | 0:2ded56b8407d | 114 | #define SET_LEFT_HORIZONTAL_SCROLL 0x27 |
wim | 0:2ded56b8407d | 115 | #define SET_VERTICAL_RIGHT_HORIZONTAL_SCROLL 0x29 |
wim | 0:2ded56b8407d | 116 | #define SET_VERTICAL_LEFT_HORIZONTAL_SCROLL 0x2A |
wim | 0:2ded56b8407d | 117 | |
wim | 0:2ded56b8407d | 118 | #define SET_DEACTIVATE_SCROLL 0x2E |
wim | 0:2ded56b8407d | 119 | #define SET_ACTIVATE_SCROLL 0x2F |
wim | 0:2ded56b8407d | 120 | |
wim | 0:2ded56b8407d | 121 | #define SET_VERTICAL_SCROLL_AREA 0xA3 |
wim | 0:2ded56b8407d | 122 | |
wim | 0:2ded56b8407d | 123 | class SSD1308 { |
wim | 0:2ded56b8407d | 124 | public: |
wim | 0:2ded56b8407d | 125 | |
wim | 0:2ded56b8407d | 126 | // constructor |
wim | 0:2ded56b8407d | 127 | // takes a 8bit I2C address to use (0x78 by default, assumes D/C# (pin 13) grounded) |
wim | 0:2ded56b8407d | 128 | SSD1308(I2C &i2c, uint8_t address = SSD1308_DEF_SA); |
wim | 0:2ded56b8407d | 129 | |
wim | 0:2ded56b8407d | 130 | void initialize(); |
wim | 0:2ded56b8407d | 131 | void clearDisplay(); |
wim | 0:2ded56b8407d | 132 | void fillDisplay(); // crosshatches |
wim | 0:2ded56b8407d | 133 | |
wim | 0:2ded56b8407d | 134 | // x, y is position (x is row (i.e., page), y is character (0-15), starting at top-left) |
wim | 0:2ded56b8407d | 135 | // text will wrap around until it is done. |
wim | 0:2ded56b8407d | 136 | void writeString(uint8_t row, uint8_t col, uint16_t len, const char* txt); |
wim | 0:2ded56b8407d | 137 | |
wim | 0:2ded56b8407d | 138 | //void setXY(uint8_t, uint8_t y); |
wim | 0:2ded56b8407d | 139 | |
wim | 0:2ded56b8407d | 140 | void setHorizontalAddressingMode(); |
wim | 0:2ded56b8407d | 141 | void setVerticalAddressingMode(); |
wim | 0:2ded56b8407d | 142 | void setPageAddressingMode(); |
wim | 0:2ded56b8407d | 143 | |
wim | 0:2ded56b8407d | 144 | void setMemoryAddressingMode(uint8_t mode); |
wim | 0:2ded56b8407d | 145 | |
wim | 0:2ded56b8407d | 146 | // takes one byte, 0x00-0x0F |
wim | 0:2ded56b8407d | 147 | void setLowerColumnStartAddressForPageAddressingMode(uint8_t address); |
wim | 0:2ded56b8407d | 148 | |
wim | 0:2ded56b8407d | 149 | // takes one byte, 0x10-0x1F |
wim | 0:2ded56b8407d | 150 | void setHigherColumnStartAddressForPageAddressingMode(uint8_t address); |
wim | 0:2ded56b8407d | 151 | |
wim | 0:2ded56b8407d | 152 | // takes two bytes, start address and end address of display data RAM |
wim | 0:2ded56b8407d | 153 | void setColumnAddress(uint8_t start, uint8_t end); |
wim | 0:2ded56b8407d | 154 | |
wim | 0:2ded56b8407d | 155 | // takes two bytes, start address and end address of display data RAM |
wim | 0:2ded56b8407d | 156 | void setPageAddress(uint8_t start, uint8_t end); |
wim | 0:2ded56b8407d | 157 | |
wim | 0:2ded56b8407d | 158 | // takes one byte, PAGE0 - PAGE7 |
wim | 0:2ded56b8407d | 159 | void setPageStartForPageAddressingMode(uint8_t page); |
wim | 0:2ded56b8407d | 160 | |
wim | 0:2ded56b8407d | 161 | // takes one byte, 0x40-0x7F |
wim | 0:2ded56b8407d | 162 | void setDisplayStartLine(uint8_t line); |
wim | 0:2ded56b8407d | 163 | |
wim | 0:2ded56b8407d | 164 | // takes one byte, 0x00 (lowest) - 0xFF (highest) |
wim | 0:2ded56b8407d | 165 | void setContrastControl(uint8_t contrast); |
wim | 0:2ded56b8407d | 166 | |
wim | 0:2ded56b8407d | 167 | void setEntireDisplayOn(); |
wim | 0:2ded56b8407d | 168 | void setEntireDisplayRAM(); |
wim | 0:2ded56b8407d | 169 | void setEntireDisplay(bool on); |
wim | 0:2ded56b8407d | 170 | void setNormalDisplay(); |
wim | 0:2ded56b8407d | 171 | void setInverseDisplay(); |
wim | 0:2ded56b8407d | 172 | |
wim | 0:2ded56b8407d | 173 | // setMultiplexRatio |
wim | 0:2ded56b8407d | 174 | |
wim | 0:2ded56b8407d | 175 | void setInternalIref(); |
wim | 0:2ded56b8407d | 176 | void setExternalIref(); |
wim | 0:2ded56b8407d | 177 | |
wim | 0:2ded56b8407d | 178 | void setDisplayOn(); |
wim | 0:2ded56b8407d | 179 | void setDisplayOff(); |
wim | 0:2ded56b8407d | 180 | void setDisplayPower(bool on); |
wim | 0:2ded56b8407d | 181 | |
wim | 0:2ded56b8407d | 182 | // Set vertical shift by COM from 0 - 63 (0x00 - 0x3F) |
wim | 0:2ded56b8407d | 183 | // set to 0x00 after RESET |
wim | 0:2ded56b8407d | 184 | void setDisplayOffset(uint8_t offset); |
wim | 0:2ded56b8407d | 185 | |
wim | 0:2ded56b8407d | 186 | // divide ratio 0x00-0x0F, value +1 (reset 0x00) |
wim | 0:2ded56b8407d | 187 | // oscillator freq 0x00-0x0F (reset 0x08) |
wim | 0:2ded56b8407d | 188 | void setDisplayClock(uint8_t divideRatio, uint8_t oscFreq); |
wim | 0:2ded56b8407d | 189 | |
wim | 0:2ded56b8407d | 190 | // phase1 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid) |
wim | 0:2ded56b8407d | 191 | // phase2 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid) |
wim | 0:2ded56b8407d | 192 | void setPrechargePeriod(uint8_t phase1, uint8_t phase2); |
wim | 0:2ded56b8407d | 193 | |
wim | 0:2ded56b8407d | 194 | #define VCOM_DESELECT_0_65 0x00 |
wim | 0:2ded56b8407d | 195 | #define VCOM_DESELECT_0_77 0x02 |
wim | 0:2ded56b8407d | 196 | #define VCOM_DESELECT_0_83 0x03 |
wim | 0:2ded56b8407d | 197 | void setVcomhDeselectLevel(uint8_t level); |
wim | 0:2ded56b8407d | 198 | |
wim | 0:2ded56b8407d | 199 | // command for no-operation |
wim | 0:2ded56b8407d | 200 | void nop(); |
wim | 0:2ded56b8407d | 201 | |
wim | 0:2ded56b8407d | 202 | #define SCROLL_INTERVAL_5_FRAMES 0x00 |
wim | 0:2ded56b8407d | 203 | #define SCROLL_INTERVAL_64_FRAMES 0x01 |
wim | 0:2ded56b8407d | 204 | #define SCROLL_INTERVAL_128_FRAMES 0x02 |
wim | 0:2ded56b8407d | 205 | #define SCROLL_INTERVAL_256_FRAMES 0x03 |
wim | 0:2ded56b8407d | 206 | #define SCROLL_INTERVAL_3_FRAMES 0x04 |
wim | 0:2ded56b8407d | 207 | #define SCROLL_INTERVAL_4_FRAMES 0x05 |
wim | 0:2ded56b8407d | 208 | #define SCROLL_INTERVAL_25_FRAMES 0x06 |
wim | 0:2ded56b8407d | 209 | #define SCROLL_INTERVAL_2_FRAMES 0x07 |
wim | 0:2ded56b8407d | 210 | // end_page must not be less than start_page |
wim | 0:2ded56b8407d | 211 | void setContinuousHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page); |
wim | 0:2ded56b8407d | 212 | // horizontal scroll by one column per interval |
wim | 0:2ded56b8407d | 213 | // offset = 1 (0x01) to 63 (0x3F) |
wim | 0:2ded56b8407d | 214 | void setContinuousVerticalAndHorizontalScroll(bool left, uint8_t start_page, uint8_t interval, uint8_t end_page, uint8_t offset); |
wim | 0:2ded56b8407d | 215 | |
wim | 0:2ded56b8407d | 216 | // note, after deactivating scrolling, the RAM data needs to be rewritten |
wim | 0:2ded56b8407d | 217 | void deactivateScroll(); |
wim | 0:2ded56b8407d | 218 | void activateScroll(); |
wim | 0:2ded56b8407d | 219 | |
wim | 0:2ded56b8407d | 220 | void setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows); |
wim | 0:2ded56b8407d | 221 | |
wim | 0:2ded56b8407d | 222 | void sendData(uint8_t data); |
wim | 0:2ded56b8407d | 223 | void sendData(uint8_t len, uint8_t* data); |
wim | 0:2ded56b8407d | 224 | // write the configuration registers in accordance with the datasheet and app note 3944 |
wim | 0:2ded56b8407d | 225 | // void initialize(); |
wim | 0:2ded56b8407d | 226 | |
wim | 0:2ded56b8407d | 227 | |
wim | 0:2ded56b8407d | 228 | private: |
wim | 0:2ded56b8407d | 229 | // sends a single-byte command (given) to device |
wim | 0:2ded56b8407d | 230 | void sendCommand(uint8_t command); |
wim | 0:2ded56b8407d | 231 | void sendCommands(uint8_t len, uint8_t* buf); |
wim | 0:2ded56b8407d | 232 | |
wim | 0:2ded56b8407d | 233 | void writeChar(char chr); |
wim | 0:2ded56b8407d | 234 | |
wim | 0:2ded56b8407d | 235 | I2C _i2c; // I2C bus |
wim | 0:2ded56b8407d | 236 | // uint8_t m_devAddr; // contains the I2C address of the device |
wim | 0:2ded56b8407d | 237 | uint8_t _readOpcode; |
wim | 0:2ded56b8407d | 238 | uint8_t _writeOpcode; |
wim | 0:2ded56b8407d | 239 | |
wim | 0:2ded56b8407d | 240 | }; |
wim | 0:2ded56b8407d | 241 | |
wim | 0:2ded56b8407d | 242 | #endif |