Library for drawing on the Hexiwear's OLED - more features being actively worked on

Dependents:   Hexidraw_Demo Hexiwear-FinalProject_v2

Discussion: https://developer.mbed.org/forum/team-4615-Hexiwear-community/topic/26595/

Hexidraw is a library for drawing on the Hexiwear's OLED. Be aware that it is not very optimized, so drawing may be slow, especially when drawing on large areas of the screen.

Please see the wiki for API documentation.

Features:

  • Screen fill with a color
  • Drawing filled rectangles
  • Drawing circles with a set radius and line width
  • Setting individual pixels
  • Turning on and off the OLED's sleep mode
  • Drawing images

Example project: https://developer.mbed.org/users/keithm01/code/Hexidraw_Demo/

Revision:
0:2d3fcb6dabd4
Child:
1:82ccc138bbe6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hexidraw.cpp	Fri Aug 19 01:17:28 2016 +0000
@@ -0,0 +1,230 @@
+#include "oled_info.h"
+#include "hexidraw.h"
+
+#include "mbed.h"
+
+/*
+    Command descriptions from:
+        https://cdn-shop.adafruit.com/datasheets/SSD1351-Revision+1.3.pdf
+*/
+
+OLED::OLED () :
+    _spi(oled_sdi_pin, NC, oled_sck_pin),
+    _cs(oled_cs_pin),
+    _reset(oled_rst_pin),
+    _dc(oled_dc_pin)
+{}
+
+void OLED::begin()
+{
+    
+    #if 0
+    // set pin directions
+    pinMode(_rs, OUTPUT);
+    
+    if (_sclk) {
+        pinMode(_sclk, OUTPUT);
+        
+        pinMode(_sid, OUTPUT);
+    } else {
+        // using the hardware SPI
+        SPI.begin();
+        SPI.setDataMode(SPI_MODE3);
+    }
+    
+    // Toggle RST low to reset; CS low so it'll listen to us
+    pinMode(_cs, OUTPUT);
+    digitalWrite(_cs, LOW);
+    
+    if (_rst) {
+        pinMode(_rst, OUTPUT);
+        digitalWrite(_rst, HIGH);
+        delay(500);
+        digitalWrite(_rst, LOW);
+        delay(500);
+        digitalWrite(_rst, HIGH);
+        delay(500);
+    }
+#endif
+    DigitalOut BOOSTEN(oled_power_enable);   //Enable OLED
+
+    //Set SPI modes
+//    _spi.format(8,3);
+    _spi.frequency(8000000);
+
+     _dc =0;
+    BOOSTEN = 0;
+    wait(0.1f);
+    _reset = 0 ; 
+    wait(0.1f);
+    _reset = 1 ;
+    wait(0.1f);
+    BOOSTEN = 1;
+
+    writeCommand(OLED_CMD_SET_CMD_LOCK);
+    writeData(0x12);
+    
+    writeCommand(OLED_CMD_SET_CMD_LOCK);
+    writeData(0xB1);
+
+    writeCommand(OLED_CMD_DISPLAYOFF);
+    
+    
+    writeCommand(OLED_CMD_SET_OSC_FREQ_AND_CLOCKDIV);
+    writeData(0xF1);
+
+    writeCommand(OLED_CMD_SET_MUX_RATIO);
+    writeData(0x5F);
+    
+    writeCommand(OLED_CMD_SET_REMAP);
+    writeData(OLED_REMAP_SETTINGS);
+    
+    writeCommand(OLED_CMD_SET_COLUMN);
+    writeData(0x00);
+    writeData(0x5F);
+    
+    writeCommand(OLED_CMD_SET_ROW);
+    writeData(0x00);
+    writeData(0x5F);
+    
+    writeCommand(OLED_CMD_STARTLINE);
+    writeData(0x80);
+    
+    writeCommand(OLED_CMD_DISPLAYOFFSET);
+    writeData(0x60);
+    
+    writeCommand(OLED_CMD_PRECHARGE);
+    writeData(0x32);
+    
+    writeCommand(OLED_CMD_VCOMH);
+    writeData(0x05);
+    
+    writeCommand(OLED_CMD_NORMALDISPLAY);
+    
+    writeCommand(OLED_CMD_CONTRASTABC);
+    writeData(0x8A);
+    writeData(0x51);
+    writeData(0x8A);
+    
+    writeCommand(OLED_CMD_CONTRASTMASTER);
+    writeData(0xCF);
+    
+    writeCommand(OLED_CMD_SETVSL);
+    writeData(0xA0);
+    writeData(0xB5);
+    writeData(0x55);
+    
+    writeCommand(OLED_CMD_PRECHARGE2);
+    writeData(0x01);
+    
+    writeCommand(OLED_CMD_DISPLAYON);
+}
+
+void OLED::writeCommand(uint8_t code)
+{
+    _dc = 0;
+ 
+    _cs = 0;
+    _spi.write(code);
+    _cs = 1;
+}
+
+void OLED::writeData(uint8_t value)
+{
+    _dc = 1;
+    
+    _cs = 0;
+    _spi.write(value);
+    _cs = 1;
+}
+//Turns on OLED sleep mode
+void OLED::sleep()
+{
+    writeCommand(0xAE);
+}
+//Turns off OLED sleep mode
+void OLED::wake()
+{
+    writeCommand(0xAF);
+}
+
+void OLED::pixel(int16_t x, int16_t y, uint16_t color)
+{
+    // Bounds check.
+    if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT)) return;
+    if ((x < 0) || (y < 0)) return;
+
+    cursor(x, y);
+
+    writeCommand(OLED_CMD_WRITERAM);
+    _dc = 1;
+    _cs = 0;
+    
+    writeData(color >> 8);
+    writeData(color);
+    
+    _cs = 1;
+}
+
+void OLED::cursor(int x, int y)   //goTo
+{
+    if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT)) return;
+
+    // set x and y coordinate
+    writeCommand(OLED_CMD_SET_COLUMN);
+    writeData(x);
+    writeData(OLED_WIDTH-1);
+
+    writeCommand(OLED_CMD_SET_ROW);
+    writeData(y);
+    writeData(OLED_HEIGHT-1);
+
+    writeCommand(OLED_CMD_WRITERAM);
+}
+
+void OLED::rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor)
+{
+    // Bounds check
+    if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT))
+        return;
+
+    // Y bounds check
+    if (y+h > OLED_HEIGHT) {
+        h = OLED_HEIGHT - y - 1;
+    }
+
+    // X bounds check
+    if (x+w > OLED_WIDTH) {
+        w = OLED_WIDTH - x - 1;
+    }
+
+    // set location
+    writeCommand(OLED_CMD_SET_COLUMN);
+    writeData(x);
+    writeData(x+(w-1));
+    //writeData(x-1);
+    writeCommand(OLED_CMD_SET_ROW);
+    writeData(y);
+    writeData(y+(h-1));
+    //writeData(y-1);
+    // fill!
+    writeCommand(OLED_CMD_WRITERAM);
+    
+    for (uint16_t i=0; i < w*h; i++) {
+        //writeData(fillcolor >> 8);
+        writeData(fillcolor >> 8);
+        writeData(fillcolor);
+    }
+}
+
+uint16_t Color565(uint8_t r, uint8_t g, uint8_t b)
+{
+    uint16_t c;
+    c = r >> 3;
+    c <<= 6;
+    c |= g >> 2;
+    c <<= 5;
+    c |= b >> 3;
+
+    return c;
+}
\ No newline at end of file