|
| 1 | +/********************************************************************* |
| 2 | +This is a library for our Monochrome OLEDs based on SSD1306 drivers |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | + ------> http://www.adafruit.com/category/63_98 |
| 6 | +
|
| 7 | +These displays use SPI to communicate, 4 or 5 pins are required to |
| 8 | +interface |
| 9 | +
|
| 10 | +Adafruit invests time and resources providing this open source code, |
| 11 | +please support Adafruit and open-source hardware by purchasing |
| 12 | +products from Adafruit! |
| 13 | +
|
| 14 | +Written by Limor Fried/Ladyada for Adafruit Industries. |
| 15 | +BSD license, check license.txt for more information |
| 16 | +All text above, and the splash screen must be included in any redistribution |
| 17 | +*********************************************************************/ |
| 18 | +#ifndef _Adafruit_SSD1306_H_ |
| 19 | +#define _Adafruit_SSD1306_H_ |
| 20 | + |
| 21 | +#if ARDUINO >= 100 |
| 22 | + #include "Arduino.h" |
| 23 | + #define WIRE_WRITE Wire.write |
| 24 | +#else |
| 25 | + #include "WProgram.h" |
| 26 | + #define WIRE_WRITE Wire.send |
| 27 | +#endif |
| 28 | + |
| 29 | +#if defined(__SAM3X8E__) |
| 30 | + typedef volatile RwReg PortReg; |
| 31 | + typedef uint32_t PortMask; |
| 32 | + #define HAVE_PORTREG |
| 33 | +#elif defined(ARDUINO_ARCH_SAMD) |
| 34 | +// not supported |
| 35 | +#elif defined(ESP8266) || defined(ARDUINO_STM32_FEATHER) |
| 36 | + typedef volatile uint32_t PortReg; |
| 37 | + typedef uint32_t PortMask; |
| 38 | +#else |
| 39 | + typedef volatile uint8_t PortReg; |
| 40 | + typedef uint8_t PortMask; |
| 41 | + #define HAVE_PORTREG |
| 42 | +#endif |
| 43 | + |
| 44 | +#include <SPI.h> |
| 45 | +#include <Adafruit_GFX.h> |
| 46 | + |
| 47 | +#define BLACK 0 |
| 48 | +#define WHITE 1 |
| 49 | +#define INVERSE 2 |
| 50 | + |
| 51 | +#define SSD1306_I2C_ADDRESS 0x3C // 011110+SA0+RW - 0x3C or 0x3D |
| 52 | +// Address for 128x32 is 0x3C |
| 53 | +// Address for 128x64 is 0x3D (default) or 0x3C (if SA0 is grounded) |
| 54 | + |
| 55 | +/*========================================================================= |
| 56 | + SSD1306 Displays |
| 57 | + ----------------------------------------------------------------------- |
| 58 | + The driver is used in multiple displays (128x64, 128x32, etc.). |
| 59 | + Select the appropriate display below to create an appropriately |
| 60 | + sized framebuffer, etc. |
| 61 | +
|
| 62 | + SSD1306_128_64 128x64 pixel display |
| 63 | +
|
| 64 | + SSD1306_128_32 128x32 pixel display |
| 65 | +
|
| 66 | + SSD1306_96_16 |
| 67 | +
|
| 68 | + -----------------------------------------------------------------------*/ |
| 69 | +#define SSD1306_64_48 |
| 70 | +// #define SSD1306_128_64 |
| 71 | +// #define SSD1306_128_32 |
| 72 | +// #define SSD1306_96_16 |
| 73 | +/*=========================================================================*/ |
| 74 | + |
| 75 | +#if defined SSD1306_128_64 && defined SSD1306_128_32 |
| 76 | + #error "Only one SSD1306 display can be specified at once in Adafruit_SSD1306.h" |
| 77 | +#endif |
| 78 | +#if !defined SSD1306_128_64 && !defined SSD1306_128_32 && !defined SSD1306_96_16 && !defined SSD1306_64_48 |
| 79 | + #error "At least one SSD1306 display must be specified in Adafruit_SSD1306.h" |
| 80 | +#endif |
| 81 | + |
| 82 | +#if defined SSD1306_128_64 |
| 83 | + #define SSD1306_LCDWIDTH 128 |
| 84 | + #define SSD1306_LCDHEIGHT 64 |
| 85 | +#elif defined SSD1306_128_32 |
| 86 | + #define SSD1306_LCDWIDTH 128 |
| 87 | + #define SSD1306_LCDHEIGHT 32 |
| 88 | +#elif defined SSD1306_96_16 |
| 89 | + #define SSD1306_LCDWIDTH 96 |
| 90 | + #define SSD1306_LCDHEIGHT 16 |
| 91 | +#elif defined SSD1306_64_48 |
| 92 | + #define SSD1306_LCDWIDTH 64 |
| 93 | + #define SSD1306_LCDHEIGHT 48 |
| 94 | +#endif |
| 95 | + |
| 96 | +#define SSD1306_SETCONTRAST 0x81 |
| 97 | +#define SSD1306_DISPLAYALLON_RESUME 0xA4 |
| 98 | +#define SSD1306_DISPLAYALLON 0xA5 |
| 99 | +#define SSD1306_NORMALDISPLAY 0xA6 |
| 100 | +#define SSD1306_INVERTDISPLAY 0xA7 |
| 101 | +#define SSD1306_DISPLAYOFF 0xAE |
| 102 | +#define SSD1306_DISPLAYON 0xAF |
| 103 | + |
| 104 | +#define SSD1306_SETDISPLAYOFFSET 0xD3 |
| 105 | +#define SSD1306_SETCOMPINS 0xDA |
| 106 | + |
| 107 | +#define SSD1306_SETVCOMDETECT 0xDB |
| 108 | + |
| 109 | +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 |
| 110 | +#define SSD1306_SETPRECHARGE 0xD9 |
| 111 | + |
| 112 | +#define SSD1306_SETMULTIPLEX 0xA8 |
| 113 | + |
| 114 | +#define SSD1306_SETLOWCOLUMN 0x00 |
| 115 | +#define SSD1306_SETHIGHCOLUMN 0x10 |
| 116 | + |
| 117 | +#define SSD1306_SETSTARTLINE 0x40 |
| 118 | + |
| 119 | +#define SSD1306_MEMORYMODE 0x20 |
| 120 | +#define SSD1306_COLUMNADDR 0x21 |
| 121 | +#define SSD1306_PAGEADDR 0x22 |
| 122 | + |
| 123 | +#define SSD1306_COMSCANINC 0xC0 |
| 124 | +#define SSD1306_COMSCANDEC 0xC8 |
| 125 | + |
| 126 | +#define SSD1306_SEGREMAP 0xA0 |
| 127 | + |
| 128 | +#define SSD1306_CHARGEPUMP 0x8D |
| 129 | + |
| 130 | +#define SSD1306_EXTERNALVCC 0x1 |
| 131 | +#define SSD1306_SWITCHCAPVCC 0x2 |
| 132 | + |
| 133 | +// Scrolling #defines |
| 134 | +#define SSD1306_ACTIVATE_SCROLL 0x2F |
| 135 | +#define SSD1306_DEACTIVATE_SCROLL 0x2E |
| 136 | +#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 |
| 137 | +#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 |
| 138 | +#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 |
| 139 | +#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 |
| 140 | +#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A |
| 141 | + |
| 142 | +class Adafruit_SSD1306 : public Adafruit_GFX { |
| 143 | + public: |
| 144 | + Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS); |
| 145 | + Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS); |
| 146 | + Adafruit_SSD1306(int8_t RST = -1); |
| 147 | + |
| 148 | + void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS, bool reset=true); |
| 149 | + void ssd1306_command(uint8_t c); |
| 150 | + |
| 151 | + void clearDisplay(void); |
| 152 | + void invertDisplay(uint8_t i); |
| 153 | + void display(); |
| 154 | + |
| 155 | + void startscrollright(uint8_t start, uint8_t stop); |
| 156 | + void startscrollleft(uint8_t start, uint8_t stop); |
| 157 | + |
| 158 | + void startscrolldiagright(uint8_t start, uint8_t stop); |
| 159 | + void startscrolldiagleft(uint8_t start, uint8_t stop); |
| 160 | + void stopscroll(void); |
| 161 | + |
| 162 | + void dim(boolean dim); |
| 163 | + |
| 164 | + void drawPixel(int16_t x, int16_t y, uint16_t color); |
| 165 | + |
| 166 | + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); |
| 167 | + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); |
| 168 | + |
| 169 | + private: |
| 170 | + int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs; |
| 171 | + void fastSPIwrite(uint8_t c); |
| 172 | + |
| 173 | + boolean hwSPI; |
| 174 | +#ifdef HAVE_PORTREG |
| 175 | + PortReg *mosiport, *clkport, *csport, *dcport; |
| 176 | + PortMask mosipinmask, clkpinmask, cspinmask, dcpinmask; |
| 177 | +#endif |
| 178 | + |
| 179 | + inline void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color) __attribute__((always_inline)); |
| 180 | + inline void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color) __attribute__((always_inline)); |
| 181 | + |
| 182 | +}; |
| 183 | + |
| 184 | +#endif /* _Adafruit_SSD1306_H_ */ |
0 commit comments