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(__arm__) || defined(ARDUINO_FEATHER52)) && \
55  !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
56 typedef volatile uint32_t PortReg;
57 typedef uint32_t PortMask;
58 #define HAVE_PORTREG
59 #endif
60 
65 #ifndef NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY
66 #define BLACK SSD1306_BLACK
67 #define WHITE SSD1306_WHITE
68 #define INVERSE SSD1306_INVERSE
69 #endif
70 #define SSD1306_BLACK 0
72 #define SSD1306_WHITE 1
73 #define SSD1306_INVERSE 2
74 
75 #define SSD1306_MEMORYMODE 0x20
76 #define SSD1306_COLUMNADDR 0x21
77 #define SSD1306_PAGEADDR 0x22
78 #define SSD1306_SETCONTRAST 0x81
79 #define SSD1306_CHARGEPUMP 0x8D
80 #define SSD1306_SEGREMAP 0xA0
81 #define SSD1306_DISPLAYALLON_RESUME 0xA4
82 #define SSD1306_DISPLAYALLON 0xA5
83 #define SSD1306_NORMALDISPLAY 0xA6
84 #define SSD1306_INVERTDISPLAY 0xA7
85 #define SSD1306_SETMULTIPLEX 0xA8
86 #define SSD1306_DISPLAYOFF 0xAE
87 #define SSD1306_DISPLAYON 0xAF
88 #define SSD1306_COMSCANINC 0xC0
89 #define SSD1306_COMSCANDEC 0xC8
90 #define SSD1306_SETDISPLAYOFFSET 0xD3
91 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
92 #define SSD1306_SETPRECHARGE 0xD9
93 #define SSD1306_SETCOMPINS 0xDA
94 #define SSD1306_SETVCOMDETECT 0xDB
95 
96 #define SSD1306_SETLOWCOLUMN 0x00
97 #define SSD1306_SETHIGHCOLUMN 0x10
98 #define SSD1306_SETSTARTLINE 0x40
99 
100 #define SSD1306_EXTERNALVCC 0x01
101 #define SSD1306_SWITCHCAPVCC 0x02
102 
103 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
104 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
105 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
106 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
107 #define SSD1306_DEACTIVATE_SCROLL 0x2E
108 #define SSD1306_ACTIVATE_SCROLL 0x2F
109 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
110 
111 // Deprecated size stuff for backwards compatibility with old sketches
112 #if defined SSD1306_128_64
113 #define SSD1306_LCDWIDTH 128
114 #define SSD1306_LCDHEIGHT 64
115 #endif
116 #if defined SSD1306_128_32
117 #define SSD1306_LCDWIDTH 128
118 #define SSD1306_LCDHEIGHT 32
119 #endif
120 #if defined SSD1306_96_16
121 #define SSD1306_LCDWIDTH 96
122 #define SSD1306_LCDHEIGHT 16
123 #endif
124 
129 class Adafruit_SSD1306 : public Adafruit_GFX {
130 public:
131  // NEW CONSTRUCTORS -- recommended for new projects
132  Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
133  int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
134  uint32_t clkAfter = 100000UL);
135  Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
136  int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
137  Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
138  int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
139 
140  // DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
141  Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
142  int8_t rst_pin, int8_t cs_pin);
143  Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
144  Adafruit_SSD1306(int8_t rst_pin = -1);
145 
146  ~Adafruit_SSD1306(void);
147 
148  bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
149  bool reset = true, bool periphBegin = true);
150  void display(void);
151  void clearDisplay(void);
152  void invertDisplay(bool i);
153  void dim(bool dim);
154  void drawPixel(int16_t x, int16_t y, uint16_t color);
155  virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
156  virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
157  void startscrollright(uint8_t start, uint8_t stop);
158  void startscrollleft(uint8_t start, uint8_t stop);
159  void startscrolldiagright(uint8_t start, uint8_t stop);
160  void startscrolldiagleft(uint8_t start, uint8_t stop);
161  void stopscroll(void);
162  void ssd1306_command(uint8_t c);
163  bool getPixel(int16_t x, int16_t y);
164  uint8_t *getBuffer(void);
165 
166 protected:
167  inline void SPIwrite(uint8_t d) __attribute__((always_inline));
168  void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color);
169  void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color);
170  void ssd1306_command1(uint8_t c);
171  void ssd1306_commandList(const uint8_t *c, uint8_t n);
172 
173  SPIClass *spi;
174  TwoWire *wire;
176  uint8_t *buffer;
178  int8_t i2caddr;
180  int8_t vccstate;
181  int8_t page_end;
182  int8_t mosiPin;
183  int8_t clkPin;
185  int8_t dcPin;
186  int8_t
188  int8_t rstPin;
189 
190 #ifdef HAVE_PORTREG
191  PortReg *mosiPort, *clkPort, *dcPort, *csPort;
192  PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask;
193 #endif
194 #if ARDUINO >= 157
195  uint32_t wireClk;
196  uint32_t restoreClk;
197 #endif
198  uint8_t contrast;
199 #if defined(SPI_HAS_TRANSACTION)
200 protected:
201  // Allow sub-class to change
202  SPISettings spiSettings;
203 #endif
204 };
205 
206 #endif // _Adafruit_SSD1306_H_
int8_t dcPin
(Data Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:185
void startscrolldiagright(uint8_t start, uint8_t stop)
Activate a diagonal scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1096
#define SSD1306_SWITCHCAPVCC
Gen. display voltage from 3.3V.
Definition: Adafruit_SSD1306.h:101
void display(void)
Push data currently in RAM to SSD1306 display.
Definition: Adafruit_SSD1306.cpp:992
~Adafruit_SSD1306(void)
Destructor for Adafruit_SSD1306 object.
Definition: Adafruit_SSD1306.cpp:338
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:641
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:416
void stopscroll(void)
Cease a previously-begun scrolling action.
Definition: Adafruit_SSD1306.cpp:1143
int8_t mosiPin
Definition: Adafruit_SSD1306.h:182
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:699
void SPIwrite(uint8_t d) __attribute__((always_inline))
Write a single byte to the SPI port.
Definition: Adafruit_SSD1306.cpp:359
int8_t csPin
(Chip Select Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:187
void clearDisplay(void)
Clear contents of display buffer (set all pixels to off).
Definition: Adafruit_SSD1306.cpp:679
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:748
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:172
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:392
TwoWire * wire
Definition: Adafruit_SSD1306.h:175
SPIClass * spi
Definition: Adafruit_SSD1306.h:173
uint8_t * getBuffer(void)
Get base address of display buffer for direct reading or writing.
Definition: Adafruit_SSD1306.cpp:981
void dim(bool dim)
Dim the display.
Definition: Adafruit_SSD1306.cpp:1178
int8_t clkPin
(Clock Pin) set when using SPI set during construction.
Definition: Adafruit_SSD1306.h:184
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:799
int8_t vccstate
VCC selection, set by begin method.
Definition: Adafruit_SSD1306.h:180
void ssd1306_command(uint8_t c)
Issue a single low-level command directly to the SSD1306 display, bypassing the library.
Definition: Adafruit_SSD1306.cpp:449
int8_t page_end
not used
Definition: Adafruit_SSD1306.h:181
bool getPixel(int16_t x, int16_t y)
Return color of a single pixel in display buffer.
Definition: Adafruit_SSD1306.cpp:954
uint8_t contrast
normal contrast setting for this device
Definition: Adafruit_SSD1306.h:198
void startscrolldiagleft(uint8_t start, uint8_t stop)
Activate alternate diagonal scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1122
int8_t rstPin
Display reset pin assignment. Set during construction.
Definition: Adafruit_SSD1306.h:188
void startscrollright(uint8_t start, uint8_t stop)
Activate a right-handed scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1050
void invertDisplay(bool i)
Enable or disable display invert mode (white-on-black vs black-on-white).
Definition: Adafruit_SSD1306.cpp:1164
int8_t i2caddr
I2C address initialized when begin method is called.
Definition: Adafruit_SSD1306.h:179
void startscrollleft(uint8_t start, uint8_t stop)
Activate a left-handed scroll for all or part of the display.
Definition: Adafruit_SSD1306.cpp:1073
uint8_t * buffer
Definition: Adafruit_SSD1306.h:177
Class that stores state and functions for interacting with SSD1306 OLED displays. ...
Definition: Adafruit_SSD1306.h:129
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:492
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:847