derived from Aidafruit SSD1306 library

Dependents:   Test_SSD1306 L152RE_OLED_SSD1306 EcranZumo

Fork of SSD1306 by Jonathan Gaul

Files at this revision

API Documentation at this revision

Comitter:
Byrn
Date:
Tue Feb 05 09:46:58 2013 +0000
Child:
1:1d58d378221c
Commit message:
[mbed] converted /OLED/SSD1306

Changed in this revision

smallfont.h Show annotated file Show diff for this revision Revisions of this file
ssd1306.cpp Show annotated file Show diff for this revision Revisions of this file
ssd1306.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smallfont.h	Tue Feb 05 09:46:58 2013 +0000
@@ -0,0 +1,103 @@
+
+#ifndef __SMALLFONT_H__
+#define __SMALLFONT_H__
+
+static char smallfont[] = { 
+    0x01, /* Height in bytes */
+    0x06, /* Widest char width (size of char data) */
+    
+    /*
+    width,----------- data -----------------*/
+    0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ' ' (space)
+    0x02, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, // !
+    0x04, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, // "
+    0x06, 0x0A, 0x1F, 0x0A, 0x1F, 0x0A, 0x00, // #
+    0x06, 0x02, 0x15, 0x1F, 0x15, 0x08, 0x00, // $
+    0x06, 0x13, 0x09, 0x04, 0x12, 0x19, 0x00, // %
+    0x06, 0x08, 0x14, 0x0F, 0x04, 0x04, 0x00, // &
+    0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // '
+    0x03, 0x1E, 0x21, 0x00, 0x00, 0x00, 0x00, // (
+    0x03, 0x21, 0x1E, 0x00, 0x00, 0x00, 0x00, // )
+    0x06, 0x15, 0x0E, 0x1F, 0x0E, 0x15, 0x00, // *
+    0x04, 0x04, 0x0C, 0x04, 0x00, 0x00, 0x00, // +
+    0x03, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, // ,
+    0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, // -
+    0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // .
+    0x06, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, // /
+    0x05, 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x00, // 0
+    0x03, 0x02, 0x1F, 0x00, 0x00, 0x00, 0x00, // 1
+    0x05, 0x18, 0x15, 0x15, 0x12, 0x00, 0x00, // 2 
+    0x05, 0x11, 0x15, 0x15, 0x0A, 0x00, 0x00, // 3
+    0x05, 0x0F, 0x08, 0x1E, 0x08, 0x00, 0x00, // 4
+    0x05, 0x17, 0x15, 0x15, 0x09, 0x00, 0x00, // 5
+    0x05, 0x0E, 0x15, 0x15, 0x08, 0x00, 0x00, // 6
+    0x05, 0x01, 0x11, 0x09, 0x07, 0x00, 0x00, // 7
+    0x05, 0x0A, 0x15, 0x15, 0x0A, 0x00, 0x00, // 8
+    0x05, 0x02, 0x05, 0x05, 0x1E, 0x00, 0x00, // 9
+    0x02, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, // :
+    0x03, 0x10, 0x0A, 0x00, 0x00, 0x00, 0x00, // ;
+    0x05, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00, // <
+    0x04, 0x0A, 0x0A, 0x0A, 0x00, 0x00, 0x00, // =
+    0x05, 0x11, 0x0A, 0x04, 0x00, 0x00, 0x00, // >
+    0x05, 0x01, 0x15, 0x05, 0x02, 0x00, 0x00, // ?
+    0x05, 0x0E, 0x11, 0x15, 0x02, 0x00, 0x00, // @
+    0x05, 0x1E, 0x09, 0x09, 0x1E, 0x00, 0x00, // A
+    0x05, 0x1F, 0x15, 0x15, 0x0A, 0x00, 0x00, // B
+    0x05, 0x0E, 0x11, 0x11, 0x10, 0x00, 0x00, // C
+    0x05, 0x1F, 0x11, 0x11, 0x0E, 0x00, 0x00, // D
+    0x05, 0x1F, 0x15, 0x15, 0x11, 0x00, 0x00, // E
+    0x05, 0x1F, 0x05, 0x05, 0x01, 0x00, 0x00, // F
+    0x05, 0x0E, 0x11, 0x15, 0x1C, 0x00, 0x00, // G
+    0x05, 0x1F, 0x04, 0x04, 0x1F, 0x00, 0x00, // H
+    0x04, 0x11, 0x1F, 0x11, 0x00, 0x00, 0x00, // I
+    0x05, 0x08, 0x11, 0x11, 0x0F, 0x00, 0x00, // J
+    0x05, 0x1F, 0x04, 0x0A, 0x11, 0x00, 0x00, // K
+    0x05, 0x1F, 0x10, 0x10, 0x10, 0x00, 0x00, // L
+    0x06, 0x1F, 0x02, 0x04, 0x02, 0x1F, 0x00, // M
+    0x05, 0x1F, 0x01, 0x01, 0x1E, 0x00, 0x00, // N
+    0x05, 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x00, // O
+    0x05, 0x1F, 0x05, 0x05, 0x02, 0x00, 0x00, // P
+    0x06, 0x0E, 0x11, 0x19, 0x1E, 0x20, 0x00, // Q
+    0x05, 0x1F, 0x05, 0x05, 0x1A, 0x00, 0x00, // R
+    0x05, 0x12, 0x15, 0x15, 0x08, 0x00, 0x00, // S
+    0x04, 0x01, 0x1F, 0x01, 0x00, 0x00, 0x00, // T
+    0x05, 0x0F, 0x10, 0x10, 0x1F, 0x00, 0x00, // U
+    0x06, 0x07, 0x08, 0x10, 0x08, 0x07, 0x00, // V
+    0x06, 0x1F, 0x08, 0x04, 0x08, 0x1F, 0x00, // W
+    0x06, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00, // X
+    0x05, 0x03, 0x14, 0x14, 0x0F, 0x00, 0x00, // Y
+    0x05, 0x19, 0x15, 0x13, 0x11, 0x00, 0x00, // Z
+    0x03, 0x3F, 0x21, 0x00, 0x00, 0x00, 0x00, // [
+    0x06, 0x01, 0x02, 0x04, 0x08, 0x10, 0x00, // \\
+    0x01, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, // ?
+    0x03, 0x21, 0x3F, 0x00, 0x00, 0x00, 0x00, // ]
+    0x04, 0x02, 0x01, 0x02, 0x00, 0x00, 0x00, // ^
+    0x05, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, // _
+    0x03, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, // `
+    0x05, 0x0C, 0x12, 0x12, 0x1C, 0x00, 0x00, // a
+    0x05, 0x1F, 0x12, 0x12, 0x0C, 0x00, 0x00, // b
+    0x05, 0x0C, 0x12, 0x12, 0x12, 0x00, 0x00, // c
+    0x05, 0x0C, 0x12, 0x12, 0x1F, 0x00, 0x00, // d
+    0x05, 0x0E, 0x15, 0x15, 0x02, 0x00, 0x00, // e
+    0x05, 0x04, 0x3E, 0x05, 0x01, 0x00, 0x00, // f
+    0x05, 0x0C, 0x2A, 0x2A, 0x1E, 0x00, 0x00, // g
+    0x05, 0x1F, 0x02, 0x02, 0x1A, 0x00, 0x00, // h
+    0x02, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, // i
+    0x04, 0x20, 0x20, 0x1D, 0x00, 0x00, 0x00, // j
+    0x04, 0x1F, 0x04, 0x1A, 0x00, 0x00, 0x00, // k
+    0x02, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, // l
+    0x06, 0x1C, 0x02, 0x0C, 0x02, 0x1C, 0x00, // m
+    0x05, 0x1E, 0x02, 0x02, 0x1C, 0x00, 0x00, // n
+    0x05, 0x0C, 0x12, 0x12, 0x0C, 0x00, 0x00, // o
+    0x05, 0x3E, 0x12, 0x12, 0x0C, 0x00, 0x00, // p
+    0x05, 0x0C, 0x12, 0x12, 0x3E, 0x00, 0x00, // q
+    0x05, 0x1C, 0x02, 0x02, 0x00, 0x00, 0x00, // r
+    0x05, 0x12, 0x15, 0x15, 0x08, 0x00, 0x00, // s
+    0x05, 0x02, 0x0F, 0x12, 0x02, 0x00, 0x00, // t
+    0x05, 0x0E, 0x10, 0x10, 0x1F, 0x00, 0x00, // u
+    0x04, 0x0C, 0x10, 0x0C, 0x00, 0x00, 0x00, // v
+    0x06, 0x0C, 0x10, 0x0A, 0x10, 0x0C, 0x00, // w
+    0x05, 0x12, 0x0C, 0x0C, 0x12, 0x00, 0x00, // x
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ssd1306.cpp	Tue Feb 05 09:46:58 2013 +0000
@@ -0,0 +1,207 @@
+
+#include "mbed.h"
+#include "ssd1306.h"
+
+SSD1306::SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data)
+    : _spi(data, NC, clk), _cs(cs), _reset(rs), _dc(dc)
+{
+}
+
+void SSD1306::set_low_column(int v)
+{
+    _send_command(SSD1306_SETLOWCOLUMN | v);
+}
+
+void SSD1306::set_high_column(int v)
+{
+    _send_command(SSD1306_SETHIGHCOLUMN | v);
+}
+
+void SSD1306::set_start_line(int v)
+{
+    _send_command(SSD1306_SETSTARTLINE | v);
+}
+
+void SSD1306::off()
+{
+    _send_command(SSD1306_DISPLAYOFF);
+}
+
+void SSD1306::on()
+{
+    _send_command(SSD1306_DISPLAYON);
+}
+
+void SSD1306::invert(int i)
+{
+    _send_command(i ? 
+        SSD1306_INVERTDISPLAY :
+        SSD1306_NORMALDISPLAY);
+}
+
+void SSD1306::set_display_offset(int v)
+{
+    _send_command(SSD1306_SETDISPLAYOFFSET);
+    _send_command(v); 
+}
+
+void SSD1306::initialise()
+{
+ // Init
+    _reset = 1;
+    wait(0.01);
+    _reset = 0;
+    wait(0.10);
+    _reset = 1;
+    
+    off();
+    
+    set_low_column(0);
+    set_high_column(0);
+    set_start_line(0);
+    
+    _send_command(SSD1306_SETCONTRAST);
+    _send_command(0xCF); // chargepump, could be 0x9F for external 9V
+    
+    _send_command(0xA1); // setment remap 95 to 0 (??)
+    
+    invert(0);
+    
+    _send_command(SSD1306_DISPLAYALLON_RESUME);
+    _send_command(SSD1306_SETMULTIPLEX);    
+    _send_command(0x3F); // 1/64 duty
+    
+    set_display_offset(0);
+    
+    _send_command(SSD1306_SETDISPLAYCLOCKDIV);
+    _send_command(0x80); // suggested value = 0x80
+    _send_command(SSD1306_SETPRECHARGE);
+    _send_command(0xF1); // dc/dc, could be 0x22 for external 9V
+    _send_command(SSD1306_SETCOMPINS);
+    _send_command(0x12); // disable COM left/right remap
+    _send_command(SSD1306_SETVCOMDETECT);
+    _send_command(0x40); // apparently, 0x20 is default...
+    _send_command(SSD1306_MEMORYMODE);
+    _send_command(0x0); // act like KS0108
+    
+    // left-to-right scan
+    _send_command(SSD1306_SEGREMAP | 0x1);
+    _send_command(SSD1306_COMSCANDEC);
+    _send_command(SSD1306_CHARGEPUMP);
+    _send_command(0x14); // disable, for external 9v 0x10 disable
+   
+    // turn it on
+    on();
+}
+
+void SSD1306::update()
+{
+    set_low_column(0);
+    set_high_column(0);
+    set_start_line(0);
+    
+    for (int i = 0; i < 1024; i++)
+        _send_data(_screen[i]);
+}
+
+void SSD1306::set_pixel(int x, int y)
+{
+    if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return;
+    
+    _screen[x + (y / 8) * 128] |= 1 << (y % 8);
+}
+
+void SSD1306::clear_pixel(int x, int y)
+{
+    if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return;
+    
+    _screen[x + (y / 8) * 128] &= ~(1 << (y % 8));
+}
+
+void SSD1306::line(int x0, int y0, int x1, int y1) {
+  int steep = abs(y1 - y0) > abs(x1 - x0);
+  int t;
+  
+  if (steep) {
+    t = x0; x0 = y0; y0 = t;
+    t = x1; x1 = y1; y1 = t;
+  }
+
+  if (x0 > x1) {
+    t = x0; x0 = x1; x1 = t;
+    t = y0; y0 = y1; y1 = t;
+  }
+
+  int dx, dy;
+  
+  dx = x1 - x0;
+  dy = abs(y1 - y0);
+
+  int err = dx / 2;
+  int ystep;
+
+  if (y0 < y1) {
+    ystep = 1;
+  } else {
+    ystep = -1;}
+
+  for (; x0<x1; x0++) {
+    if (steep) {
+      set_pixel(y0, x0);
+    } else {
+      set_pixel(x0, y0);
+    }
+    err -= dy;
+    if (err < 0) {
+      y0 += ystep;
+      err += dx;
+    }
+  }
+}
+
+void SSD1306::draw_string(char *font, int x, int y, const char *string)
+{
+    _cursor_x = x;
+    _cursor_y = y;
+    
+    for (int i = 0; i < strlen(string); i++) 
+        draw_char(font, _cursor_x, _cursor_y, string[i]);
+}
+
+void SSD1306::draw_char(char *font, int x, int y, 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];
+    
+    _cursor_x = x + char_width;
+    _cursor_y = y;
+}
+
+void SSD1306::clear()
+{
+    for (int i = 0; i < 1024; i++)
+        _screen[i] = 0;
+}
+
+void SSD1306::_send_command(int code)
+{
+    _cs = 1;
+    _dc = 0;
+    _cs = 0;
+    _spi.write(code);
+    _cs = 1;
+}
+
+void SSD1306::_send_data(int value)
+{
+    _cs = 1;
+    _dc = 1;
+    _cs = 0;
+    _spi.write(value);
+    _cs = 1;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ssd1306.h	Tue Feb 05 09:46:58 2013 +0000
@@ -0,0 +1,85 @@
+#ifndef __SSD1306_H__
+#define __SSD1306_H__
+
+#define FONT_HEIGHT_OFFSET  0    /* Character pixel height (in multiples of 8) at this position */
+#define FONT_SIZE_OFFSET    1    /* Character data size (in bytes) at this position */
+#define FONT_DATA_OFFSET    2    /* Data starts at this position */
+#define FONT_START          ' '  /* First character value in the font table */
+
+class SSD1306
+{
+public:
+    SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data);
+
+    void initialise();
+    void update();
+
+    void off();
+    void on();
+    
+    void invert(int i);
+
+    void set_low_column(int value);
+    void set_high_column(int value);
+    void set_start_line(int value);
+
+    void set_display_offset(int value);
+
+    void clear();
+    void set_pixel(int x, int y);
+    void clear_pixel(int x, int y);
+    void line(int x0, int y0, int x1, int y1);
+    
+    void draw_string(char *font, int x, int y, const char *string);
+    void draw_char(char *font, int x, int y, char c);
+
+private:
+    SPI _spi;
+    DigitalOut _cs, _reset, _dc;
+    char _screen[1024];
+
+    int _cursor_x, _cursor_y;
+
+    void _send_command(int code);
+    void _send_data(int value);
+};
+
+#define SSD1306_LCDWIDTH 128
+#define SSD1306_LCDHEIGHT 64
+
+#define SSD1306_SETCONTRAST 0x81
+#define SSD1306_DISPLAYALLON_RESUME 0xA4
+#define SSD1306_DISPLAYALLON 0xA5
+#define SSD1306_NORMALDISPLAY 0xA6
+#define SSD1306_INVERTDISPLAY 0xA7
+#define SSD1306_DISPLAYOFF 0xAE
+#define SSD1306_DISPLAYON 0xAF
+
+#define SSD1306_SETDISPLAYOFFSET 0xD3
+#define SSD1306_SETCOMPINS 0xDA
+
+#define SSD1306_SETVCOMDETECT 0xDB
+
+#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
+#define SSD1306_SETPRECHARGE 0xD9
+
+#define SSD1306_SETMULTIPLEX 0xA8
+
+#define SSD1306_SETLOWCOLUMN 0x00
+#define SSD1306_SETHIGHCOLUMN 0x10
+
+#define SSD1306_SETSTARTLINE 0x40
+
+#define SSD1306_MEMORYMODE 0x20
+
+#define SSD1306_COMSCANINC 0xC0
+#define SSD1306_COMSCANDEC 0xC8
+
+#define SSD1306_SEGREMAP 0xA0
+
+#define SSD1306_CHARGEPUMP 0x8D
+
+#define SSD1306_EXTERNALVCC 0x1
+#define SSD1306_SWITCHCAPVCC 0x2
+
+#endif