derived from Aidafruit SSD1306 library

Dependents:   Test_SSD1306 L152RE_OLED_SSD1306 EcranZumo

Fork of SSD1306 by Jonathan Gaul

Committer:
masato
Date:
Sun May 04 14:29:51 2014 +0000
Revision:
4:ec5add86f335
Parent:
3:1d9df877c90a
derived from Aidafruit SSD1306 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Byrn 0:21cb91208386 1 #ifndef __SSD1306_H__
Byrn 0:21cb91208386 2 #define __SSD1306_H__
Byrn 0:21cb91208386 3
Byrn 0:21cb91208386 4 #define FONT_START ' ' /* First character value in the font table */
Byrn 0:21cb91208386 5
Byrn 1:1d58d378221c 6 /** SSD1306 Controller Driver
Byrn 3:1d9df877c90a 7 *
Byrn 3:1d9df877c90a 8 * This class provides a buffered display for the SSD1306 OLED controller.
Byrn 3:1d9df877c90a 9 *
Byrn 3:1d9df877c90a 10 * TODO:
Byrn 3:1d9df877c90a 11 * - At the moment, the driver assumes a 128x64 pixel display.
Byrn 3:1d9df877c90a 12 * - Only fonts of 8 pixel height are supported (different widths can be used).
Byrn 3:1d9df877c90a 13 * - Pretty much no drawing functions are provided as yet.
Byrn 3:1d9df877c90a 14 * - Possible "auto-update", automatically calling update() after a printf etc.
Byrn 3:1d9df877c90a 15 *
Byrn 3:1d9df877c90a 16 * Information taken from the datasheet at:
Byrn 3:1d9df877c90a 17 * http://www.adafruit.com/datasheets/SSD1306.pdf
Byrn 3:1d9df877c90a 18 *
Byrn 3:1d9df877c90a 19 */
Byrn 0:21cb91208386 20 class SSD1306
Byrn 0:21cb91208386 21 {
Byrn 0:21cb91208386 22 public:
Byrn 1:1d58d378221c 23 /** Construct a new SSD1306 object.
Byrn 1:1d58d378221c 24 * @param cs The connected C/S pin.
Byrn 1:1d58d378221c 25 * @param rs The connected RS pin.
Byrn 1:1d58d378221c 26 * @param dc The connected DC pin.
Byrn 1:1d58d378221c 27 * @param clk The connected CLK pin.
Byrn 1:1d58d378221c 28 * @param data The connected Data pin.
Byrn 2:e479b0296757 29 */
Byrn 0:21cb91208386 30 SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data);
Byrn 0:21cb91208386 31
Byrn 2:e479b0296757 32 // ----- HARDWARE CONTROL -----
Byrn 2:e479b0296757 33
Byrn 2:e479b0296757 34 /** Initialise the display with defaults.*/
Byrn 0:21cb91208386 35 void initialise();
Byrn 2:e479b0296757 36
Byrn 2:e479b0296757 37 /** Force a refresh of the display. Copies the buffer to the controller. */
Byrn 0:21cb91208386 38 void update();
Byrn 0:21cb91208386 39
Byrn 2:e479b0296757 40 /** Turn the whole display off. This will reset all configuration settings on the controller to their defaults. */
Byrn 0:21cb91208386 41 void off();
Byrn 2:e479b0296757 42
Byrn 2:e479b0296757 43 /** Turn the whole display on. Used during initialisation. */
Byrn 0:21cb91208386 44 void on();
Byrn 2:e479b0296757 45
Byrn 2:e479b0296757 46 /** Sends the display to sleep, but leaves RAM intact. */
Byrn 2:e479b0296757 47 void sleep();
Byrn 2:e479b0296757 48
Byrn 2:e479b0296757 49 /** Wakes up this display following a sleep() call.
Byrn 2:e479b0296757 50 * @see sleep()
Byrn 2:e479b0296757 51 */
Byrn 2:e479b0296757 52 void wake();
Byrn 2:e479b0296757 53
Byrn 2:e479b0296757 54 /** Set the display contrast.
Byrn 2:e479b0296757 55 * @param value The contrast, from 1 to 256.
Byrn 2:e479b0296757 56 */
Byrn 2:e479b0296757 57 void set_contrast(unsigned char value); // 1-256
Byrn 2:e479b0296757 58
Byrn 2:e479b0296757 59 /** Set the display to normal or inverse.
Byrn 2:e479b0296757 60 * @param value 0 for normal mode, or 1 for inverse mode.
Byrn 2:e479b0296757 61 */
Byrn 2:e479b0296757 62 void set_inverse(unsigned char value); // 0 or 1
Byrn 0:21cb91208386 63
Byrn 2:e479b0296757 64 /** Set the display start line. This is the line at which the display will start rendering.
Byrn 2:e479b0296757 65 * @param value A value from 0 to 63 denoting the line to start at.
Byrn 2:e479b0296757 66 */
Byrn 2:e479b0296757 67 void set_display_start_line(unsigned char value); // 0-63
Byrn 2:e479b0296757 68
Byrn 2:e479b0296757 69 /** Set the segment remap state. This allows the module to be addressed as if flipped horizontally.
Byrn 2:e479b0296757 70 * NOTE: Changing this setting has no effect on data already in the module's GDDRAM.
Byrn 2:e479b0296757 71 * @param value 0 = column address 0 = segment 0 (the default), 1 = column address 127 = segment 0 (flipped).
Byrn 2:e479b0296757 72 */
Byrn 2:e479b0296757 73 void set_segment_remap(unsigned char value); // 0 or 1
Byrn 2:e479b0296757 74
Byrn 2:e479b0296757 75 /** Set the vertical shift by COM.
Byrn 2:e479b0296757 76 * @param value The number of rows to shift, from 0 - 63.
Byrn 2:e479b0296757 77 */
Byrn 2:e479b0296757 78 void set_display_offset(unsigned char value); // 0-63
Byrn 2:e479b0296757 79
Byrn 2:e479b0296757 80 /** Set the multiplex ratio.
Byrn 2:e479b0296757 81 * @param value MUX will be set to (value+1). Valid values range from 15 to 63 - MUX 16 to 64.
Byrn 2:e479b0296757 82 */
Byrn 2:e479b0296757 83 void set_multiplex_ratio(unsigned char value); // 15-63 (value+1 mux)
Byrn 2:e479b0296757 84
Byrn 2:e479b0296757 85 /** Set COM output scan direction. If the display is active, this will immediately vertically
Byrn 2:e479b0296757 86 * flip the display.
Byrn 2:e479b0296757 87 * @param value 0 = Scan from COM0 (default), 1 = reversed (scan from COM[N-1]).
Byrn 2:e479b0296757 88 */
Byrn 2:e479b0296757 89 void set_com_output_scan_direction(unsigned char value); // 0 or 1
Byrn 1:1d58d378221c 90
Byrn 2:e479b0296757 91 /** Set COM pins hardware configuration.
Byrn 2:e479b0296757 92 * @param sequential 0 = Sequental COM pin configuration, 1 = Alternative COM pin configuration (default).
Byrn 2:e479b0296757 93 * @param lr_remap 0 = Disable COM left/right remap (default), 1 = enable COM left/right remap.
Byrn 2:e479b0296757 94 */
Byrn 2:e479b0296757 95 void set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap); // 0 or 1 for both parametrs
Byrn 2:e479b0296757 96
Byrn 2:e479b0296757 97 /** Set up and start a continuous horizontal scroll.
Byrn 2:e479b0296757 98 * Once you have set up the scrolling, you can deactivate it with stop_scroll().
Byrn 2:e479b0296757 99 * @param direction 0 for right, 1 for left.
Byrn 2:e479b0296757 100 * @param start Start page address, 0 - 5.
Byrn 2:e479b0296757 101 * @param end End page address, 0 - 5.
Byrn 2:e479b0296757 102 * @param interval Interval in frame frequency. Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
Byrn 2:e479b0296757 103 * @see stop_scrol
Byrn 2:e479b0296757 104 */
Byrn 2:e479b0296757 105 void start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval);
Byrn 2:e479b0296757 106
Byrn 2:e479b0296757 107 /** Set up and start a continuous horizontal and vertical scroll.
Byrn 2:e479b0296757 108 * NOTE: No continuous vertical scroll is available.
Byrn 2:e479b0296757 109 * Once you have set up the scrolling, you can deactivate it with stop_scroll().
Byrn 2:e479b0296757 110 * @param direction 0 for vertical and right horizontal scroll, 1 for vertical and left horizontal scroll.
Byrn 2:e479b0296757 111 * @param start Start page address, 0 - 5.
Byrn 2:e479b0296757 112 * @param end End page address, 0 - 5.
Byrn 2:e479b0296757 113 * @param interval Interval in frame frequency. Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
Byrn 2:e479b0296757 114 * @param vertical_offset Offset of vertical scroll, 1 - 63.
Byrn 2:e479b0296757 115 * @see stop_scroll
Byrn 2:e479b0296757 116 */
Byrn 2:e479b0296757 117 void start_vertical_and_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval, unsigned char vertical_offset);
Byrn 2:e479b0296757 118
Byrn 2:e479b0296757 119 /** Deactivate the continuous scroll set up with start_horizontal_scroll() or
Byrn 2:e479b0296757 120 * start_vertical_and_horizontal_scroll().
Byrn 2:e479b0296757 121 * @see set_horizontal_scroll, set_vertical_and_horizontal_scroll
Byrn 2:e479b0296757 122 */
Byrn 2:e479b0296757 123 void stop_scroll();
Byrn 2:e479b0296757 124
Byrn 2:e479b0296757 125 // ----- ADDRESSING -----
Byrn 2:e479b0296757 126
Byrn 2:e479b0296757 127 /** Set memory addressing mode to the given value.
Byrn 2:e479b0296757 128 * @param mode 0 for Horizontal addressing mode, 1 for Vertical addressing mode, or 2 for Page addressing mode (PAM). 2 is the default.
Byrn 2:e479b0296757 129 */
Byrn 2:e479b0296757 130 void set_memory_addressing_mode(unsigned char mode);
Byrn 2:e479b0296757 131
Byrn 2:e479b0296757 132 /** Page Addressing Mode: Set the column start address register for
Byrn 2:e479b0296757 133 * page addressing mode.
Byrn 2:e479b0296757 134 * @param address The address (full byte).
Byrn 2:e479b0296757 135 */
Byrn 2:e479b0296757 136 void pam_set_start_address(unsigned char address);
Byrn 2:e479b0296757 137
Byrn 2:e479b0296757 138 /** Set the GDDRAM page start address for page addressing mode.
Byrn 2:e479b0296757 139 * @param address The start page, 0 - 7.
Byrn 2:e479b0296757 140 */
Byrn 2:e479b0296757 141 void pam_set_page_start(unsigned char address);
Byrn 2:e479b0296757 142
Byrn 2:e479b0296757 143 /** Set page start and end address for horizontal/vertical addressing mode.
Byrn 2:e479b0296757 144 * @param start The start page, 0 - 7.
Byrn 2:e479b0296757 145 * @param end The end page, 0 - 7.
Byrn 2:e479b0296757 146 */
Byrn 2:e479b0296757 147 void hv_set_page_address(unsigned char start, unsigned char end);
Byrn 2:e479b0296757 148
Byrn 2:e479b0296757 149 /** Set column address range for horizontal/vertical addressing mode.
Byrn 2:e479b0296757 150 * @param start Column start address, 0 - 127.
Byrn 2:e479b0296757 151 * @param end Column end address, 0 - 127.
Byrn 2:e479b0296757 152 */
Byrn 2:e479b0296757 153 void hv_set_column_address(unsigned char start, unsigned char end);
Byrn 2:e479b0296757 154
Byrn 2:e479b0296757 155 // ----- TIMING & DRIVING -----
Byrn 2:e479b0296757 156 /** Set the display clock divide ratio and the oscillator frequency.
Byrn 2:e479b0296757 157 * @param ratio The divide ratio, default is 0.
Byrn 2:e479b0296757 158 * @param frequency The oscillator frequency, 0 - 127. Default is 8.
Byrn 2:e479b0296757 159 */
Byrn 2:e479b0296757 160 void set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency);
Byrn 2:e479b0296757 161
Byrn 2:e479b0296757 162 /** Set the precharge period.
Byrn 2:e479b0296757 163 * @param phase1 Phase 1 period in DCLK clocks. 1 - 15, default is 2.
Byrn 2:e479b0296757 164 * @param phase2 Phase 2 period in DCLK clocks. 1 - 15, default is 2.
Byrn 2:e479b0296757 165 */
Byrn 2:e479b0296757 166 void set_precharge_period(unsigned char phase1, unsigned char phase2);
Byrn 2:e479b0296757 167
Byrn 2:e479b0296757 168 /** Set the Vcomh deselect level.
Byrn 2:e479b0296757 169 * @param level 0 = 0.65 x Vcc, 1 = 0.77 x Vcc (default), 2 = 0.83 x Vcc.
Byrn 2:e479b0296757 170 */
Byrn 2:e479b0296757 171 void set_vcomh_deselect_level(unsigned char level);
Byrn 2:e479b0296757 172
Byrn 2:e479b0296757 173 /** Perform a "no operation".
Byrn 2:e479b0296757 174 */
Byrn 2:e479b0296757 175 void nop();
Byrn 2:e479b0296757 176
Byrn 2:e479b0296757 177 /** Enable/disable charge pump.
Byrn 2:e479b0296757 178 @param enable 0 to disable, 1 to enable the internal charge pump.
Byrn 2:e479b0296757 179 */
Byrn 2:e479b0296757 180 void set_charge_pump_enable(unsigned char enable);
Byrn 2:e479b0296757 181
Byrn 2:e479b0296757 182 // ----- BUFFER EDITING -----
Byrn 0:21cb91208386 183
masato 4:ec5add86f335 184 void clear();
masato 4:ec5add86f335 185 void drawBitmap(int x, int y, const unsigned char *bitmap, int w, int h, int color = 1);
Byrn 0:21cb91208386 186 void set_pixel(int x, int y);
Byrn 0:21cb91208386 187 void clear_pixel(int x, int y);
Byrn 0:21cb91208386 188 void line(int x0, int y0, int x1, int y1);
Byrn 2:e479b0296757 189
Byrn 3:1d9df877c90a 190 /** Set the current console font.
Byrn 3:1d9df877c90a 191 * @param font Font data, layed out vertically!
Byrn 3:1d9df877c90a 192 * @param width Width of the font characters in pixels.
Byrn 3:1d9df877c90a 193 * Fonts are always (at present) 8 pixels in height.
Byrn 3:1d9df877c90a 194 */
Byrn 3:1d9df877c90a 195 void set_font(unsigned char *font, unsigned int width);
Byrn 3:1d9df877c90a 196
Byrn 3:1d9df877c90a 197 /** Set double height text output.
Byrn 3:1d9df877c90a 198 * @param double_height If 1, calls to putc(), printf() etc will
Byrn 3:1d9df877c90a 199 * result in text taking up 2 lines instead of 1.
Byrn 3:1d9df877c90a 200 */
Byrn 3:1d9df877c90a 201 void set_double_height_text(unsigned int double_height);
Byrn 3:1d9df877c90a 202
Byrn 3:1d9df877c90a 203 /** Put a single character to the screen buffer.
Byrn 3:1d9df877c90a 204 * Repeated calls to putc() will cause the cursor to move across and
Byrn 3:1d9df877c90a 205 * then down as needed, with scrolling.
Byrn 3:1d9df877c90a 206 * @param c The character to write.
Byrn 3:1d9df877c90a 207 */
Byrn 3:1d9df877c90a 208 void putc(unsigned char c);
Byrn 3:1d9df877c90a 209
Byrn 3:1d9df877c90a 210 /** Print to the screen buffer.
Byrn 3:1d9df877c90a 211 * printf() will wrap and scroll the screen as needed to display the text given.
Byrn 3:1d9df877c90a 212 * @param format Format specifier, same as printf() in normal C.
Byrn 3:1d9df877c90a 213 */
Byrn 3:1d9df877c90a 214 void printf(const char *format, ...);
Byrn 3:1d9df877c90a 215
Byrn 3:1d9df877c90a 216 /** Scroll the screen buffer up by one line. */
Byrn 3:1d9df877c90a 217 void scroll_up();
Byrn 0:21cb91208386 218
Byrn 0:21cb91208386 219 private:
Byrn 0:21cb91208386 220 SPI _spi;
Byrn 0:21cb91208386 221 DigitalOut _cs, _reset, _dc;
Byrn 2:e479b0296757 222 unsigned char _screen[1024];
Byrn 2:e479b0296757 223
Byrn 0:21cb91208386 224 int _cursor_x, _cursor_y;
Byrn 0:21cb91208386 225
Byrn 1:1d58d378221c 226 void _send_command(unsigned char code);
Byrn 1:1d58d378221c 227 void _send_data(unsigned char value);
Byrn 3:1d9df877c90a 228
Byrn 3:1d9df877c90a 229 unsigned char *_console_font_data;
Byrn 3:1d9df877c90a 230 unsigned int _console_font_width;
Byrn 3:1d9df877c90a 231 unsigned int _double_height_text;
Byrn 0:21cb91208386 232 };
Byrn 0:21cb91208386 233
Byrn 0:21cb91208386 234 #define SSD1306_LCDWIDTH 128
Byrn 0:21cb91208386 235 #define SSD1306_LCDHEIGHT 64
Byrn 0:21cb91208386 236
Byrn 0:21cb91208386 237 #endif