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