BLYNK TEST

Dependencies:   mbed Blynk

Committer:
lixianyu
Date:
Thu Jun 16 08:12:33 2016 +0000
Revision:
4:e5018e5ba340
Parent:
1:0e75de2a5d21
ok

Who changed what in which revision?

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