p kj
/
LPC824-BlynkWeatherstation
Microduino
Fork of Io_moon by
Adafruit_GFX/Adafruit_SSD1306.h@0:740c1eb2df13, 2016-06-23 (annotated)
- Committer:
- lixianyu
- Date:
- Thu Jun 23 11:16:14 2016 +0000
- Revision:
- 0:740c1eb2df13
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:740c1eb2df13 | 1 | /********************************************************************* |
lixianyu | 0:740c1eb2df13 | 2 | This is a library for our Monochrome OLEDs based on SSD1306 drivers |
lixianyu | 0:740c1eb2df13 | 3 | |
lixianyu | 0:740c1eb2df13 | 4 | Pick one up today in the adafruit shop! |
lixianyu | 0:740c1eb2df13 | 5 | ------> http://www.adafruit.com/category/63_98 |
lixianyu | 0:740c1eb2df13 | 6 | |
lixianyu | 0:740c1eb2df13 | 7 | These displays use SPI to communicate, 4 or 5 pins are required to |
lixianyu | 0:740c1eb2df13 | 8 | interface |
lixianyu | 0:740c1eb2df13 | 9 | |
lixianyu | 0:740c1eb2df13 | 10 | Adafruit invests time and resources providing this open source code, |
lixianyu | 0:740c1eb2df13 | 11 | please support Adafruit and open-source hardware by purchasing |
lixianyu | 0:740c1eb2df13 | 12 | products from Adafruit! |
lixianyu | 0:740c1eb2df13 | 13 | |
lixianyu | 0:740c1eb2df13 | 14 | Written by Limor Fried/Ladyada for Adafruit Industries. |
lixianyu | 0:740c1eb2df13 | 15 | BSD license, check license.txt for more information |
lixianyu | 0:740c1eb2df13 | 16 | All text above, and the splash screen must be included in any redistribution |
lixianyu | 0:740c1eb2df13 | 17 | *********************************************************************/ |
lixianyu | 0:740c1eb2df13 | 18 | |
lixianyu | 0:740c1eb2df13 | 19 | /* |
lixianyu | 0:740c1eb2df13 | 20 | * Modified by Neal Horman 7/14/2012 for use in mbed |
lixianyu | 0:740c1eb2df13 | 21 | */ |
lixianyu | 0:740c1eb2df13 | 22 | |
lixianyu | 0:740c1eb2df13 | 23 | #ifndef _ADAFRUIT_SSD1306_H_ |
lixianyu | 0:740c1eb2df13 | 24 | #define _ADAFRUIT_SSD1306_H_ |
lixianyu | 0:740c1eb2df13 | 25 | |
lixianyu | 0:740c1eb2df13 | 26 | #include "mbed.h" |
lixianyu | 0:740c1eb2df13 | 27 | #include "Adafruit_GFX.h" |
lixianyu | 0:740c1eb2df13 | 28 | #if 0 |
lixianyu | 0:740c1eb2df13 | 29 | #include <vector> |
lixianyu | 0:740c1eb2df13 | 30 | #include <algorithm> |
lixianyu | 0:740c1eb2df13 | 31 | #endif |
lixianyu | 0:740c1eb2df13 | 32 | // A DigitalOut sub-class that provides a constructed default state |
lixianyu | 0:740c1eb2df13 | 33 | class DigitalOut2 : public DigitalOut |
lixianyu | 0:740c1eb2df13 | 34 | { |
lixianyu | 0:740c1eb2df13 | 35 | public: |
lixianyu | 0:740c1eb2df13 | 36 | DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { |
lixianyu | 0:740c1eb2df13 | 37 | write(active); |
lixianyu | 0:740c1eb2df13 | 38 | }; |
lixianyu | 0:740c1eb2df13 | 39 | DigitalOut2& operator= (int value) { |
lixianyu | 0:740c1eb2df13 | 40 | write(value); |
lixianyu | 0:740c1eb2df13 | 41 | return *this; |
lixianyu | 0:740c1eb2df13 | 42 | }; |
lixianyu | 0:740c1eb2df13 | 43 | DigitalOut2& operator= (DigitalOut2& rhs) { |
lixianyu | 0:740c1eb2df13 | 44 | write(rhs.read()); |
lixianyu | 0:740c1eb2df13 | 45 | return *this; |
lixianyu | 0:740c1eb2df13 | 46 | }; |
lixianyu | 0:740c1eb2df13 | 47 | operator int() { |
lixianyu | 0:740c1eb2df13 | 48 | return read(); |
lixianyu | 0:740c1eb2df13 | 49 | }; |
lixianyu | 0:740c1eb2df13 | 50 | }; |
lixianyu | 0:740c1eb2df13 | 51 | |
lixianyu | 0:740c1eb2df13 | 52 | #define SSD1306_EXTERNALVCC 0x1 |
lixianyu | 0:740c1eb2df13 | 53 | #define SSD1306_SWITCHCAPVCC 0x2 |
lixianyu | 0:740c1eb2df13 | 54 | #ifdef ADAFRUIT_SSD1306_CPP |
lixianyu | 0:740c1eb2df13 | 55 | uint8_t buffer[1024]; |
lixianyu | 0:740c1eb2df13 | 56 | #else |
lixianyu | 0:740c1eb2df13 | 57 | extern uint8_t buffer[1024]; |
lixianyu | 0:740c1eb2df13 | 58 | #endif |
lixianyu | 0:740c1eb2df13 | 59 | /** The pure base class for the SSD1306 display driver. |
lixianyu | 0:740c1eb2df13 | 60 | * |
lixianyu | 0:740c1eb2df13 | 61 | * You should derive from this for a new transport interface type, |
lixianyu | 0:740c1eb2df13 | 62 | * such as the SPI and I2C drivers. |
lixianyu | 0:740c1eb2df13 | 63 | */ |
lixianyu | 0:740c1eb2df13 | 64 | class Adafruit_SSD1306 : public Adafruit_GFX |
lixianyu | 0:740c1eb2df13 | 65 | { |
lixianyu | 0:740c1eb2df13 | 66 | public: |
lixianyu | 0:740c1eb2df13 | 67 | Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128) |
lixianyu | 0:740c1eb2df13 | 68 | : Adafruit_GFX(rawWidth,rawHeight) |
lixianyu | 0:740c1eb2df13 | 69 | , rst(RST,false) { |
lixianyu | 0:740c1eb2df13 | 70 | //buffer.resize(rawHeight * rawWidth / 8); |
lixianyu | 0:740c1eb2df13 | 71 | }; |
lixianyu | 0:740c1eb2df13 | 72 | |
lixianyu | 0:740c1eb2df13 | 73 | void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); |
lixianyu | 0:740c1eb2df13 | 74 | |
lixianyu | 0:740c1eb2df13 | 75 | // These must be implemented in the derived transport driver |
lixianyu | 0:740c1eb2df13 | 76 | virtual void command(uint8_t c) = 0; |
lixianyu | 0:740c1eb2df13 | 77 | virtual void data(uint8_t c) = 0; |
lixianyu | 0:740c1eb2df13 | 78 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color); |
lixianyu | 0:740c1eb2df13 | 79 | |
lixianyu | 0:740c1eb2df13 | 80 | /// Clear the display buffer |
lixianyu | 0:740c1eb2df13 | 81 | void clearDisplay(void); |
lixianyu | 0:740c1eb2df13 | 82 | virtual void invertDisplay(bool i); |
lixianyu | 0:740c1eb2df13 | 83 | |
lixianyu | 0:740c1eb2df13 | 84 | /// Cause the display to be updated with the buffer content. |
lixianyu | 0:740c1eb2df13 | 85 | void display(); |
lixianyu | 0:740c1eb2df13 | 86 | /// Fill the buffer with the AdaFruit splash screen. |
lixianyu | 0:740c1eb2df13 | 87 | virtual void splash(); |
lixianyu | 0:740c1eb2df13 | 88 | |
lixianyu | 0:740c1eb2df13 | 89 | protected: |
lixianyu | 0:740c1eb2df13 | 90 | virtual void sendDisplayBuffer() = 0; |
lixianyu | 0:740c1eb2df13 | 91 | DigitalOut2 rst; |
lixianyu | 0:740c1eb2df13 | 92 | |
lixianyu | 0:740c1eb2df13 | 93 | // the memory buffer for the LCD |
lixianyu | 0:740c1eb2df13 | 94 | //std::vector<uint8_t> buffer; |
lixianyu | 0:740c1eb2df13 | 95 | }; |
lixianyu | 0:740c1eb2df13 | 96 | |
lixianyu | 0:740c1eb2df13 | 97 | #if 0 |
lixianyu | 0:740c1eb2df13 | 98 | /** This is the SPI SSD1306 display driver transport class |
lixianyu | 0:740c1eb2df13 | 99 | * |
lixianyu | 0:740c1eb2df13 | 100 | */ |
lixianyu | 0:740c1eb2df13 | 101 | class Adafruit_SSD1306_Spi : public Adafruit_SSD1306 |
lixianyu | 0:740c1eb2df13 | 102 | { |
lixianyu | 0:740c1eb2df13 | 103 | public: |
lixianyu | 0:740c1eb2df13 | 104 | /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions |
lixianyu | 0:740c1eb2df13 | 105 | * |
lixianyu | 0:740c1eb2df13 | 106 | * Required parameters |
lixianyu | 0:740c1eb2df13 | 107 | * @param spi - a reference to an initialized SPI object |
lixianyu | 0:740c1eb2df13 | 108 | * @param DC (Data/Command) pin name |
lixianyu | 0:740c1eb2df13 | 109 | * @param RST (Reset) pin name |
lixianyu | 0:740c1eb2df13 | 110 | * @param CS (Chip Select) pin name |
lixianyu | 0:740c1eb2df13 | 111 | * |
lixianyu | 0:740c1eb2df13 | 112 | * Optional parameters |
lixianyu | 0:740c1eb2df13 | 113 | * @param rawHeight - the vertical number of pixels for the display, defaults to 32 |
lixianyu | 0:740c1eb2df13 | 114 | * @param rawWidth - the horizonal number of pixels for the display, defaults to 128 |
lixianyu | 0:740c1eb2df13 | 115 | */ |
lixianyu | 0:740c1eb2df13 | 116 | Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128) |
lixianyu | 0:740c1eb2df13 | 117 | : Adafruit_SSD1306(RST, rawHieght, rawWidth) |
lixianyu | 0:740c1eb2df13 | 118 | , cs(CS,true) |
lixianyu | 0:740c1eb2df13 | 119 | , dc(DC,false) |
lixianyu | 0:740c1eb2df13 | 120 | , mspi(spi) { |
lixianyu | 0:740c1eb2df13 | 121 | begin(); |
lixianyu | 0:740c1eb2df13 | 122 | splash(); |
lixianyu | 0:740c1eb2df13 | 123 | display(); |
lixianyu | 0:740c1eb2df13 | 124 | }; |
lixianyu | 0:740c1eb2df13 | 125 | |
lixianyu | 0:740c1eb2df13 | 126 | virtual void command(uint8_t c) { |
lixianyu | 0:740c1eb2df13 | 127 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 128 | dc = 0; |
lixianyu | 0:740c1eb2df13 | 129 | cs = 0; |
lixianyu | 0:740c1eb2df13 | 130 | mspi.write(c); |
lixianyu | 0:740c1eb2df13 | 131 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 132 | }; |
lixianyu | 0:740c1eb2df13 | 133 | |
lixianyu | 0:740c1eb2df13 | 134 | virtual void data(uint8_t c) { |
lixianyu | 0:740c1eb2df13 | 135 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 136 | dc = 1; |
lixianyu | 0:740c1eb2df13 | 137 | cs = 0; |
lixianyu | 0:740c1eb2df13 | 138 | mspi.write(c); |
lixianyu | 0:740c1eb2df13 | 139 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 140 | }; |
lixianyu | 0:740c1eb2df13 | 141 | |
lixianyu | 0:740c1eb2df13 | 142 | protected: |
lixianyu | 0:740c1eb2df13 | 143 | virtual void sendDisplayBuffer() { |
lixianyu | 0:740c1eb2df13 | 144 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 145 | dc = 1; |
lixianyu | 0:740c1eb2df13 | 146 | cs = 0; |
lixianyu | 0:740c1eb2df13 | 147 | |
lixianyu | 0:740c1eb2df13 | 148 | for(uint16_t i=0, q=buffer.size(); i<q; i++) |
lixianyu | 0:740c1eb2df13 | 149 | mspi.write(buffer[i]); |
lixianyu | 0:740c1eb2df13 | 150 | |
lixianyu | 0:740c1eb2df13 | 151 | if(height() == 32) { |
lixianyu | 0:740c1eb2df13 | 152 | for(uint16_t i=0, q=buffer.size(); i<q; i++) |
lixianyu | 0:740c1eb2df13 | 153 | mspi.write(0); |
lixianyu | 0:740c1eb2df13 | 154 | } |
lixianyu | 0:740c1eb2df13 | 155 | |
lixianyu | 0:740c1eb2df13 | 156 | cs = 1; |
lixianyu | 0:740c1eb2df13 | 157 | }; |
lixianyu | 0:740c1eb2df13 | 158 | |
lixianyu | 0:740c1eb2df13 | 159 | DigitalOut2 cs, dc; |
lixianyu | 0:740c1eb2df13 | 160 | SPI &mspi; |
lixianyu | 0:740c1eb2df13 | 161 | }; |
lixianyu | 0:740c1eb2df13 | 162 | #endif |
lixianyu | 0:740c1eb2df13 | 163 | |
lixianyu | 0:740c1eb2df13 | 164 | /** This is the I2C SSD1306 display driver transport class |
lixianyu | 0:740c1eb2df13 | 165 | * |
lixianyu | 0:740c1eb2df13 | 166 | */ |
lixianyu | 0:740c1eb2df13 | 167 | class Adafruit_SSD1306_I2c : public Adafruit_SSD1306 |
lixianyu | 0:740c1eb2df13 | 168 | { |
lixianyu | 0:740c1eb2df13 | 169 | public: |
lixianyu | 0:740c1eb2df13 | 170 | #define SSD_I2C_ADDRESS 0x78 |
lixianyu | 0:740c1eb2df13 | 171 | /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions |
lixianyu | 0:740c1eb2df13 | 172 | * |
lixianyu | 0:740c1eb2df13 | 173 | * Required parameters |
lixianyu | 0:740c1eb2df13 | 174 | * @param i2c - A reference to an initialized I2C object |
lixianyu | 0:740c1eb2df13 | 175 | * @param RST - The Reset pin name |
lixianyu | 0:740c1eb2df13 | 176 | * |
lixianyu | 0:740c1eb2df13 | 177 | * Optional parameters |
lixianyu | 0:740c1eb2df13 | 178 | * @param i2cAddress - The i2c address of the display |
lixianyu | 0:740c1eb2df13 | 179 | * @param rawHeight - The vertical number of pixels for the display, defaults to 32 |
lixianyu | 0:740c1eb2df13 | 180 | * @param rawWidth - The horizonal number of pixels for the display, defaults to 128 |
lixianyu | 0:740c1eb2df13 | 181 | */ |
lixianyu | 0:740c1eb2df13 | 182 | Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128) |
lixianyu | 0:740c1eb2df13 | 183 | : Adafruit_SSD1306(RST, rawHeight, rawWidth) |
lixianyu | 0:740c1eb2df13 | 184 | , mi2c(i2c) |
lixianyu | 0:740c1eb2df13 | 185 | , mi2cAddress(i2cAddress) { |
lixianyu | 0:740c1eb2df13 | 186 | begin(); |
lixianyu | 0:740c1eb2df13 | 187 | splash(); |
lixianyu | 0:740c1eb2df13 | 188 | display(); |
lixianyu | 0:740c1eb2df13 | 189 | }; |
lixianyu | 0:740c1eb2df13 | 190 | |
lixianyu | 0:740c1eb2df13 | 191 | virtual void command(uint8_t c) { |
lixianyu | 0:740c1eb2df13 | 192 | char buff[2]; |
lixianyu | 0:740c1eb2df13 | 193 | buff[0] = 0; // Command Mode |
lixianyu | 0:740c1eb2df13 | 194 | buff[1] = c; |
lixianyu | 0:740c1eb2df13 | 195 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
lixianyu | 0:740c1eb2df13 | 196 | } |
lixianyu | 0:740c1eb2df13 | 197 | |
lixianyu | 0:740c1eb2df13 | 198 | virtual void data(uint8_t c) { |
lixianyu | 0:740c1eb2df13 | 199 | char buff[2]; |
lixianyu | 0:740c1eb2df13 | 200 | buff[0] = 0x40; // Data Mode |
lixianyu | 0:740c1eb2df13 | 201 | buff[1] = c; |
lixianyu | 0:740c1eb2df13 | 202 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
lixianyu | 0:740c1eb2df13 | 203 | }; |
lixianyu | 0:740c1eb2df13 | 204 | |
lixianyu | 0:740c1eb2df13 | 205 | protected: |
lixianyu | 0:740c1eb2df13 | 206 | virtual void sendDisplayBuffer() { |
lixianyu | 0:740c1eb2df13 | 207 | char buff[17]; |
lixianyu | 0:740c1eb2df13 | 208 | buff[0] = 0x40; // Data Mode |
lixianyu | 0:740c1eb2df13 | 209 | #if 0 |
lixianyu | 0:740c1eb2df13 | 210 | // send display buffer in 16 byte chunks |
lixianyu | 0:740c1eb2df13 | 211 | for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) { |
lixianyu | 0:740c1eb2df13 | 212 | uint8_t x ; |
lixianyu | 0:740c1eb2df13 | 213 | |
lixianyu | 0:740c1eb2df13 | 214 | // TODO - this will segfault if buffer.size() % 16 != 0 |
lixianyu | 0:740c1eb2df13 | 215 | for(x=1; x<sizeof(buff); x++) |
lixianyu | 0:740c1eb2df13 | 216 | buff[x] = buffer[i+x-1]; |
lixianyu | 0:740c1eb2df13 | 217 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
lixianyu | 0:740c1eb2df13 | 218 | } |
lixianyu | 0:740c1eb2df13 | 219 | #else |
lixianyu | 0:740c1eb2df13 | 220 | for(uint16_t i=0; i<1024; i+=16 ) { |
lixianyu | 0:740c1eb2df13 | 221 | uint8_t x ; |
lixianyu | 0:740c1eb2df13 | 222 | for(x=1; x<sizeof(buff); x++) |
lixianyu | 0:740c1eb2df13 | 223 | buff[x] = buffer[i+x-1]; |
lixianyu | 0:740c1eb2df13 | 224 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
lixianyu | 0:740c1eb2df13 | 225 | } |
lixianyu | 0:740c1eb2df13 | 226 | #endif |
lixianyu | 0:740c1eb2df13 | 227 | }; |
lixianyu | 0:740c1eb2df13 | 228 | |
lixianyu | 0:740c1eb2df13 | 229 | I2C &mi2c; |
lixianyu | 0:740c1eb2df13 | 230 | uint8_t mi2cAddress; |
lixianyu | 0:740c1eb2df13 | 231 | }; |
lixianyu | 0:740c1eb2df13 | 232 | |
lixianyu | 0:740c1eb2df13 | 233 | #endif |