Demonstration of SSD1308 OLED driver library
Dependencies: mbed SSD1308_128x64_I2C
SSD1308.cpp@2:d86478c0f5da, 2012-07-09 (annotated)
- Committer:
- wim
- Date:
- Mon Jul 09 20:46:27 2012 +0000
- Revision:
- 2:d86478c0f5da
- Parent:
- 1:00053cb70ac5
- Child:
- 3:1337e3d65ed0
Temp version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 1:00053cb70ac5 | 1 | // SSD1308 I2C device class file |
wim | 1:00053cb70ac5 | 2 | // Based on Solomon Systech SSD1308 datasheet, rev. 1, 10/2008 |
wim | 1:00053cb70ac5 | 3 | // The SSD1308 is used for example in the Seeed 128x64 OLED Display |
wim | 1:00053cb70ac5 | 4 | // http://www.seeedstudio.com/depot/grove-oled-display-12864-p-781.html?cPath=163_167 |
wim | 0:2ded56b8407d | 5 | // |
wim | 2:d86478c0f5da | 6 | // The original code is using (and has been 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 | // |
wim | 0:2ded56b8407d | 9 | // Changelog: |
wim | 2:d86478c0f5da | 10 | // 2011-08-25 - Initial release by Andrew Schamp <schamp@gmail.com> |
wim | 1:00053cb70ac5 | 11 | // 2012-06-19 - Ported to mbed and optimised (WH) |
wim | 1:00053cb70ac5 | 12 | // |
wim | 0:2ded56b8407d | 13 | /* ============================================ |
wim | 0:2ded56b8407d | 14 | I2Cdev device library code is placed under the MIT license |
wim | 0:2ded56b8407d | 15 | Copyright (c) 2011 Andrew Schamp |
wim | 0:2ded56b8407d | 16 | Copyright (c) 2012 WH (mbed port) |
wim | 0:2ded56b8407d | 17 | |
wim | 0:2ded56b8407d | 18 | Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:2ded56b8407d | 19 | of this software and associated documentation files (the "Software"), to deal |
wim | 0:2ded56b8407d | 20 | in the Software without restriction, including without limitation the rights |
wim | 0:2ded56b8407d | 21 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:2ded56b8407d | 22 | copies of the Software, and to permit persons to whom the Software is |
wim | 0:2ded56b8407d | 23 | furnished to do so, subject to the following conditions: |
wim | 0:2ded56b8407d | 24 | |
wim | 0:2ded56b8407d | 25 | The above copyright notice and this permission notice shall be included in |
wim | 0:2ded56b8407d | 26 | all copies or substantial portions of the Software. |
wim | 0:2ded56b8407d | 27 | |
wim | 0:2ded56b8407d | 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:2ded56b8407d | 29 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:2ded56b8407d | 30 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:2ded56b8407d | 31 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:2ded56b8407d | 32 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:2ded56b8407d | 33 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:2ded56b8407d | 34 | THE SOFTWARE. |
wim | 0:2ded56b8407d | 35 | =============================================== |
wim | 0:2ded56b8407d | 36 | */ |
wim | 0:2ded56b8407d | 37 | #include "mbed.h" |
wim | 0:2ded56b8407d | 38 | #include "SSD1308.h" |
wim | 0:2ded56b8407d | 39 | |
wim | 1:00053cb70ac5 | 40 | //#define SSD1308_USE_FONT |
wim | 0:2ded56b8407d | 41 | //#ifdef SSD1308_USE_FONT |
wim | 0:2ded56b8407d | 42 | #include "FixedWidthFont.h" |
wim | 0:2ded56b8407d | 43 | //#endif |
wim | 0:2ded56b8407d | 44 | |
wim | 1:00053cb70ac5 | 45 | //Constructor |
wim | 1:00053cb70ac5 | 46 | // |
wim | 0:2ded56b8407d | 47 | SSD1308::SSD1308(I2C &i2c, uint8_t deviceAddress) : _i2c(i2c) { |
wim | 0:2ded56b8407d | 48 | // m_devAddr = deviceAddress; |
wim | 0:2ded56b8407d | 49 | |
wim | 0:2ded56b8407d | 50 | _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write |
wim | 0:2ded56b8407d | 51 | _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read |
wim | 1:00053cb70ac5 | 52 | |
wim | 1:00053cb70ac5 | 53 | initialize(); |
wim | 0:2ded56b8407d | 54 | } |
wim | 0:2ded56b8407d | 55 | |
wim | 1:00053cb70ac5 | 56 | // High level Init, most settings remain at Power-On reset value |
wim | 1:00053cb70ac5 | 57 | // |
wim | 0:2ded56b8407d | 58 | void SSD1308::initialize() { |
wim | 0:2ded56b8407d | 59 | setHorizontalAddressingMode(); |
wim | 0:2ded56b8407d | 60 | clearDisplay(); |
wim | 0:2ded56b8407d | 61 | } |
wim | 0:2ded56b8407d | 62 | |
wim | 1:00053cb70ac5 | 63 | |
wim | 1:00053cb70ac5 | 64 | #if(0) |
wim | 1:00053cb70ac5 | 65 | // Standard version |
wim | 0:2ded56b8407d | 66 | void SSD1308::clearDisplay() { |
wim | 2:d86478c0f5da | 67 | |
wim | 1:00053cb70ac5 | 68 | //setDisplayOff(); |
wim | 0:2ded56b8407d | 69 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 0:2ded56b8407d | 70 | setColumnAddress(0, MAX_COL); // all columns |
wim | 1:00053cb70ac5 | 71 | |
wim | 1:00053cb70ac5 | 72 | for (uint8_t page = 0; page < PAGES; page++) { |
wim | 1:00053cb70ac5 | 73 | for (uint8_t col = 0; col < COLUMNS; col++) { |
wim | 1:00053cb70ac5 | 74 | sendData(0x00); |
wim | 0:2ded56b8407d | 75 | } |
wim | 0:2ded56b8407d | 76 | } |
wim | 1:00053cb70ac5 | 77 | |
wim | 2:d86478c0f5da | 78 | //setDisplayOn(); |
wim | 0:2ded56b8407d | 79 | } |
wim | 1:00053cb70ac5 | 80 | #else |
wim | 1:00053cb70ac5 | 81 | //Optimised version |
wim | 1:00053cb70ac5 | 82 | // Save lots of I2C S,P, address and datacommands: |
wim | 1:00053cb70ac5 | 83 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 1:00053cb70ac5 | 84 | // |
wim | 1:00053cb70ac5 | 85 | void SSD1308::clearDisplay() { |
wim | 0:2ded56b8407d | 86 | |
wim | 1:00053cb70ac5 | 87 | //setDisplayOff(); |
wim | 1:00053cb70ac5 | 88 | |
wim | 0:2ded56b8407d | 89 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 0:2ded56b8407d | 90 | setColumnAddress(0, MAX_COL); // all columns |
wim | 0:2ded56b8407d | 91 | |
wim | 1:00053cb70ac5 | 92 | _i2c.start(); |
wim | 1:00053cb70ac5 | 93 | _i2c.write(_writeOpcode); |
wim | 1:00053cb70ac5 | 94 | _i2c.write(DATA_MODE); |
wim | 1:00053cb70ac5 | 95 | for (int i=0; i<(PAGES * COLUMNS); i++) { |
wim | 1:00053cb70ac5 | 96 | _i2c.write(0x00); // Write Data |
wim | 1:00053cb70ac5 | 97 | } |
wim | 1:00053cb70ac5 | 98 | _i2c.stop(); |
wim | 1:00053cb70ac5 | 99 | |
wim | 2:d86478c0f5da | 100 | //setDisplayOn(); |
wim | 1:00053cb70ac5 | 101 | } |
wim | 1:00053cb70ac5 | 102 | #endif |
wim | 1:00053cb70ac5 | 103 | |
wim | 1:00053cb70ac5 | 104 | |
wim | 1:00053cb70ac5 | 105 | #if(0) |
wim | 1:00053cb70ac5 | 106 | //Standard version |
wim | 1:00053cb70ac5 | 107 | void SSD1308::fillDisplay(uint8_t pattern) { |
wim | 1:00053cb70ac5 | 108 | |
wim | 1:00053cb70ac5 | 109 | //setDisplayOff(); |
wim | 1:00053cb70ac5 | 110 | |
wim | 1:00053cb70ac5 | 111 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 1:00053cb70ac5 | 112 | setColumnAddress(0, MAX_COL); // all columns |
wim | 1:00053cb70ac5 | 113 | |
wim | 1:00053cb70ac5 | 114 | for (uint8_t page = 0; page < PAGES; page++) { |
wim | 1:00053cb70ac5 | 115 | for (uint8_t col = 0; col < COLUMNS; col++) { |
wim | 1:00053cb70ac5 | 116 | sendData(pattern); |
wim | 0:2ded56b8407d | 117 | } |
wim | 0:2ded56b8407d | 118 | } |
wim | 1:00053cb70ac5 | 119 | |
wim | 2:d86478c0f5da | 120 | //setDisplayOn(); |
wim | 1:00053cb70ac5 | 121 | } |
wim | 1:00053cb70ac5 | 122 | #else |
wim | 1:00053cb70ac5 | 123 | //Optimised version |
wim | 1:00053cb70ac5 | 124 | // Save lots of I2C S,P, address and datacommands: |
wim | 1:00053cb70ac5 | 125 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 1:00053cb70ac5 | 126 | // |
wim | 1:00053cb70ac5 | 127 | void SSD1308::fillDisplay(uint8_t pattern) { |
wim | 1:00053cb70ac5 | 128 | |
wim | 1:00053cb70ac5 | 129 | //setDisplayOff(); |
wim | 1:00053cb70ac5 | 130 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 1:00053cb70ac5 | 131 | setColumnAddress(0, MAX_COL); // all columns |
wim | 1:00053cb70ac5 | 132 | |
wim | 1:00053cb70ac5 | 133 | _i2c.start(); |
wim | 1:00053cb70ac5 | 134 | _i2c.write(_writeOpcode); |
wim | 1:00053cb70ac5 | 135 | _i2c.write(DATA_MODE); |
wim | 1:00053cb70ac5 | 136 | for (int i=0; i<(PAGES * COLUMNS); i++) { |
wim | 1:00053cb70ac5 | 137 | _i2c.write(pattern); // Write Data |
wim | 1:00053cb70ac5 | 138 | } |
wim | 1:00053cb70ac5 | 139 | _i2c.stop(); |
wim | 1:00053cb70ac5 | 140 | |
wim | 2:d86478c0f5da | 141 | //setDisplayOn(); |
wim | 1:00053cb70ac5 | 142 | } |
wim | 1:00053cb70ac5 | 143 | #endif |
wim | 1:00053cb70ac5 | 144 | |
wim | 1:00053cb70ac5 | 145 | |
wim | 1:00053cb70ac5 | 146 | void SSD1308::writeBitmap(int len, uint8_t* data) { |
wim | 2:d86478c0f5da | 147 | |
wim | 1:00053cb70ac5 | 148 | //setDisplayOff(); |
wim | 1:00053cb70ac5 | 149 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 1:00053cb70ac5 | 150 | setColumnAddress(0, MAX_COL); // all columns |
wim | 1:00053cb70ac5 | 151 | |
wim | 1:00053cb70ac5 | 152 | _i2c.start(); |
wim | 1:00053cb70ac5 | 153 | _i2c.write(_writeOpcode); |
wim | 1:00053cb70ac5 | 154 | _i2c.write(DATA_MODE); |
wim | 1:00053cb70ac5 | 155 | for (int i=0; i<len; i++) { |
wim | 1:00053cb70ac5 | 156 | _i2c.write(data[i]); // Write Data |
wim | 1:00053cb70ac5 | 157 | } |
wim | 1:00053cb70ac5 | 158 | _i2c.stop(); |
wim | 1:00053cb70ac5 | 159 | |
wim | 2:d86478c0f5da | 160 | //setDisplayOn(); |
wim | 0:2ded56b8407d | 161 | } |
wim | 0:2ded56b8407d | 162 | |
wim | 2:d86478c0f5da | 163 | |
wim | 2:d86478c0f5da | 164 | // Write single character to the display using the 8x8 fontable |
wim | 2:d86478c0f5da | 165 | // Start at current cursor location |
wim | 2:d86478c0f5da | 166 | // char chr character |
wim | 2:d86478c0f5da | 167 | // bool inverted invert pixels |
wim | 2:d86478c0f5da | 168 | void SSD1308::writeChar(char chr, bool inverted) { |
wim | 1:00053cb70ac5 | 169 | |
wim | 0:2ded56b8407d | 170 | const uint8_t char_index = chr - 0x20; |
wim | 0:2ded56b8407d | 171 | for (uint8_t i = 0; i < 8; i++) { |
wim | 0:2ded56b8407d | 172 | // const uint8_t b = pgm_read_byte( &fontData[char_index][i] ); |
wim | 1:00053cb70ac5 | 173 | // const uint8_t b = fontData[char_index][i]; |
wim | 1:00053cb70ac5 | 174 | // sendData( b ); |
wim | 2:d86478c0f5da | 175 | if (inverted) { |
wim | 2:d86478c0f5da | 176 | sendData( ~fontData[char_index][i] ); |
wim | 2:d86478c0f5da | 177 | } |
wim | 2:d86478c0f5da | 178 | else { |
wim | 2:d86478c0f5da | 179 | sendData( fontData[char_index][i] ); |
wim | 2:d86478c0f5da | 180 | } |
wim | 0:2ded56b8407d | 181 | } |
wim | 1:00053cb70ac5 | 182 | |
wim | 0:2ded56b8407d | 183 | } |
wim | 0:2ded56b8407d | 184 | |
wim | 2:d86478c0f5da | 185 | |
wim | 2:d86478c0f5da | 186 | // Write a string to the display using the 8x8 fontable |
wim | 2:d86478c0f5da | 187 | // Start at current cursor location |
wim | 2:d86478c0f5da | 188 | void SSD1308::writeString(uint8_t row, uint8_t col, uint16_t len, const char * text, bool inverted) { |
wim | 0:2ded56b8407d | 189 | uint16_t index = 0; |
wim | 0:2ded56b8407d | 190 | setPageAddress(row, MAX_PAGE); |
wim | 0:2ded56b8407d | 191 | const uint8_t col_addr = FONT_WIDTH*col; |
wim | 0:2ded56b8407d | 192 | setColumnAddress(col_addr, MAX_COL); |
wim | 0:2ded56b8407d | 193 | |
wim | 0:2ded56b8407d | 194 | while ((col+index) < CHARS && (index < len)) { |
wim | 0:2ded56b8407d | 195 | // write first line, starting at given position |
wim | 2:d86478c0f5da | 196 | writeChar(text[index++], inverted); |
wim | 0:2ded56b8407d | 197 | } |
wim | 0:2ded56b8407d | 198 | |
wim | 0:2ded56b8407d | 199 | // write remaining lines |
wim | 0:2ded56b8407d | 200 | // write until the end of memory |
wim | 0:2ded56b8407d | 201 | // then wrap around again from the top. |
wim | 0:2ded56b8407d | 202 | if (index + 1 < len) { |
wim | 0:2ded56b8407d | 203 | setPageAddress(row + 1, MAX_PAGE); |
wim | 0:2ded56b8407d | 204 | setColumnAddress(0, MAX_COL); |
wim | 0:2ded56b8407d | 205 | bool wrapEntireScreen = false; |
wim | 0:2ded56b8407d | 206 | while (index + 1 < len) { |
wim | 2:d86478c0f5da | 207 | writeChar(text[index++], inverted); |
wim | 0:2ded56b8407d | 208 | // if we've written the last character space on the screen, |
wim | 0:2ded56b8407d | 209 | // reset the page and column address so that it wraps around from the top again |
wim | 0:2ded56b8407d | 210 | if (!wrapEntireScreen && (row*CHARS + col + index) > 127) { |
wim | 0:2ded56b8407d | 211 | setPageAddress(0, MAX_PAGE); |
wim | 0:2ded56b8407d | 212 | setColumnAddress(0, MAX_COL); |
wim | 0:2ded56b8407d | 213 | wrapEntireScreen = true; |
wim | 0:2ded56b8407d | 214 | } |
wim | 0:2ded56b8407d | 215 | } |
wim | 0:2ded56b8407d | 216 | } |
wim | 0:2ded56b8407d | 217 | } |
wim | 0:2ded56b8407d | 218 | |
wim | 2:d86478c0f5da | 219 | // Write command that has no parameters |
wim | 2:d86478c0f5da | 220 | // |
wim | 0:2ded56b8407d | 221 | void SSD1308::sendCommand(uint8_t command) { |
wim | 0:2ded56b8407d | 222 | // I2Cdev::writeByte(m_devAddr, COMMAND_MODE, command); |
wim | 0:2ded56b8407d | 223 | |
wim | 0:2ded56b8407d | 224 | char databytes[2]; |
wim | 0:2ded56b8407d | 225 | |
wim | 0:2ded56b8407d | 226 | databytes[0] = COMMAND_MODE; |
wim | 0:2ded56b8407d | 227 | databytes[1] = command; |
wim | 0:2ded56b8407d | 228 | _i2c.write(_writeOpcode, databytes, 2); // Write command |
wim | 0:2ded56b8407d | 229 | |
wim | 0:2ded56b8407d | 230 | } |
wim | 0:2ded56b8407d | 231 | |
wim | 2:d86478c0f5da | 232 | // Write command that has one parameter |
wim | 2:d86478c0f5da | 233 | // |
wim | 2:d86478c0f5da | 234 | void SSD1308::sendCommand(uint8_t command, uint8_t param1) { |
wim | 2:d86478c0f5da | 235 | |
wim | 2:d86478c0f5da | 236 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 2:d86478c0f5da | 237 | // repeated before each databyte that serves as parameter! |
wim | 2:d86478c0f5da | 238 | |
wim | 2:d86478c0f5da | 239 | _i2c.start(); |
wim | 2:d86478c0f5da | 240 | _i2c.write(_writeOpcode); |
wim | 2:d86478c0f5da | 241 | |
wim | 2:d86478c0f5da | 242 | _i2c.write(COMMAND_MODE); |
wim | 2:d86478c0f5da | 243 | _i2c.write(command); // Write Command |
wim | 2:d86478c0f5da | 244 | _i2c.write(COMMAND_MODE); |
wim | 2:d86478c0f5da | 245 | _i2c.write(param1); // Write Param1 |
wim | 2:d86478c0f5da | 246 | |
wim | 2:d86478c0f5da | 247 | _i2c.stop(); |
wim | 2:d86478c0f5da | 248 | |
wim | 2:d86478c0f5da | 249 | } |
wim | 2:d86478c0f5da | 250 | |
wim | 2:d86478c0f5da | 251 | // Write command that has two parameters |
wim | 2:d86478c0f5da | 252 | // |
wim | 2:d86478c0f5da | 253 | void SSD1308::sendCommand(uint8_t command, uint8_t param1, uint8_t param2) { |
wim | 2:d86478c0f5da | 254 | |
wim | 2:d86478c0f5da | 255 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 2:d86478c0f5da | 256 | // repeated before each databyte that serves as parameter! |
wim | 2:d86478c0f5da | 257 | |
wim | 2:d86478c0f5da | 258 | _i2c.start(); |
wim | 2:d86478c0f5da | 259 | _i2c.write(_writeOpcode); |
wim | 2:d86478c0f5da | 260 | |
wim | 2:d86478c0f5da | 261 | _i2c.write(COMMAND_MODE); |
wim | 2:d86478c0f5da | 262 | _i2c.write(command); // Write Command |
wim | 2:d86478c0f5da | 263 | _i2c.write(COMMAND_MODE); |
wim | 2:d86478c0f5da | 264 | _i2c.write(param1); // Write Param1 |
wim | 2:d86478c0f5da | 265 | _i2c.write(COMMAND_MODE); |
wim | 2:d86478c0f5da | 266 | _i2c.write(param2); // Write Param2 |
wim | 2:d86478c0f5da | 267 | |
wim | 2:d86478c0f5da | 268 | _i2c.stop(); |
wim | 2:d86478c0f5da | 269 | |
wim | 2:d86478c0f5da | 270 | } |
wim | 2:d86478c0f5da | 271 | |
wim | 2:d86478c0f5da | 272 | |
wim | 2:d86478c0f5da | 273 | |
wim | 2:d86478c0f5da | 274 | // Write command that has multiple parameters |
wim | 2:d86478c0f5da | 275 | // |
wim | 0:2ded56b8407d | 276 | void SSD1308::sendCommands(uint8_t len, uint8_t* commands) { |
wim | 0:2ded56b8407d | 277 | |
wim | 1:00053cb70ac5 | 278 | // I2Cdev::writeBytes(m_devAddr, COMMAND_MODE, len, commands); |
wim | 2:d86478c0f5da | 279 | // Note this original code is not correct, continuationbit is set, |
wim | 2:d86478c0f5da | 280 | // so COMMAND_MODE must be repeated before each databyte that serves as parameter! |
wim | 1:00053cb70ac5 | 281 | |
wim | 0:2ded56b8407d | 282 | _i2c.start(); |
wim | 0:2ded56b8407d | 283 | _i2c.write(_writeOpcode); |
wim | 1:00053cb70ac5 | 284 | |
wim | 1:00053cb70ac5 | 285 | for (int i=0; i<len ; i++) { |
wim | 1:00053cb70ac5 | 286 | _i2c.write(COMMAND_MODE); |
wim | 0:2ded56b8407d | 287 | _i2c.write(commands[i]); // Write Commands |
wim | 0:2ded56b8407d | 288 | } |
wim | 0:2ded56b8407d | 289 | _i2c.stop(); |
wim | 0:2ded56b8407d | 290 | |
wim | 0:2ded56b8407d | 291 | } |
wim | 0:2ded56b8407d | 292 | |
wim | 2:d86478c0f5da | 293 | // Write databyte to display |
wim | 2:d86478c0f5da | 294 | // Start at current cursor location |
wim | 0:2ded56b8407d | 295 | void SSD1308::sendData(uint8_t data){ |
wim | 0:2ded56b8407d | 296 | // I2Cdev::writeByte(m_devAddr, DATA_MODE, data); |
wim | 0:2ded56b8407d | 297 | |
wim | 0:2ded56b8407d | 298 | char databytes[2]; |
wim | 0:2ded56b8407d | 299 | |
wim | 0:2ded56b8407d | 300 | databytes[0] = DATA_MODE; |
wim | 0:2ded56b8407d | 301 | databytes[1] = data; |
wim | 0:2ded56b8407d | 302 | _i2c.write(_writeOpcode, databytes, 2); // Write Data |
wim | 0:2ded56b8407d | 303 | |
wim | 0:2ded56b8407d | 304 | } |
wim | 0:2ded56b8407d | 305 | |
wim | 2:d86478c0f5da | 306 | // Write len bytes from buffer data to display |
wim | 2:d86478c0f5da | 307 | // Start at current cursor location |
wim | 0:2ded56b8407d | 308 | void SSD1308::sendData(uint8_t len, uint8_t* data) { |
wim | 0:2ded56b8407d | 309 | // I2Cdev::writeBytes(m_devAddr, DATA_MODE, len, data); |
wim | 0:2ded56b8407d | 310 | |
wim | 0:2ded56b8407d | 311 | _i2c.start(); |
wim | 0:2ded56b8407d | 312 | _i2c.write(_writeOpcode); |
wim | 0:2ded56b8407d | 313 | _i2c.write(DATA_MODE); |
wim | 1:00053cb70ac5 | 314 | for (int i=0; i<len ; i++) { |
wim | 0:2ded56b8407d | 315 | _i2c.write(data[i]); // Write Data |
wim | 0:2ded56b8407d | 316 | } |
wim | 0:2ded56b8407d | 317 | _i2c.stop(); |
wim | 0:2ded56b8407d | 318 | |
wim | 0:2ded56b8407d | 319 | } |
wim | 0:2ded56b8407d | 320 | |
wim | 1:00053cb70ac5 | 321 | |
wim | 1:00053cb70ac5 | 322 | |
wim | 0:2ded56b8407d | 323 | void SSD1308::setHorizontalAddressingMode(){ |
wim | 0:2ded56b8407d | 324 | setMemoryAddressingMode(HORIZONTAL_ADDRESSING_MODE); |
wim | 0:2ded56b8407d | 325 | } |
wim | 0:2ded56b8407d | 326 | |
wim | 0:2ded56b8407d | 327 | void SSD1308::setVerticalAddressingMode() { |
wim | 0:2ded56b8407d | 328 | setMemoryAddressingMode(VERTICAL_ADDRESSING_MODE); |
wim | 0:2ded56b8407d | 329 | } |
wim | 0:2ded56b8407d | 330 | |
wim | 0:2ded56b8407d | 331 | void SSD1308::setPageAddressingMode(){ |
wim | 0:2ded56b8407d | 332 | setMemoryAddressingMode(PAGE_ADDRESSING_MODE); |
wim | 0:2ded56b8407d | 333 | } |
wim | 0:2ded56b8407d | 334 | |
wim | 0:2ded56b8407d | 335 | void SSD1308::setMemoryAddressingMode(uint8_t mode){ |
wim | 0:2ded56b8407d | 336 | uint8_t cmds[2] = { SET_MEMORY_ADDRESSING_MODE, mode }; |
wim | 0:2ded56b8407d | 337 | sendCommands(2, cmds); |
wim | 0:2ded56b8407d | 338 | } |
wim | 0:2ded56b8407d | 339 | |
wim | 1:00053cb70ac5 | 340 | void SSD1308::setPageAddress(uint8_t start, uint8_t end) { |
wim | 1:00053cb70ac5 | 341 | uint8_t data[3] = { SET_PAGE_ADDRESS, start, end }; |
wim | 1:00053cb70ac5 | 342 | sendCommands(3, data); |
wim | 1:00053cb70ac5 | 343 | } |
wim | 1:00053cb70ac5 | 344 | |
wim | 1:00053cb70ac5 | 345 | void SSD1308::setColumnAddress(uint8_t start, uint8_t end) { |
wim | 1:00053cb70ac5 | 346 | uint8_t data[3] = { SET_COLUMN_ADDRESS, start, end }; |
wim | 1:00053cb70ac5 | 347 | sendCommands(3, data); |
wim | 1:00053cb70ac5 | 348 | } |
wim | 1:00053cb70ac5 | 349 | |
wim | 1:00053cb70ac5 | 350 | |
wim | 2:d86478c0f5da | 351 | // takes one byte, 0x00 (lowest) - 0xFF (highest) |
wim | 2:d86478c0f5da | 352 | void SSD1308::setContrastControl(uint8_t contrast) { |
wim | 2:d86478c0f5da | 353 | // uint8_t cmds[2] = { SET_CONTRAST, contrast }; |
wim | 2:d86478c0f5da | 354 | // sendCommands(2, cmds); |
wim | 2:d86478c0f5da | 355 | |
wim | 2:d86478c0f5da | 356 | sendCommand(SET_CONTRAST, contrast); |
wim | 2:d86478c0f5da | 357 | } |
wim | 2:d86478c0f5da | 358 | |
wim | 2:d86478c0f5da | 359 | // Enable Display |
wim | 2:d86478c0f5da | 360 | // |
wim | 0:2ded56b8407d | 361 | void SSD1308::setDisplayOn() { |
wim | 0:2ded56b8407d | 362 | sendCommand(SET_DISPLAY_POWER_ON); |
wim | 0:2ded56b8407d | 363 | } |
wim | 0:2ded56b8407d | 364 | |
wim | 2:d86478c0f5da | 365 | // Disable Display |
wim | 2:d86478c0f5da | 366 | // |
wim | 0:2ded56b8407d | 367 | void SSD1308::setDisplayOff() { |
wim | 0:2ded56b8407d | 368 | sendCommand(SET_DISPLAY_POWER_OFF); |
wim | 0:2ded56b8407d | 369 | } |
wim | 0:2ded56b8407d | 370 | |
wim | 0:2ded56b8407d | 371 | void SSD1308::setDisplayPower(bool on) { |
wim | 0:2ded56b8407d | 372 | if (on) { |
wim | 0:2ded56b8407d | 373 | setDisplayOn(); |
wim | 0:2ded56b8407d | 374 | } else { |
wim | 0:2ded56b8407d | 375 | setDisplayOff(); |
wim | 0:2ded56b8407d | 376 | } |
wim | 0:2ded56b8407d | 377 | } |
wim | 0:2ded56b8407d | 378 | |
wim | 2:d86478c0f5da | 379 | // White on Black background |
wim | 2:d86478c0f5da | 380 | // |
wim | 1:00053cb70ac5 | 381 | void SSD1308::setDisplayNormal() { |
wim | 1:00053cb70ac5 | 382 | sendCommand(SET_NORMAL_DISPLAY); |
wim | 1:00053cb70ac5 | 383 | } |
wim | 1:00053cb70ac5 | 384 | |
wim | 2:d86478c0f5da | 385 | // Black on White background |
wim | 2:d86478c0f5da | 386 | // |
wim | 1:00053cb70ac5 | 387 | void SSD1308::setDisplayInverse() { |
wim | 1:00053cb70ac5 | 388 | sendCommand(SET_INVERSE_DISPLAY); |
wim | 0:2ded56b8407d | 389 | } |
wim | 0:2ded56b8407d | 390 | |
wim | 1:00053cb70ac5 | 391 | |
wim | 2:d86478c0f5da | 392 | // Display Flip (Left/Right, Up/Down) |
wim | 2:d86478c0f5da | 393 | // |
wim | 2:d86478c0f5da | 394 | void SSD1308::setDisplayFlip(bool left, bool down) { |
wim | 2:d86478c0f5da | 395 | if (left) { |
wim | 2:d86478c0f5da | 396 | // column address 0 is mapped to SEG0 (Reset) |
wim | 2:d86478c0f5da | 397 | sendCommand(SET_SEGMENT_REMAP_0); |
wim | 2:d86478c0f5da | 398 | } |
wim | 2:d86478c0f5da | 399 | else { |
wim | 2:d86478c0f5da | 400 | // column address 127 is mapped to SEG0 |
wim | 2:d86478c0f5da | 401 | sendCommand(SET_SEGMENT_REMAP_127); |
wim | 2:d86478c0f5da | 402 | } |
wim | 2:d86478c0f5da | 403 | |
wim | 2:d86478c0f5da | 404 | if (down) { |
wim | 2:d86478c0f5da | 405 | // Reset mode |
wim | 2:d86478c0f5da | 406 | sendCommand(SET_COMMON_REMAP_0); |
wim | 2:d86478c0f5da | 407 | } |
wim | 2:d86478c0f5da | 408 | else { |
wim | 2:d86478c0f5da | 409 | // Flip Up/Down (Need to rewrite display before H effect shows) |
wim | 2:d86478c0f5da | 410 | sendCommand(SET_COMMON_REMAP_63); |
wim | 2:d86478c0f5da | 411 | } |
wim | 2:d86478c0f5da | 412 | |
wim | 2:d86478c0f5da | 413 | } |
wim | 2:d86478c0f5da | 414 | |
wim | 2:d86478c0f5da | 415 | // Sets Internal Iref |
wim | 2:d86478c0f5da | 416 | // |
wim | 2:d86478c0f5da | 417 | void SSD1308::setInternalIref() { |
wim | 2:d86478c0f5da | 418 | uint8_t cmds[2] = {SET_IREF_SELECTION, INTERNAL_IREF}; |
wim | 2:d86478c0f5da | 419 | sendCommands(2, cmds); |
wim | 2:d86478c0f5da | 420 | } |
wim | 2:d86478c0f5da | 421 | |
wim | 2:d86478c0f5da | 422 | // Sets External Iref |
wim | 2:d86478c0f5da | 423 | // |
wim | 2:d86478c0f5da | 424 | void SSD1308::setExternalIref() { |
wim | 2:d86478c0f5da | 425 | uint8_t cmds[2] = {SET_IREF_SELECTION, EXTERNAL_IREF}; |
wim | 2:d86478c0f5da | 426 | sendCommands(2, cmds); |
wim | 2:d86478c0f5da | 427 | } |
wim | 2:d86478c0f5da | 428 | |
wim | 2:d86478c0f5da | 429 | |
wim | 1:00053cb70ac5 | 430 | // Low level Init |
wim | 1:00053cb70ac5 | 431 | // Init the configuration registers in accordance with the datasheet |
wim | 1:00053cb70ac5 | 432 | // |
wim | 1:00053cb70ac5 | 433 | void SSD1308::_init() { |
wim | 2:d86478c0f5da | 434 | |
wim | 2:d86478c0f5da | 435 | //not complete yet |
wim | 2:d86478c0f5da | 436 | sendCommand(SET_DISPLAY_POWER_OFF); |
wim | 2:d86478c0f5da | 437 | |
wim | 2:d86478c0f5da | 438 | // column address 0 is mapped to SEG0 (Reset) |
wim | 2:d86478c0f5da | 439 | // row address 0 is mapped to COMM0 (Reset) |
wim | 2:d86478c0f5da | 440 | sendCommand(SET_SEGMENT_REMAP_0); |
wim | 2:d86478c0f5da | 441 | sendCommand(SET_COMMON_REMAP_0); |
wim | 2:d86478c0f5da | 442 | |
wim | 2:d86478c0f5da | 443 | uint8_t cmds[2] = { SET_COMMON_CONF, COMMON_BASE | COMMON_ALTERNATIVE | COMMON_LEFTRIGHT_NORMAL}; |
wim | 2:d86478c0f5da | 444 | sendCommands(2, cmds); |
wim | 2:d86478c0f5da | 445 | |
wim | 1:00053cb70ac5 | 446 | setHorizontalAddressingMode(); |
wim | 1:00053cb70ac5 | 447 | |
wim | 2:d86478c0f5da | 448 | setExternalIref(); |
wim | 1:00053cb70ac5 | 449 | |
wim | 1:00053cb70ac5 | 450 | sendCommand(SET_NORMAL_DISPLAY); |
wim | 1:00053cb70ac5 | 451 | |
wim | 2:d86478c0f5da | 452 | clearDisplay(); |
wim | 2:d86478c0f5da | 453 | |
wim | 2:d86478c0f5da | 454 | sendCommand(SET_DISPLAY_POWER_ON); |
wim | 0:2ded56b8407d | 455 | } |
wim | 1:00053cb70ac5 | 456 |