A derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C.
Dependents: mbed-TFT-example-NCS36510
Fork of Adafruit_GFX by
Revision 9:ddb97c9850a2, committed 2014-10-19
- Comitter:
- nkhorman
- Date:
- Sun Oct 19 20:55:27 2014 +0000
- Parent:
- 8:416b8fe451b3
- Child:
- 10:d5aee2d2f8dd
- Child:
- 11:86909e6db3c8
- Commit message:
- c++'ify the SPI vs I2C driver portions, instead of compile time defines.; The SSD1306 driver can now be instantiated multiple times, supporting simultaneous displays of differing dimensions.
Changed in this revision
--- a/Adafruit_GFX.cpp Sun Oct 19 04:45:11 2014 +0000 +++ b/Adafruit_GFX.cpp Sun Oct 19 20:55:27 2014 +0000 @@ -14,7 +14,7 @@ ****************************************/ /* - * Modified by Neal Horman 7/14/2012 for use in LPC1768 + * Modified by Neal Horman 7/14/2012 for use in mbed */ #include "mbed.h" @@ -22,7 +22,7 @@ #include "Adafruit_GFX.h" #include "glcdfont.h" -#ifdef WANT_ABSTRACTS +#if defined(GFX_WANT_ABSTRACTS) // draw a circle outline void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) { @@ -146,7 +146,9 @@ } } } +#endif +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) // bresenham's algorithm - thx wikpedia void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) { @@ -192,7 +194,21 @@ } } +void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) +{ + // stupidest version - update in subclasses if desired! + drawLine(x, y, x, y+h-1, color); +} +void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) +{ + // stupidest version - update in subclasses if desired! + for (int16_t i=x; i<x+w; i++) + drawFastVLine(i, y, h, color); +} +#endif + +#if defined(GFX_WANT_ABSTRACTS) // draw a rectangle void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) { @@ -202,26 +218,12 @@ drawFastVLine(x+w-1, y, h, color); } -void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) -{ - // stupidest version - update in subclasses if desired! - drawLine(x, y, x, y+h-1, color); -} - void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) { // stupidest version - update in subclasses if desired! drawLine(x, y, x+w-1, y, color); } -void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) -{ - // stupidest version - update in subclasses if desired! - for (int16_t i=x; i<x+w; i++) - drawFastVLine(i, y, h, color); -} - - void Adafruit_GFX::fillScreen(uint16_t color) { fillRect(0, 0, _width, _height, color); @@ -407,26 +409,31 @@ { if (line & 0x1) { +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) if (size == 1) // default size drawPixel(x+i, y+j, color); -#ifdef WANT_ABSTRACTS else // big size fillRect(x+(i*size), y+(j*size), size, size, color); +#else + drawPixel(x+i, y+j, color); #endif } else if (bg != color) { +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) if (size == 1) // default size drawPixel(x+i, y+j, bg); -#ifdef WANT_ABSTRACTS else // big size fillRect(x+i*size, y+j*size, size, size, bg); +#else + drawPixel(x+i, y+j, bg); #endif } line >>= 1; } } } + void Adafruit_GFX::setRotation(uint8_t x) { x %= 4; // cant be higher than 3
--- a/Adafruit_GFX.h Sun Oct 19 04:45:11 2014 +0000 +++ b/Adafruit_GFX.h Sun Oct 19 20:55:27 2014 +0000 @@ -1,111 +1,122 @@ -/*********************************** -This is a our graphics core library, for all our displays. -We'll be adapting all the -existing libaries to use this core to make updating, support -and upgrading easier! - -Adafruit invests time and resources providing this open source code, -please support Adafruit and open-source hardware by purchasing -products from Adafruit! - -Written by Limor Fried/Ladyada for Adafruit Industries. -BSD license, check license.txt for more information -All text above must be included in any redistribution -****************************************/ - -/* - * Modified by Neal Horman 7/14/2012 for use in LPC1768 - */ - -#ifndef _ADAFRUIT_GFX_H_ -#define _ADAFRUIT_GFX_H_ - -static inline void swap(int16_t &a, int16_t &b) -{ - int16_t t = a; - - a = b; - b = t; -} - -#ifndef _BV -#define _BV(bit) (1<<(bit)) -#endif - -#define BLACK 0 -#define WHITE 1 - -class Adafruit_GFX : public Stream -{ - public: - Adafruit_GFX(int16_t w, int16_t h) - : _rawWidth(w) - , _rawHeight(h) - , _width(w) - , _height(h) - , cursor_x(0) - , cursor_y(0) - , textcolor(WHITE) - , textbgcolor(BLACK) - , textsize(1) - , rotation(0) - , wrap(true) - {}; - - // this must be defined by the subclass - virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; - // this is optional - virtual void invertDisplay(bool i) {}; - - // Stream implementation - provides printf() interface - // You would otherwise be forced to use writeChar() - virtual int _putc(int value) { return writeChar(value); }; - virtual int _getc() { return -1; }; - -#ifdef WANT_ABSTRACTS - // these are 'generic' drawing functions, so we can share them! - virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); - virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); - virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); - virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); - virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); - virtual void fillScreen(uint16_t color); - - void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); - void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color); - void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); - void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color); - - void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); - void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); - void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); - void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); -#endif - - void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); - void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size); - size_t writeChar(uint8_t); - - int16_t width(void) { return _width; }; - int16_t height(void) { return _height; }; - - void setCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; }; - void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; }; - void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; } - void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; }; - void setTextWrap(bool w) { wrap = w; }; - - void setRotation(uint8_t r); - uint8_t getRotation(void) { rotation %= 4; return rotation; }; - -protected: - int16_t _rawWidth, _rawHeight; // this is the 'raw' display w/h - never changes - int16_t _width, _height; // dependent on rotation - int16_t cursor_x, cursor_y; - uint16_t textcolor, textbgcolor; - uint8_t textsize; - uint8_t rotation; - bool wrap; // If set, 'wrap' text at right edge of display -}; - -#endif +/*********************************** +This is a our graphics core library, for all our displays. +We'll be adapting all the +existing libaries to use this core to make updating, support +and upgrading easier! + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above must be included in any redistribution +****************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in mbed + */ + +#ifndef _ADAFRUIT_GFX_H_ +#define _ADAFRUIT_GFX_H_ + +// Uncomment this on to enable all functionality +//#define GFX_WANT_ABSTRACTS + +// Uncomment this to enable only runtime font scaling, without all the rest of the Abstracts +//#define GFX_SIZEABLE_TEXT + +static inline void swap(int16_t &a, int16_t &b) +{ + int16_t t = a; + + a = b; + b = t; +} + +#ifndef _BV +#define _BV(bit) (1<<(bit)) +#endif + +#define BLACK 0 +#define WHITE 1 + +class Adafruit_GFX : public Stream +{ + public: + Adafruit_GFX(int16_t w, int16_t h) + : _rawWidth(w) + , _rawHeight(h) + , _width(w) + , _height(h) + , cursor_x(0) + , cursor_y(0) + , textcolor(WHITE) + , textbgcolor(BLACK) + , textsize(1) + , rotation(0) + , wrap(true) + {}; + + // this must be defined by the subclass + virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; + // this is optional + virtual void invertDisplay(bool i) {}; + + // Stream implementation - provides printf() interface + // You would otherwise be forced to use writeChar() + virtual int _putc(int value) { return writeChar(value); }; + virtual int _getc() { return -1; }; + +#ifdef GFX_WANT_ABSTRACTS + // these are 'generic' drawing functions, so we can share them! + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + virtual void fillScreen(uint16_t color); + + void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); + void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color); + void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); + void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color); + + void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); + void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); + void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); + void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); +#endif + +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) + virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); +#endif + + void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); + void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size); + size_t writeChar(uint8_t); + + int16_t width(void) { return _width; }; + int16_t height(void) { return _height; }; + + void setTextCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; }; +#if defined(GFX_SIZEABLE_TEXT) + void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; }; +#endif + void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; } + void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; }; + void setTextWrap(bool w) { wrap = w; }; + + void setRotation(uint8_t r); + uint8_t getRotation(void) { rotation %= 4; return rotation; }; + +protected: + int16_t _rawWidth, _rawHeight; // this is the 'raw' display w/h - never changes + int16_t _width, _height; // dependent on rotation + int16_t cursor_x, cursor_y; + uint16_t textcolor, textbgcolor; + uint8_t textsize; + uint8_t rotation; + bool wrap; // If set, 'wrap' text at right edge of display +}; + +#endif
--- a/Adafruit_SSD1306.cpp Sun Oct 19 04:45:11 2014 +0000 +++ b/Adafruit_SSD1306.cpp Sun Oct 19 20:55:27 2014 +0000 @@ -17,110 +17,33 @@ *********************************************************************/ /* - * Modified by Neal Horman 7/14/2012 for use in LPC1768 + * Modified by Neal Horman 7/14/2012 for use in mbed */ #include "mbed.h" #include "Adafruit_SSD1306.h" -#ifndef WITHOUT_SPLASH -uint8_t splashScreen[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = { -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, -0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, -0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, -0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, -0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, -0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, -0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, -0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, -0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, -0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, -0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, -0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, -0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, -0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, -0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, -0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, -0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, -0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, -0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, -0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -#if (SSD1306_LCDHEIGHT == 64) -0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, -0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, -0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, -0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, -0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, -0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, -0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, -0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, -0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, -0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, -0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, -0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, -0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, -0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -#endif -}; -#endif - -#ifdef SSD_USES_SPI -Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS) - : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) - , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi) -{ -#ifndef WITHOUT_SPLASH - memcpy(buffer,splashScreen,sizeof(buffer)); -#endif - begin(); - display(); -}; -#elif defined SSD_USES_I2C -Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST) - : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) - , rst(RST,false),mi2c(i2c) -{ - - mi2c.frequency(400000); - mi2c.start(); -#ifndef WITHOUT_SPLASH - memcpy(buffer,splashScreen,sizeof(buffer)); -#endif - begin(); - display(); -}; -#endif - +#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 void Adafruit_SSD1306::begin(uint8_t vccstate) { @@ -134,48 +57,49 @@ // bring out of reset rst = 1; // turn on VCC (9V?) - -#if defined SSD1306_128_32 -#define VAL_MULTIPLEX 0x1F -#define VAL_COMPINS 0x02 -#define VAL_CONTRAST 0x8F -#elif defined SSD1306_128_64 -#define VAL_MULTIPLEX 0x3F -#define VAL_COMPINS 0x12 -#define VAL_CONTRAST ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) -#else -#error "Display dimensions must be defined" -#endif + + command(SSD1306_DISPLAYOFF); + command(SSD1306_SETDISPLAYCLOCKDIV); + command(0x80); // the suggested ratio 0x80 + + command(SSD1306_SETMULTIPLEX); + command(_rawHeight-1); + + command(SSD1306_SETDISPLAYOFFSET); + command(0x0); // no offset + + command(SSD1306_SETSTARTLINE | 0x0); // line #0 + + command(SSD1306_CHARGEPUMP); + command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14); + + command(SSD1306_MEMORYMODE); + command(0x00); // 0x0 act like ks0108 - ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE - ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 - ssd1306_command(0x80); // the suggested ratio 0x80 - ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 - ssd1306_command(VAL_MULTIPLEX); - ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 - ssd1306_command(0x0); // no offset - ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 - ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D - ssd1306_command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14); - ssd1306_command(SSD1306_MEMORYMODE); // 0x20 - ssd1306_command(0x00); // 0x0 act like ks0108 - ssd1306_command(SSD1306_SEGREMAP | 0x1); - ssd1306_command(SSD1306_COMSCANDEC); - ssd1306_command(SSD1306_SETCOMPINS); // 0xDA - ssd1306_command(VAL_COMPINS); - ssd1306_command(SSD1306_SETCONTRAST); // 0x81 - ssd1306_command(VAL_CONTRAST); - ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 - ssd1306_command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1); - ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB - ssd1306_command(0x40); - ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 - ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 + command(SSD1306_SEGREMAP | 0x1); + + command(SSD1306_COMSCANDEC); + + command(SSD1306_SETCOMPINS); + command(_rawHeight == 32 ? 0x02 : 0x12); // TODO - calculate based on _rawHieght ? + + command(SSD1306_SETCONTRAST); + command(_rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) ); + + command(SSD1306_SETPRECHARGE); + command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1); + + command(SSD1306_SETVCOMDETECT); + command(0x40); + + command(SSD1306_DISPLAYALLON_RESUME); + + command(SSD1306_NORMALDISPLAY); - ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel + command(SSD1306_DISPLAYON); } -// the most basic function, set a single pixel +// Set a single pixel void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) { if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) @@ -200,97 +124,107 @@ // x is which column if (color == WHITE) - buffer[x+ (y/8)*SSD1306_LCDWIDTH] |= _BV((y%8)); - else - buffer[x+ (y/8)*SSD1306_LCDWIDTH] &= ~_BV((y%8)); + buffer[x+ (y/8)*_rawWidth] |= _BV((y%8)); + else // else black + buffer[x+ (y/8)*_rawWidth] &= ~_BV((y%8)); } void Adafruit_SSD1306::invertDisplay(bool i) { - if (i) - ssd1306_command(SSD1306_INVERTDISPLAY); - else - ssd1306_command(SSD1306_NORMALDISPLAY); + command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY); } -void Adafruit_SSD1306::ssd1306_command(uint8_t c) +// Send the display buffer out to the display +void Adafruit_SSD1306::display(void) { -#ifdef SSD_USES_SPI - cs = 1; - dc = 0; - cs = 0; - mspi.write(c); - cs = 1; - -#elif defined SSD_USES_I2C - char buff[2] ; - buff[0] = SSD_Command_Mode ; - buff[1] = c; - mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff)); -#endif - + command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 + command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 + command(SSD1306_SETSTARTLINE | 0x0); // line #0 + sendDisplayBuffer(); } -void Adafruit_SSD1306::ssd1306_data(uint8_t c) +// Clear the display buffer. Requires a display() call at some point afterwards +void Adafruit_SSD1306::clearDisplay(void) { -#ifdef SSD_USES_SPI - cs = 1; - dc = 1; - cs = 0; - mspi.write(c); - cs = 1; -#elif defined SSD_USES_I2C - char buff[2] ; - // Setup D/C to switch to data mode - buff[0] = SSD_Data_Mode; - buff[1] = c; - // Write on i2c - mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff)); -#endif - + std::fill(buffer.begin(),buffer.end(),0); } -void Adafruit_SSD1306::display(void) +void Adafruit_SSD1306::splash(void) { - ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 - ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 - ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 -#ifdef SSD_USES_SPI - cs = 1; - dc = 1; - cs = 0; - - for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) - mspi.write(buffer[i]); - - // i wonder why we have to do this (check datasheet) - if (SSD1306_LCDHEIGHT == 32) - { - for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) - mspi.write(0); - } - - cs = 1; - -#elif defined SSD_USES_I2C - char buff[17] ; - uint8_t x ; - // Setup D/C to switch to data mode - buff[0] = SSD_Data_Mode; - - // loop trough all OLED buffer and - // send a bunch of 16 data byte in one xmission - for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 ) - { - for (x=1; x<=16; x++) - buff[x] = buffer[i+x]; - mi2c.write(SSD_I2C_ADDRESS, buff, 17); - } +#ifndef NO_SPLASH_ADAFRUIT + uint8_t adaFruitLogo[64 * 128 / 8] = + { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, + 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, + 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, + 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, + 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, + 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, + 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, + 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, + 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, + 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, + 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, + 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, + 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, + 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, + 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // 128x32^^^ 128x64vvv + 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, + 0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, + 0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, + 0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, + 0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, + 0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, + 0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, + 0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, + 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, + 0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, + 0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, + 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, + 0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + std::copy( + &adaFruitLogo[0] + , &adaFruitLogo[0] + (_rawHeight == 32 ? sizeof(adaFruitLogo)/2 : sizeof(adaFruitLogo)) + , buffer.begin() + ); #endif } - -// clear everything -void Adafruit_SSD1306::clearDisplay(void) -{ - memset(buffer,0,sizeof(buffer)); -}
--- a/Adafruit_SSD1306.h Sun Oct 19 04:45:11 2014 +0000 +++ b/Adafruit_SSD1306.h Sun Oct 19 20:55:27 2014 +0000 @@ -17,7 +17,7 @@ *********************************************************************/ /* - * Modified by Neal Horman 7/14/2012 for use in LPC1768 + * Modified by Neal Horman 7/14/2012 for use in mbed */ #ifndef _ADAFRUIT_SSD1306_H_ @@ -26,120 +26,147 @@ #include "mbed.h" #include "Adafruit_GFX.h" -/*========================================================================= - SSD1306 Displays - ----------------------------------------------------------------------- - The driver is used in multiple displays (128x64, 128x32, etc.). - Select the appropriate display below to create an appropriately - sized framebuffer, etc. - - SSD1306_128_64 128x64 pixel display - - SSD1306_128_32 128x32 pixel display - - You also need to set the LCDWIDTH and LCDHEIGHT defines to an - appropriate size - - -----------------------------------------------------------------------*/ -//#define SSD1306_128_64 -#define SSD1306_128_32 - -#define SSD_USES_SPI -//#define SSD_USES_I2C -/*=========================================================================*/ - -#if defined SSD1306_128_64 && defined SSD1306_128_32 - #error "Only one SSD1306 display can be specified at once in SSD1306.h" -#endif -#if !defined SSD1306_128_64 && !defined SSD1306_128_32 - #error "At least one SSD1306 display must be specified in SSD1306.h" -#endif +#include <vector> +#include <algorithm> -#if defined SSD1306_128_64 - #define SSD1306_LCDWIDTH 128 - #define SSD1306_LCDHEIGHT 64 -#endif -#if defined SSD1306_128_32 - #define SSD1306_LCDWIDTH 128 - #define SSD1306_LCDHEIGHT 32 -#endif - -#if defined SSD_USES_SPI && defined SSD_USES_I2C - #error "Only one communication mode can be specified at once in SSD1306.h" -#endif -#if !defined SSD_USES_SPI && !defined SSD_USES_I2C - #error "At least one communication mode must be specified in SSD1306.h" -#endif - -//#define WITHOUT_SPLASH -#define SSD_I2C_ADDRESS 0x78 - -#define SSD_Command_Mode 0x00 -#define SSD_Data_Mode 0x40 +// A DigitalOut sub-class that provides a constructed default state +class DigitalOut2 : public DigitalOut +{ +public: + DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); }; + DigitalOut2& operator= (int value) { write(value); return *this; }; + DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; }; + operator int() { return read(); }; +}; -#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 -// an DigitalOut sub-class that provides a constructed default state -class DigitalOut2 : public DigitalOut -{ -public: - DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); }; - DigitalOut2& operator= (int value) { write(value); return *this; }; - DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; }; - operator int() { return read(); }; -}; - class Adafruit_SSD1306 : public Adafruit_GFX { - public: -#ifdef SSD_USES_SPI - Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS); -#elif defined SSD_USES_I2C - Adafruit_SSD1306(I2C &i2c, PinName RST); -#endif - void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); - void ssd1306_command(uint8_t c); - void ssd1306_data(uint8_t c); +public: + Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128) + : Adafruit_GFX(rawWidth,rawHeight) + , rst(RST,false) + {}; + + void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); + virtual void command(uint8_t c) = 0; + virtual void data(uint8_t c) = 0; - void clearDisplay(void); - virtual void invertDisplay(bool i); - void display(); + void clearDisplay(void); + virtual void invertDisplay(bool i); + void display(); + virtual void splash(); + + virtual void drawPixel(int16_t x, int16_t y, uint16_t color); - virtual void drawPixel(int16_t x, int16_t y, uint16_t color); - -private: - - DigitalOut2 rst; -#ifdef SSD_USES_SPI - DigitalOut2 cs,dc; - SPI &mspi; -#elif defined SSD_USES_I2C - I2C &mi2c; -#endif - // the memory buffer for the LCD - uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8]; +protected: + virtual void sendDisplayBuffer() = 0; + DigitalOut2 rst; + + // the memory buffer for the LCD + std::vector<uint8_t> buffer; +}; + +class Adafruit_SSD1306_Spi : public Adafruit_SSD1306 +{ +public: + Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128) + : Adafruit_SSD1306(RST, rawHieght, rawWidth) + , cs(CS,true) + , dc(DC,false) + , mspi(spi) + { + begin(); + splash(); + display(); + }; + + virtual void command(uint8_t c) + { + cs = 1; + dc = 0; + cs = 0; + mspi.write(c); + cs = 1; + }; + + virtual void data(uint8_t c) + { + cs = 1; + dc = 1; + cs = 0; + mspi.write(c); + cs = 1; + }; + +protected: + virtual void sendDisplayBuffer() + { + cs = 1; + dc = 1; + cs = 0; + + for(uint16_t i=0, q=buffer.size(); i<q; i++) + mspi.write(buffer[i]); + + cs = 1; + }; + + DigitalOut2 cs, dc; + SPI &mspi; +}; + +class Adafruit_SSD1306_I2c : public Adafruit_SSD1306 +{ +public: + #define SSD_I2C_ADDRESS 0x78 + Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128) + : Adafruit_SSD1306(RST, rawHeight, rawWidth) + , mi2c(i2c) + , mi2cAddress(i2cAddress) + { + begin(); + splash(); + display(); + }; + + virtual void command(uint8_t c) + { + char buff[2]; + buff[0] = 0; // Command Mode + buff[1] = c; + mi2c.write(mi2cAddress, buff, sizeof(buff)); + } + + virtual void data(uint8_t c) + { + char buff[2]; + buff[0] = 0x40; // Data Mode + buff[1] = c; + mi2c.write(mi2cAddress, buff, sizeof(buff)); + }; + +protected: + virtual void sendDisplayBuffer() + { + char buff[17]; + buff[0] = 0x40; // Data Mode + + // send display buffer in 16 byte chunks + for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) + { uint8_t x ; + + // TODO - this will segfault if buffer.size() % 16 != 0 + for(x=1; x<sizeof(buff); x++) + buff[x] = buffer[i+x]; + mi2c.write(mi2cAddress, buff, sizeof(buff)); + } + }; + + I2C &mi2c; + uint8_t mi2cAddress; }; #endif \ No newline at end of file