I added some member functions to the OLED library.
SSD1308.cpp@6:881ee0c6dbda, 2019-12-11 (annotated)
- Committer:
- dcj001
- Date:
- Wed Dec 11 17:02:09 2019 +0000
- Revision:
- 6:881ee0c6dbda
- Parent:
- 5:e564cde8e03e
I added some member functions to the OLED library. It's just for this project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 1:b7e8f5139026 | 1 | /** @file SSD1308 I2C device class file |
wim | 1:b7e8f5139026 | 2 | * Based on Solomon Systech SSD1308 datasheet, rev. 1, 10/2008 |
wim | 1:b7e8f5139026 | 3 | * The SSD1308 is used for example in the Seeed 128x64 OLED Display |
wim | 1:b7e8f5139026 | 4 | * http://www.seeedstudio.com/depot/grove-oled-display-12864-p-781.html?cPath=163_167 |
wim | 1:b7e8f5139026 | 5 | */ |
wim | 0:300d08d9b058 | 6 | // The original code by Andrew Schamp is using (and has been submitted as a part of) Jeff Rowberg's I2Cdevlib library, |
wim | 0:300d08d9b058 | 7 | // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib |
wim | 0:300d08d9b058 | 8 | // Some parts also mashed up from Graphic Library for driving monochrome displays based on the PCD8544, |
wim | 0:300d08d9b058 | 9 | // Copyright (c) 2011, Wim De Roeve, who in turn did partial port of code found on |
wim | 0:300d08d9b058 | 10 | // http://serdisplib.sourceforge.net/ser/pcd8544.html#links and by Petras Saduikis <petras@petras.co.uk> |
wim | 0:300d08d9b058 | 11 | // |
wim | 0:300d08d9b058 | 12 | // Changelog: |
wim | 0:300d08d9b058 | 13 | // 2011-08-25 - Initial release by Andrew Schamp <schamp@gmail.com> |
wim | 0:300d08d9b058 | 14 | // 2012-06-19 - Ported to mbed and optimised (WH) |
wim | 4:df92b0c0cb92 | 15 | // 2013-07-12 - Minor comment fix and placeholder for SSD1306 (WH) |
wim | 4:df92b0c0cb92 | 16 | // 2015-01-01 - Switch for optimised I2C calls to test on F401 (WH) |
wim | 5:e564cde8e03e | 17 | // 2017-12-18 - Fixed non-copyable issue (Thx kenjiArai) |
wim | 0:300d08d9b058 | 18 | // |
wim | 4:df92b0c0cb92 | 19 | /* |
wim | 1:b7e8f5139026 | 20 | ================================================================================ |
wim | 0:300d08d9b058 | 21 | I2Cdev device library code is placed under the MIT license |
wim | 0:300d08d9b058 | 22 | Copyright (c) 2011 Andrew Schamp |
wim | 5:e564cde8e03e | 23 | Copyright (c) 2012,2013,2017 WH (mbed port) |
wim | 0:300d08d9b058 | 24 | |
wim | 0:300d08d9b058 | 25 | Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:300d08d9b058 | 26 | of this software and associated documentation files (the "Software"), to deal |
wim | 0:300d08d9b058 | 27 | in the Software without restriction, including without limitation the rights |
wim | 0:300d08d9b058 | 28 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:300d08d9b058 | 29 | copies of the Software, and to permit persons to whom the Software is |
wim | 0:300d08d9b058 | 30 | furnished to do so, subject to the following conditions: |
wim | 0:300d08d9b058 | 31 | |
wim | 0:300d08d9b058 | 32 | The above copyright notice and this permission notice shall be included in |
wim | 0:300d08d9b058 | 33 | all copies or substantial portions of the Software. |
wim | 0:300d08d9b058 | 34 | |
wim | 0:300d08d9b058 | 35 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:300d08d9b058 | 36 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:300d08d9b058 | 37 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:300d08d9b058 | 38 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:300d08d9b058 | 39 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:300d08d9b058 | 40 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:300d08d9b058 | 41 | THE SOFTWARE. |
wim | 1:b7e8f5139026 | 42 | ================================================================================ |
wim | 0:300d08d9b058 | 43 | */ |
wim | 0:300d08d9b058 | 44 | #include "mbed.h" |
wim | 0:300d08d9b058 | 45 | #include "SSD1308.h" |
dcj001 | 6:881ee0c6dbda | 46 | #include "logo.h" |
wim | 0:300d08d9b058 | 47 | |
wim | 0:300d08d9b058 | 48 | //#include "font_3x5.h" |
wim | 0:300d08d9b058 | 49 | //#include "font_5x7.h" |
wim | 0:300d08d9b058 | 50 | //#include "font_6x8.h" |
wim | 0:300d08d9b058 | 51 | #include "font_8x8.h" |
wim | 0:300d08d9b058 | 52 | //#include "font_8x12.h" |
wim | 0:300d08d9b058 | 53 | //#include "font_16x20.h" |
wim | 0:300d08d9b058 | 54 | #include "font_16x24.h" |
wim | 0:300d08d9b058 | 55 | |
wim | 4:df92b0c0cb92 | 56 | #if defined(TARGET_LPC1768) |
wim | 4:df92b0c0cb92 | 57 | #define I2C_OPTIMIZE 1 |
wim | 4:df92b0c0cb92 | 58 | #else |
wim | 4:df92b0c0cb92 | 59 | #define I2C_OPTIMIZE 0 |
wim | 4:df92b0c0cb92 | 60 | #endif |
wim | 4:df92b0c0cb92 | 61 | |
wim | 1:b7e8f5139026 | 62 | /** |
wim | 1:b7e8f5139026 | 63 | *@brief Constructor |
wim | 5:e564cde8e03e | 64 | *@param I2C *i2c reference to i2c |
wim | 1:b7e8f5139026 | 65 | *@param uint8_t deviceAddress slaveaddress |
wim | 1:b7e8f5139026 | 66 | */ |
wim | 5:e564cde8e03e | 67 | SSD1308::SSD1308(I2C *i2c, uint8_t deviceAddress) : _i2c(i2c) { |
wim | 0:300d08d9b058 | 68 | |
wim | 0:300d08d9b058 | 69 | _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write |
wim | 0:300d08d9b058 | 70 | _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read |
wim | 0:300d08d9b058 | 71 | |
wim | 0:300d08d9b058 | 72 | initialize(); |
wim | 0:300d08d9b058 | 73 | } |
wim | 0:300d08d9b058 | 74 | |
wim | 1:b7e8f5139026 | 75 | /** @brief High level Init, most settings remain at Power-On reset value |
wim | 1:b7e8f5139026 | 76 | */ |
wim | 0:300d08d9b058 | 77 | void SSD1308::initialize() { |
wim | 0:300d08d9b058 | 78 | setHorizontalAddressingMode(); |
wim | 0:300d08d9b058 | 79 | |
wim | 0:300d08d9b058 | 80 | clearDisplay(); |
wim | 0:300d08d9b058 | 81 | |
wim | 0:300d08d9b058 | 82 | setInverted(false); |
wim | 0:300d08d9b058 | 83 | |
wim | 0:300d08d9b058 | 84 | setDisplayOn(); |
wim | 0:300d08d9b058 | 85 | } |
wim | 0:300d08d9b058 | 86 | |
wim | 0:300d08d9b058 | 87 | |
wim | 1:b7e8f5139026 | 88 | /** @brief clear the display |
wim | 1:b7e8f5139026 | 89 | */ |
wim | 4:df92b0c0cb92 | 90 | #if (I2C_OPTIMIZE == 0) |
wim | 0:300d08d9b058 | 91 | // Standard version |
wim | 0:300d08d9b058 | 92 | void SSD1308::clearDisplay() { |
wim | 0:300d08d9b058 | 93 | |
wim | 0:300d08d9b058 | 94 | //setDisplayOff(); |
wim | 0:300d08d9b058 | 95 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 0:300d08d9b058 | 96 | setColumnAddress(0, MAX_COL); // all columns |
wim | 0:300d08d9b058 | 97 | |
wim | 0:300d08d9b058 | 98 | for (uint8_t page = 0; page < PAGES; page++) { |
wim | 0:300d08d9b058 | 99 | for (uint8_t col = 0; col < COLUMNS; col++) { |
wim | 0:300d08d9b058 | 100 | _sendData(0x00); |
wim | 0:300d08d9b058 | 101 | } |
wim | 0:300d08d9b058 | 102 | } |
wim | 0:300d08d9b058 | 103 | |
wim | 0:300d08d9b058 | 104 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 105 | } |
wim | 0:300d08d9b058 | 106 | #else |
wim | 0:300d08d9b058 | 107 | //Optimised version |
wim | 0:300d08d9b058 | 108 | // Save lots of I2C S,P, address and datacommands: |
wim | 0:300d08d9b058 | 109 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 0:300d08d9b058 | 110 | // |
wim | 0:300d08d9b058 | 111 | void SSD1308::clearDisplay() { |
wim | 0:300d08d9b058 | 112 | |
wim | 0:300d08d9b058 | 113 | //setDisplayOff(); |
wim | 0:300d08d9b058 | 114 | |
wim | 0:300d08d9b058 | 115 | setPageAddress(0, MAX_PAGE); // all pages |
wim | 0:300d08d9b058 | 116 | setColumnAddress(0, MAX_COL); // all columns |
wim | 0:300d08d9b058 | 117 | |
wim | 5:e564cde8e03e | 118 | _i2c->start(); |
wim | 5:e564cde8e03e | 119 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 120 | _i2c->write(DATA_MODE); |
wim | 0:300d08d9b058 | 121 | for (int i=0; i<(PAGES * COLUMNS); i++) { |
wim | 5:e564cde8e03e | 122 | _i2c->write(0x00); // Write Data |
wim | 0:300d08d9b058 | 123 | } |
wim | 5:e564cde8e03e | 124 | _i2c->stop(); |
wim | 0:300d08d9b058 | 125 | |
wim | 0:300d08d9b058 | 126 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 127 | } |
wim | 0:300d08d9b058 | 128 | #endif |
wim | 0:300d08d9b058 | 129 | |
wim | 0:300d08d9b058 | 130 | |
wim | 1:b7e8f5139026 | 131 | /** @brief fill the display |
wim | 1:b7e8f5139026 | 132 | * @param uint8_t pattern fillpattern vertical patch or 8 bits |
wim | 4:df92b0c0cb92 | 133 | * @param uint8_t start_page begin page (0..MAX_PAGE) |
wim | 4:df92b0c0cb92 | 134 | * @param uint8_t end_page end page (start_page..MAX_PAGE) |
wim | 4:df92b0c0cb92 | 135 | * @param uint8_t start_col begin column (0..MAX_COL) |
wim | 4:df92b0c0cb92 | 136 | * @param uint8_t end_col end column (start_col..MAX_COL) |
wim | 1:b7e8f5139026 | 137 | */ |
wim | 4:df92b0c0cb92 | 138 | #if (I2C_OPTIMIZE == 0) |
wim | 0:300d08d9b058 | 139 | //Standard version |
wim | 4:df92b0c0cb92 | 140 | |
wim | 4:df92b0c0cb92 | 141 | void SSD1308::fillDisplay(uint8_t pattern, |
wim | 4:df92b0c0cb92 | 142 | uint8_t start_page, uint8_t end_page, |
wim | 4:df92b0c0cb92 | 143 | uint8_t start_col, uint8_t end_col) { |
wim | 0:300d08d9b058 | 144 | |
wim | 4:df92b0c0cb92 | 145 | int count = (end_page - start_page + 1) * (end_col - start_col + 1); |
wim | 4:df92b0c0cb92 | 146 | |
wim | 4:df92b0c0cb92 | 147 | //setDisplayOff(); |
wim | 4:df92b0c0cb92 | 148 | setPageAddress(start_page, end_page); // set page window |
wim | 4:df92b0c0cb92 | 149 | setColumnAddress(start_col, end_col); // set column window |
wim | 4:df92b0c0cb92 | 150 | |
wim | 4:df92b0c0cb92 | 151 | for (int i=0; i<count; i++) { |
wim | 4:df92b0c0cb92 | 152 | _sendData(pattern); // Write Data |
wim | 4:df92b0c0cb92 | 153 | } |
wim | 0:300d08d9b058 | 154 | |
wim | 4:df92b0c0cb92 | 155 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 156 | } |
wim | 4:df92b0c0cb92 | 157 | |
wim | 0:300d08d9b058 | 158 | #else |
wim | 0:300d08d9b058 | 159 | |
wim | 0:300d08d9b058 | 160 | //Optimised version |
wim | 0:300d08d9b058 | 161 | // Save lots of I2C S,P, address and datacommands: |
wim | 0:300d08d9b058 | 162 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 0:300d08d9b058 | 163 | // |
wim | 0:300d08d9b058 | 164 | void SSD1308::fillDisplay(uint8_t pattern, |
wim | 0:300d08d9b058 | 165 | uint8_t start_page, uint8_t end_page, |
wim | 0:300d08d9b058 | 166 | uint8_t start_col, uint8_t end_col) { |
wim | 0:300d08d9b058 | 167 | |
wim | 0:300d08d9b058 | 168 | int count = (end_page - start_page + 1) * (end_col - start_col + 1); |
wim | 0:300d08d9b058 | 169 | |
wim | 0:300d08d9b058 | 170 | //setDisplayOff(); |
wim | 0:300d08d9b058 | 171 | setPageAddress(start_page, end_page); // set page window |
wim | 0:300d08d9b058 | 172 | setColumnAddress(start_col, end_col); // set column window |
wim | 0:300d08d9b058 | 173 | |
wim | 5:e564cde8e03e | 174 | _i2c->start(); |
wim | 5:e564cde8e03e | 175 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 176 | _i2c->write(DATA_MODE); |
wim | 0:300d08d9b058 | 177 | for (int i=0; i<count; i++) { |
wim | 5:e564cde8e03e | 178 | _i2c->write(pattern); // Write Data |
wim | 0:300d08d9b058 | 179 | } |
wim | 5:e564cde8e03e | 180 | _i2c->stop(); |
wim | 0:300d08d9b058 | 181 | |
wim | 0:300d08d9b058 | 182 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 183 | } |
wim | 0:300d08d9b058 | 184 | |
wim | 0:300d08d9b058 | 185 | #endif |
wim | 0:300d08d9b058 | 186 | |
wim | 0:300d08d9b058 | 187 | |
wim | 1:b7e8f5139026 | 188 | /** @brief write a bitmap to the display |
wim | 1:b7e8f5139026 | 189 | * @param uint8_t* data pointer to bitmap |
wim | 1:b7e8f5139026 | 190 | * @param uint8_t start_page begin page (0..MAX_PAGE) |
wim | 1:b7e8f5139026 | 191 | * @param uint8_t end_page end page (start_page..MAX_PAGE) |
wim | 1:b7e8f5139026 | 192 | * @param uint8_t start_col begin column (0..MAX_COL) |
wim | 1:b7e8f5139026 | 193 | * @param uint8_t end_col end column (start_col..MAX_COL) |
wim | 1:b7e8f5139026 | 194 | */ |
wim | 4:df92b0c0cb92 | 195 | #if (I2C_OPTIMIZE == 0) |
wim | 0:300d08d9b058 | 196 | //Standard version |
wim | 4:df92b0c0cb92 | 197 | void SSD1308::writeBitmap(uint8_t* data, |
wim | 4:df92b0c0cb92 | 198 | uint8_t start_page, uint8_t end_page, |
wim | 4:df92b0c0cb92 | 199 | uint8_t start_col, uint8_t end_col){ |
wim | 4:df92b0c0cb92 | 200 | |
wim | 4:df92b0c0cb92 | 201 | int count = (end_page - start_page + 1) * (end_col - start_col + 1); |
wim | 0:300d08d9b058 | 202 | |
wim | 0:300d08d9b058 | 203 | //setDisplayOff(); |
wim | 4:df92b0c0cb92 | 204 | setPageAddress(start_page, end_page); // set page window |
wim | 4:df92b0c0cb92 | 205 | setColumnAddress(start_col, end_col); // set column window |
wim | 0:300d08d9b058 | 206 | |
wim | 4:df92b0c0cb92 | 207 | for (int i=0; i<count; i++) { |
wim | 4:df92b0c0cb92 | 208 | _sendData(data[i]); // Write Data |
wim | 0:300d08d9b058 | 209 | } |
wim | 0:300d08d9b058 | 210 | |
wim | 0:300d08d9b058 | 211 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 212 | } |
wim | 4:df92b0c0cb92 | 213 | |
wim | 0:300d08d9b058 | 214 | #else |
wim | 0:300d08d9b058 | 215 | //Optimised version |
wim | 0:300d08d9b058 | 216 | // Save lots of I2C S,P, address and datacommands: |
wim | 0:300d08d9b058 | 217 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 0:300d08d9b058 | 218 | // |
wim | 0:300d08d9b058 | 219 | void SSD1308::writeBitmap(uint8_t* data, |
wim | 0:300d08d9b058 | 220 | uint8_t start_page, uint8_t end_page, |
wim | 0:300d08d9b058 | 221 | uint8_t start_col, uint8_t end_col){ |
wim | 0:300d08d9b058 | 222 | |
wim | 0:300d08d9b058 | 223 | int count = (end_page - start_page + 1) * (end_col - start_col + 1); |
wim | 0:300d08d9b058 | 224 | |
wim | 0:300d08d9b058 | 225 | //setDisplayOff(); |
wim | 0:300d08d9b058 | 226 | setPageAddress(start_page, end_page); // set page window |
wim | 0:300d08d9b058 | 227 | setColumnAddress(start_col, end_col); // set column window |
wim | 0:300d08d9b058 | 228 | |
wim | 5:e564cde8e03e | 229 | _i2c->start(); |
wim | 5:e564cde8e03e | 230 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 231 | _i2c->write(DATA_MODE); |
wim | 0:300d08d9b058 | 232 | for (int i=0; i<count; i++) { |
wim | 5:e564cde8e03e | 233 | _i2c->write(data[i]); // Write Data |
wim | 0:300d08d9b058 | 234 | } |
wim | 5:e564cde8e03e | 235 | _i2c->stop(); |
wim | 0:300d08d9b058 | 236 | |
wim | 0:300d08d9b058 | 237 | //setDisplayOn(); |
wim | 0:300d08d9b058 | 238 | } |
wim | 4:df92b0c0cb92 | 239 | |
wim | 0:300d08d9b058 | 240 | #endif |
wim | 0:300d08d9b058 | 241 | |
wim | 0:300d08d9b058 | 242 | |
wim | 2:16c84a134393 | 243 | /** @brief write a progressbar to the display, Width is (PRG_MAX_SCALE + 2) pixels |
wim | 1:b7e8f5139026 | 244 | * @param uint8_t page begin page (0..MAX_PAGE) |
wim | 1:b7e8f5139026 | 245 | * @param uint8_t col begin column (0..MAX_COL) |
wim | 1:b7e8f5139026 | 246 | * @param int percentage value (0..100) |
wim | 1:b7e8f5139026 | 247 | */ |
wim | 1:b7e8f5139026 | 248 | #define PRG_MAX_SCALE 50 |
wim | 1:b7e8f5139026 | 249 | #define PRG_LEFT_EDGE 0xFF |
wim | 1:b7e8f5139026 | 250 | #define PRG_RIGHT_EDGE 0xFF |
wim | 1:b7e8f5139026 | 251 | #define PRG_ACTIVE 0xFF |
wim | 2:16c84a134393 | 252 | //#define PRG_ACTIVE 0xBD |
wim | 1:b7e8f5139026 | 253 | #define PRG_NOT_ACTIVE 0x81 |
wim | 1:b7e8f5139026 | 254 | |
wim | 4:df92b0c0cb92 | 255 | #if (I2C_OPTIMIZE == 0) |
wim | 1:b7e8f5139026 | 256 | //Standard version |
wim | 1:b7e8f5139026 | 257 | void SSD1308::writeProgressBar(uint8_t page, uint8_t col, int percentage) { |
wim | 1:b7e8f5139026 | 258 | uint8_t scale_value; |
wim | 1:b7e8f5139026 | 259 | |
wim | 2:16c84a134393 | 260 | if (percentage <= 0) { |
wim | 1:b7e8f5139026 | 261 | scale_value = 0; |
wim | 2:16c84a134393 | 262 | } else if (percentage >= 100) { |
wim | 2:16c84a134393 | 263 | scale_value = PRG_MAX_SCALE - 1; |
wim | 1:b7e8f5139026 | 264 | } |
wim | 1:b7e8f5139026 | 265 | else { |
wim | 1:b7e8f5139026 | 266 | scale_value = (percentage * PRG_MAX_SCALE) / 100; |
wim | 1:b7e8f5139026 | 267 | } |
wim | 1:b7e8f5139026 | 268 | |
wim | 1:b7e8f5139026 | 269 | //setDisplayOff(); |
wim | 1:b7e8f5139026 | 270 | setPageAddress(page, page); |
wim | 1:b7e8f5139026 | 271 | setColumnAddress(col, MAX_COL); |
wim | 1:b7e8f5139026 | 272 | |
wim | 1:b7e8f5139026 | 273 | _sendData(PRG_LEFT_EDGE); |
wim | 1:b7e8f5139026 | 274 | |
wim | 1:b7e8f5139026 | 275 | for (uint8_t col = 0; col < scale_value; col++) { |
wim | 1:b7e8f5139026 | 276 | _sendData(PRG_ACTIVE); |
wim | 1:b7e8f5139026 | 277 | } |
wim | 2:16c84a134393 | 278 | |
wim | 2:16c84a134393 | 279 | _sendData(PRG_ACTIVE); |
wim | 2:16c84a134393 | 280 | |
wim | 2:16c84a134393 | 281 | for (uint8_t col = (scale_value+1); col < PRG_MAX_SCALE; col++) { |
wim | 1:b7e8f5139026 | 282 | _sendData(PRG_NOT_ACTIVE); |
wim | 1:b7e8f5139026 | 283 | } |
wim | 1:b7e8f5139026 | 284 | |
wim | 1:b7e8f5139026 | 285 | _sendData(PRG_RIGHT_EDGE); |
wim | 1:b7e8f5139026 | 286 | |
wim | 1:b7e8f5139026 | 287 | //setDisplayOn(); |
wim | 1:b7e8f5139026 | 288 | } |
wim | 1:b7e8f5139026 | 289 | #else |
wim | 1:b7e8f5139026 | 290 | |
wim | 1:b7e8f5139026 | 291 | //Optimised version |
wim | 1:b7e8f5139026 | 292 | // Save lots of I2C S,P, address and datacommands: |
wim | 1:b7e8f5139026 | 293 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 0:300d08d9b058 | 294 | // |
wim | 1:b7e8f5139026 | 295 | void SSD1308::writeProgressBar(uint8_t page, uint8_t col, int percentage) { |
wim | 1:b7e8f5139026 | 296 | uint8_t scale_value; |
wim | 1:b7e8f5139026 | 297 | |
wim | 2:16c84a134393 | 298 | if (percentage <= 0) { |
wim | 1:b7e8f5139026 | 299 | scale_value = 0; |
wim | 2:16c84a134393 | 300 | } else if (percentage >= 100) { |
wim | 2:16c84a134393 | 301 | scale_value = PRG_MAX_SCALE - 1 ; |
wim | 1:b7e8f5139026 | 302 | } |
wim | 1:b7e8f5139026 | 303 | else { |
wim | 1:b7e8f5139026 | 304 | scale_value = (percentage * PRG_MAX_SCALE) / 100; |
wim | 1:b7e8f5139026 | 305 | } |
wim | 1:b7e8f5139026 | 306 | |
wim | 1:b7e8f5139026 | 307 | //setDisplayOff(); |
wim | 1:b7e8f5139026 | 308 | setPageAddress(page, page); |
wim | 1:b7e8f5139026 | 309 | setColumnAddress(col, MAX_COL); |
wim | 1:b7e8f5139026 | 310 | |
wim | 5:e564cde8e03e | 311 | _i2c->start(); |
wim | 5:e564cde8e03e | 312 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 313 | _i2c->write(DATA_MODE); |
wim | 1:b7e8f5139026 | 314 | |
wim | 5:e564cde8e03e | 315 | _i2c->write(PRG_LEFT_EDGE); // Write Data |
wim | 1:b7e8f5139026 | 316 | |
wim | 1:b7e8f5139026 | 317 | for (uint8_t col = 0; col < scale_value; col++) { |
wim | 5:e564cde8e03e | 318 | _i2c->write(PRG_ACTIVE); // Write Data |
wim | 1:b7e8f5139026 | 319 | } |
wim | 1:b7e8f5139026 | 320 | |
wim | 5:e564cde8e03e | 321 | _i2c->write(PRG_ACTIVE); // Write Data |
wim | 2:16c84a134393 | 322 | |
wim | 2:16c84a134393 | 323 | for (uint8_t col = (scale_value+1); col < PRG_MAX_SCALE; col++) { |
wim | 5:e564cde8e03e | 324 | _i2c->write(PRG_NOT_ACTIVE); // Write Data |
wim | 1:b7e8f5139026 | 325 | } |
wim | 1:b7e8f5139026 | 326 | |
wim | 5:e564cde8e03e | 327 | _i2c->write(PRG_RIGHT_EDGE); // Write Data |
wim | 1:b7e8f5139026 | 328 | |
wim | 5:e564cde8e03e | 329 | _i2c->stop(); |
wim | 1:b7e8f5139026 | 330 | |
wim | 1:b7e8f5139026 | 331 | //setDisplayOn(); |
wim | 1:b7e8f5139026 | 332 | } |
wim | 1:b7e8f5139026 | 333 | #endif |
wim | 1:b7e8f5139026 | 334 | |
wim | 2:16c84a134393 | 335 | /** @brief write a level meter to the display, Width is (PRG_MAX_SCALE + 2) pixels |
wim | 2:16c84a134393 | 336 | * @param uint8_t page begin page (0..MAX_PAGE) |
wim | 2:16c84a134393 | 337 | * @param uint8_t col begin column (0..MAX_COL) |
wim | 2:16c84a134393 | 338 | * @param int percentage value (0..100) |
wim | 2:16c84a134393 | 339 | */ |
wim | 4:df92b0c0cb92 | 340 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 341 | void SSD1308::writeLevelBar(uint8_t page, uint8_t col, int percentage) { |
wim | 4:df92b0c0cb92 | 342 | uint8_t scale_value; |
wim | 4:df92b0c0cb92 | 343 | |
wim | 4:df92b0c0cb92 | 344 | if (percentage <= 0) { |
wim | 4:df92b0c0cb92 | 345 | scale_value = 0; |
wim | 4:df92b0c0cb92 | 346 | } else if (percentage >= 100) { |
wim | 4:df92b0c0cb92 | 347 | scale_value = PRG_MAX_SCALE - 1; |
wim | 4:df92b0c0cb92 | 348 | } |
wim | 4:df92b0c0cb92 | 349 | else { |
wim | 4:df92b0c0cb92 | 350 | scale_value = (percentage * PRG_MAX_SCALE) / 100; |
wim | 4:df92b0c0cb92 | 351 | } |
wim | 4:df92b0c0cb92 | 352 | |
wim | 4:df92b0c0cb92 | 353 | //setDisplayOff(); |
wim | 4:df92b0c0cb92 | 354 | setPageAddress(page, page); |
wim | 4:df92b0c0cb92 | 355 | setColumnAddress(col, MAX_COL); |
wim | 4:df92b0c0cb92 | 356 | |
wim | 4:df92b0c0cb92 | 357 | _sendData(PRG_LEFT_EDGE); |
wim | 4:df92b0c0cb92 | 358 | |
wim | 4:df92b0c0cb92 | 359 | for (uint8_t col = 0; col < scale_value; col++) { |
wim | 4:df92b0c0cb92 | 360 | _sendData(PRG_NOT_ACTIVE); // Write Data |
wim | 4:df92b0c0cb92 | 361 | } |
wim | 4:df92b0c0cb92 | 362 | |
wim | 4:df92b0c0cb92 | 363 | _sendData(PRG_ACTIVE); // Write Data at active meterlevel |
wim | 4:df92b0c0cb92 | 364 | |
wim | 4:df92b0c0cb92 | 365 | for (uint8_t col = scale_value+1; col < PRG_MAX_SCALE; col++) { |
wim | 4:df92b0c0cb92 | 366 | _sendData(PRG_NOT_ACTIVE); |
wim | 4:df92b0c0cb92 | 367 | } |
wim | 4:df92b0c0cb92 | 368 | |
wim | 4:df92b0c0cb92 | 369 | _sendData(PRG_RIGHT_EDGE); |
wim | 4:df92b0c0cb92 | 370 | |
wim | 4:df92b0c0cb92 | 371 | //setDisplayOn(); |
wim | 4:df92b0c0cb92 | 372 | } |
wim | 4:df92b0c0cb92 | 373 | #else |
wim | 2:16c84a134393 | 374 | //Optimised version |
wim | 2:16c84a134393 | 375 | // Save lots of I2C S,P, address and datacommands: |
wim | 2:16c84a134393 | 376 | // Send S, address, DATA_MODE, data, data, data,...., P |
wim | 2:16c84a134393 | 377 | // |
wim | 2:16c84a134393 | 378 | void SSD1308::writeLevelBar(uint8_t page, uint8_t col, int percentage) { |
wim | 2:16c84a134393 | 379 | uint8_t scale_value; |
wim | 2:16c84a134393 | 380 | |
wim | 2:16c84a134393 | 381 | if (percentage <= 0) { |
wim | 2:16c84a134393 | 382 | scale_value = 0; |
wim | 2:16c84a134393 | 383 | } else if (percentage >= 100) { |
wim | 2:16c84a134393 | 384 | scale_value = PRG_MAX_SCALE - 1; |
wim | 2:16c84a134393 | 385 | } |
wim | 2:16c84a134393 | 386 | else { |
wim | 2:16c84a134393 | 387 | scale_value = (percentage * PRG_MAX_SCALE) / 100; |
wim | 2:16c84a134393 | 388 | } |
wim | 2:16c84a134393 | 389 | |
wim | 2:16c84a134393 | 390 | //setDisplayOff(); |
wim | 2:16c84a134393 | 391 | setPageAddress(page, page); |
wim | 2:16c84a134393 | 392 | setColumnAddress(col, MAX_COL); |
wim | 1:b7e8f5139026 | 393 | |
wim | 5:e564cde8e03e | 394 | _i2c->start(); |
wim | 5:e564cde8e03e | 395 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 396 | _i2c->write(DATA_MODE); |
wim | 1:b7e8f5139026 | 397 | |
wim | 5:e564cde8e03e | 398 | _i2c->write(PRG_LEFT_EDGE); // Write Data |
wim | 2:16c84a134393 | 399 | |
wim | 2:16c84a134393 | 400 | for (uint8_t col = 0; col < scale_value; col++) { |
wim | 5:e564cde8e03e | 401 | _i2c->write(PRG_NOT_ACTIVE); // Write Data |
wim | 2:16c84a134393 | 402 | } |
wim | 2:16c84a134393 | 403 | |
wim | 5:e564cde8e03e | 404 | _i2c->write(PRG_ACTIVE); // Write Data at active meterlevel |
wim | 2:16c84a134393 | 405 | |
wim | 2:16c84a134393 | 406 | for (uint8_t col = scale_value+1; col < PRG_MAX_SCALE; col++) { |
wim | 5:e564cde8e03e | 407 | _i2c->write(PRG_NOT_ACTIVE); // Write Data |
wim | 2:16c84a134393 | 408 | } |
wim | 2:16c84a134393 | 409 | |
wim | 5:e564cde8e03e | 410 | _i2c->write(PRG_RIGHT_EDGE); // Write Data |
wim | 2:16c84a134393 | 411 | |
wim | 5:e564cde8e03e | 412 | _i2c->stop(); |
wim | 2:16c84a134393 | 413 | |
wim | 2:16c84a134393 | 414 | //setDisplayOn(); |
wim | 2:16c84a134393 | 415 | } |
wim | 4:df92b0c0cb92 | 416 | #endif |
wim | 1:b7e8f5139026 | 417 | |
wim | 1:b7e8f5139026 | 418 | /** @brief Write single character to the display using the 8x8 fontable |
wim | 1:b7e8f5139026 | 419 | * @brief Start at current cursor location |
wim | 1:b7e8f5139026 | 420 | * @param char chr character to write |
wim | 1:b7e8f5139026 | 421 | */ |
wim | 0:300d08d9b058 | 422 | void SSD1308::writeChar(char chr) { |
wim | 0:300d08d9b058 | 423 | |
wim | 0:300d08d9b058 | 424 | const uint8_t char_index = chr - 0x20; |
wim | 0:300d08d9b058 | 425 | |
wim | 0:300d08d9b058 | 426 | for (uint8_t i = 0; i < 8; i++) { |
wim | 0:300d08d9b058 | 427 | if (_inverted) { |
wim | 0:300d08d9b058 | 428 | _sendData( ~font_8x8[char_index][i] ); |
wim | 0:300d08d9b058 | 429 | } |
wim | 0:300d08d9b058 | 430 | else { |
wim | 0:300d08d9b058 | 431 | _sendData( font_8x8[char_index][i] ); |
wim | 0:300d08d9b058 | 432 | } |
wim | 0:300d08d9b058 | 433 | } |
wim | 0:300d08d9b058 | 434 | |
wim | 0:300d08d9b058 | 435 | } |
wim | 0:300d08d9b058 | 436 | |
wim | 0:300d08d9b058 | 437 | |
wim | 1:b7e8f5139026 | 438 | /** @brief Write a string to the display using the 8x8 font |
wim | 2:16c84a134393 | 439 | * @brief Start at selected cursor location, text will wrap around until it is done |
wim | 1:b7e8f5139026 | 440 | * @param uint8_t row row number (0...ROWS/FONT_HEIGHT) |
wim | 1:b7e8f5139026 | 441 | * @param uint8_t col column number (0...COLUMNS/FONT_WIDTH) |
wim | 1:b7e8f5139026 | 442 | * @param const char * text pointer to text |
wim | 1:b7e8f5139026 | 443 | */ |
wim | 2:16c84a134393 | 444 | void SSD1308::writeString(uint8_t row, uint8_t col, const char * text) { |
wim | 0:300d08d9b058 | 445 | uint16_t index = 0; |
wim | 2:16c84a134393 | 446 | uint16_t len = strlen(text); |
wim | 2:16c84a134393 | 447 | |
wim | 0:300d08d9b058 | 448 | setPageAddress(row, MAX_PAGE); |
wim | 0:300d08d9b058 | 449 | const uint8_t col_addr = FONT8x8_WIDTH*col; |
wim | 0:300d08d9b058 | 450 | setColumnAddress(col_addr, MAX_COL); |
wim | 0:300d08d9b058 | 451 | |
wim | 0:300d08d9b058 | 452 | while ((col+index) < CHARS && (index < len)) { |
wim | 0:300d08d9b058 | 453 | // write first line, starting at given position |
wim | 0:300d08d9b058 | 454 | writeChar(text[index++]); |
wim | 0:300d08d9b058 | 455 | } |
wim | 0:300d08d9b058 | 456 | |
wim | 0:300d08d9b058 | 457 | // write remaining lines |
wim | 0:300d08d9b058 | 458 | // write until the end of memory |
wim | 0:300d08d9b058 | 459 | // then wrap around again from the top. |
wim | 0:300d08d9b058 | 460 | if (index + 1 < len) { |
wim | 0:300d08d9b058 | 461 | setPageAddress(row + 1, MAX_PAGE); |
wim | 0:300d08d9b058 | 462 | setColumnAddress(0, MAX_COL); |
wim | 0:300d08d9b058 | 463 | bool wrapEntireScreen = false; |
wim | 0:300d08d9b058 | 464 | while (index + 1 < len) { |
wim | 0:300d08d9b058 | 465 | writeChar(text[index++]); |
wim | 0:300d08d9b058 | 466 | // if we've written the last character space on the screen, |
wim | 0:300d08d9b058 | 467 | // reset the page and column address so that it wraps around from the top again |
wim | 0:300d08d9b058 | 468 | if (!wrapEntireScreen && (row*CHARS + col + index) > 127) { |
wim | 0:300d08d9b058 | 469 | setPageAddress(0, MAX_PAGE); |
wim | 0:300d08d9b058 | 470 | setColumnAddress(0, MAX_COL); |
wim | 0:300d08d9b058 | 471 | wrapEntireScreen = true; |
wim | 0:300d08d9b058 | 472 | } |
wim | 0:300d08d9b058 | 473 | } |
wim | 0:300d08d9b058 | 474 | } |
wim | 0:300d08d9b058 | 475 | } |
wim | 0:300d08d9b058 | 476 | |
wim | 0:300d08d9b058 | 477 | |
wim | 0:300d08d9b058 | 478 | |
wim | 1:b7e8f5139026 | 479 | /** @brief Write large character (16x24 font) |
wim | 1:b7e8f5139026 | 480 | * @param uint8_t row row number (0...MAX_ROW) |
wim | 1:b7e8f5139026 | 481 | * @param uint8_t col column number (0...MAX_COL) |
wim | 1:b7e8f5139026 | 482 | * @param char chr Used for displaying numbers 0 - 9 and '+', '-', '.' |
wim | 1:b7e8f5139026 | 483 | */ |
wim | 0:300d08d9b058 | 484 | void SSD1308::writeBigChar(uint8_t row, uint8_t col, char chr) { |
wim | 0:300d08d9b058 | 485 | |
wim | 0:300d08d9b058 | 486 | writeBitmap((uint8_t*) font_16x24[int(chr) - FONT16x24_START], |
wim | 0:300d08d9b058 | 487 | row, (row + FONT16x24_BYTES - 1), |
wim | 0:300d08d9b058 | 488 | col, (col + FONT16x24_WIDTH - 1)); |
wim | 0:300d08d9b058 | 489 | } |
wim | 0:300d08d9b058 | 490 | |
wim | 0:300d08d9b058 | 491 | |
wim | 1:b7e8f5139026 | 492 | /** @brief Write command that has no parameters |
wim | 1:b7e8f5139026 | 493 | */ |
wim | 0:300d08d9b058 | 494 | void SSD1308::_sendCommand(uint8_t command) { |
wim | 0:300d08d9b058 | 495 | // I2Cdev::writeByte(m_devAddr, COMMAND_MODE, command); |
wim | 0:300d08d9b058 | 496 | |
wim | 4:df92b0c0cb92 | 497 | #if (I2C_OPTIMIZE == 0) |
wim | 0:300d08d9b058 | 498 | char databytes[2]; |
wim | 0:300d08d9b058 | 499 | |
wim | 0:300d08d9b058 | 500 | databytes[0] = COMMAND_MODE; |
wim | 0:300d08d9b058 | 501 | databytes[1] = command; |
wim | 5:e564cde8e03e | 502 | _i2c->write(_writeOpcode, databytes, 2); // Write command |
wim | 4:df92b0c0cb92 | 503 | #else |
wim | 4:df92b0c0cb92 | 504 | |
wim | 5:e564cde8e03e | 505 | _i2c->start(); |
wim | 5:e564cde8e03e | 506 | _i2c->write(_writeOpcode); |
wim | 0:300d08d9b058 | 507 | |
wim | 5:e564cde8e03e | 508 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 509 | _i2c->write(command); // Write Command |
wim | 0:300d08d9b058 | 510 | |
wim | 5:e564cde8e03e | 511 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 512 | #endif |
wim | 0:300d08d9b058 | 513 | } |
wim | 0:300d08d9b058 | 514 | |
wim | 1:b7e8f5139026 | 515 | /** @brief Write command that has one parameter |
wim | 1:b7e8f5139026 | 516 | */ |
wim | 0:300d08d9b058 | 517 | void SSD1308::_sendCommand(uint8_t command, uint8_t param1) { |
wim | 0:300d08d9b058 | 518 | |
wim | 0:300d08d9b058 | 519 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 0:300d08d9b058 | 520 | // repeated before each databyte that serves as parameter! |
wim | 4:df92b0c0cb92 | 521 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 522 | char databytes[4]; |
wim | 4:df92b0c0cb92 | 523 | |
wim | 4:df92b0c0cb92 | 524 | databytes[0] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 525 | databytes[1] = command; |
wim | 4:df92b0c0cb92 | 526 | databytes[2] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 527 | databytes[3] = param1; |
wim | 5:e564cde8e03e | 528 | _i2c->write(_writeOpcode, databytes, 4); // Write command |
wim | 4:df92b0c0cb92 | 529 | #else |
wim | 0:300d08d9b058 | 530 | |
wim | 5:e564cde8e03e | 531 | _i2c->start(); |
wim | 5:e564cde8e03e | 532 | _i2c->write(_writeOpcode); |
wim | 0:300d08d9b058 | 533 | |
wim | 5:e564cde8e03e | 534 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 535 | _i2c->write(command); // Write Command |
wim | 5:e564cde8e03e | 536 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 537 | _i2c->write(param1); // Write Param1 |
wim | 0:300d08d9b058 | 538 | |
wim | 5:e564cde8e03e | 539 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 540 | #endif |
wim | 0:300d08d9b058 | 541 | } |
wim | 0:300d08d9b058 | 542 | |
wim | 1:b7e8f5139026 | 543 | /** @brief Write command that has two parameters |
wim | 1:b7e8f5139026 | 544 | */ |
wim | 0:300d08d9b058 | 545 | void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2) { |
wim | 0:300d08d9b058 | 546 | |
wim | 0:300d08d9b058 | 547 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 0:300d08d9b058 | 548 | // repeated before each databyte that serves as parameter! |
wim | 4:df92b0c0cb92 | 549 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 550 | char databytes[6]; |
wim | 4:df92b0c0cb92 | 551 | |
wim | 4:df92b0c0cb92 | 552 | databytes[0] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 553 | databytes[1] = command; |
wim | 4:df92b0c0cb92 | 554 | databytes[2] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 555 | databytes[3] = param1; |
wim | 4:df92b0c0cb92 | 556 | databytes[4] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 557 | databytes[5] = param2; |
wim | 5:e564cde8e03e | 558 | _i2c->write(_writeOpcode, databytes, 6); // Write command |
wim | 4:df92b0c0cb92 | 559 | #else |
wim | 5:e564cde8e03e | 560 | _i2c->start(); |
wim | 5:e564cde8e03e | 561 | _i2c->write(_writeOpcode); |
wim | 0:300d08d9b058 | 562 | |
wim | 5:e564cde8e03e | 563 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 564 | _i2c->write(command); // Write Command |
wim | 5:e564cde8e03e | 565 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 566 | _i2c->write(param1); // Write Param1 |
wim | 5:e564cde8e03e | 567 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 568 | _i2c->write(param2); // Write Param2 |
wim | 0:300d08d9b058 | 569 | |
wim | 5:e564cde8e03e | 570 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 571 | #endif |
wim | 0:300d08d9b058 | 572 | } |
wim | 0:300d08d9b058 | 573 | |
wim | 2:16c84a134393 | 574 | /** @brief Write command that has five parameters |
wim | 2:16c84a134393 | 575 | */ |
wim | 2:16c84a134393 | 576 | void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2, |
wim | 2:16c84a134393 | 577 | uint8_t param3, uint8_t param4, |
wim | 2:16c84a134393 | 578 | uint8_t param5) { |
wim | 2:16c84a134393 | 579 | |
wim | 2:16c84a134393 | 580 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 2:16c84a134393 | 581 | // repeated before each databyte that serves as parameter! |
wim | 4:df92b0c0cb92 | 582 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 583 | char databytes[12]; |
wim | 4:df92b0c0cb92 | 584 | |
wim | 4:df92b0c0cb92 | 585 | databytes[0] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 586 | databytes[1] = command; |
wim | 4:df92b0c0cb92 | 587 | databytes[2] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 588 | databytes[3] = param1; |
wim | 4:df92b0c0cb92 | 589 | databytes[4] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 590 | databytes[5] = param2; |
wim | 4:df92b0c0cb92 | 591 | databytes[6] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 592 | databytes[7] = param3; |
wim | 4:df92b0c0cb92 | 593 | databytes[8] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 594 | databytes[9] = param4; |
wim | 4:df92b0c0cb92 | 595 | databytes[10] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 596 | databytes[11] = param5; |
wim | 5:e564cde8e03e | 597 | _i2c->write(_writeOpcode, databytes, 12); // Write command |
wim | 4:df92b0c0cb92 | 598 | #else |
wim | 5:e564cde8e03e | 599 | _i2c->start(); |
wim | 5:e564cde8e03e | 600 | _i2c->write(_writeOpcode); |
wim | 2:16c84a134393 | 601 | |
wim | 5:e564cde8e03e | 602 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 603 | _i2c->write(command); // Write Command |
wim | 5:e564cde8e03e | 604 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 605 | _i2c->write(param1); // Write Param1 |
wim | 5:e564cde8e03e | 606 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 607 | _i2c->write(param2); // Write Param2 |
wim | 5:e564cde8e03e | 608 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 609 | _i2c->write(param3); // Write Param3 |
wim | 5:e564cde8e03e | 610 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 611 | _i2c->write(param4); // Write Param4 |
wim | 5:e564cde8e03e | 612 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 613 | _i2c->write(param5); // Write Param5 |
wim | 2:16c84a134393 | 614 | |
wim | 5:e564cde8e03e | 615 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 616 | #endif |
wim | 2:16c84a134393 | 617 | } |
wim | 2:16c84a134393 | 618 | |
wim | 2:16c84a134393 | 619 | |
wim | 2:16c84a134393 | 620 | /** @brief Write command that has six parameters |
wim | 2:16c84a134393 | 621 | */ |
wim | 2:16c84a134393 | 622 | void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2, |
wim | 2:16c84a134393 | 623 | uint8_t param3, uint8_t param4, |
wim | 2:16c84a134393 | 624 | uint8_t param5, uint8_t param6) { |
wim | 2:16c84a134393 | 625 | |
wim | 2:16c84a134393 | 626 | // Note continuationbit is set, so COMMAND_MODE must be |
wim | 2:16c84a134393 | 627 | // repeated before each databyte that serves as parameter! |
wim | 4:df92b0c0cb92 | 628 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 629 | char databytes[14]; |
wim | 4:df92b0c0cb92 | 630 | |
wim | 4:df92b0c0cb92 | 631 | databytes[0] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 632 | databytes[1] = command; |
wim | 4:df92b0c0cb92 | 633 | databytes[2] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 634 | databytes[3] = param1; |
wim | 4:df92b0c0cb92 | 635 | databytes[4] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 636 | databytes[5] = param2; |
wim | 4:df92b0c0cb92 | 637 | databytes[6] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 638 | databytes[7] = param3; |
wim | 4:df92b0c0cb92 | 639 | databytes[8] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 640 | databytes[9] = param4; |
wim | 4:df92b0c0cb92 | 641 | databytes[10] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 642 | databytes[11] = param5; |
wim | 4:df92b0c0cb92 | 643 | databytes[12] = COMMAND_MODE; |
wim | 4:df92b0c0cb92 | 644 | databytes[13] = param6; |
wim | 5:e564cde8e03e | 645 | _i2c->write(_writeOpcode, databytes, 14); // Write command |
wim | 4:df92b0c0cb92 | 646 | #else |
wim | 5:e564cde8e03e | 647 | _i2c->start(); |
wim | 5:e564cde8e03e | 648 | _i2c->write(_writeOpcode); |
wim | 2:16c84a134393 | 649 | |
wim | 5:e564cde8e03e | 650 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 651 | _i2c->write(command); // Write Command |
wim | 5:e564cde8e03e | 652 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 653 | _i2c->write(param1); // Write Param1 |
wim | 5:e564cde8e03e | 654 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 655 | _i2c->write(param2); // Write Param2 |
wim | 5:e564cde8e03e | 656 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 657 | _i2c->write(param3); // Write Param3 |
wim | 5:e564cde8e03e | 658 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 659 | _i2c->write(param4); // Write Param4 |
wim | 5:e564cde8e03e | 660 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 661 | _i2c->write(param5); // Write Param5 |
wim | 5:e564cde8e03e | 662 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 663 | _i2c->write(param6); // Write Param6 |
wim | 2:16c84a134393 | 664 | |
wim | 5:e564cde8e03e | 665 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 666 | #endif |
wim | 2:16c84a134393 | 667 | } |
wim | 2:16c84a134393 | 668 | |
wim | 2:16c84a134393 | 669 | |
wim | 0:300d08d9b058 | 670 | #if(0) |
wim | 1:b7e8f5139026 | 671 | /** @brief Write command that has multiple parameters |
wim | 1:b7e8f5139026 | 672 | */ |
wim | 0:300d08d9b058 | 673 | void SSD1308::_sendCommands(uint8_t len, uint8_t* commands) { |
wim | 0:300d08d9b058 | 674 | |
wim | 0:300d08d9b058 | 675 | // I2Cdev::writeBytes(m_devAddr, COMMAND_MODE, len, commands); |
wim | 0:300d08d9b058 | 676 | // Note this original code is not correct, continuationbit is set, |
wim | 0:300d08d9b058 | 677 | // so COMMAND_MODE must be repeated before each databyte that serves as parameter! |
wim | 0:300d08d9b058 | 678 | |
wim | 5:e564cde8e03e | 679 | _i2c->start(); |
wim | 5:e564cde8e03e | 680 | _i2c->write(_writeOpcode); |
wim | 0:300d08d9b058 | 681 | |
wim | 0:300d08d9b058 | 682 | for (int i=0; i<len ; i++) { |
wim | 5:e564cde8e03e | 683 | _i2c->write(COMMAND_MODE); |
wim | 5:e564cde8e03e | 684 | _i2c->write(commands[i]); // Write Commands |
wim | 0:300d08d9b058 | 685 | } |
wim | 5:e564cde8e03e | 686 | _i2c->stop(); |
wim | 0:300d08d9b058 | 687 | |
wim | 0:300d08d9b058 | 688 | } |
wim | 0:300d08d9b058 | 689 | #endif |
wim | 0:300d08d9b058 | 690 | |
wim | 1:b7e8f5139026 | 691 | /** @brief Write databyte to display |
wim | 1:b7e8f5139026 | 692 | * @brief Start at current cursor location |
wim | 1:b7e8f5139026 | 693 | * @param uint8_t data databyte to write |
wim | 1:b7e8f5139026 | 694 | */ |
wim | 0:300d08d9b058 | 695 | void SSD1308::_sendData(uint8_t data){ |
wim | 0:300d08d9b058 | 696 | |
wim | 4:df92b0c0cb92 | 697 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 698 | //I2C Blockwrite versions dont seem to work ? |
wim | 4:df92b0c0cb92 | 699 | //That may be related to fact that the SSD1308/SSD1306 does NOT return an acknowledge: blockwrite may abort the operation |
wim | 4:df92b0c0cb92 | 700 | //Noted for mbed lib v63 on 20/7/13 |
wim | 0:300d08d9b058 | 701 | char databytes[2]; |
wim | 0:300d08d9b058 | 702 | |
wim | 0:300d08d9b058 | 703 | databytes[0] = DATA_MODE; |
wim | 0:300d08d9b058 | 704 | databytes[1] = data; |
wim | 5:e564cde8e03e | 705 | _i2c->write(_writeOpcode, databytes, 2); // Write Data |
wim | 4:df92b0c0cb92 | 706 | |
wim | 4:df92b0c0cb92 | 707 | #else |
wim | 5:e564cde8e03e | 708 | _i2c->start(); |
wim | 5:e564cde8e03e | 709 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 710 | _i2c->write(DATA_MODE); |
wim | 5:e564cde8e03e | 711 | _i2c->write(data); |
wim | 5:e564cde8e03e | 712 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 713 | #endif |
wim | 4:df92b0c0cb92 | 714 | |
wim | 0:300d08d9b058 | 715 | } |
wim | 0:300d08d9b058 | 716 | |
wim | 1:b7e8f5139026 | 717 | /** @brief Write len bytes from buffer data to display, |
wim | 1:b7e8f5139026 | 718 | * @brief Start at current cursor location |
wim | 1:b7e8f5139026 | 719 | * @param uint8_t len number of bytes to write |
wim | 1:b7e8f5139026 | 720 | * @param uint8_t* data pointer to data |
wim | 1:b7e8f5139026 | 721 | */ |
wim | 0:300d08d9b058 | 722 | void SSD1308::_sendData(uint8_t len, uint8_t* data) { |
wim | 0:300d08d9b058 | 723 | // I2Cdev::writeBytes(m_devAddr, DATA_MODE, len, data); |
wim | 4:df92b0c0cb92 | 724 | #if (I2C_OPTIMIZE == 0) |
wim | 4:df92b0c0cb92 | 725 | for (int i=0; i<len ; i++) { |
wim | 4:df92b0c0cb92 | 726 | _sendData(data[i]); // Write Data |
wim | 4:df92b0c0cb92 | 727 | } |
wim | 4:df92b0c0cb92 | 728 | #else |
wim | 5:e564cde8e03e | 729 | _i2c->start(); |
wim | 5:e564cde8e03e | 730 | _i2c->write(_writeOpcode); |
wim | 5:e564cde8e03e | 731 | _i2c->write(DATA_MODE); |
wim | 0:300d08d9b058 | 732 | for (int i=0; i<len ; i++) { |
wim | 5:e564cde8e03e | 733 | _i2c->write(data[i]); // Write Data |
wim | 0:300d08d9b058 | 734 | } |
wim | 5:e564cde8e03e | 735 | _i2c->stop(); |
wim | 4:df92b0c0cb92 | 736 | #endif |
wim | 0:300d08d9b058 | 737 | } |
wim | 0:300d08d9b058 | 738 | |
wim | 0:300d08d9b058 | 739 | |
wim | 2:16c84a134393 | 740 | /** @brief Set Horizontal Addressing Mode (cursor incr left-to-right, top-to-bottom) |
wim | 2:16c84a134393 | 741 | * |
wim | 2:16c84a134393 | 742 | */ |
wim | 0:300d08d9b058 | 743 | void SSD1308::setHorizontalAddressingMode(){ |
wim | 0:300d08d9b058 | 744 | setMemoryAddressingMode(HORIZONTAL_ADDRESSING_MODE); |
wim | 0:300d08d9b058 | 745 | } |
wim | 0:300d08d9b058 | 746 | |
wim | 2:16c84a134393 | 747 | /** @brief Set Vertical Addressing Mode (cursor incr top-to-bottom, left-to-right) |
wim | 2:16c84a134393 | 748 | * |
wim | 2:16c84a134393 | 749 | */ |
wim | 0:300d08d9b058 | 750 | void SSD1308::setVerticalAddressingMode() { |
wim | 0:300d08d9b058 | 751 | setMemoryAddressingMode(VERTICAL_ADDRESSING_MODE); |
wim | 0:300d08d9b058 | 752 | } |
wim | 0:300d08d9b058 | 753 | |
wim | 2:16c84a134393 | 754 | /** @brief Set Page Addressing Mode (cursor incr left-to-right) |
wim | 2:16c84a134393 | 755 | * |
wim | 2:16c84a134393 | 756 | */ |
wim | 0:300d08d9b058 | 757 | void SSD1308::setPageAddressingMode(){ |
wim | 0:300d08d9b058 | 758 | setMemoryAddressingMode(PAGE_ADDRESSING_MODE); |
wim | 0:300d08d9b058 | 759 | } |
wim | 0:300d08d9b058 | 760 | |
wim | 2:16c84a134393 | 761 | /** @brief Set Addressing Mode |
wim | 2:16c84a134393 | 762 | * @param uint8_t mode |
wim | 2:16c84a134393 | 763 | */ |
wim | 0:300d08d9b058 | 764 | void SSD1308::setMemoryAddressingMode(uint8_t mode){ |
wim | 0:300d08d9b058 | 765 | |
wim | 0:300d08d9b058 | 766 | _sendCommand(SET_MEMORY_ADDRESSING_MODE, mode); |
wim | 0:300d08d9b058 | 767 | } |
wim | 0:300d08d9b058 | 768 | |
wim | 0:300d08d9b058 | 769 | |
wim | 1:b7e8f5139026 | 770 | /** @param uint8_t start startpage (valid range 0..MAX_PAGE) |
wim | 1:b7e8f5139026 | 771 | * @param uint8_t end endpage (valid range start..MAX_PAGE) |
wim | 1:b7e8f5139026 | 772 | */ |
wim | 0:300d08d9b058 | 773 | void SSD1308::setPageAddress(uint8_t start, uint8_t end) { |
wim | 0:300d08d9b058 | 774 | |
wim | 0:300d08d9b058 | 775 | _sendCommand(SET_PAGE_ADDRESS, start, end); |
wim | 0:300d08d9b058 | 776 | } |
wim | 0:300d08d9b058 | 777 | |
wim | 0:300d08d9b058 | 778 | |
wim | 1:b7e8f5139026 | 779 | /** @param uint8_t start startcolumn (valid range 0..MAX_COL) |
wim | 1:b7e8f5139026 | 780 | * @param uint8_t end endcolumn (valid range start..MAX_COL) |
wim | 1:b7e8f5139026 | 781 | */ |
wim | 0:300d08d9b058 | 782 | void SSD1308::setColumnAddress(uint8_t start, uint8_t end) { |
wim | 0:300d08d9b058 | 783 | |
wim | 0:300d08d9b058 | 784 | _sendCommand(SET_COLUMN_ADDRESS, start, end); |
wim | 0:300d08d9b058 | 785 | } |
wim | 0:300d08d9b058 | 786 | |
wim | 2:16c84a134393 | 787 | /** |
wim | 2:16c84a134393 | 788 | * @brief Set Display StartLine, takes one byte, 0x00-0x3F |
wim | 2:16c84a134393 | 789 | * @param uint8_t line startline (valid range 0..MAX_ROWS) |
wim | 2:16c84a134393 | 790 | */ |
wim | 2:16c84a134393 | 791 | void SSD1308::setDisplayStartLine(uint8_t line) { |
wim | 2:16c84a134393 | 792 | |
wim | 2:16c84a134393 | 793 | line = line & MAX_ROW; |
wim | 2:16c84a134393 | 794 | |
wim | 2:16c84a134393 | 795 | _sendCommand(SET_DISPLAY_START_LINE | line); |
wim | 2:16c84a134393 | 796 | } |
wim | 2:16c84a134393 | 797 | |
wim | 2:16c84a134393 | 798 | |
wim | 2:16c84a134393 | 799 | /** |
wim | 2:16c84a134393 | 800 | * @brief Set Column Start (for Page Addressing Mode only) |
wim | 2:16c84a134393 | 801 | * @param uint8_t column column start (valid range 0..MAX_COL) |
wim | 2:16c84a134393 | 802 | */ |
wim | 2:16c84a134393 | 803 | void SSD1308::setColumnStartForPageAddressingMode(uint8_t column) { |
wim | 2:16c84a134393 | 804 | |
wim | 2:16c84a134393 | 805 | column = column & MAX_COL; |
wim | 2:16c84a134393 | 806 | |
wim | 2:16c84a134393 | 807 | _sendCommand(SET_LOWER_COLUMN | ( column & 0x0F)); // lower nibble |
wim | 2:16c84a134393 | 808 | _sendCommand(SET_HIGHER_COLUMN | ((column>>4) & 0x0F)); // higher nibble |
wim | 2:16c84a134393 | 809 | } |
wim | 2:16c84a134393 | 810 | |
wim | 2:16c84a134393 | 811 | |
wim | 2:16c84a134393 | 812 | /** |
wim | 2:16c84a134393 | 813 | * @brief Set Page Start (for Page Addressing Mode only) |
wim | 2:16c84a134393 | 814 | * @param uint8_t page page start (valid range PAGE0 - PAGE7) |
wim | 2:16c84a134393 | 815 | */ |
wim | 2:16c84a134393 | 816 | void SSD1308::setPageStartForPageAddressingMode(uint8_t page) { |
wim | 2:16c84a134393 | 817 | |
wim | 2:16c84a134393 | 818 | page = page & MAX_PAGE; |
wim | 2:16c84a134393 | 819 | |
wim | 2:16c84a134393 | 820 | _sendCommand(SET_PAGE_START_ADDRESS | page); |
wim | 2:16c84a134393 | 821 | |
wim | 2:16c84a134393 | 822 | } |
wim | 2:16c84a134393 | 823 | |
wim | 2:16c84a134393 | 824 | |
wim | 1:b7e8f5139026 | 825 | /** @brief Set Contrast |
wim | 1:b7e8f5139026 | 826 | * @param uint8_t contrast (valid range 0x00 (lowest) - 0xFF (highest)) |
wim | 1:b7e8f5139026 | 827 | */ |
wim | 0:300d08d9b058 | 828 | void SSD1308::setContrastControl(uint8_t contrast) { |
wim | 0:300d08d9b058 | 829 | |
wim | 0:300d08d9b058 | 830 | _sendCommand(SET_CONTRAST, contrast); |
wim | 0:300d08d9b058 | 831 | } |
wim | 0:300d08d9b058 | 832 | |
wim | 1:b7e8f5139026 | 833 | /** @brief Enable Display |
wim | 1:b7e8f5139026 | 834 | */ |
wim | 0:300d08d9b058 | 835 | void SSD1308::setDisplayOn() { |
wim | 0:300d08d9b058 | 836 | _sendCommand(SET_DISPLAY_POWER_ON); |
wim | 0:300d08d9b058 | 837 | } |
wim | 0:300d08d9b058 | 838 | |
wim | 1:b7e8f5139026 | 839 | /** @brief Disable Display |
wim | 1:b7e8f5139026 | 840 | */ |
wim | 0:300d08d9b058 | 841 | void SSD1308::setDisplayOff() { |
wim | 0:300d08d9b058 | 842 | _sendCommand(SET_DISPLAY_POWER_OFF); |
wim | 0:300d08d9b058 | 843 | } |
wim | 0:300d08d9b058 | 844 | |
wim | 1:b7e8f5139026 | 845 | /** @brief Enable or Disable Display |
wim | 1:b7e8f5139026 | 846 | * @param bool on |
wim | 1:b7e8f5139026 | 847 | */ |
wim | 0:300d08d9b058 | 848 | void SSD1308::setDisplayPower(bool on) { |
wim | 0:300d08d9b058 | 849 | if (on) { |
wim | 0:300d08d9b058 | 850 | setDisplayOn(); |
wim | 0:300d08d9b058 | 851 | } else { |
wim | 0:300d08d9b058 | 852 | setDisplayOff(); |
wim | 0:300d08d9b058 | 853 | } |
wim | 0:300d08d9b058 | 854 | } |
wim | 0:300d08d9b058 | 855 | |
wim | 1:b7e8f5139026 | 856 | /** @brief Show White pixels on Black background |
wim | 1:b7e8f5139026 | 857 | */ |
wim | 0:300d08d9b058 | 858 | void SSD1308::setDisplayNormal() { |
wim | 0:300d08d9b058 | 859 | _sendCommand(SET_NORMAL_DISPLAY); |
wim | 0:300d08d9b058 | 860 | } |
wim | 0:300d08d9b058 | 861 | |
wim | 1:b7e8f5139026 | 862 | /** @brief Show Black pixels on White background |
wim | 1:b7e8f5139026 | 863 | */ |
wim | 0:300d08d9b058 | 864 | void SSD1308::setDisplayInverse() { |
wim | 0:300d08d9b058 | 865 | _sendCommand(SET_INVERSE_DISPLAY); |
wim | 0:300d08d9b058 | 866 | } |
wim | 0:300d08d9b058 | 867 | |
wim | 1:b7e8f5139026 | 868 | /** @brief Blink display by fading in and out over a set number of frames |
wim | 1:b7e8f5139026 | 869 | * @param bool on |
wim | 1:b7e8f5139026 | 870 | */ |
wim | 0:300d08d9b058 | 871 | void SSD1308::setDisplayBlink(bool on){ |
wim | 0:300d08d9b058 | 872 | if (on) { |
wim | 0:300d08d9b058 | 873 | _sendCommand(SET_FADE_BLINK, (BLINK_ENABLE | FADE_INTERVAL_128_FRAMES)); |
wim | 0:300d08d9b058 | 874 | } |
wim | 0:300d08d9b058 | 875 | else { |
wim | 0:300d08d9b058 | 876 | _sendCommand(SET_FADE_BLINK, FADE_BLINK_DISABLE); |
wim | 0:300d08d9b058 | 877 | } |
wim | 0:300d08d9b058 | 878 | } |
wim | 0:300d08d9b058 | 879 | |
wim | 0:300d08d9b058 | 880 | |
wim | 1:b7e8f5139026 | 881 | /** @brief Fade out display in set number of frames |
wim | 1:b7e8f5139026 | 882 | * @param bool on |
wim | 1:b7e8f5139026 | 883 | */ |
wim | 0:300d08d9b058 | 884 | void SSD1308::setDisplayFade(bool on) { |
wim | 0:300d08d9b058 | 885 | if (on) { |
wim | 0:300d08d9b058 | 886 | _sendCommand(SET_FADE_BLINK, (FADE_OUT_ENABLE | FADE_INTERVAL_128_FRAMES)); |
wim | 0:300d08d9b058 | 887 | } |
wim | 0:300d08d9b058 | 888 | else { |
wim | 0:300d08d9b058 | 889 | _sendCommand(SET_FADE_BLINK, FADE_BLINK_DISABLE); |
wim | 0:300d08d9b058 | 890 | } |
wim | 0:300d08d9b058 | 891 | } |
wim | 0:300d08d9b058 | 892 | |
wim | 1:b7e8f5139026 | 893 | /** @brief Display Flip (Left/Right, Up/Down) |
wim | 1:b7e8f5139026 | 894 | * @param bool left flip Left/Right |
wim | 1:b7e8f5139026 | 895 | * @param bool down flip Up/Down |
wim | 1:b7e8f5139026 | 896 | */ |
wim | 0:300d08d9b058 | 897 | void SSD1308::setDisplayFlip(bool left, bool down) { |
wim | 0:300d08d9b058 | 898 | if (left) { |
wim | 0:300d08d9b058 | 899 | // column address 0 is mapped to SEG0 (Reset) |
wim | 0:300d08d9b058 | 900 | _sendCommand(SET_SEGMENT_REMAP_0); |
wim | 0:300d08d9b058 | 901 | } |
wim | 0:300d08d9b058 | 902 | else { |
wim | 0:300d08d9b058 | 903 | // column address 127 is mapped to SEG0 |
wim | 0:300d08d9b058 | 904 | _sendCommand(SET_SEGMENT_REMAP_127); |
wim | 0:300d08d9b058 | 905 | } |
wim | 0:300d08d9b058 | 906 | |
wim | 0:300d08d9b058 | 907 | if (down) { |
wim | 0:300d08d9b058 | 908 | // Reset mode |
wim | 0:300d08d9b058 | 909 | _sendCommand(SET_COMMON_REMAP_0); |
wim | 0:300d08d9b058 | 910 | } |
wim | 0:300d08d9b058 | 911 | else { |
wim | 0:300d08d9b058 | 912 | // Flip Up/Down (Need to rewrite display before H effect shows) |
wim | 0:300d08d9b058 | 913 | _sendCommand(SET_COMMON_REMAP_63); |
wim | 0:300d08d9b058 | 914 | } |
wim | 0:300d08d9b058 | 915 | |
wim | 0:300d08d9b058 | 916 | } |
wim | 0:300d08d9b058 | 917 | |
wim | 1:b7e8f5139026 | 918 | /** @brief Sets Internal Iref |
wim | 1:b7e8f5139026 | 919 | */ |
wim | 0:300d08d9b058 | 920 | void SSD1308::setInternalIref() { |
wim | 0:300d08d9b058 | 921 | // uint8_t cmds[2] = {SET_IREF_SELECTION, INTERNAL_IREF}; |
wim | 0:300d08d9b058 | 922 | // _sendCommands(2, cmds); |
wim | 0:300d08d9b058 | 923 | |
wim | 0:300d08d9b058 | 924 | _sendCommand(SET_IREF_SELECTION, INTERNAL_IREF); |
wim | 0:300d08d9b058 | 925 | } |
wim | 0:300d08d9b058 | 926 | |
wim | 1:b7e8f5139026 | 927 | /** @brief Sets External Iref (default) |
wim | 1:b7e8f5139026 | 928 | */ |
wim | 0:300d08d9b058 | 929 | void SSD1308::setExternalIref() { |
wim | 0:300d08d9b058 | 930 | // uint8_t cmds[2] = {SET_IREF_SELECTION, EXTERNAL_IREF}; |
wim | 0:300d08d9b058 | 931 | // _sendCommands(2, cmds); |
wim | 0:300d08d9b058 | 932 | _sendCommand(SET_IREF_SELECTION, EXTERNAL_IREF); |
wim | 0:300d08d9b058 | 933 | } |
wim | 0:300d08d9b058 | 934 | |
wim | 0:300d08d9b058 | 935 | |
wim | 2:16c84a134393 | 936 | /** @brief Shows All Pixels On |
wim | 2:16c84a134393 | 937 | */ |
wim | 2:16c84a134393 | 938 | void SSD1308::setEntireDisplayOn(){ |
wim | 2:16c84a134393 | 939 | _sendCommand(SET_ENTIRE_DISPLAY_ON); |
wim | 2:16c84a134393 | 940 | } |
wim | 2:16c84a134393 | 941 | |
wim | 2:16c84a134393 | 942 | /** @brief Shows Pixels as RAM content |
wim | 2:16c84a134393 | 943 | */ |
wim | 2:16c84a134393 | 944 | void SSD1308::setEntireDisplayRAM(){ |
wim | 2:16c84a134393 | 945 | _sendCommand(SET_DISPLAY_GDDRAM); |
wim | 2:16c84a134393 | 946 | } |
wim | 2:16c84a134393 | 947 | |
wim | 2:16c84a134393 | 948 | /** @brief Shows Pixels On or as RAM content |
wim | 2:16c84a134393 | 949 | * @param bool on (true is All on, false is RAM content) |
wim | 2:16c84a134393 | 950 | */ |
wim | 2:16c84a134393 | 951 | void SSD1308::setEntireDisplay(bool on){ |
wim | 2:16c84a134393 | 952 | if (on) { |
wim | 2:16c84a134393 | 953 | setEntireDisplayOn(); // All Pixels on |
wim | 2:16c84a134393 | 954 | } |
wim | 2:16c84a134393 | 955 | else { |
wim | 2:16c84a134393 | 956 | setEntireDisplayRAM(); // Pixels are RAM content |
wim | 2:16c84a134393 | 957 | } |
wim | 2:16c84a134393 | 958 | } |
wim | 2:16c84a134393 | 959 | |
wim | 2:16c84a134393 | 960 | |
wim | 2:16c84a134393 | 961 | /** @brief Horizontal scroll by one column per interval |
wim | 2:16c84a134393 | 962 | * @param bool left select Left/Right scroll |
wim | 2:16c84a134393 | 963 | * @param uint8_t start_page begin page (0..MAX_PAGE) |
wim | 2:16c84a134393 | 964 | * @param uint8_t end_page end page (start_page..MAX_PAGE) |
wim | 2:16c84a134393 | 965 | * @param uint8_t interval scroll interval in frames (see codes above) |
wim | 2:16c84a134393 | 966 | */ |
wim | 2:16c84a134393 | 967 | void SSD1308::setContinuousHorizontalScroll(bool left, uint8_t start_page, uint8_t end_page, uint8_t interval) { |
wim | 2:16c84a134393 | 968 | if (left) { |
wim | 2:16c84a134393 | 969 | _sendCommand(SET_LEFT_HOR_SCROLL, 0x00, start_page, interval, end_page, 0x00, 0xFF); // Scroll Left |
wim | 2:16c84a134393 | 970 | } |
wim | 2:16c84a134393 | 971 | else { |
wim | 2:16c84a134393 | 972 | _sendCommand(SET_RIGHT_HOR_SCROLL, 0x00, start_page, interval, end_page, 0x00, 0xFF); // Scroll Right |
wim | 2:16c84a134393 | 973 | } |
wim | 2:16c84a134393 | 974 | |
wim | 2:16c84a134393 | 975 | } |
wim | 2:16c84a134393 | 976 | |
wim | 2:16c84a134393 | 977 | |
wim | 2:16c84a134393 | 978 | /** @brief Horizontal and Vertical scroll by one column per interval |
wim | 2:16c84a134393 | 979 | * @param bool left select Left/Right scroll |
wim | 2:16c84a134393 | 980 | * @param uint8_t start_page begin page (0..MAX_PAGE) |
wim | 2:16c84a134393 | 981 | * @param uint8_t end_page end page (start_page..MAX_PAGE) |
wim | 2:16c84a134393 | 982 | * @param uint8_t offset vert offset (0x01..0x63) |
wim | 2:16c84a134393 | 983 | * @param uint8_t interval scroll interval in frames (see codes above) |
wim | 2:16c84a134393 | 984 | */ |
wim | 2:16c84a134393 | 985 | void SSD1308::setContinuousVerticalAndHorizontalScroll(bool left, uint8_t start_page, uint8_t end_page, |
wim | 2:16c84a134393 | 986 | uint8_t offset, uint8_t interval) { |
wim | 2:16c84a134393 | 987 | if (left) { |
wim | 2:16c84a134393 | 988 | _sendCommand(SET_VERT_LEFT_HOR_SCROLL, 0x00, start_page, interval, end_page, offset); // Scroll Left |
wim | 2:16c84a134393 | 989 | } |
wim | 2:16c84a134393 | 990 | else { |
wim | 2:16c84a134393 | 991 | _sendCommand(SET_VERT_RIGHT_HOR_SCROLL, 0x00, start_page, interval, end_page, offset); // Scroll Right |
wim | 2:16c84a134393 | 992 | } |
wim | 2:16c84a134393 | 993 | |
wim | 2:16c84a134393 | 994 | } |
wim | 2:16c84a134393 | 995 | |
wim | 2:16c84a134393 | 996 | /** @brief Set Vertical scroll area |
wim | 2:16c84a134393 | 997 | * @param uint8_t topRowsFixed fixed rows (0..MAX_ROW) |
wim | 2:16c84a134393 | 998 | * @param uint8_t scrollRowsoffset scroll rows (topRowsFixed..ROWS) |
wim | 2:16c84a134393 | 999 | */ |
wim | 2:16c84a134393 | 1000 | void SSD1308::setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows) { |
wim | 2:16c84a134393 | 1001 | |
wim | 2:16c84a134393 | 1002 | if ((topRowsFixed + scrollRows) > ROWS) { |
wim | 2:16c84a134393 | 1003 | scrollRows = ROWS - topRowsFixed; |
wim | 2:16c84a134393 | 1004 | }; |
wim | 2:16c84a134393 | 1005 | |
wim | 2:16c84a134393 | 1006 | _sendCommand(SET_VERTICAL_SCROLL_AREA, topRowsFixed, scrollRows); |
wim | 2:16c84a134393 | 1007 | } |
wim | 2:16c84a134393 | 1008 | |
wim | 2:16c84a134393 | 1009 | /** @brief Activate or Deactivate Horizontal and Vertical scroll |
wim | 2:16c84a134393 | 1010 | * @brief Note: after deactivating scrolling, the RAM data needs to be rewritten |
wim | 2:16c84a134393 | 1011 | * @param bool on activate scroll |
wim | 2:16c84a134393 | 1012 | */ |
wim | 2:16c84a134393 | 1013 | void SSD1308::setDisplayScroll(bool on) { |
wim | 2:16c84a134393 | 1014 | if (on) { |
wim | 2:16c84a134393 | 1015 | _sendCommand(SET_ACTIVATE_SCROLL); // Scroll on |
wim | 2:16c84a134393 | 1016 | } |
wim | 2:16c84a134393 | 1017 | else { |
wim | 2:16c84a134393 | 1018 | _sendCommand(SET_DEACTIVATE_SCROLL); // Scroll off |
wim | 2:16c84a134393 | 1019 | } |
wim | 2:16c84a134393 | 1020 | } |
wim | 2:16c84a134393 | 1021 | |
wim | 2:16c84a134393 | 1022 | |
wim | 2:16c84a134393 | 1023 | |
wim | 1:b7e8f5139026 | 1024 | /** @brief Low level Init |
wim | 1:b7e8f5139026 | 1025 | * @brief Init the configuration registers in accordance with the datasheet |
wim | 1:b7e8f5139026 | 1026 | */ |
wim | 0:300d08d9b058 | 1027 | void SSD1308::_init() { |
wim | 0:300d08d9b058 | 1028 | |
wim | 2:16c84a134393 | 1029 | _sendCommand(SET_DISPLAY_POWER_OFF); // 0xAE |
wim | 0:300d08d9b058 | 1030 | |
wim | 0:300d08d9b058 | 1031 | // column address 0 is mapped to SEG0 (Reset) |
wim | 2:16c84a134393 | 1032 | // row address 0 is mapped to COM0 (Reset) |
wim | 2:16c84a134393 | 1033 | _sendCommand(SET_SEGMENT_REMAP_0); // 0xA0 (Reset) |
wim | 2:16c84a134393 | 1034 | _sendCommand(SET_COMMON_REMAP_0); // 0xC0 (Reset) |
wim | 2:16c84a134393 | 1035 | |
wim | 2:16c84a134393 | 1036 | setDisplayStartLine(0); // 0x40 (Reset) |
wim | 0:300d08d9b058 | 1037 | |
wim | 2:16c84a134393 | 1038 | _sendCommand(SET_COMMON_CONF, COMMON_BASE | COMMON_ALTERNATIVE | COMMON_LEFTRIGHT_NORMAL); // 0xDA, 0x12 (Reset) |
wim | 0:300d08d9b058 | 1039 | |
wim | 2:16c84a134393 | 1040 | // Pagemode or Horizontal mode |
wim | 2:16c84a134393 | 1041 | // setPageAddressingMode(); // 0x20, 0x02 (Reset) |
wim | 2:16c84a134393 | 1042 | // setColumnStartForPageAddressingMode(0); // 0x00, 0x10 (Reset = Column 0) |
wim | 2:16c84a134393 | 1043 | // setPageStartForPageAddressingMode(PAGE_0);// 0xBO (Reset = Page 0) |
wim | 2:16c84a134393 | 1044 | setHorizontalAddressingMode(); // 0x20, 0x00 (Non-Reset) |
wim | 2:16c84a134393 | 1045 | setColumnAddress(0, MAX_COL); // 0x21, 0x00, 0x37 (Reset) |
wim | 2:16c84a134393 | 1046 | setPageAddress(0, MAX_PAGE); // 0x22, 0x00, 0x07 (Reset) |
wim | 0:300d08d9b058 | 1047 | |
wim | 2:16c84a134393 | 1048 | setExternalIref(); // 0xAD, 0x10 (Reset) |
wim | 2:16c84a134393 | 1049 | |
wim | 2:16c84a134393 | 1050 | _sendCommand(SET_DISPLAY_CLOCK, 0x70); // 0xD5, 0x70 (Reset = 0x80) |
wim | 2:16c84a134393 | 1051 | _sendCommand(SET_PRECHARGE_TIME, 0x21); // 0xD9, 0x21 (Reset = 0x22) |
wim | 2:16c84a134393 | 1052 | _sendCommand(SET_VCOMH_DESELECT_LEVEL, 0x30); // 0xDB, 0x30 (Reset = 0x20) |
wim | 2:16c84a134393 | 1053 | _sendCommand(SET_MULTIPLEX_RATIO, 0x3F); // 0xA8, 0x3F (Reset) |
wim | 2:16c84a134393 | 1054 | _sendCommand(SET_DISPLAY_OFFSET, 0x00); // 0xD3, 0x00 (Reset) |
wim | 2:16c84a134393 | 1055 | |
wim | 2:16c84a134393 | 1056 | _sendCommand(SET_CONTRAST, 0x7F); // 0x81, 0x7F (Reset) |
wim | 0:300d08d9b058 | 1057 | |
wim | 2:16c84a134393 | 1058 | _sendCommand(SET_NORMAL_DISPLAY); // 0xA6 (Reset) |
wim | 2:16c84a134393 | 1059 | |
wim | 2:16c84a134393 | 1060 | setEntireDisplayRAM(); // 0xA4 (Reset) |
wim | 2:16c84a134393 | 1061 | setDisplayScroll(false); |
wim | 0:300d08d9b058 | 1062 | |
wim | 0:300d08d9b058 | 1063 | clearDisplay(); |
wim | 0:300d08d9b058 | 1064 | |
wim | 2:16c84a134393 | 1065 | _sendCommand(SET_DISPLAY_POWER_ON); // 0xAF |
wim | 0:300d08d9b058 | 1066 | } |
wim | 0:300d08d9b058 | 1067 | |
dcj001 | 6:881ee0c6dbda | 1068 | /////////////////// |
dcj001 | 6:881ee0c6dbda | 1069 | void SSD1308::clearDisplay_zone() { |
dcj001 | 6:881ee0c6dbda | 1070 | |
dcj001 | 6:881ee0c6dbda | 1071 | int cl; |
dcj001 | 6:881ee0c6dbda | 1072 | cl=127; |
dcj001 | 6:881ee0c6dbda | 1073 | for(int m=127;m>=5;m--) |
dcj001 | 6:881ee0c6dbda | 1074 | { |
dcj001 | 6:881ee0c6dbda | 1075 | write_point(3,cl,32); |
dcj001 | 6:881ee0c6dbda | 1076 | cl=cl-1; |
dcj001 | 6:881ee0c6dbda | 1077 | //wait(0.1); |
dcj001 | 6:881ee0c6dbda | 1078 | } |
dcj001 | 6:881ee0c6dbda | 1079 | |
dcj001 | 6:881ee0c6dbda | 1080 | } |
dcj001 | 6:881ee0c6dbda | 1081 | |
dcj001 | 6:881ee0c6dbda | 1082 | |
dcj001 | 6:881ee0c6dbda | 1083 | |
dcj001 | 6:881ee0c6dbda | 1084 | void SSD1308::write_point(uint8_t row, uint8_t col, uint8_t line){ |
dcj001 | 6:881ee0c6dbda | 1085 | |
dcj001 | 6:881ee0c6dbda | 1086 | |
dcj001 | 6:881ee0c6dbda | 1087 | setVerticalAddressingMode(); |
dcj001 | 6:881ee0c6dbda | 1088 | |
dcj001 | 6:881ee0c6dbda | 1089 | //setPageAddress(row, MAX_PAGE); |
dcj001 | 6:881ee0c6dbda | 1090 | // setColumnAddress(col, ); |
dcj001 | 6:881ee0c6dbda | 1091 | |
dcj001 | 6:881ee0c6dbda | 1092 | setPageAddress(row, 6); |
dcj001 | 6:881ee0c6dbda | 1093 | setColumnAddress(col,MAX_COL); |
dcj001 | 6:881ee0c6dbda | 1094 | |
dcj001 | 6:881ee0c6dbda | 1095 | for(uint8_t i=0 ; i<4; i++) |
dcj001 | 6:881ee0c6dbda | 1096 | { |
dcj001 | 6:881ee0c6dbda | 1097 | _sendData(grah2_point[line][i]); |
dcj001 | 6:881ee0c6dbda | 1098 | } |
dcj001 | 6:881ee0c6dbda | 1099 | |
dcj001 | 6:881ee0c6dbda | 1100 | } |
dcj001 | 6:881ee0c6dbda | 1101 | |
dcj001 | 6:881ee0c6dbda | 1102 | void SSD1308::graph(int *temp,int times,int count) |
dcj001 | 6:881ee0c6dbda | 1103 | { |
dcj001 | 6:881ee0c6dbda | 1104 | int cl; |
dcj001 | 6:881ee0c6dbda | 1105 | int m; |
dcj001 | 6:881ee0c6dbda | 1106 | |
dcj001 | 6:881ee0c6dbda | 1107 | if(count==1) |
dcj001 | 6:881ee0c6dbda | 1108 | { cl=127; |
dcj001 | 6:881ee0c6dbda | 1109 | for(m=times;m>=0;m--) |
dcj001 | 6:881ee0c6dbda | 1110 | { |
dcj001 | 6:881ee0c6dbda | 1111 | write_point(3,cl,temp[m]); |
dcj001 | 6:881ee0c6dbda | 1112 | cl=cl-4; |
dcj001 | 6:881ee0c6dbda | 1113 | //wait(0.1); |
dcj001 | 6:881ee0c6dbda | 1114 | } |
dcj001 | 6:881ee0c6dbda | 1115 | cl=127; |
dcj001 | 6:881ee0c6dbda | 1116 | } |
dcj001 | 6:881ee0c6dbda | 1117 | |
dcj001 | 6:881ee0c6dbda | 1118 | else |
dcj001 | 6:881ee0c6dbda | 1119 | { cl=7; |
dcj001 | 6:881ee0c6dbda | 1120 | write_point(3,127,32); //clear the column127 |
dcj001 | 6:881ee0c6dbda | 1121 | for(m=0;m<30;m++) |
dcj001 | 6:881ee0c6dbda | 1122 | { |
dcj001 | 6:881ee0c6dbda | 1123 | write_point(3,cl,temp[m]); |
dcj001 | 6:881ee0c6dbda | 1124 | cl=cl+4; |
dcj001 | 6:881ee0c6dbda | 1125 | //wait(0.1); |
dcj001 | 6:881ee0c6dbda | 1126 | } |
dcj001 | 6:881ee0c6dbda | 1127 | cl=7; |
dcj001 | 6:881ee0c6dbda | 1128 | } |
dcj001 | 6:881ee0c6dbda | 1129 | |
dcj001 | 6:881ee0c6dbda | 1130 | |
dcj001 | 6:881ee0c6dbda | 1131 | } |
dcj001 | 6:881ee0c6dbda | 1132 | |
dcj001 | 6:881ee0c6dbda | 1133 | void SSD1308::display_Axis_y() |
dcj001 | 6:881ee0c6dbda | 1134 | { |
dcj001 | 6:881ee0c6dbda | 1135 | setVerticalAddressingMode(); |
dcj001 | 6:881ee0c6dbda | 1136 | |
dcj001 | 6:881ee0c6dbda | 1137 | setPageAddress(2,6); |
dcj001 | 6:881ee0c6dbda | 1138 | setColumnAddress(0,4); |
dcj001 | 6:881ee0c6dbda | 1139 | for(int j=0;j<5; j++) |
dcj001 | 6:881ee0c6dbda | 1140 | { |
dcj001 | 6:881ee0c6dbda | 1141 | for(uint8_t i=0 ; i<5; i++) |
dcj001 | 6:881ee0c6dbda | 1142 | { |
dcj001 | 6:881ee0c6dbda | 1143 | _sendData(Axis_y[j][i]); |
dcj001 | 6:881ee0c6dbda | 1144 | } |
dcj001 | 6:881ee0c6dbda | 1145 | } |
dcj001 | 6:881ee0c6dbda | 1146 | } |
dcj001 | 6:881ee0c6dbda | 1147 | |
dcj001 | 6:881ee0c6dbda | 1148 | void SSD1308::display_Axis_x() |
dcj001 | 6:881ee0c6dbda | 1149 | { |
dcj001 | 6:881ee0c6dbda | 1150 | setVerticalAddressingMode(); |
dcj001 | 6:881ee0c6dbda | 1151 | |
dcj001 | 6:881ee0c6dbda | 1152 | setPageAddress(7,7); |
dcj001 | 6:881ee0c6dbda | 1153 | setColumnAddress(3,127); |
dcj001 | 6:881ee0c6dbda | 1154 | for(uint8_t j=0; j<127; j++) |
dcj001 | 6:881ee0c6dbda | 1155 | { |
dcj001 | 6:881ee0c6dbda | 1156 | for(uint8_t i=0 ; i<8; i++) |
dcj001 | 6:881ee0c6dbda | 1157 | { |
dcj001 | 6:881ee0c6dbda | 1158 | _sendData(Axis_x[i]); |
dcj001 | 6:881ee0c6dbda | 1159 | } |
dcj001 | 6:881ee0c6dbda | 1160 | } |
dcj001 | 6:881ee0c6dbda | 1161 | } |
dcj001 | 6:881ee0c6dbda | 1162 | |
dcj001 | 6:881ee0c6dbda | 1163 |