derived from Aidafruit SSD1306 library

Dependents:   Test_SSD1306 L152RE_OLED_SSD1306 EcranZumo

Fork of SSD1306 by Jonathan Gaul

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssd1306.h Source File

ssd1306.h

00001 #ifndef __SSD1306_H__
00002 #define __SSD1306_H__
00003 
00004 #define FONT_START          ' '  /* First character value in the font table */
00005 
00006 /** SSD1306 Controller Driver
00007   *
00008   * This class provides a buffered display for the SSD1306 OLED controller.
00009   * 
00010   * TODO: 
00011   *   - At the moment, the driver assumes a 128x64 pixel display.
00012   *   - Only fonts of 8 pixel height are supported (different widths can be used).
00013   *   - Pretty much no drawing functions are provided as yet.
00014   *   - Possible "auto-update", automatically calling update() after a printf etc.
00015   *
00016   * Information taken from the datasheet at:
00017   *   http://www.adafruit.com/datasheets/SSD1306.pdf
00018   *
00019   */
00020 class SSD1306
00021 {
00022 public:
00023     /** Construct a new SSD1306 object.
00024      *  @param cs The connected C/S pin.
00025      *  @param rs The connected RS pin.
00026      *  @param dc The connected DC pin.
00027      *  @param clk The connected CLK pin.
00028      *  @param data The connected Data pin.
00029      */
00030     SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data);
00031 
00032     // ----- HARDWARE CONTROL -----
00033 
00034     /** Initialise the display with defaults.*/
00035     void initialise();
00036     
00037     /** Force a refresh of the display.  Copies the buffer to the controller. */
00038     void update();
00039 
00040     /** Turn the whole display off.  This will reset all configuration settings on the controller to their defaults. */
00041     void off();
00042     
00043     /** Turn the whole display on.  Used during initialisation. */
00044     void on();
00045 
00046     /** Sends the display to sleep, but leaves RAM intact. */
00047     void sleep();
00048 
00049     /** Wakes up this display following a sleep() call.
00050      *  @see sleep()
00051      */
00052     void wake();
00053 
00054     /** Set the display contrast.
00055      *  @param value The contrast, from 1 to 256.
00056      */
00057     void set_contrast(unsigned char value); // 1-256
00058     
00059     /** Set the display to normal or inverse.
00060      *  @param value 0 for normal mode, or 1 for inverse mode.
00061      */
00062     void set_inverse(unsigned char value); // 0 or 1
00063     
00064     /** Set the display start line.  This is the line at which the display will start rendering.
00065      *  @param value A value from 0 to 63 denoting the line to start at.
00066      */
00067     void set_display_start_line(unsigned char value); // 0-63
00068     
00069     /** Set the segment remap state.  This allows the module to be addressed as if flipped horizontally.
00070       * NOTE: Changing this setting has no effect on data already in the module's GDDRAM.
00071       * @param value 0 = column address 0 = segment 0 (the default), 1 = column address 127 = segment 0 (flipped).
00072       */
00073     void set_segment_remap(unsigned char value); // 0 or 1
00074     
00075     /** Set the vertical shift by COM.
00076       * @param value The number of rows to shift, from 0 - 63.
00077       */
00078     void set_display_offset(unsigned char value); // 0-63
00079     
00080     /** Set the multiplex ratio.
00081      *  @param value MUX will be set to (value+1). Valid values range from 15 to 63 - MUX 16 to 64.
00082      */
00083     void set_multiplex_ratio(unsigned char value); // 15-63 (value+1 mux)
00084     
00085     /** Set COM output scan direction.  If the display is active, this will immediately vertically
00086       * flip the display.
00087       * @param value 0 = Scan from COM0 (default), 1 = reversed (scan from COM[N-1]).
00088       */
00089     void set_com_output_scan_direction(unsigned char value); // 0 or 1
00090     
00091     /** Set COM pins hardware configuration.
00092       * @param sequential 0 = Sequental COM pin configuration, 1 = Alternative COM pin configuration (default).
00093       * @param lr_remap 0 = Disable COM left/right remap (default), 1 = enable COM left/right remap.
00094       */
00095     void set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap); // 0 or 1 for both parametrs
00096 
00097     /** Set up and start a continuous horizontal scroll.
00098       * Once you have set up the scrolling, you can deactivate it with stop_scroll().
00099       * @param direction 0 for right, 1 for left.
00100       * @param start Start page address, 0 - 5.
00101       * @param end End page address, 0 - 5.
00102       * @param interval Interval in frame frequency.  Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
00103       * @see stop_scrol
00104       */
00105     void start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval);
00106 
00107     /** Set up and start a continuous horizontal and vertical scroll.
00108       * NOTE: No continuous vertical scroll is available.
00109       * Once you have set up the scrolling, you can deactivate it with stop_scroll().
00110       * @param direction 0 for vertical and right horizontal scroll, 1 for vertical and left horizontal scroll.
00111       * @param start Start page address, 0 - 5.
00112       * @param end End page address, 0 - 5.
00113       * @param interval Interval in frame frequency.  Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
00114       * @param vertical_offset Offset of vertical scroll, 1 - 63.
00115       * @see stop_scroll
00116       */
00117     void start_vertical_and_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval, unsigned char vertical_offset);
00118     
00119     /** Deactivate the continuous scroll set up with start_horizontal_scroll() or 
00120       * start_vertical_and_horizontal_scroll().
00121       * @see set_horizontal_scroll, set_vertical_and_horizontal_scroll
00122       */
00123     void stop_scroll();
00124     
00125     // ----- ADDRESSING -----
00126     
00127     /** Set memory addressing mode to the given value.
00128       * @param mode 0 for Horizontal addressing mode, 1 for Vertical addressing mode, or 2 for Page addressing mode (PAM).  2 is the default.
00129       */
00130     void set_memory_addressing_mode(unsigned char mode);
00131     
00132     /** Page Addressing Mode: Set the column start address register for
00133       * page addressing mode.
00134       * @param address The address (full byte).
00135       */
00136     void pam_set_start_address(unsigned char address);       
00137     
00138     /** Set the GDDRAM page start address for page addressing mode.
00139       * @param address The start page, 0 - 7.
00140       */
00141     void pam_set_page_start(unsigned char address);
00142     
00143     /** Set page start and end address for horizontal/vertical addressing mode.
00144       * @param start The start page, 0 - 7.
00145       * @param end The end page, 0 - 7.
00146       */
00147     void hv_set_page_address(unsigned char start, unsigned char end);
00148     
00149     /** Set column address range for horizontal/vertical addressing mode.
00150       * @param start Column start address, 0 - 127.
00151       * @param end Column end address, 0 - 127.
00152       */
00153     void hv_set_column_address(unsigned char start, unsigned char end);
00154     
00155     // ----- TIMING & DRIVING -----
00156     /** Set the display clock divide ratio and the oscillator frequency.
00157       * @param ratio The divide ratio, default is 0.
00158       * @param frequency The oscillator frequency, 0 - 127. Default is 8.  
00159       */
00160     void set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency);
00161     
00162     /** Set the precharge period.
00163       * @param phase1 Phase 1 period in DCLK clocks.  1 - 15, default is 2.
00164       * @param phase2 Phase 2 period in DCLK clocks.  1 - 15, default is 2.
00165       */
00166     void set_precharge_period(unsigned char phase1, unsigned char phase2);
00167     
00168     /** Set the Vcomh deselect level.
00169       * @param level 0 = 0.65 x Vcc, 1 = 0.77 x Vcc (default), 2 = 0.83 x Vcc.
00170       */
00171     void set_vcomh_deselect_level(unsigned char level);
00172     
00173     /** Perform a "no operation".
00174       */
00175     void nop();
00176     
00177     /** Enable/disable charge pump.
00178       @param enable 0 to disable, 1 to enable the internal charge pump.
00179       */
00180     void set_charge_pump_enable(unsigned char enable);
00181     
00182     // ----- BUFFER EDITING -----
00183 
00184     void clear();  
00185     void drawBitmap(int x, int y, const unsigned char *bitmap, int w, int h, int color = 1);
00186     void set_pixel(int x, int y);
00187     void clear_pixel(int x, int y);
00188     void line(int x0, int y0, int x1, int y1);
00189 
00190     /** Set the current console font.
00191       * @param font Font data, layed out vertically!
00192       * @param width Width of the font characters in pixels.
00193       * Fonts are always (at present) 8 pixels in height.
00194       */
00195     void set_font(unsigned char *font, unsigned int width);
00196     
00197     /** Set double height text output.
00198       * @param double_height If 1, calls to putc(), printf() etc will
00199       * result in text taking up 2 lines instead of 1.
00200       */
00201     void set_double_height_text(unsigned int double_height);
00202     
00203     /** Put a single character to the screen buffer.
00204       * Repeated calls to putc() will cause the cursor to move across and
00205       * then down as needed, with scrolling.
00206       * @param c The character to write.
00207       */
00208     void putc(unsigned char c);
00209     
00210     /** Print to the screen buffer.
00211       * printf() will wrap and scroll the screen as needed to display the text given.
00212       * @param format Format specifier, same as printf() in normal C.
00213       */
00214     void printf(const char *format, ...);
00215 
00216     /** Scroll the screen buffer up by one line. */
00217     void scroll_up();
00218 
00219 private:
00220     SPI _spi;
00221     DigitalOut _cs, _reset, _dc;
00222     unsigned char _screen[1024];
00223     
00224     int _cursor_x, _cursor_y;
00225 
00226     void _send_command(unsigned char code);
00227     void _send_data(unsigned char value);
00228     
00229     unsigned char *_console_font_data;  
00230     unsigned int _console_font_width;
00231     unsigned int _double_height_text;
00232 };
00233 
00234 #define SSD1306_LCDWIDTH 128
00235 #define SSD1306_LCDHEIGHT 64
00236 
00237 #endif