derived from Aidafruit SSD1306 library

Dependents:   Test_SSD1306 L152RE_OLED_SSD1306 EcranZumo

Fork of SSD1306 by Jonathan Gaul

ssd1306.cpp

Committer:
Byrn
Date:
2013-02-05
Revision:
1:1d58d378221c
Parent:
0:21cb91208386
Child:
2:e479b0296757

File content as of revision 1:1d58d378221c:


#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::sleep()
{
    _send_command(0xAE);
}

void SSD1306::wake()
{
    _send_command(0xAF);
}

void SSD1306::set_inverse(unsigned char value)
{
    _send_command(value ? 0xA7 : 0xA6);
}

void SSD1306::set_display_offset(unsigned char value)
{
    _send_command(0xD3);
    _send_command(value); 
}

void SSD1306::set_contrast(unsigned char value) 
{
    _send_command(0x81);
    _send_command(value);
}

void SSD1306::set_display_start_line(unsigned char value)
{
    _send_command(0x40 | value);
}

void SSD1306::set_horizontal_flip(unsigned char value)
{
    _send_command(value ? 0xA1 : 0xA0);
}

void SSD1306::set_multiplex_ratio(unsigned char value)
{
    _send_command(0xA8 | value);
}

void SSD1306::set_com_output_scan_direction(unsigned char value)
{
    _send_command(value ? 0xC8 : 0xC0);
}

void SSD1306::set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap)
{
    _send_command(0xDA);
    _send_command(0x02 | ((sequential & 1) << 5) | ((lr_remap & 1) << 6));
}

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(0xAE);  // turn off
    _send_command(0x00);  // set low column address
    _send_command(0x10);  // set high column address
    _send_command(0x40);  // set start line address
    
    set_contrast(0xFF);
    
    _send_command(0xA1);  // set segment re-map 95 to 0
    _send_command(0xA6);  // set normal display
    _send_command(0xA8);  // set multiplex ratio (1-64);
    _send_command(0x3F);  // 1/64 duty
    _send_command(0xD3);  // set display offset
    _send_command(0x00);  // -- no offset
    _send_command(0xD5);  // set display clock divide ratio/oscillator frequency
    _send_command(0x80);  // -- set divide ratio
    _send_command(0xD9);  // set precharge period;
    _send_command(0xF1);
    _send_command(0xDA);  // set com pins hardware config
    _send_command(0x12);
    _send_command(0xDB);  // set vcomh
    _send_command(0x40); 
    _send_command(0x8D);  // set charge pump enable/disable
    _send_command(0x14);  // enable  (0x10 disable)
    _send_command(0xAF);  // switch panel on
    
    set_inverse(1);
    
    /*
    _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(unsigned char code)
{
    _cs = 1;
    _dc = 0;
    _cs = 0;
    _spi.write(code);
    _cs = 1;
}

void SSD1306::_send_data(unsigned char value)
{
    _cs = 1;
    _dc = 1;
    _cs = 0;
    _spi.write(value);
    _cs = 1;
}