Adafruit SSD1306
Adafruit_SSD1306.h
Go to the documentation of this file.
1 
24 #ifndef _Adafruit_SSD1306_H_
25 #define _Adafruit_SSD1306_H_
26 
27 // ONE of the following three lines must be #defined:
28 // #define SSD1306_128_64 ///< DEPRECTAED: old way to specify 128x64 screen
29 #define SSD1306_128_32
30 // #define SSD1306_96_16
31 // This establishes the screen dimensions in old Adafruit_SSD1306 sketches
32 // (NEW CODE SHOULD IGNORE THIS, USE THE CONSTRUCTORS THAT ACCEPT WIDTH
33 // AND HEIGHT ARGUMENTS).
34 
35 // Uncomment to disable Adafruit splash logo
36 // #define SSD1306_NO_SPLASH
37 
38 #if defined(ARDUINO_STM32_FEATHER)
39 typedef class HardwareSPI SPIClass;
40 #endif
41 
42 #include <Adafruit_GFX.h>
43 #include <SPI.h>
44 #include <Wire.h>
45 
46 #if defined(__AVR__)
47 typedef volatile uint8_t PortReg;
48 typedef uint8_t PortMask;
49 #define HAVE_PORTREG
50 #elif defined(__SAM3X8E__)
51 typedef volatile RwReg PortReg;
52 typedef uint32_t PortMask;
53 #define HAVE_PORTREG
54 #elif defined(ARDUINO_ARCH_RTTHREAD)
55 #undef HAVE_PORTREG
56 #elif defined(ARDUINO_ARCH_ZEPHYR)
57 #undef HAVE_PORTREG
58 #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
59  !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
60 typedef volatile uint32_t PortReg;
61 typedef uint32_t PortMask;
62 #define HAVE_PORTREG
63 #endif
64 
69 #ifndef NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY
70 #define BLACK SSD1306_BLACK
71 #define WHITE SSD1306_WHITE
72 #define INVERSE SSD1306_INVERSE
73 #endif
74 #define SSD1306_BLACK 0
76 #define SSD1306_WHITE 1
77 #define SSD1306_INVERSE 2
78 
79 #define SSD1306_MEMORYMODE 0x20
80 #define SSD1306_COLUMNADDR 0x21
81 #define SSD1306_PAGEADDR 0x22
82 #define SSD1306_SETCONTRAST 0x81
83 #define SSD1306_CHARGEPUMP 0x8D
84 #define SSD1306_SEGREMAP 0xA0
85 #define SSD1306_DISPLAYALLON_RESUME 0xA4
86 #define SSD1306_DISPLAYALLON 0xA5
87 #define SSD1306_NORMALDISPLAY 0xA6
88 #define SSD1306_INVERTDISPLAY 0xA7
89 #define SSD1306_SETMULTIPLEX 0xA8
90 #define SSD1306_DISPLAYOFF 0xAE
91 #define SSD1306_DISPLAYON 0xAF
92 #define SSD1306_COMSCANINC 0xC0
93 #define SSD1306_COMSCANDEC 0xC8
94 #define SSD1306_SETDISPLAYOFFSET 0xD3
95 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
96 #define SSD1306_SETPRECHARGE 0xD9
97 #define SSD1306_SETCOMPINS 0xDA
98 #define SSD1306_SETVCOMDETECT 0xDB
99 
100 #define SSD1306_SETLOWCOLUMN 0x00
101 #define SSD1306_SETHIGHCOLUMN 0x10
102 #define SSD1306_SETSTARTLINE 0x40
103 
104 #define SSD1306_EXTERNALVCC 0x01
105 #define SSD1306_SWITCHCAPVCC 0x02
106 
107 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
108 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
109 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
110 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
111 #define SSD1306_DEACTIVATE_SCROLL 0x2E
112 #define SSD1306_ACTIVATE_SCROLL 0x2F
113 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
114 
115 // Deprecated size stuff for backwards compatibility with old sketches
116 #if defined SSD1306_128_64
117 #define SSD1306_LCDWIDTH 128
118 #define SSD1306_LCDHEIGHT 64
119 #endif
120 #if defined SSD1306_128_32
121 #define SSD1306_LCDWIDTH 128
122 #define SSD1306_LCDHEIGHT 32
123 #endif
124 #if defined SSD1306_96_16
125 #define SSD1306_LCDWIDTH 96
126 #define SSD1306_LCDHEIGHT 16
127 #endif
128 
133 class Adafruit_SSD1306 : public Adafruit_GFX {
134 public:
135  // NEW CONSTRUCTORS -- recommended for new projects
136  Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
137  int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
138  uint32_t clkAfter = 100000UL);
139  Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
140  int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
141  Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
142  int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
143 
144  // DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
145  Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
146  int8_t rst_pin, int8_t cs_pin);
147  Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
148  Adafruit_SSD1306(int8_t rst_pin = -1);
149 
150  ~Adafruit_SSD1306(void);
151 
152  bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
153  bool reset = true, bool periphBegin = true);
154  void display(void);
155  void clearDisplay(void);
156  void invertDisplay(bool i);
157  void dim(bool dim);
158  void drawPixel(int16_t x, int16_t y, uint16_t color);
159  virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
160  virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
161  void startscrollright(uint8_t start, uint8_t stop);
162  void startscrollleft(uint8_t start, uint8_t stop);
163  void startscrolldiagright(uint8_t start, uint8_t stop);
164  void startscrolldiagleft(uint8_t start, uint8_t stop);
165  void stopscroll(void);
166  void ssd1306_command(uint8_t c);
167  bool getPixel(int16_t x, int16_t y);
168  uint8_t *getBuffer(void);
169 
170 protected:
171  inline void SPIwrite(uint8_t d) __attribute__((always_inline));
172  void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color);
173  void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color);
174  void ssd1306_command1(uint8_t c);
175  void ssd1306_commandList(const uint8_t *c, uint8_t n);
176 
177  SPIClass *spi;
178  TwoWire *wire;
180  uint8_t *buffer;
182  int8_t i2caddr;
184  int8_t vccstate;
185  int8_t page_end;
186  int8_t mosiPin;
187  int8_t clkPin;
189  int8_t dcPin;
190  int8_t
192  int8_t rstPin;
193 
194 #ifdef HAVE_PORTREG
195  PortReg *mosiPort, *clkPort, *dcPort, *csPort;
196  PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask;
197 #endif
198 #if ARDUINO >= 157
199  uint32_t wireClk;
200  uint32_t restoreClk;
201 #endif
202  uint8_t contrast;
203 #if defined(SPI_HAS_TRANSACTION)
204 protected:
205  // Allow sub-class to change
206  SPISettings spiSettings;
207 #endif
208 };
209 
210 #endif // _Adafruit_SSD1306_H_
int8_t dcPin
(Data Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:189
void startscrolldiagright(uint8_t start, uint8_t stop)
Activate a diagonal scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1122
#define SSD1306_SWITCHCAPVCC
Gen. display voltage from 3.3V.
Definition: Adafruit_SSD1306.h:105
void display(void)
Push data currently in RAM to SSD1306 display.
Definition: Adafruit_SSD1306.cpp:1011
~Adafruit_SSD1306(void)
Destructor for Adafruit_SSD1306 object.
Definition: Adafruit_SSD1306.cpp:343
void drawPixel(int16_t x, int16_t y, uint16_t color)
Set/clear/invert a single pixel. This is also invoked by the Adafruit_GFX library in generating many ...
Definition: Adafruit_SSD1306.cpp:660
void ssd1306_commandList(const uint8_t *c, uint8_t n)
Issue list of commands to SSD1306, same rules as above re: transactions. This is a protected function...
Definition: Adafruit_SSD1306.cpp:421
void stopscroll(void)
Cease a previously-begun scrolling action.
Definition: Adafruit_SSD1306.cpp:1169
int8_t mosiPin
Definition: Adafruit_SSD1306.h:186
virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
Draw a horizontal line. This is also invoked by the Adafruit_GFX library in generating many higher-le...
Definition: Adafruit_SSD1306.cpp:718
void SPIwrite(uint8_t d) __attribute__((always_inline))
Write a single byte to the SPI port.
Definition: Adafruit_SSD1306.cpp:364
int8_t csPin
(Chip Select Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:191
void clearDisplay(void)
Clear contents of display buffer (set all pixels to off).
Definition: Adafruit_SSD1306.cpp:698
void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color)
Draw a horizontal line with a width and color. Used by public methods drawFastHLine,drawFastVLine.
Definition: Adafruit_SSD1306.cpp:767
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi=&Wire, int8_t rst_pin=-1, uint32_t clkDuring=400000UL, uint32_t clkAfter=100000UL)
Constructor for I2C-interfaced SSD1306 displays.
Definition: Adafruit_SSD1306.cpp:176
void ssd1306_command1(uint8_t c)
Issue single command to SSD1306, using I2C or hard/soft SPI as needed. Because command calls are ofte...
Definition: Adafruit_SSD1306.cpp:397
TwoWire * wire
Definition: Adafruit_SSD1306.h:179
SPIClass * spi
Definition: Adafruit_SSD1306.h:177
uint8_t * getBuffer(void)
Get base address of display buffer for direct reading or writing.
Definition: Adafruit_SSD1306.cpp:1000
void dim(bool dim)
Dim the display.
Definition: Adafruit_SSD1306.cpp:1204
int8_t clkPin
(Clock Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:188
virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
Draw a vertical line. This is also invoked by the Adafruit_GFX library in generating many higher-leve...
Definition: Adafruit_SSD1306.cpp:818
int8_t vccstate
VCC selection, set by begin method.
Definition: Adafruit_SSD1306.h:184
void ssd1306_command(uint8_t c)
Issue a single low-level command directly to the SSD1306 display, bypassing the library.
Definition: Adafruit_SSD1306.cpp:454
int8_t page_end
not used
Definition: Adafruit_SSD1306.h:185
bool getPixel(int16_t x, int16_t y)
Return color of a single pixel in display buffer.
Definition: Adafruit_SSD1306.cpp:973
uint8_t contrast
normal contrast setting for this device
Definition: Adafruit_SSD1306.h:202
void startscrolldiagleft(uint8_t start, uint8_t stop)
Activate alternate diagonal scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1148
int8_t rstPin
Display reset pin assignment. Set during construction.
Definition: Adafruit_SSD1306.h:192
void startscrollright(uint8_t start, uint8_t stop)
Activate a right-handed scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1076
void invertDisplay(bool i)
Enable or disable display invert mode (white-on-black vs black-on-white).
Definition: Adafruit_SSD1306.cpp:1190
int8_t i2caddr
I2C address initialized when begin method is called.
Definition: Adafruit_SSD1306.h:183
void startscrollleft(uint8_t start, uint8_t stop)
Activate a left-handed scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1099
uint8_t * buffer
Definition: Adafruit_SSD1306.h:181
Class that stores state and functions for interacting with SSD1306 OLED displays. ...
Definition: Adafruit_SSD1306.h:133
bool begin(uint8_t switchvcc=SSD1306_SWITCHCAPVCC, uint8_t i2caddr=0, bool reset=true, bool periphBegin=true)
Allocate RAM for image buffer, initialize peripherals and pins.
Definition: Adafruit_SSD1306.cpp:497
void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color)
Draw a vertical line with a width and color. Used by public method drawFastHLine,drawFastVLine.
Definition: Adafruit_SSD1306.cpp:866