SSD1308 128x64 OLED Driver with I2C interface

Dependents:   sense xadow_m0_ada_gps xadow_m0_SD_Hello sense-DHT11 ... more

See http://mbed.org/users/wim/notebook/oled-display-with-ssd1308-driver/#c6729

Committer:
wim
Date:
Thu Jan 01 17:53:40 2015 +0000
Revision:
4:df92b0c0cb92
Parent:
3:fa18169dd7e6
Child:
5:e564cde8e03e
Added option to select optimisation using low level I2C methods. Used for testing on F401 and LPC1768.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 1:b7e8f5139026 1 /** @file SSD1308 I2C device class header 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 0:300d08d9b058 17 //
wim 4:df92b0c0cb92 18 /*
wim 4:df92b0c0cb92 19 ================================================================================
wim 0:300d08d9b058 20 I2Cdev device library code is placed under the MIT license
wim 0:300d08d9b058 21 Copyright (c) 2011 Andrew Schamp
wim 4:df92b0c0cb92 22 Copyright (c) 2012,2013 WH (mbed port)
wim 0:300d08d9b058 23
wim 0:300d08d9b058 24 Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:300d08d9b058 25 of this software and associated documentation files (the "Software"), to deal
wim 0:300d08d9b058 26 in the Software without restriction, including without limitation the rights
wim 0:300d08d9b058 27 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:300d08d9b058 28 copies of the Software, and to permit persons to whom the Software is
wim 0:300d08d9b058 29 furnished to do so, subject to the following conditions:
wim 0:300d08d9b058 30
wim 0:300d08d9b058 31 The above copyright notice and this permission notice shall be included in
wim 0:300d08d9b058 32 all copies or substantial portions of the Software.
wim 0:300d08d9b058 33
wim 0:300d08d9b058 34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:300d08d9b058 35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:300d08d9b058 36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:300d08d9b058 37 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:300d08d9b058 38 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:300d08d9b058 39 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:300d08d9b058 40 THE SOFTWARE.
wim 4:df92b0c0cb92 41 ================================================================================
wim 0:300d08d9b058 42 */
wim 0:300d08d9b058 43
wim 0:300d08d9b058 44 #ifndef SSD1308_H
wim 0:300d08d9b058 45 #define SSD1308_H
wim 0:300d08d9b058 46
wim 0:300d08d9b058 47 // This is the I2C address (8 bit)
wim 0:300d08d9b058 48 // There are two possible addresses: with D/C# (pin 13) grounded, the address is 0x78,
wim 0:300d08d9b058 49 // with D/C# tied high it is 0x7A. Assume grounded by default.
wim 0:300d08d9b058 50 #define SSD1308_SA0 0x78
wim 0:300d08d9b058 51 #define SSD1308_SA1 0x7A
wim 0:300d08d9b058 52 #define SSD1308_DEF_SA SSD1308_SA0
wim 0:300d08d9b058 53
wim 0:300d08d9b058 54 // Display dimensions
wim 0:300d08d9b058 55 #define ROWS 64
wim 0:300d08d9b058 56 #define COLUMNS 128
wim 0:300d08d9b058 57 #define PAGES (ROWS / 8)
wim 0:300d08d9b058 58 #define MAX_PAGE (PAGES - 1)
wim 2:16c84a134393 59 #define MAX_ROW (ROWS - 1)
wim 0:300d08d9b058 60 #define MAX_COL (COLUMNS - 1)
wim 0:300d08d9b058 61
wim 0:300d08d9b058 62 // Character dimensions 8x8 font
wim 0:300d08d9b058 63 #define CHARS (COLUMNS / FONT8x8_WIDTH)
wim 0:300d08d9b058 64
wim 0:300d08d9b058 65 // Command and Datamode
wim 0:300d08d9b058 66 #define COMMAND_MODE 0x80 // continuation bit is set!
wim 0:300d08d9b058 67 #define DATA_MODE 0x40
wim 0:300d08d9b058 68
wim 0:300d08d9b058 69 // Commands and Parameter defines
wim 2:16c84a134393 70 #define SET_LOWER_COLUMN 0x00 // | with lower nibble (Page mode only)
wim 2:16c84a134393 71 #define SET_HIGHER_COLUMN 0x10 // | with higher nibble (Page mode only)
wim 2:16c84a134393 72
wim 0:300d08d9b058 73 #define HORIZONTAL_ADDRESSING_MODE 0x00
wim 0:300d08d9b058 74 #define VERTICAL_ADDRESSING_MODE 0x01
wim 0:300d08d9b058 75 #define PAGE_ADDRESSING_MODE 0x02
wim 0:300d08d9b058 76 #define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte as given above
wim 0:300d08d9b058 77
wim 0:300d08d9b058 78 #define SET_COLUMN_ADDRESS 0x21 // takes two bytes, start address and end address of display data RAM
wim 0:300d08d9b058 79 #define SET_PAGE_ADDRESS 0x22 // takes two bytes, start address and end address of display data RAM
wim 0:300d08d9b058 80
wim 0:300d08d9b058 81 // Command maybe unsupported by SSD1308
wim 0:300d08d9b058 82 #define FADE_INTERVAL_8_FRAMES 0x00
wim 0:300d08d9b058 83 #define FADE_INTERVAL_16_FRAMES 0x01
wim 0:300d08d9b058 84 #define FADE_INTERVAL_24_FRAMES 0x02
wim 0:300d08d9b058 85 #define FADE_INTERVAL_32_FRAMES 0x03
wim 0:300d08d9b058 86 #define FADE_INTERVAL_64_FRAMES 0x07
wim 0:300d08d9b058 87 #define FADE_INTERVAL_128_FRAMES 0x0F
wim 0:300d08d9b058 88 #define FADE_BLINK_DISABLE 0x00
wim 0:300d08d9b058 89 #define FADE_OUT_ENABLE 0x20
wim 0:300d08d9b058 90 #define BLINK_ENABLE 0x30
wim 0:300d08d9b058 91 #define SET_FADE_BLINK 0x23 // takes one byte
wim 0:300d08d9b058 92 // bit5-4 = 0, fade/blink mode
wim 0:300d08d9b058 93 // bit3-0 = Time interval in frames
wim 0:300d08d9b058 94
wim 2:16c84a134393 95 #define SET_DISPLAY_START_LINE 0x40 // | with a row number 0-63 to set start row. (Reset = 0)
wim 0:300d08d9b058 96
wim 0:300d08d9b058 97 #define SET_CONTRAST 0x81 // takes one byte, 0x00 - 0xFF
wim 0:300d08d9b058 98
wim 0:300d08d9b058 99 #define SET_SEGMENT_REMAP_0 0xA0 // column address 0 is mapped to SEG0 (Reset)
wim 0:300d08d9b058 100 #define SET_SEGMENT_REMAP_127 0xA1 // column address 127 is mapped to SEG0
wim 0:300d08d9b058 101
wim 0:300d08d9b058 102 #define SET_DISPLAY_GDDRAM 0xA4 // restores display to contents of RAM
wim 0:300d08d9b058 103 #define SET_ENTIRE_DISPLAY_ON 0xA5 // turns all pixels on, does not affect RAM
wim 0:300d08d9b058 104
wim 0:300d08d9b058 105 #define SET_NORMAL_DISPLAY 0xA6 // a databit of 1 indicates pixel 'ON'
wim 0:300d08d9b058 106 #define SET_INVERSE_DISPLAY 0xA7 // a databit of 1 indicates pixel 'OFF'
wim 0:300d08d9b058 107
wim 0:300d08d9b058 108 #define SET_MULTIPLEX_RATIO 0xA8 // takes one byte, from 16xMUX to 64xMUX (MUX Ratio = byte+1; Default 64)
wim 0:300d08d9b058 109
wim 0:300d08d9b058 110 #define EXTERNAL_IREF 0x10
wim 0:300d08d9b058 111 #define INTERNAL_IREF 0x00
wim 0:300d08d9b058 112 #define SET_IREF_SELECTION 0xAD // sets internal or external Iref
wim 0:300d08d9b058 113
wim 0:300d08d9b058 114 #define SET_DISPLAY_POWER_OFF 0xAE
wim 0:300d08d9b058 115 #define SET_DISPLAY_POWER_ON 0xAF
wim 0:300d08d9b058 116
wim 0:300d08d9b058 117 #define PAGE0 0x00
wim 0:300d08d9b058 118 #define PAGE1 0x01
wim 0:300d08d9b058 119 #define PAGE2 0x02
wim 0:300d08d9b058 120 #define PAGE3 0x03
wim 0:300d08d9b058 121 #define PAGE4 0x04
wim 0:300d08d9b058 122 #define PAGE5 0x05
wim 0:300d08d9b058 123 #define PAGE6 0x06
wim 0:300d08d9b058 124 #define PAGE7 0x07
wim 2:16c84a134393 125 #define SET_PAGE_START_ADDRESS 0xB0 // | with a page number to get start address (Page mode only)
wim 0:300d08d9b058 126
wim 0:300d08d9b058 127 #define SET_COMMON_REMAP_0 0xC0 // row address 0 is mapped to COM0 (Reset)
wim 0:300d08d9b058 128 #define SET_COMMON_REMAP_63 0xC8 // row address 63 is mapped to COM0
wim 0:300d08d9b058 129
wim 0:300d08d9b058 130 #define SET_DISPLAY_OFFSET 0xD3 // takes one byte from 0-63 for vertical shift, Reset = 0
wim 0:300d08d9b058 131
wim 0:300d08d9b058 132 #define SET_DISPLAY_CLOCK 0xD5 // takes one byte
wim 0:300d08d9b058 133 // bit7-4 = Osc Freq DCLK (Reset = 1000b)
wim 0:300d08d9b058 134 // bit3-0 = Divide ration (Reset = oooob, Ratio = 1)
wim 0:300d08d9b058 135
wim 0:300d08d9b058 136 #define SET_PRECHARGE_TIME 0xD9 // takes one byte
wim 0:300d08d9b058 137 // bit7-4 = Phase2, upto 15 DCLKs (Reset = 0010b)
wim 0:300d08d9b058 138 // bit3-0 = Phase1, upto 15 DCLKs (Reset = 0010b)
wim 0:300d08d9b058 139
wim 0:300d08d9b058 140
wim 0:300d08d9b058 141 #define COMMON_BASE 0x02 //
wim 0:300d08d9b058 142 #define COMMON_SEQUENTIAL 0x00 // Sequential common pins config
wim 0:300d08d9b058 143 #define COMMON_ALTERNATIVE 0x10 // Odd/Even common pins config (Reset)
wim 0:300d08d9b058 144 #define COMMON_LEFTRIGHT_NORMAL 0x00 // LeftRight Normal (Reset)
wim 0:300d08d9b058 145 #define COMMON_LEFTRIGHT_FLIP 0x20 // LeftRight Flip
wim 0:300d08d9b058 146 #define SET_COMMON_CONF 0xDA // takes one byte as given above
wim 0:300d08d9b058 147
wim 0:300d08d9b058 148
wim 0:300d08d9b058 149 #define VCOMH_DESELECT_0_65_CODE 0x00
wim 0:300d08d9b058 150 #define VCOMH_DESELECT_0_77_CODE 0x20
wim 0:300d08d9b058 151 #define VCOMH_DESELECT_0_83_CODE 0x30
wim 0:300d08d9b058 152 #define SET_VCOMH_DESELECT_LEVEL 0xDB // takes one byte as given above
wim 0:300d08d9b058 153
wim 0:300d08d9b058 154 #define NOP 0xE3
wim 0:300d08d9b058 155
wim 0:300d08d9b058 156 #define SCROLL_INTERVAL_5_FRAMES 0x00
wim 0:300d08d9b058 157 #define SCROLL_INTERVAL_64_FRAMES 0x01
wim 0:300d08d9b058 158 #define SCROLL_INTERVAL_128_FRAMES 0x02
wim 0:300d08d9b058 159 #define SCROLL_INTERVAL_256_FRAMES 0x03
wim 0:300d08d9b058 160 #define SCROLL_INTERVAL_3_FRAMES 0x04
wim 0:300d08d9b058 161 #define SCROLL_INTERVAL_4_FRAMES 0x05
wim 0:300d08d9b058 162 #define SCROLL_INTERVAL_25_FRAMES 0x06
wim 0:300d08d9b058 163 #define SCROLL_INTERVAL_2_FRAMES 0x07
wim 0:300d08d9b058 164
wim 0:300d08d9b058 165 #define SET_RIGHT_HOR_SCROLL 0x26 // takes 6 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, 0x00, 0xFF
wim 0:300d08d9b058 166 #define SET_LEFT_HOR_SCROLL 0x27 // takes 6 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, 0x00, 0xFF
wim 0:300d08d9b058 167
wim 0:300d08d9b058 168 #define SET_VERT_RIGHT_HOR_SCROLL 0x29 // takes 5 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, VertOffset
wim 0:300d08d9b058 169 #define SET_VERT_LEFT_HOR_SCROLL 0x2A // takes 5 bytes: 0x00, PageStart, Scroll_Interval, PageEnd, VertOffset
wim 0:300d08d9b058 170
wim 0:300d08d9b058 171 #define SET_DEACTIVATE_SCROLL 0x2E
wim 0:300d08d9b058 172 #define SET_ACTIVATE_SCROLL 0x2F
wim 0:300d08d9b058 173
wim 0:300d08d9b058 174 #define SET_VERTICAL_SCROLL_AREA 0xA3 // takes 2 bytes: Rows in Top Area (Reset=0), Rows in Scroll Area (Reset=64)
wim 0:300d08d9b058 175
wim 3:fa18169dd7e6 176
wim 3:fa18169dd7e6 177
wim 3:fa18169dd7e6 178 /** Class to control an SSD1308 based oled Display
wim 3:fa18169dd7e6 179 *
wim 3:fa18169dd7e6 180 * Example:
wim 3:fa18169dd7e6 181 * @code
wim 3:fa18169dd7e6 182 * #include "mbed.h"
wim 3:fa18169dd7e6 183 * #include "mbed_logo.h"
wim 3:fa18169dd7e6 184 * #include "SSD1308.h"
wim 3:fa18169dd7e6 185
wim 3:fa18169dd7e6 186 * //Pin Defines for I2C Bus
wim 3:fa18169dd7e6 187 * #define D_SDA p28
wim 3:fa18169dd7e6 188 * #define D_SCL p27
wim 3:fa18169dd7e6 189 * I2C i2c(D_SDA, D_SCL);
wim 3:fa18169dd7e6 190 *
wim 3:fa18169dd7e6 191 * // Host PC Communication channels
wim 3:fa18169dd7e6 192 * Serial pc(USBTX, USBRX); // tx, rx
wim 3:fa18169dd7e6 193 *
wim 3:fa18169dd7e6 194 * // Instantiate OLED
wim 3:fa18169dd7e6 195 * SSD1308 oled = SSD1308(i2c, SSD1308_SA0);
wim 3:fa18169dd7e6 196 *
wim 3:fa18169dd7e6 197 * int main() {
wim 3:fa18169dd7e6 198 * pc.printf("OLED test start\r");
wim 3:fa18169dd7e6 199 * oled.writeString(0, 0, "Hello World !");
wim 3:fa18169dd7e6 200 * // oled.printf("Hello World !");
wim 3:fa18169dd7e6 201 *
wim 3:fa18169dd7e6 202 * oled.fillDisplay(0xAA);
wim 3:fa18169dd7e6 203 * oled.setDisplayOff();
wim 3:fa18169dd7e6 204 * wait(1);
wim 3:fa18169dd7e6 205 * oled.setDisplayOn();
wim 3:fa18169dd7e6 206 *
wim 3:fa18169dd7e6 207 * oled.clearDisplay();
wim 3:fa18169dd7e6 208 * oled.setDisplayInverse();
wim 3:fa18169dd7e6 209 * wait(0.5);
wim 3:fa18169dd7e6 210 * oled.setDisplayNormal();
wim 3:fa18169dd7e6 211 *
wim 3:fa18169dd7e6 212 * oled.writeBitmap((uint8_t*) mbed_logo);
wim 3:fa18169dd7e6 213 *
wim 3:fa18169dd7e6 214 * pc.printf("OLED test done\r\n");
wim 3:fa18169dd7e6 215 * }
wim 3:fa18169dd7e6 216 *
wim 3:fa18169dd7e6 217 * @endcode
wim 3:fa18169dd7e6 218 */
wim 0:300d08d9b058 219 class SSD1308 : public Stream {
wim 0:300d08d9b058 220 public:
wim 0:300d08d9b058 221
wim 2:16c84a134393 222 /**
wim 2:16c84a134393 223 *@brief Constructor
wim 2:16c84a134393 224 *@param I2C &i2c reference to i2c,
wim 2:16c84a134393 225 *@param uint8_t deviceAddress slaveaddress (8bit to use for the controller (0x78 by default, assumes D/C# (pin 13) grounded)
wim 2:16c84a134393 226 */
wim 0:300d08d9b058 227 SSD1308(I2C &i2c, uint8_t address = SSD1308_DEF_SA);
wim 0:300d08d9b058 228
wim 0:300d08d9b058 229 // High Level methods
wim 2:16c84a134393 230
wim 2:16c84a134393 231 /** @brief High level Init, most settings remain at Power-On reset value
wim 2:16c84a134393 232 */
wim 0:300d08d9b058 233 void initialize();
wim 0:300d08d9b058 234
wim 2:16c84a134393 235 /** @brief clear the display
wim 2:16c84a134393 236 */
wim 0:300d08d9b058 237 void clearDisplay();
wim 2:16c84a134393 238
wim 2:16c84a134393 239
wim 2:16c84a134393 240 /** @brief fill the display
wim 2:16c84a134393 241 * @param uint8_t pattern fillpattern vertical patch or 8 bits
wim 4:df92b0c0cb92 242 * @param uint8_t start_page begin page (0..MAX_PAGE)
wim 4:df92b0c0cb92 243 * @param uint8_t end_page end page (start_page..MAX_PAGE)
wim 4:df92b0c0cb92 244 * @param uint8_t start_col begin column (0..MAX_COL)
wim 4:df92b0c0cb92 245 * @param uint8_t end_col end column (start_col..MAX_COL)
wim 2:16c84a134393 246 */
wim 0:300d08d9b058 247 void fillDisplay(uint8_t pattern = 0x00,
wim 0:300d08d9b058 248 uint8_t start_page=0, uint8_t end_page=MAX_PAGE,
wim 0:300d08d9b058 249 uint8_t start_col=0, uint8_t end_col=MAX_COL);
wim 2:16c84a134393 250
wim 2:16c84a134393 251
wim 2:16c84a134393 252 /** @brief write a bitmap to the display
wim 2:16c84a134393 253 * @param uint8_t* data pointer to bitmap
wim 2:16c84a134393 254 * @param uint8_t start_page begin page (0..MAX_PAGE)
wim 2:16c84a134393 255 * @param uint8_t end_page end page (start_page..MAX_PAGE)
wim 2:16c84a134393 256 * @param uint8_t start_col begin column (0..MAX_COL)
wim 2:16c84a134393 257 * @param uint8_t end_col end column (start_col..MAX_COL)
wim 2:16c84a134393 258 */
wim 0:300d08d9b058 259 void writeBitmap(uint8_t* data,
wim 0:300d08d9b058 260 uint8_t start_page=0, uint8_t end_page=MAX_PAGE,
wim 0:300d08d9b058 261 uint8_t start_col=0, uint8_t end_col=MAX_COL);
wim 0:300d08d9b058 262
wim 2:16c84a134393 263 /** @brief write a level meter to the display, Width is (PRG_MAX_SCALE + 2) pixels
wim 2:16c84a134393 264 * @param uint8_t page begin page (0..MAX_PAGE)
wim 2:16c84a134393 265 * @param uint8_t col begin column (0..MAX_COL)
wim 2:16c84a134393 266 * @param int percentage value (0..100)
wim 2:16c84a134393 267 */
wim 2:16c84a134393 268 void writeProgressBar(uint8_t page, uint8_t col, int percentage);
wim 1:b7e8f5139026 269
wim 2:16c84a134393 270
wim 2:16c84a134393 271 /** @brief write a level meter to the display, Width is (PRG_MAX_SCALE + 2) pixels
wim 2:16c84a134393 272 * @param uint8_t page begin page (0..MAX_PAGE)
wim 2:16c84a134393 273 * @param uint8_t col begin column (0..MAX_COL)
wim 2:16c84a134393 274 * @param int percentage value (0..100)
wim 2:16c84a134393 275 */
wim 2:16c84a134393 276 void writeLevelBar(uint8_t page, uint8_t col, int percentage);
wim 1:b7e8f5139026 277
wim 0:300d08d9b058 278 //void setXY(uint8_t, uint8_t y);
wim 0:300d08d9b058 279
wim 0:300d08d9b058 280 // Select inverted or normal text
wim 0:300d08d9b058 281 void setInverted(bool inverted) { _inverted = inverted; };
wim 0:300d08d9b058 282
wim 2:16c84a134393 283 /** @brief Write single character to the display using the 8x8 fontable
wim 2:16c84a134393 284 * @brief Start at current cursor location
wim 2:16c84a134393 285 * @param char chr character to write
wim 2:16c84a134393 286 */
wim 0:300d08d9b058 287 void writeChar(char chr);
wim 0:300d08d9b058 288
wim 2:16c84a134393 289 /** @brief Write large character (16x24 font)
wim 2:16c84a134393 290 * @param uint8_t row row number (0...MAX_ROW)
wim 2:16c84a134393 291 * @param uint8_t col column number (0...MAX_COL)
wim 2:16c84a134393 292 * @param char chr Used for displaying numbers 0 - 9 and '+', '-', '.'
wim 2:16c84a134393 293 */
wim 0:300d08d9b058 294 void writeBigChar(uint8_t row, uint8_t col, char chr);
wim 0:300d08d9b058 295
wim 2:16c84a134393 296 /** @brief Write a string to the display using the 8x8 font
wim 2:16c84a134393 297 * @brief Start at selected cursor location, text will wrap around until it is done
wim 2:16c84a134393 298 * @param uint8_t row row number (0...ROWS/FONT_HEIGHT)
wim 2:16c84a134393 299 * @param uint8_t col column number (0...COLUMNS/FONT_WIDTH)
wim 2:16c84a134393 300 * @param const char * text pointer to text
wim 2:16c84a134393 301 */
wim 2:16c84a134393 302 void writeString(uint8_t row, uint8_t col, const char* txt);
wim 2:16c84a134393 303
wim 0:300d08d9b058 304 // Stream implementation - provides printf() interface
wim 0:300d08d9b058 305 // You would otherwise be forced to use writeChar() or writeString()
wim 0:300d08d9b058 306 virtual int _putc(int value) { writeChar(value); return 1; };
wim 0:300d08d9b058 307 virtual int _getc() { return -1; };
wim 0:300d08d9b058 308
wim 0:300d08d9b058 309 // Future extension with graphics features
wim 0:300d08d9b058 310 // this must be defined by the subclass
wim 0:300d08d9b058 311 // virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
wim 0:300d08d9b058 312
wim 0:300d08d9b058 313 // Medium Level methods
wim 2:16c84a134393 314
wim 2:16c84a134393 315 /** @brief Set Horizontal Addressing Mode (cursor incr left-to-right, top-to-bottom)
wim 2:16c84a134393 316 *
wim 2:16c84a134393 317 */
wim 0:300d08d9b058 318 void setHorizontalAddressingMode();
wim 2:16c84a134393 319
wim 2:16c84a134393 320 /** @brief Set Vertical Addressing Mode (cursor incr top-to-bottom, left-to-right)
wim 2:16c84a134393 321 *
wim 2:16c84a134393 322 */
wim 0:300d08d9b058 323 void setVerticalAddressingMode();
wim 0:300d08d9b058 324
wim 2:16c84a134393 325 /** @brief Set Page Addressing Mode (cursor incr left-to-right)
wim 2:16c84a134393 326 *
wim 2:16c84a134393 327 */
wim 2:16c84a134393 328 void setPageAddressingMode();
wim 2:16c84a134393 329
wim 2:16c84a134393 330 /** @brief Set Addressing Mode
wim 2:16c84a134393 331 * @param uint8_t mode
wim 2:16c84a134393 332 */
wim 2:16c84a134393 333 void setMemoryAddressingMode(uint8_t mode);
wim 2:16c84a134393 334
wim 2:16c84a134393 335
wim 2:16c84a134393 336 /**
wim 2:16c84a134393 337 * @brief Set Column Start (for Page Addressing Mode only)
wim 2:16c84a134393 338 * @param uint8_t column column start (valid range 0..MAX_COLS)
wim 2:16c84a134393 339 */
wim 2:16c84a134393 340 void setColumnStartForPageAddressingMode(uint8_t column);
wim 0:300d08d9b058 341
wim 2:16c84a134393 342 /**
wim 2:16c84a134393 343 * @brief Set Page Start (for Page Addressing Mode only)
wim 2:16c84a134393 344 * @param uint8_t page page start (valid range PAGE0 - PAGE7)
wim 2:16c84a134393 345 */
wim 2:16c84a134393 346 void setPageStartForPageAddressingMode(uint8_t page);
wim 2:16c84a134393 347
wim 2:16c84a134393 348
wim 2:16c84a134393 349
wim 2:16c84a134393 350 /** @param uint8_t start startcolumn (valid range 0..MAX_COL)
wim 2:16c84a134393 351 * @param uint8_t end endcolumn (valid range start..MAX_COL)
wim 2:16c84a134393 352 */
wim 0:300d08d9b058 353 void setColumnAddress(uint8_t start, uint8_t end);
wim 0:300d08d9b058 354
wim 2:16c84a134393 355 /** @param uint8_t start startpage (valid range 0..MAX_PAGE)
wim 2:16c84a134393 356 * @param uint8_t end endpage (valid range start..MAX_PAGE)
wim 2:16c84a134393 357 */
wim 0:300d08d9b058 358 void setPageAddress(uint8_t start, uint8_t end);
wim 0:300d08d9b058 359
wim 2:16c84a134393 360
wim 2:16c84a134393 361 /**
wim 2:16c84a134393 362 * @brief Set Display StartLine, takes one byte, 0x00-0x3F
wim 2:16c84a134393 363 * @param uint8_t line startline (valid range 0..MAX_ROWS)
wim 2:16c84a134393 364 */
wim 0:300d08d9b058 365 void setDisplayStartLine(uint8_t line);
wim 0:300d08d9b058 366
wim 2:16c84a134393 367 /** @brief Set Contrast
wim 2:16c84a134393 368 * @param uint8_t contrast (valid range 0x00 (lowest) - 0xFF (highest))
wim 2:16c84a134393 369 */
wim 0:300d08d9b058 370 void setContrastControl(uint8_t contrast);
wim 2:16c84a134393 371
wim 2:16c84a134393 372
wim 2:16c84a134393 373 /** @brief Shows All Pixels On
wim 2:16c84a134393 374 */
wim 2:16c84a134393 375 void setEntireDisplayOn();
wim 0:300d08d9b058 376
wim 2:16c84a134393 377 /** @brief Shows Pixels as RAM content
wim 2:16c84a134393 378 */
wim 0:300d08d9b058 379 void setEntireDisplayRAM();
wim 2:16c84a134393 380
wim 2:16c84a134393 381 /** @brief Shows Pixels On or as RAM content
wim 2:16c84a134393 382 * @param bool on (true is All on, false is RAM content)
wim 2:16c84a134393 383 */
wim 0:300d08d9b058 384 void setEntireDisplay(bool on);
wim 0:300d08d9b058 385
wim 2:16c84a134393 386
wim 2:16c84a134393 387 // @brief Set Display line MPX Ratio, takes one byte, 0x00-0x3F
wim 2:16c84a134393 388 // @param uint8_t lines (valid range 0..MAX_ROWS)
wim 2:16c84a134393 389 void setMultiplexRatio(uint8_t lines);
wim 2:16c84a134393 390
wim 2:16c84a134393 391
wim 2:16c84a134393 392 /** @brief Sets Internal Iref
wim 2:16c84a134393 393 */
wim 0:300d08d9b058 394 void setInternalIref();
wim 2:16c84a134393 395
wim 2:16c84a134393 396 /** @brief Sets External Iref (default)
wim 2:16c84a134393 397 */
wim 0:300d08d9b058 398 void setExternalIref();
wim 2:16c84a134393 399
wim 2:16c84a134393 400
wim 2:16c84a134393 401 /** @brief Enable Display
wim 2:16c84a134393 402 */
wim 0:300d08d9b058 403 void setDisplayOn();
wim 2:16c84a134393 404
wim 2:16c84a134393 405 /** @brief Disable Display
wim 2:16c84a134393 406 */
wim 0:300d08d9b058 407 void setDisplayOff();
wim 2:16c84a134393 408
wim 2:16c84a134393 409 /** @brief Enable or Disable Display
wim 2:16c84a134393 410 * @param bool on
wim 2:16c84a134393 411 */
wim 0:300d08d9b058 412 void setDisplayPower(bool on);
wim 0:300d08d9b058 413
wim 2:16c84a134393 414 /** @brief Show White pixels on Black background
wim 2:16c84a134393 415 */
wim 0:300d08d9b058 416 void setDisplayNormal();
wim 2:16c84a134393 417
wim 2:16c84a134393 418 /** @brief Show Black pixels on White background
wim 2:16c84a134393 419 */
wim 0:300d08d9b058 420 void setDisplayInverse();
wim 2:16c84a134393 421
wim 2:16c84a134393 422 /** @brief Blink display by fading in and out over a set number of frames
wim 2:16c84a134393 423 * @param bool on
wim 2:16c84a134393 424 */
wim 2:16c84a134393 425 void setDisplayBlink(bool on);
wim 0:300d08d9b058 426
wim 2:16c84a134393 427 /** @brief Fade out display in set number of frames
wim 2:16c84a134393 428 * @param bool on
wim 2:16c84a134393 429 */
wim 0:300d08d9b058 430 void setDisplayFade(bool on);
wim 0:300d08d9b058 431
wim 2:16c84a134393 432 /** @brief Display Flip (Left/Right, Up/Down)
wim 2:16c84a134393 433 * @param bool left flip Left/Right
wim 2:16c84a134393 434 * @param bool down flip Up/Down
wim 2:16c84a134393 435 */
wim 0:300d08d9b058 436 void setDisplayFlip(bool left, bool down);
wim 0:300d08d9b058 437
wim 2:16c84a134393 438 // Set vertical shift by COM from 0 - 63 (0x00 - 0x3F) (Reset = 0x00)
wim 0:300d08d9b058 439 void setDisplayOffset(uint8_t offset);
wim 0:300d08d9b058 440
wim 2:16c84a134393 441 // Oscillator freq 0x00-0x0F (reset 0x08)
wim 0:300d08d9b058 442 // Divide ratio 0x00-0x0F, value +1 (reset 0x00)
wim 0:300d08d9b058 443 void setDisplayClock(uint8_t divideRatio, uint8_t oscFreq);
wim 0:300d08d9b058 444
wim 0:300d08d9b058 445 // Phase1 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
wim 0:300d08d9b058 446 // Phase2 0x01-0x0F period of up to 15 DCLK clocks (reset 0x02, 0 is invalid)
wim 0:300d08d9b058 447 void setPrechargePeriod(uint8_t phase1, uint8_t phase2);
wim 0:300d08d9b058 448
wim 2:16c84a134393 449 // See defines above for levels
wim 0:300d08d9b058 450 void setVcomhDeselectLevel(uint8_t level);
wim 2:16c84a134393 451
wim 2:16c84a134393 452
wim 0:300d08d9b058 453 // Command for no-operation
wim 0:300d08d9b058 454 void nop();
wim 0:300d08d9b058 455
wim 2:16c84a134393 456
wim 2:16c84a134393 457 /** @brief Horizontal scroll by one column per interval
wim 2:16c84a134393 458 * @param bool left select Left/Right scroll
wim 2:16c84a134393 459 * @param uint8_t start_page begin page (0..MAX_PAGE)
wim 2:16c84a134393 460 * @param uint8_t end_page end page (start_page..MAX_PAGE)
wim 2:16c84a134393 461 * @param uint8_t interval scroll interval in frames (see codes above)
wim 2:16c84a134393 462 */
wim 2:16c84a134393 463 void setContinuousHorizontalScroll(bool left, uint8_t start_page, uint8_t end_page, uint8_t interval);
wim 2:16c84a134393 464
wim 2:16c84a134393 465
wim 2:16c84a134393 466 /** @brief Horizontal and Vertical scroll by one column per interval
wim 2:16c84a134393 467 * @param bool left select Left/Right scroll
wim 2:16c84a134393 468 * @param uint8_t start_page begin page (0..MAX_PAGE)
wim 2:16c84a134393 469 * @param uint8_t end_page end page (start_page..MAX_PAGE)
wim 2:16c84a134393 470 * @param uint8_t offset vert offset (0x01..0x63)
wim 2:16c84a134393 471 * @param uint8_t interval scroll interval in frames (see codes above)
wim 2:16c84a134393 472 */
wim 2:16c84a134393 473 void setContinuousVerticalAndHorizontalScroll(bool left, uint8_t start_page, uint8_t end_page,
wim 2:16c84a134393 474 uint8_t offset, uint8_t interval);
wim 0:300d08d9b058 475
wim 2:16c84a134393 476 /** @brief Activate or Deactivate Horizontal and Vertical scroll
wim 2:16c84a134393 477 * @brief Note: after deactivating scrolling, the RAM data needs to be rewritten
wim 2:16c84a134393 478 * @param bool on activate scroll
wim 2:16c84a134393 479 */
wim 2:16c84a134393 480 void setDisplayScroll(bool on);
wim 0:300d08d9b058 481
wim 2:16c84a134393 482
wim 2:16c84a134393 483 /** @brief Set Vertical scroll area
wim 2:16c84a134393 484 * @param uint8_t topRowsFixed fixed rows (0..MAX_ROW)
wim 2:16c84a134393 485 * @param uint8_t scrollRowsoffset scroll rows (topRowsFixed..MAX_ROW)
wim 2:16c84a134393 486 */
wim 0:300d08d9b058 487 void setVerticalScrollArea(uint8_t topRowsFixed, uint8_t scrollRows);
wim 0:300d08d9b058 488
wim 0:300d08d9b058 489 private:
wim 0:300d08d9b058 490
wim 0:300d08d9b058 491 // Low Level methods
wim 2:16c84a134393 492
wim 2:16c84a134393 493 /** @brief Write command that has no parameters
wim 2:16c84a134393 494 */
wim 2:16c84a134393 495 void _sendCommand(uint8_t command);
wim 2:16c84a134393 496
wim 2:16c84a134393 497 /** @brief Write command that has one parameter
wim 2:16c84a134393 498 */
wim 0:300d08d9b058 499 void _sendCommand(uint8_t command, uint8_t param1);
wim 2:16c84a134393 500
wim 2:16c84a134393 501 /** @brief Write command that has two parameters
wim 2:16c84a134393 502 */
wim 0:300d08d9b058 503 void _sendCommand(uint8_t command, uint8_t param1, uint8_t param2);
wim 0:300d08d9b058 504 // void sendCommands(uint8_t len, uint8_t* buf);
wim 2:16c84a134393 505
wim 2:16c84a134393 506 /** @brief Write command that has five parameters
wim 2:16c84a134393 507 */
wim 2:16c84a134393 508 void _sendCommand(uint8_t command, uint8_t param1, uint8_t param2,
wim 2:16c84a134393 509 uint8_t param3, uint8_t param4,
wim 2:16c84a134393 510 uint8_t param5);
wim 2:16c84a134393 511
wim 2:16c84a134393 512 /** @brief Write command that has six parameters
wim 2:16c84a134393 513 */
wim 2:16c84a134393 514 void _sendCommand(uint8_t command, uint8_t param1, uint8_t param2,
wim 2:16c84a134393 515 uint8_t param3, uint8_t param4,
wim 2:16c84a134393 516 uint8_t param5, uint8_t param6);
wim 0:300d08d9b058 517
wim 2:16c84a134393 518 /** @brief Write databyte to display
wim 2:16c84a134393 519 * @brief Start at current cursor location
wim 2:16c84a134393 520 * @param uint8_t data databyte to write
wim 2:16c84a134393 521 */
wim 0:300d08d9b058 522 void _sendData(uint8_t data);
wim 2:16c84a134393 523
wim 2:16c84a134393 524 /** @brief Write len bytes from buffer data to display,
wim 2:16c84a134393 525 * @brief Start at current cursor location
wim 2:16c84a134393 526 * @param uint8_t len number of bytes to write
wim 2:16c84a134393 527 * @param uint8_t* data pointer to data
wim 2:16c84a134393 528 */
wim 0:300d08d9b058 529 void _sendData(uint8_t len, uint8_t* data);
wim 0:300d08d9b058 530
wim 2:16c84a134393 531 /** @brief Low level Init
wim 2:16c84a134393 532 * @brief Init the configuration registers in accordance with the datasheet
wim 2:16c84a134393 533 */
wim 0:300d08d9b058 534 void _init();
wim 0:300d08d9b058 535
wim 1:b7e8f5139026 536 I2C _i2c; // I2C bus reference
wim 1:b7e8f5139026 537 uint8_t _readOpcode; // contains the I2C address of the device
wim 1:b7e8f5139026 538 uint8_t _writeOpcode; // contains the I2C address of the device
wim 0:300d08d9b058 539
wim 0:300d08d9b058 540 bool _inverted; // inverted or normal text
wim 0:300d08d9b058 541 };
wim 0:300d08d9b058 542
wim 0:300d08d9b058 543 #endif