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