derived from Aidafruit SSD1306 library
Dependents: Test_SSD1306 L152RE_OLED_SSD1306 EcranZumo
Fork of SSD1306 by
ssd1306.h@2:e479b0296757, 2013-02-05 (annotated)
- Committer:
- Byrn
- Date:
- Tue Feb 05 21:21:22 2013 +0000
- Revision:
- 2:e479b0296757
- Parent:
- 1:1d58d378221c
- Child:
- 3:1d9df877c90a
commit before initial publish
Who changed what in which revision?
User | Revision | Line number | New 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_HEIGHT_OFFSET 0 /* Character pixel height (in multiples of 8) at this position */ |
Byrn | 0:21cb91208386 | 5 | #define FONT_SIZE_OFFSET 1 /* Character data size (in bytes) at this position */ |
Byrn | 0:21cb91208386 | 6 | #define FONT_DATA_OFFSET 2 /* Data starts at this position */ |
Byrn | 0:21cb91208386 | 7 | #define FONT_START ' ' /* First character value in the font table */ |
Byrn | 0:21cb91208386 | 8 | |
Byrn | 1:1d58d378221c | 9 | /** SSD1306 Controller Driver |
Byrn | 1:1d58d378221c | 10 | * |
Byrn | 2:e479b0296757 | 11 | * Information taken from the datasheet at: |
Byrn | 1:1d58d378221c | 12 | * http://www.adafruit.com/datasheets/SSD1306.pdf |
Byrn | 1:1d58d378221c | 13 | */ |
Byrn | 0:21cb91208386 | 14 | class SSD1306 |
Byrn | 0:21cb91208386 | 15 | { |
Byrn | 0:21cb91208386 | 16 | public: |
Byrn | 1:1d58d378221c | 17 | /** Construct a new SSD1306 object. |
Byrn | 1:1d58d378221c | 18 | * @param cs The connected C/S pin. |
Byrn | 1:1d58d378221c | 19 | * @param rs The connected RS pin. |
Byrn | 1:1d58d378221c | 20 | * @param dc The connected DC pin. |
Byrn | 1:1d58d378221c | 21 | * @param clk The connected CLK pin. |
Byrn | 1:1d58d378221c | 22 | * @param data The connected Data pin. |
Byrn | 2:e479b0296757 | 23 | */ |
Byrn | 0:21cb91208386 | 24 | SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data); |
Byrn | 0:21cb91208386 | 25 | |
Byrn | 2:e479b0296757 | 26 | // ----- HARDWARE CONTROL ----- |
Byrn | 2:e479b0296757 | 27 | |
Byrn | 2:e479b0296757 | 28 | /** Initialise the display with defaults.*/ |
Byrn | 0:21cb91208386 | 29 | void initialise(); |
Byrn | 2:e479b0296757 | 30 | |
Byrn | 2:e479b0296757 | 31 | /** Force a refresh of the display. Copies the buffer to the controller. */ |
Byrn | 0:21cb91208386 | 32 | void update(); |
Byrn | 0:21cb91208386 | 33 | |
Byrn | 2:e479b0296757 | 34 | /** Turn the whole display off. This will reset all configuration settings on the controller to their defaults. */ |
Byrn | 0:21cb91208386 | 35 | void off(); |
Byrn | 2:e479b0296757 | 36 | |
Byrn | 2:e479b0296757 | 37 | /** Turn the whole display on. Used during initialisation. */ |
Byrn | 0:21cb91208386 | 38 | void on(); |
Byrn | 2:e479b0296757 | 39 | |
Byrn | 2:e479b0296757 | 40 | /** Sends the display to sleep, but leaves RAM intact. */ |
Byrn | 2:e479b0296757 | 41 | void sleep(); |
Byrn | 2:e479b0296757 | 42 | |
Byrn | 2:e479b0296757 | 43 | /** Wakes up this display following a sleep() call. |
Byrn | 2:e479b0296757 | 44 | * @see sleep() |
Byrn | 2:e479b0296757 | 45 | */ |
Byrn | 2:e479b0296757 | 46 | void wake(); |
Byrn | 2:e479b0296757 | 47 | |
Byrn | 2:e479b0296757 | 48 | /** Set the display contrast. |
Byrn | 2:e479b0296757 | 49 | * @param value The contrast, from 1 to 256. |
Byrn | 2:e479b0296757 | 50 | */ |
Byrn | 2:e479b0296757 | 51 | void set_contrast(unsigned char value); // 1-256 |
Byrn | 2:e479b0296757 | 52 | |
Byrn | 2:e479b0296757 | 53 | /** Set the display to normal or inverse. |
Byrn | 2:e479b0296757 | 54 | * @param value 0 for normal mode, or 1 for inverse mode. |
Byrn | 2:e479b0296757 | 55 | */ |
Byrn | 2:e479b0296757 | 56 | void set_inverse(unsigned char value); // 0 or 1 |
Byrn | 0:21cb91208386 | 57 | |
Byrn | 2:e479b0296757 | 58 | /** Set the display start line. This is the line at which the display will start rendering. |
Byrn | 2:e479b0296757 | 59 | * @param value A value from 0 to 63 denoting the line to start at. |
Byrn | 2:e479b0296757 | 60 | */ |
Byrn | 2:e479b0296757 | 61 | void set_display_start_line(unsigned char value); // 0-63 |
Byrn | 2:e479b0296757 | 62 | |
Byrn | 2:e479b0296757 | 63 | /** Set the segment remap state. This allows the module to be addressed as if flipped horizontally. |
Byrn | 2:e479b0296757 | 64 | * NOTE: Changing this setting has no effect on data already in the module's GDDRAM. |
Byrn | 2:e479b0296757 | 65 | * @param value 0 = column address 0 = segment 0 (the default), 1 = column address 127 = segment 0 (flipped). |
Byrn | 2:e479b0296757 | 66 | */ |
Byrn | 2:e479b0296757 | 67 | void set_segment_remap(unsigned char value); // 0 or 1 |
Byrn | 2:e479b0296757 | 68 | |
Byrn | 2:e479b0296757 | 69 | /** Set the vertical shift by COM. |
Byrn | 2:e479b0296757 | 70 | * @param value The number of rows to shift, from 0 - 63. |
Byrn | 2:e479b0296757 | 71 | */ |
Byrn | 2:e479b0296757 | 72 | void set_display_offset(unsigned char value); // 0-63 |
Byrn | 2:e479b0296757 | 73 | |
Byrn | 2:e479b0296757 | 74 | /** Set the multiplex ratio. |
Byrn | 2:e479b0296757 | 75 | * @param value MUX will be set to (value+1). Valid values range from 15 to 63 - MUX 16 to 64. |
Byrn | 2:e479b0296757 | 76 | */ |
Byrn | 2:e479b0296757 | 77 | void set_multiplex_ratio(unsigned char value); // 15-63 (value+1 mux) |
Byrn | 2:e479b0296757 | 78 | |
Byrn | 2:e479b0296757 | 79 | /** Set COM output scan direction. If the display is active, this will immediately vertically |
Byrn | 2:e479b0296757 | 80 | * flip the display. |
Byrn | 2:e479b0296757 | 81 | * @param value 0 = Scan from COM0 (default), 1 = reversed (scan from COM[N-1]). |
Byrn | 2:e479b0296757 | 82 | */ |
Byrn | 2:e479b0296757 | 83 | void set_com_output_scan_direction(unsigned char value); // 0 or 1 |
Byrn | 1:1d58d378221c | 84 | |
Byrn | 2:e479b0296757 | 85 | /** Set COM pins hardware configuration. |
Byrn | 2:e479b0296757 | 86 | * @param sequential 0 = Sequental COM pin configuration, 1 = Alternative COM pin configuration (default). |
Byrn | 2:e479b0296757 | 87 | * @param lr_remap 0 = Disable COM left/right remap (default), 1 = enable COM left/right remap. |
Byrn | 2:e479b0296757 | 88 | */ |
Byrn | 2:e479b0296757 | 89 | void set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap); // 0 or 1 for both parametrs |
Byrn | 2:e479b0296757 | 90 | |
Byrn | 2:e479b0296757 | 91 | /** Set up and start a continuous horizontal scroll. |
Byrn | 2:e479b0296757 | 92 | * Once you have set up the scrolling, you can deactivate it with stop_scroll(). |
Byrn | 2:e479b0296757 | 93 | * @param direction 0 for right, 1 for left. |
Byrn | 2:e479b0296757 | 94 | * @param start Start page address, 0 - 5. |
Byrn | 2:e479b0296757 | 95 | * @param end End page address, 0 - 5. |
Byrn | 2:e479b0296757 | 96 | * @param interval Interval in frame frequency. Valid values are: 2, 3, 4, 5, 25, 64, 128, 256. |
Byrn | 2:e479b0296757 | 97 | * @see stop_scrol |
Byrn | 2:e479b0296757 | 98 | */ |
Byrn | 2:e479b0296757 | 99 | void start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval); |
Byrn | 2:e479b0296757 | 100 | |
Byrn | 2:e479b0296757 | 101 | /** Set up and start a continuous horizontal and vertical scroll. |
Byrn | 2:e479b0296757 | 102 | * NOTE: No continuous vertical scroll is available. |
Byrn | 2:e479b0296757 | 103 | * Once you have set up the scrolling, you can deactivate it with stop_scroll(). |
Byrn | 2:e479b0296757 | 104 | * @param direction 0 for vertical and right horizontal scroll, 1 for vertical and left horizontal scroll. |
Byrn | 2:e479b0296757 | 105 | * @param start Start page address, 0 - 5. |
Byrn | 2:e479b0296757 | 106 | * @param end End page address, 0 - 5. |
Byrn | 2:e479b0296757 | 107 | * @param interval Interval in frame frequency. Valid values are: 2, 3, 4, 5, 25, 64, 128, 256. |
Byrn | 2:e479b0296757 | 108 | * @param vertical_offset Offset of vertical scroll, 1 - 63. |
Byrn | 2:e479b0296757 | 109 | * @see stop_scroll |
Byrn | 2:e479b0296757 | 110 | */ |
Byrn | 2:e479b0296757 | 111 | 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 | 112 | |
Byrn | 2:e479b0296757 | 113 | /** Deactivate the continuous scroll set up with start_horizontal_scroll() or |
Byrn | 2:e479b0296757 | 114 | * start_vertical_and_horizontal_scroll(). |
Byrn | 2:e479b0296757 | 115 | * @see set_horizontal_scroll, set_vertical_and_horizontal_scroll |
Byrn | 2:e479b0296757 | 116 | */ |
Byrn | 2:e479b0296757 | 117 | void stop_scroll(); |
Byrn | 2:e479b0296757 | 118 | |
Byrn | 2:e479b0296757 | 119 | // ----- ADDRESSING ----- |
Byrn | 2:e479b0296757 | 120 | |
Byrn | 2:e479b0296757 | 121 | /** Set memory addressing mode to the given value. |
Byrn | 2:e479b0296757 | 122 | * @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 | 123 | */ |
Byrn | 2:e479b0296757 | 124 | void set_memory_addressing_mode(unsigned char mode); |
Byrn | 2:e479b0296757 | 125 | |
Byrn | 2:e479b0296757 | 126 | /** Page Addressing Mode: Set the column start address register for |
Byrn | 2:e479b0296757 | 127 | * page addressing mode. |
Byrn | 2:e479b0296757 | 128 | * @param address The address (full byte). |
Byrn | 2:e479b0296757 | 129 | */ |
Byrn | 2:e479b0296757 | 130 | void pam_set_start_address(unsigned char address); |
Byrn | 2:e479b0296757 | 131 | |
Byrn | 2:e479b0296757 | 132 | /** Set the GDDRAM page start address for page addressing mode. |
Byrn | 2:e479b0296757 | 133 | * @param address The start page, 0 - 7. |
Byrn | 2:e479b0296757 | 134 | */ |
Byrn | 2:e479b0296757 | 135 | void pam_set_page_start(unsigned char address); |
Byrn | 2:e479b0296757 | 136 | |
Byrn | 2:e479b0296757 | 137 | /** Set page start and end address for horizontal/vertical addressing mode. |
Byrn | 2:e479b0296757 | 138 | * @param start The start page, 0 - 7. |
Byrn | 2:e479b0296757 | 139 | * @param end The end page, 0 - 7. |
Byrn | 2:e479b0296757 | 140 | */ |
Byrn | 2:e479b0296757 | 141 | void hv_set_page_address(unsigned char start, unsigned char end); |
Byrn | 2:e479b0296757 | 142 | |
Byrn | 2:e479b0296757 | 143 | /** Set column address range for horizontal/vertical addressing mode. |
Byrn | 2:e479b0296757 | 144 | * @param start Column start address, 0 - 127. |
Byrn | 2:e479b0296757 | 145 | * @param end Column end address, 0 - 127. |
Byrn | 2:e479b0296757 | 146 | */ |
Byrn | 2:e479b0296757 | 147 | void hv_set_column_address(unsigned char start, unsigned char end); |
Byrn | 2:e479b0296757 | 148 | |
Byrn | 2:e479b0296757 | 149 | // ----- TIMING & DRIVING ----- |
Byrn | 2:e479b0296757 | 150 | /** Set the display clock divide ratio and the oscillator frequency. |
Byrn | 2:e479b0296757 | 151 | * @param ratio The divide ratio, default is 0. |
Byrn | 2:e479b0296757 | 152 | * @param frequency The oscillator frequency, 0 - 127. Default is 8. |
Byrn | 2:e479b0296757 | 153 | */ |
Byrn | 2:e479b0296757 | 154 | void set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency); |
Byrn | 2:e479b0296757 | 155 | |
Byrn | 2:e479b0296757 | 156 | /** Set the precharge period. |
Byrn | 2:e479b0296757 | 157 | * @param phase1 Phase 1 period in DCLK clocks. 1 - 15, default is 2. |
Byrn | 2:e479b0296757 | 158 | * @param phase2 Phase 2 period in DCLK clocks. 1 - 15, default is 2. |
Byrn | 2:e479b0296757 | 159 | */ |
Byrn | 2:e479b0296757 | 160 | void set_precharge_period(unsigned char phase1, unsigned char phase2); |
Byrn | 2:e479b0296757 | 161 | |
Byrn | 2:e479b0296757 | 162 | /** Set the Vcomh deselect level. |
Byrn | 2:e479b0296757 | 163 | * @param level 0 = 0.65 x Vcc, 1 = 0.77 x Vcc (default), 2 = 0.83 x Vcc. |
Byrn | 2:e479b0296757 | 164 | */ |
Byrn | 2:e479b0296757 | 165 | void set_vcomh_deselect_level(unsigned char level); |
Byrn | 2:e479b0296757 | 166 | |
Byrn | 2:e479b0296757 | 167 | /** Perform a "no operation". |
Byrn | 2:e479b0296757 | 168 | */ |
Byrn | 2:e479b0296757 | 169 | void nop(); |
Byrn | 2:e479b0296757 | 170 | |
Byrn | 2:e479b0296757 | 171 | /** Enable/disable charge pump. |
Byrn | 2:e479b0296757 | 172 | @param enable 0 to disable, 1 to enable the internal charge pump. |
Byrn | 2:e479b0296757 | 173 | */ |
Byrn | 2:e479b0296757 | 174 | void set_charge_pump_enable(unsigned char enable); |
Byrn | 2:e479b0296757 | 175 | |
Byrn | 2:e479b0296757 | 176 | // ----- BUFFER EDITING ----- |
Byrn | 0:21cb91208386 | 177 | |
Byrn | 0:21cb91208386 | 178 | void clear(); |
Byrn | 0:21cb91208386 | 179 | void set_pixel(int x, int y); |
Byrn | 0:21cb91208386 | 180 | void clear_pixel(int x, int y); |
Byrn | 0:21cb91208386 | 181 | void line(int x0, int y0, int x1, int y1); |
Byrn | 2:e479b0296757 | 182 | |
Byrn | 0:21cb91208386 | 183 | void draw_string(char *font, int x, int y, const char *string); |
Byrn | 0:21cb91208386 | 184 | void draw_char(char *font, int x, int y, char c); |
Byrn | 0:21cb91208386 | 185 | |
Byrn | 0:21cb91208386 | 186 | private: |
Byrn | 0:21cb91208386 | 187 | SPI _spi; |
Byrn | 0:21cb91208386 | 188 | DigitalOut _cs, _reset, _dc; |
Byrn | 2:e479b0296757 | 189 | unsigned char _screen[1024]; |
Byrn | 2:e479b0296757 | 190 | |
Byrn | 0:21cb91208386 | 191 | int _cursor_x, _cursor_y; |
Byrn | 0:21cb91208386 | 192 | |
Byrn | 1:1d58d378221c | 193 | void _send_command(unsigned char code); |
Byrn | 1:1d58d378221c | 194 | void _send_data(unsigned char value); |
Byrn | 0:21cb91208386 | 195 | }; |
Byrn | 0:21cb91208386 | 196 | |
Byrn | 0:21cb91208386 | 197 | #define SSD1306_LCDWIDTH 128 |
Byrn | 0:21cb91208386 | 198 | #define SSD1306_LCDHEIGHT 64 |
Byrn | 0:21cb91208386 | 199 | |
Byrn | 0:21cb91208386 | 200 | #endif |