Adafruit

Dependents:   line_tracing

Committer:
nkhorman
Date:
Mon Oct 20 03:16:15 2014 +0000
Revision:
10:d5aee2d2f8dd
Parent:
9:ddb97c9850a2
Child:
12:7964c2cfdebc
fix - missed this in the refactor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 0:c3dcd4c4983a 1 /*********************************************************************
nkhorman 0:c3dcd4c4983a 2 This is a library for our Monochrome OLEDs based on SSD1306 drivers
nkhorman 0:c3dcd4c4983a 3
nkhorman 0:c3dcd4c4983a 4 Pick one up today in the adafruit shop!
nkhorman 0:c3dcd4c4983a 5 ------> http://www.adafruit.com/category/63_98
nkhorman 0:c3dcd4c4983a 6
nkhorman 0:c3dcd4c4983a 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
nkhorman 0:c3dcd4c4983a 10 Adafruit invests time and resources providing this open source code,
nkhorman 0:c3dcd4c4983a 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
nkhorman 0:c3dcd4c4983a 14 Written by Limor Fried/Ladyada for Adafruit Industries.
nkhorman 0:c3dcd4c4983a 15 BSD license, check license.txt for more information
nkhorman 0:c3dcd4c4983a 16 All text above, and the splash screen must be included in any redistribution
nkhorman 0:c3dcd4c4983a 17 *********************************************************************/
nkhorman 0:c3dcd4c4983a 18
nkhorman 0:c3dcd4c4983a 19 /*
nkhorman 9:ddb97c9850a2 20 * Modified by Neal Horman 7/14/2012 for use in mbed
nkhorman 0:c3dcd4c4983a 21 */
nkhorman 0:c3dcd4c4983a 22
nkhorman 0:c3dcd4c4983a 23 #ifndef _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 24 #define _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 25
nkhorman 0:c3dcd4c4983a 26 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 27 #include "Adafruit_GFX.h"
nkhorman 0:c3dcd4c4983a 28
nkhorman 9:ddb97c9850a2 29 #include <vector>
nkhorman 9:ddb97c9850a2 30 #include <algorithm>
nkhorman 0:c3dcd4c4983a 31
nkhorman 9:ddb97c9850a2 32 // A DigitalOut sub-class that provides a constructed default state
nkhorman 9:ddb97c9850a2 33 class DigitalOut2 : public DigitalOut
nkhorman 9:ddb97c9850a2 34 {
nkhorman 9:ddb97c9850a2 35 public:
nkhorman 9:ddb97c9850a2 36 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
nkhorman 9:ddb97c9850a2 37 DigitalOut2& operator= (int value) { write(value); return *this; };
nkhorman 9:ddb97c9850a2 38 DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
nkhorman 9:ddb97c9850a2 39 operator int() { return read(); };
nkhorman 9:ddb97c9850a2 40 };
Neal Horman 6:1be3e3b46eb7 41
nkhorman 0:c3dcd4c4983a 42 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 43 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 44
nkhorman 0:c3dcd4c4983a 45 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 46 {
nkhorman 9:ddb97c9850a2 47 public:
nkhorman 9:ddb97c9850a2 48 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 49 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 9:ddb97c9850a2 50 , rst(RST,false)
nkhorman 10:d5aee2d2f8dd 51 {
nkhorman 10:d5aee2d2f8dd 52 buffer.resize(rawHeight * rawWidth / 8);
nkhorman 10:d5aee2d2f8dd 53 };
nkhorman 9:ddb97c9850a2 54
nkhorman 9:ddb97c9850a2 55 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 9:ddb97c9850a2 56 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 57 virtual void data(uint8_t c) = 0;
nkhorman 0:c3dcd4c4983a 58
nkhorman 9:ddb97c9850a2 59 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 60 virtual void invertDisplay(bool i);
nkhorman 9:ddb97c9850a2 61 void display();
nkhorman 9:ddb97c9850a2 62 virtual void splash();
nkhorman 9:ddb97c9850a2 63
nkhorman 9:ddb97c9850a2 64 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 0:c3dcd4c4983a 65
nkhorman 9:ddb97c9850a2 66 protected:
nkhorman 9:ddb97c9850a2 67 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 68 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 69
nkhorman 9:ddb97c9850a2 70 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 71 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 72 };
nkhorman 9:ddb97c9850a2 73
nkhorman 9:ddb97c9850a2 74 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 75 {
nkhorman 9:ddb97c9850a2 76 public:
nkhorman 9:ddb97c9850a2 77 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 78 : Adafruit_SSD1306(RST, rawHieght, rawWidth)
nkhorman 9:ddb97c9850a2 79 , cs(CS,true)
nkhorman 9:ddb97c9850a2 80 , dc(DC,false)
nkhorman 9:ddb97c9850a2 81 , mspi(spi)
nkhorman 9:ddb97c9850a2 82 {
nkhorman 9:ddb97c9850a2 83 begin();
nkhorman 9:ddb97c9850a2 84 splash();
nkhorman 9:ddb97c9850a2 85 display();
nkhorman 9:ddb97c9850a2 86 };
nkhorman 9:ddb97c9850a2 87
nkhorman 9:ddb97c9850a2 88 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 89 {
nkhorman 9:ddb97c9850a2 90 cs = 1;
nkhorman 9:ddb97c9850a2 91 dc = 0;
nkhorman 9:ddb97c9850a2 92 cs = 0;
nkhorman 9:ddb97c9850a2 93 mspi.write(c);
nkhorman 9:ddb97c9850a2 94 cs = 1;
nkhorman 9:ddb97c9850a2 95 };
nkhorman 9:ddb97c9850a2 96
nkhorman 9:ddb97c9850a2 97 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 98 {
nkhorman 9:ddb97c9850a2 99 cs = 1;
nkhorman 9:ddb97c9850a2 100 dc = 1;
nkhorman 9:ddb97c9850a2 101 cs = 0;
nkhorman 9:ddb97c9850a2 102 mspi.write(c);
nkhorman 9:ddb97c9850a2 103 cs = 1;
nkhorman 9:ddb97c9850a2 104 };
nkhorman 9:ddb97c9850a2 105
nkhorman 9:ddb97c9850a2 106 protected:
nkhorman 9:ddb97c9850a2 107 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 108 {
nkhorman 9:ddb97c9850a2 109 cs = 1;
nkhorman 9:ddb97c9850a2 110 dc = 1;
nkhorman 9:ddb97c9850a2 111 cs = 0;
nkhorman 9:ddb97c9850a2 112
nkhorman 9:ddb97c9850a2 113 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 9:ddb97c9850a2 114 mspi.write(buffer[i]);
nkhorman 9:ddb97c9850a2 115
nkhorman 9:ddb97c9850a2 116 cs = 1;
nkhorman 9:ddb97c9850a2 117 };
nkhorman 9:ddb97c9850a2 118
nkhorman 9:ddb97c9850a2 119 DigitalOut2 cs, dc;
nkhorman 9:ddb97c9850a2 120 SPI &mspi;
nkhorman 9:ddb97c9850a2 121 };
nkhorman 9:ddb97c9850a2 122
nkhorman 9:ddb97c9850a2 123 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 124 {
nkhorman 9:ddb97c9850a2 125 public:
nkhorman 9:ddb97c9850a2 126 #define SSD_I2C_ADDRESS 0x78
nkhorman 9:ddb97c9850a2 127 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 128 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
nkhorman 9:ddb97c9850a2 129 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 130 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 131 {
nkhorman 9:ddb97c9850a2 132 begin();
nkhorman 9:ddb97c9850a2 133 splash();
nkhorman 9:ddb97c9850a2 134 display();
nkhorman 9:ddb97c9850a2 135 };
nkhorman 9:ddb97c9850a2 136
nkhorman 9:ddb97c9850a2 137 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 138 {
nkhorman 9:ddb97c9850a2 139 char buff[2];
nkhorman 9:ddb97c9850a2 140 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 141 buff[1] = c;
nkhorman 9:ddb97c9850a2 142 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 143 }
nkhorman 9:ddb97c9850a2 144
nkhorman 9:ddb97c9850a2 145 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 146 {
nkhorman 9:ddb97c9850a2 147 char buff[2];
nkhorman 9:ddb97c9850a2 148 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 149 buff[1] = c;
nkhorman 9:ddb97c9850a2 150 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 151 };
nkhorman 9:ddb97c9850a2 152
nkhorman 9:ddb97c9850a2 153 protected:
nkhorman 9:ddb97c9850a2 154 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 155 {
nkhorman 9:ddb97c9850a2 156 char buff[17];
nkhorman 9:ddb97c9850a2 157 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 158
nkhorman 9:ddb97c9850a2 159 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 160 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 161 { uint8_t x ;
nkhorman 9:ddb97c9850a2 162
nkhorman 9:ddb97c9850a2 163 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 164 for(x=1; x<sizeof(buff); x++)
nkhorman 9:ddb97c9850a2 165 buff[x] = buffer[i+x];
nkhorman 9:ddb97c9850a2 166 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 167 }
nkhorman 9:ddb97c9850a2 168 };
nkhorman 9:ddb97c9850a2 169
nkhorman 9:ddb97c9850a2 170 I2C &mi2c;
nkhorman 9:ddb97c9850a2 171 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 172 };
nkhorman 0:c3dcd4c4983a 173
nkhorman 0:c3dcd4c4983a 174 #endif