Demonstration of SSD1308 OLED driver library

Dependencies:   mbed SSD1308_128x64_I2C

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?

UserRevisionLine numberNew 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 #include "mbed.h"
wim 0:2ded56b8407d 39 #include "SSD1308.h"
wim 0:2ded56b8407d 40
wim 0:2ded56b8407d 41 #define SSD1308_USE_FONT
wim 0:2ded56b8407d 42 //#ifdef SSD1308_USE_FONT
wim 0:2ded56b8407d 43 #include "FixedWidthFont.h"
wim 0:2ded56b8407d 44 //#endif
wim 0:2ded56b8407d 45
wim 0:2ded56b8407d 46 SSD1308::SSD1308(I2C &i2c, uint8_t deviceAddress) : _i2c(i2c) {
wim 0:2ded56b8407d 47 // m_devAddr = deviceAddress;
wim 0:2ded56b8407d 48
wim 0:2ded56b8407d 49 _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
wim 0:2ded56b8407d 50 _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read
wim 0:2ded56b8407d 51 }
wim 0:2ded56b8407d 52
wim 0:2ded56b8407d 53 void SSD1308::initialize() {
wim 0:2ded56b8407d 54 setHorizontalAddressingMode();
wim 0:2ded56b8407d 55 clearDisplay();
wim 0:2ded56b8407d 56 }
wim 0:2ded56b8407d 57
wim 0:2ded56b8407d 58 void SSD1308::clearDisplay() {
wim 0:2ded56b8407d 59 setDisplayOff();
wim 0:2ded56b8407d 60 setPageAddress(0, MAX_PAGE); // all pages
wim 0:2ded56b8407d 61 setColumnAddress(0, MAX_COL); // all columns
wim 0:2ded56b8407d 62 for (uint8_t page = 0; page < PAGES; page++)
wim 0:2ded56b8407d 63 {
wim 0:2ded56b8407d 64 for (uint8_t col = 0; col < COLUMNS; col++)
wim 0:2ded56b8407d 65 {
wim 0:2ded56b8407d 66 sendData(0x0);
wim 0:2ded56b8407d 67 }
wim 0:2ded56b8407d 68 }
wim 0:2ded56b8407d 69 setDisplayOn();
wim 0:2ded56b8407d 70 }
wim 0:2ded56b8407d 71
wim 0:2ded56b8407d 72 void SSD1308::fillDisplay() {
wim 0:2ded56b8407d 73 setPageAddress(0, MAX_PAGE); // all pages
wim 0:2ded56b8407d 74 setColumnAddress(0, MAX_COL); // all columns
wim 0:2ded56b8407d 75
wim 0:2ded56b8407d 76 uint8_t b = 0;
wim 0:2ded56b8407d 77 for (uint8_t page = 0; page < PAGES; page++)
wim 0:2ded56b8407d 78 {
wim 0:2ded56b8407d 79 for (uint8_t col = 0; col < COLUMNS; col++)
wim 0:2ded56b8407d 80 {
wim 0:2ded56b8407d 81 sendData(b++);
wim 0:2ded56b8407d 82 }
wim 0:2ded56b8407d 83 }
wim 0:2ded56b8407d 84 }
wim 0:2ded56b8407d 85
wim 0:2ded56b8407d 86 void SSD1308::writeChar(char chr) {
wim 0:2ded56b8407d 87 //#ifdef SSD1308_USE_FONT
wim 0:2ded56b8407d 88 const uint8_t char_index = chr - 0x20;
wim 0:2ded56b8407d 89 for (uint8_t i = 0; i < 8; i++) {
wim 0:2ded56b8407d 90 // const uint8_t b = pgm_read_byte( &fontData[char_index][i] );
wim 0:2ded56b8407d 91 const uint8_t b = fontData[char_index][i];
wim 0:2ded56b8407d 92 sendData( b );
wim 0:2ded56b8407d 93 }
wim 0:2ded56b8407d 94 //#endif
wim 0:2ded56b8407d 95 }
wim 0:2ded56b8407d 96
wim 0:2ded56b8407d 97 void SSD1308::writeString(uint8_t row, uint8_t col, uint16_t len, const char * text) {
wim 0:2ded56b8407d 98 uint16_t index = 0;
wim 0:2ded56b8407d 99 setPageAddress(row, MAX_PAGE);
wim 0:2ded56b8407d 100 const uint8_t col_addr = FONT_WIDTH*col;
wim 0:2ded56b8407d 101 setColumnAddress(col_addr, MAX_COL);
wim 0:2ded56b8407d 102
wim 0:2ded56b8407d 103 while ((col+index) < CHARS && (index < len)) {
wim 0:2ded56b8407d 104 // write first line, starting at given position
wim 0:2ded56b8407d 105 writeChar(text[index++]);
wim 0:2ded56b8407d 106 }
wim 0:2ded56b8407d 107
wim 0:2ded56b8407d 108 // write remaining lines
wim 0:2ded56b8407d 109 // write until the end of memory
wim 0:2ded56b8407d 110 // then wrap around again from the top.
wim 0:2ded56b8407d 111 if (index + 1 < len) {
wim 0:2ded56b8407d 112 setPageAddress(row + 1, MAX_PAGE);
wim 0:2ded56b8407d 113 setColumnAddress(0, MAX_COL);
wim 0:2ded56b8407d 114 bool wrapEntireScreen = false;
wim 0:2ded56b8407d 115 while (index + 1 < len) {
wim 0:2ded56b8407d 116 writeChar(text[index++]);
wim 0:2ded56b8407d 117 // if we've written the last character space on the screen,
wim 0:2ded56b8407d 118 // reset the page and column address so that it wraps around from the top again
wim 0:2ded56b8407d 119 if (!wrapEntireScreen && (row*CHARS + col + index) > 127) {
wim 0:2ded56b8407d 120 setPageAddress(0, MAX_PAGE);
wim 0:2ded56b8407d 121 setColumnAddress(0, MAX_COL);
wim 0:2ded56b8407d 122 wrapEntireScreen = true;
wim 0:2ded56b8407d 123 }
wim 0:2ded56b8407d 124 }
wim 0:2ded56b8407d 125 }
wim 0:2ded56b8407d 126 }
wim 0:2ded56b8407d 127
wim 0:2ded56b8407d 128 void SSD1308::sendCommand(uint8_t command) {
wim 0:2ded56b8407d 129 // I2Cdev::writeByte(m_devAddr, COMMAND_MODE, command);
wim 0:2ded56b8407d 130
wim 0:2ded56b8407d 131 char databytes[2];
wim 0:2ded56b8407d 132
wim 0:2ded56b8407d 133 databytes[0] = COMMAND_MODE;
wim 0:2ded56b8407d 134 databytes[1] = command;
wim 0:2ded56b8407d 135 _i2c.write(_writeOpcode, databytes, 2); // Write command
wim 0:2ded56b8407d 136
wim 0:2ded56b8407d 137 }
wim 0:2ded56b8407d 138
wim 0:2ded56b8407d 139 void SSD1308::sendCommands(uint8_t len, uint8_t* commands) {
wim 0:2ded56b8407d 140 // I2Cdev::writeBytes(m_devAddr, COMMAND_MODE, len, commands);
wim 0:2ded56b8407d 141
wim 0:2ded56b8407d 142 int i;
wim 0:2ded56b8407d 143
wim 0:2ded56b8407d 144 _i2c.start();
wim 0:2ded56b8407d 145 _i2c.write(_writeOpcode);
wim 0:2ded56b8407d 146 _i2c.write(COMMAND_MODE);
wim 0:2ded56b8407d 147 for (i=0; i<len ; i++) {
wim 0:2ded56b8407d 148 _i2c.write(commands[i]); // Write Commands
wim 0:2ded56b8407d 149 }
wim 0:2ded56b8407d 150 _i2c.stop();
wim 0:2ded56b8407d 151
wim 0:2ded56b8407d 152 }
wim 0:2ded56b8407d 153
wim 0:2ded56b8407d 154 void SSD1308::sendData(uint8_t data){
wim 0:2ded56b8407d 155 // I2Cdev::writeByte(m_devAddr, DATA_MODE, data);
wim 0:2ded56b8407d 156
wim 0:2ded56b8407d 157 char databytes[2];
wim 0:2ded56b8407d 158
wim 0:2ded56b8407d 159 databytes[0] = DATA_MODE;
wim 0:2ded56b8407d 160 databytes[1] = data;
wim 0:2ded56b8407d 161 _i2c.write(_writeOpcode, databytes, 2); // Write Data
wim 0:2ded56b8407d 162
wim 0:2ded56b8407d 163 }
wim 0:2ded56b8407d 164
wim 0:2ded56b8407d 165 void SSD1308::sendData(uint8_t len, uint8_t* data) {
wim 0:2ded56b8407d 166 // I2Cdev::writeBytes(m_devAddr, DATA_MODE, len, data);
wim 0:2ded56b8407d 167
wim 0:2ded56b8407d 168 int i;
wim 0:2ded56b8407d 169
wim 0:2ded56b8407d 170 _i2c.start();
wim 0:2ded56b8407d 171 _i2c.write(_writeOpcode);
wim 0:2ded56b8407d 172 _i2c.write(DATA_MODE);
wim 0:2ded56b8407d 173 for (i=0; i<len ; i++) {
wim 0:2ded56b8407d 174 _i2c.write(data[i]); // Write Data
wim 0:2ded56b8407d 175 }
wim 0:2ded56b8407d 176 _i2c.stop();
wim 0:2ded56b8407d 177
wim 0:2ded56b8407d 178 }
wim 0:2ded56b8407d 179
wim 0:2ded56b8407d 180 void SSD1308::setHorizontalAddressingMode(){
wim 0:2ded56b8407d 181 setMemoryAddressingMode(HORIZONTAL_ADDRESSING_MODE);
wim 0:2ded56b8407d 182 }
wim 0:2ded56b8407d 183
wim 0:2ded56b8407d 184 void SSD1308::setVerticalAddressingMode() {
wim 0:2ded56b8407d 185 setMemoryAddressingMode(VERTICAL_ADDRESSING_MODE);
wim 0:2ded56b8407d 186 }
wim 0:2ded56b8407d 187
wim 0:2ded56b8407d 188 void SSD1308::setPageAddressingMode(){
wim 0:2ded56b8407d 189 setMemoryAddressingMode(PAGE_ADDRESSING_MODE);
wim 0:2ded56b8407d 190 }
wim 0:2ded56b8407d 191
wim 0:2ded56b8407d 192 void SSD1308::setMemoryAddressingMode(uint8_t mode){
wim 0:2ded56b8407d 193 uint8_t cmds[2] = { SET_MEMORY_ADDRESSING_MODE, mode };
wim 0:2ded56b8407d 194 sendCommands(2, cmds);
wim 0:2ded56b8407d 195 }
wim 0:2ded56b8407d 196
wim 0:2ded56b8407d 197 void SSD1308::setDisplayOn() {
wim 0:2ded56b8407d 198 sendCommand(SET_DISPLAY_POWER_ON);
wim 0:2ded56b8407d 199 }
wim 0:2ded56b8407d 200
wim 0:2ded56b8407d 201 void SSD1308::setDisplayOff() {
wim 0:2ded56b8407d 202 sendCommand(SET_DISPLAY_POWER_OFF);
wim 0:2ded56b8407d 203 }
wim 0:2ded56b8407d 204
wim 0:2ded56b8407d 205 void SSD1308::setDisplayPower(bool on) {
wim 0:2ded56b8407d 206 if (on) {
wim 0:2ded56b8407d 207 setDisplayOn();
wim 0:2ded56b8407d 208 } else {
wim 0:2ded56b8407d 209 setDisplayOff();
wim 0:2ded56b8407d 210 }
wim 0:2ded56b8407d 211 }
wim 0:2ded56b8407d 212
wim 0:2ded56b8407d 213 void SSD1308::setPageAddress(uint8_t start, uint8_t end) {
wim 0:2ded56b8407d 214 uint8_t data[3] = { SET_PAGE_ADDRESS, start, end };
wim 0:2ded56b8407d 215 sendCommands(3, data);
wim 0:2ded56b8407d 216 }
wim 0:2ded56b8407d 217
wim 0:2ded56b8407d 218 void SSD1308::setColumnAddress(uint8_t start, uint8_t end) {
wim 0:2ded56b8407d 219 uint8_t data[3] = { SET_COLUMN_ADDRESS, start, end };
wim 0:2ded56b8407d 220 sendCommands(3, data);
wim 0:2ded56b8407d 221 }