add scroll function

Dependents:   I2C_SSD1306_nucleo_l432KCc I2C_SSD1306_nucleo_l432KC

Fork of Adafruit_GFX by DongEun Koak

Committer:
bibi94
Date:
Fri Sep 21 08:24:03 2018 +0000
Revision:
19:e31b31e3128a
Parent:
15:77feec1c0684
add scroll functions

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 };
bibi94 19:e31b31e3128a 41 #define SSD1306_LCDWIDTH 128
bibi94 19:e31b31e3128a 42 #define SSD1306_LCDHEIGHT 64
nkhorman 0:c3dcd4c4983a 43 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 44 #define SSD1306_SWITCHCAPVCC 0x2
bibi94 19:e31b31e3128a 45 #define SSD1306_ACTIVATE_SCROLL 0x2F
bibi94 19:e31b31e3128a 46 #define SSD1306_DEACTIVATE_SCROLL 0x2E
bibi94 19:e31b31e3128a 47 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
bibi94 19:e31b31e3128a 48 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
bibi94 19:e31b31e3128a 49 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
bibi94 19:e31b31e3128a 50 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
bibi94 19:e31b31e3128a 51 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
bibi94 19:e31b31e3128a 52 #define SSD1306_SETCONTRAST 0x81
bibi94 19:e31b31e3128a 53 #define SSD1306_DISPLAYALLON_RESUME 0xA4
bibi94 19:e31b31e3128a 54 #define SSD1306_DISPLAYALLON 0xA5
bibi94 19:e31b31e3128a 55 #define SSD1306_NORMALDISPLAY 0xA6
bibi94 19:e31b31e3128a 56 #define SSD1306_INVERTDISPLAY 0xA7
bibi94 19:e31b31e3128a 57 #define SSD1306_DISPLAYOFF 0xAE
bibi94 19:e31b31e3128a 58 #define SSD1306_DISPLAYON 0xAF
bibi94 19:e31b31e3128a 59
bibi94 19:e31b31e3128a 60 #define SSD1306_SETDISPLAYOFFSET 0xD3
bibi94 19:e31b31e3128a 61 #define SSD1306_SETCOMPINS 0xDA
bibi94 19:e31b31e3128a 62
bibi94 19:e31b31e3128a 63 #define SSD1306_SETVCOMDETECT 0xDB
bibi94 19:e31b31e3128a 64
bibi94 19:e31b31e3128a 65 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
bibi94 19:e31b31e3128a 66 #define SSD1306_SETPRECHARGE 0xD9
bibi94 19:e31b31e3128a 67
bibi94 19:e31b31e3128a 68 #define SSD1306_SETMULTIPLEX 0xA8
bibi94 19:e31b31e3128a 69
bibi94 19:e31b31e3128a 70 #define SSD1306_SETLOWCOLUMN 0x00
bibi94 19:e31b31e3128a 71 #define SSD1306_SETHIGHCOLUMN 0x10
bibi94 19:e31b31e3128a 72
bibi94 19:e31b31e3128a 73 #define SSD1306_SETSTARTLINE 0x40
bibi94 19:e31b31e3128a 74
bibi94 19:e31b31e3128a 75 #define SSD1306_MEMORYMODE 0x20
bibi94 19:e31b31e3128a 76 #define SSD1306_COLUMNADDR 0x21
bibi94 19:e31b31e3128a 77 #define SSD1306_PAGEADDR 0x22
bibi94 19:e31b31e3128a 78
bibi94 19:e31b31e3128a 79 #define SSD1306_COMSCANINC 0xC0
bibi94 19:e31b31e3128a 80 #define SSD1306_COMSCANDEC 0xC8
bibi94 19:e31b31e3128a 81
bibi94 19:e31b31e3128a 82 #define SSD1306_SEGREMAP 0xA0
bibi94 19:e31b31e3128a 83
bibi94 19:e31b31e3128a 84 #define SSD1306_CHARGEPUMP 0x8D
nkhorman 0:c3dcd4c4983a 85
nkhorman 11:86909e6db3c8 86 /** The pure base class for the SSD1306 display driver.
nkhorman 11:86909e6db3c8 87 *
nkhorman 11:86909e6db3c8 88 * You should derive from this for a new transport interface type,
nkhorman 11:86909e6db3c8 89 * such as the SPI and I2C drivers.
nkhorman 11:86909e6db3c8 90 */
nkhorman 0:c3dcd4c4983a 91 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 92 {
nkhorman 9:ddb97c9850a2 93 public:
nkhorman 9:ddb97c9850a2 94 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 95 : Adafruit_GFX(rawWidth,rawHeight)
nkhorman 9:ddb97c9850a2 96 , rst(RST,false)
nkhorman 11:86909e6db3c8 97 {
nkhorman 11:86909e6db3c8 98 buffer.resize(rawHeight * rawWidth / 8);
nkhorman 11:86909e6db3c8 99 };
nkhorman 9:ddb97c9850a2 100
nkhorman 9:ddb97c9850a2 101 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 11:86909e6db3c8 102
nkhorman 11:86909e6db3c8 103 // These must be implemented in the derived transport driver
nkhorman 9:ddb97c9850a2 104 virtual void command(uint8_t c) = 0;
nkhorman 9:ddb97c9850a2 105 virtual void data(uint8_t c) = 0;
nkhorman 11:86909e6db3c8 106 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
bibi94 19:e31b31e3128a 107 //virtual void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
nkhorman 11:86909e6db3c8 108
nkhorman 11:86909e6db3c8 109 /// Clear the display buffer
nkhorman 9:ddb97c9850a2 110 void clearDisplay(void);
nkhorman 9:ddb97c9850a2 111 virtual void invertDisplay(bool i);
nkhorman 11:86909e6db3c8 112
nkhorman 11:86909e6db3c8 113 /// Cause the display to be updated with the buffer content.
nkhorman 9:ddb97c9850a2 114 void display();
bibi94 19:e31b31e3128a 115 /// Scroll
bibi94 19:e31b31e3128a 116 virtual void startscrollright(uint8_t start, uint8_t stop);
bibi94 19:e31b31e3128a 117 virtual void startscrollleft(uint8_t start, uint8_t stop);
bibi94 19:e31b31e3128a 118 virtual void startscrolldiagright(uint8_t start, uint8_t stop);
bibi94 19:e31b31e3128a 119 virtual void startscrolldiagleft(uint8_t start, uint8_t stop);
bibi94 19:e31b31e3128a 120 void stopscroll(void);
nkhorman 11:86909e6db3c8 121 /// Fill the buffer with the AdaFruit splash screen.
nkhorman 9:ddb97c9850a2 122 virtual void splash();
nkhorman 9:ddb97c9850a2 123
nkhorman 9:ddb97c9850a2 124 protected:
nkhorman 9:ddb97c9850a2 125 virtual void sendDisplayBuffer() = 0;
nkhorman 9:ddb97c9850a2 126 DigitalOut2 rst;
nkhorman 9:ddb97c9850a2 127
nkhorman 9:ddb97c9850a2 128 // the memory buffer for the LCD
nkhorman 9:ddb97c9850a2 129 std::vector<uint8_t> buffer;
nkhorman 9:ddb97c9850a2 130 };
nkhorman 9:ddb97c9850a2 131
nkhorman 11:86909e6db3c8 132
nkhorman 11:86909e6db3c8 133 /** This is the SPI SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 134 *
nkhorman 11:86909e6db3c8 135 */
nkhorman 9:ddb97c9850a2 136 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 137 {
nkhorman 9:ddb97c9850a2 138 public:
nkhorman 11:86909e6db3c8 139 /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
nkhorman 11:86909e6db3c8 140 *
nkhorman 11:86909e6db3c8 141 * Required parameters
nkhorman 11:86909e6db3c8 142 * @param spi - a reference to an initialized SPI object
nkhorman 11:86909e6db3c8 143 * @param DC (Data/Command) pin name
nkhorman 11:86909e6db3c8 144 * @param RST (Reset) pin name
nkhorman 11:86909e6db3c8 145 * @param CS (Chip Select) pin name
nkhorman 11:86909e6db3c8 146 *
nkhorman 11:86909e6db3c8 147 * Optional parameters
nkhorman 11:86909e6db3c8 148 * @param rawHeight - the vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 149 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 150 */
nkhorman 9:ddb97c9850a2 151 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 152 : Adafruit_SSD1306(RST, rawHieght, rawWidth)
nkhorman 9:ddb97c9850a2 153 , cs(CS,true)
nkhorman 9:ddb97c9850a2 154 , dc(DC,false)
nkhorman 9:ddb97c9850a2 155 , mspi(spi)
nkhorman 9:ddb97c9850a2 156 {
nkhorman 9:ddb97c9850a2 157 begin();
nkhorman 9:ddb97c9850a2 158 splash();
nkhorman 9:ddb97c9850a2 159 display();
nkhorman 9:ddb97c9850a2 160 };
nkhorman 9:ddb97c9850a2 161
nkhorman 9:ddb97c9850a2 162 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 163 {
nkhorman 9:ddb97c9850a2 164 cs = 1;
nkhorman 9:ddb97c9850a2 165 dc = 0;
nkhorman 9:ddb97c9850a2 166 cs = 0;
nkhorman 9:ddb97c9850a2 167 mspi.write(c);
nkhorman 9:ddb97c9850a2 168 cs = 1;
nkhorman 9:ddb97c9850a2 169 };
nkhorman 9:ddb97c9850a2 170
nkhorman 9:ddb97c9850a2 171 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 172 {
nkhorman 9:ddb97c9850a2 173 cs = 1;
nkhorman 9:ddb97c9850a2 174 dc = 1;
nkhorman 9:ddb97c9850a2 175 cs = 0;
nkhorman 9:ddb97c9850a2 176 mspi.write(c);
nkhorman 9:ddb97c9850a2 177 cs = 1;
nkhorman 9:ddb97c9850a2 178 };
nkhorman 9:ddb97c9850a2 179
nkhorman 9:ddb97c9850a2 180 protected:
nkhorman 9:ddb97c9850a2 181 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 182 {
nkhorman 9:ddb97c9850a2 183 cs = 1;
nkhorman 9:ddb97c9850a2 184 dc = 1;
nkhorman 9:ddb97c9850a2 185 cs = 0;
nkhorman 9:ddb97c9850a2 186
nkhorman 9:ddb97c9850a2 187 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 9:ddb97c9850a2 188 mspi.write(buffer[i]);
nkhorman 9:ddb97c9850a2 189
nkhorman 11:86909e6db3c8 190 if(height() == 32)
nkhorman 11:86909e6db3c8 191 {
nkhorman 11:86909e6db3c8 192 for(uint16_t i=0, q=buffer.size(); i<q; i++)
nkhorman 11:86909e6db3c8 193 mspi.write(0);
nkhorman 11:86909e6db3c8 194 }
nkhorman 11:86909e6db3c8 195
nkhorman 9:ddb97c9850a2 196 cs = 1;
nkhorman 9:ddb97c9850a2 197 };
nkhorman 9:ddb97c9850a2 198
nkhorman 9:ddb97c9850a2 199 DigitalOut2 cs, dc;
nkhorman 9:ddb97c9850a2 200 SPI &mspi;
nkhorman 9:ddb97c9850a2 201 };
nkhorman 9:ddb97c9850a2 202
nkhorman 11:86909e6db3c8 203 /** This is the I2C SSD1306 display driver transport class
nkhorman 11:86909e6db3c8 204 *
nkhorman 11:86909e6db3c8 205 */
nkhorman 9:ddb97c9850a2 206 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
nkhorman 9:ddb97c9850a2 207 {
nkhorman 9:ddb97c9850a2 208 public:
bibi94 19:e31b31e3128a 209 #define SSD_I2C_ADDRESS 0x78//0x78
nkhorman 11:86909e6db3c8 210 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
nkhorman 11:86909e6db3c8 211 *
nkhorman 11:86909e6db3c8 212 * Required parameters
nkhorman 11:86909e6db3c8 213 * @param i2c - A reference to an initialized I2C object
nkhorman 11:86909e6db3c8 214 * @param RST - The Reset pin name
nkhorman 11:86909e6db3c8 215 *
nkhorman 11:86909e6db3c8 216 * Optional parameters
nkhorman 11:86909e6db3c8 217 * @param i2cAddress - The i2c address of the display
nkhorman 11:86909e6db3c8 218 * @param rawHeight - The vertical number of pixels for the display, defaults to 32
nkhorman 11:86909e6db3c8 219 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
nkhorman 11:86909e6db3c8 220 */
nkhorman 9:ddb97c9850a2 221 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
nkhorman 9:ddb97c9850a2 222 : Adafruit_SSD1306(RST, rawHeight, rawWidth)
nkhorman 9:ddb97c9850a2 223 , mi2c(i2c)
nkhorman 9:ddb97c9850a2 224 , mi2cAddress(i2cAddress)
nkhorman 9:ddb97c9850a2 225 {
nkhorman 9:ddb97c9850a2 226 begin();
nkhorman 9:ddb97c9850a2 227 splash();
nkhorman 9:ddb97c9850a2 228 display();
nkhorman 9:ddb97c9850a2 229 };
nkhorman 9:ddb97c9850a2 230
nkhorman 9:ddb97c9850a2 231 virtual void command(uint8_t c)
nkhorman 9:ddb97c9850a2 232 {
nkhorman 9:ddb97c9850a2 233 char buff[2];
nkhorman 9:ddb97c9850a2 234 buff[0] = 0; // Command Mode
nkhorman 9:ddb97c9850a2 235 buff[1] = c;
nkhorman 9:ddb97c9850a2 236 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 237 }
nkhorman 9:ddb97c9850a2 238
nkhorman 9:ddb97c9850a2 239 virtual void data(uint8_t c)
nkhorman 9:ddb97c9850a2 240 {
nkhorman 9:ddb97c9850a2 241 char buff[2];
nkhorman 9:ddb97c9850a2 242 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 243 buff[1] = c;
nkhorman 9:ddb97c9850a2 244 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 245 };
nkhorman 9:ddb97c9850a2 246
nkhorman 9:ddb97c9850a2 247 protected:
nkhorman 9:ddb97c9850a2 248 virtual void sendDisplayBuffer()
nkhorman 9:ddb97c9850a2 249 {
nkhorman 9:ddb97c9850a2 250 char buff[17];
nkhorman 9:ddb97c9850a2 251 buff[0] = 0x40; // Data Mode
nkhorman 9:ddb97c9850a2 252
nkhorman 9:ddb97c9850a2 253 // send display buffer in 16 byte chunks
nkhorman 9:ddb97c9850a2 254 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 )
nkhorman 9:ddb97c9850a2 255 { uint8_t x ;
nkhorman 9:ddb97c9850a2 256
nkhorman 9:ddb97c9850a2 257 // TODO - this will segfault if buffer.size() % 16 != 0
nkhorman 9:ddb97c9850a2 258 for(x=1; x<sizeof(buff); x++)
JojoS 15:77feec1c0684 259 buff[x] = buffer[i+x-1];
nkhorman 9:ddb97c9850a2 260 mi2c.write(mi2cAddress, buff, sizeof(buff));
nkhorman 9:ddb97c9850a2 261 }
nkhorman 9:ddb97c9850a2 262 };
nkhorman 9:ddb97c9850a2 263
nkhorman 9:ddb97c9850a2 264 I2C &mi2c;
nkhorman 9:ddb97c9850a2 265 uint8_t mi2cAddress;
nkhorman 0:c3dcd4c4983a 266 };
nkhorman 0:c3dcd4c4983a 267
nkhorman 0:c3dcd4c4983a 268 #endif