A buffered display driver for the SSD1306 OLED controller. Please note that this is a work-in-progress; only very rudimentary drawing support is provided.
Dependents: Projetv0 greenhouse_proj ProjetLong_Serre_V3 ProjetLong_Serre_V3_1 ... more
Example of use:
#include "mbed.h" #include "ssd1306.h" #include "standard_font.h" #include "bold_font.h" SSD1306 oled(p8 /* cs */, p9 /* reset */, p14 /* dc */, p13 /* clock */, p11 /* data */); int main() { oled.initialise(); oled.clear(); oled.set_contrast(255); // max contrast oled.set_font(bold_font, 8); oled.printf("Heading\r\n"); oled.set_font(standard_font, 6); oled.printf("Hello World!\r\n"); oled.printf("Some more text here..."); oled.update(); while (1) { wait(2); oled.scroll_up(); oled.update(); } }
Diff: ssd1306.cpp
- Revision:
- 3:1d9df877c90a
- Parent:
- 2:e479b0296757
--- a/ssd1306.cpp Tue Feb 05 21:21:22 2013 +0000 +++ b/ssd1306.cpp Sat Feb 09 16:43:49 2013 +0000 @@ -2,11 +2,15 @@ #include "mbed.h" #include "ssd1306.h" +#include <stdarg.h> + SSD1306::SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data) : _spi(data, NC, clk), _cs(cs), _reset(rs), - _dc(dc) + _dc(dc), + _cursor_x(0), + _cursor_y(0) { } @@ -211,7 +215,7 @@ set_display_offset(0); set_display_start_line(0); set_charge_pump_enable(1); - set_memory_addressing_mode(0); // horizontal addressing mode + set_memory_addressing_mode(0); // horizontal addressing mode; across then down set_segment_remap(1); set_com_output_scan_direction(1); set_com_pins_hardware_configuration(1, 0); @@ -294,32 +298,95 @@ } } -void SSD1306::draw_string(char *font, int x, int y, const char *string) +void SSD1306::set_font(unsigned char *font, unsigned int width) { - _cursor_x = x; - _cursor_y = y; - - for (int i = 0; i < strlen(string); i++) - draw_char(font, _cursor_x, _cursor_y, string[i]); + _console_font_data = font; + _console_font_width = width; +} + +void SSD1306::set_double_height_text(unsigned int double_height) +{ + _double_height_text = double_height; } -void SSD1306::draw_char(char *font, int x, int y, char c) +void SSD1306::putc(unsigned char c) { - int height = font[FONT_HEIGHT_OFFSET]; - int max_width = font[FONT_SIZE_OFFSET]; - int char_size_bytes = max_width * height + 1; - int char_width = font[(c - FONT_START) * char_size_bytes + FONT_DATA_OFFSET]; - for (int i = 0; i < char_width; i++) - _screen[(x + i) + (y * SSD1306_LCDWIDTH)] = font[(c - FONT_START) * (char_size_bytes) + i + FONT_DATA_OFFSET + 1]; + while (_cursor_x >= (128 / _console_font_width)) + { + _cursor_x -= (128 / _console_font_width); + _cursor_y++; + } + + while (_cursor_y > 7) + { + scroll_up(); + } + + switch (c) + { + case '\n': + _cursor_y++; + break; + + case '\r': + _cursor_x = 0; + break; + + case '\t': + _cursor_x = (_cursor_x + 4) % 4; + break; + + default: + for (int b = 0; b < _console_font_width; b++) + { + _screen[_cursor_x * _console_font_width + _cursor_y * 128 + b] = _console_font_data[(c - FONT_START) * _console_font_width + b]; + } + + _cursor_x++; + } +} + +void SSD1306::scroll_up() +{ + for (int y = 1; y <= 7; y++) + { + for (int x = 0; x < 128; x++) + { + _screen[x + 128 * (y - 1)] = _screen[x + 128 * y]; + } + } - _cursor_x = x + char_width; - _cursor_y = y; + for (int x = 0; x < 128; x++) + { + _screen[x + 128 * 7] = 0; + } + + _cursor_y--; +} + +void SSD1306::printf(const char *format, ...) +{ + static char buffer[128]; + + va_list args; + va_start(args, format); + vsprintf(buffer, format, args); + va_end(args); + + char *c = (char *)&buffer; + while (*c != 0) + { + putc(*c++); + } } void SSD1306::clear() { for (int i = 0; i < 1024; i++) _screen[i] = 0; + + _cursor_x = 0; + _cursor_y = 0; } void SSD1306::_send_command(unsigned char code)