Adafruit_SSD1306.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*!
  2. * @file Adafruit_SSD1306.h
  3. *
  4. * This is part of for Adafruit's SSD1306 library for monochrome
  5. * OLED displays: http://www.adafruit.com/category/63_98
  6. *
  7. * These displays use I2C or SPI to communicate. I2C requires 2 pins
  8. * (SCL+SDA) and optionally a RESET pin. SPI requires 4 pins (MOSI, SCK,
  9. * select, data/command) and optionally a reset pin. Hardware SPI or
  10. * 'bitbang' software SPI are both supported.
  11. *
  12. * Adafruit invests time and resources providing this open source code,
  13. * please support Adafruit and open-source hardware by purchasing
  14. * products from Adafruit!
  15. *
  16. * Written by Limor Fried/Ladyada for Adafruit Industries, with
  17. * contributions from the open source community.
  18. *
  19. * BSD license, all text above, and the splash screen header file,
  20. * must be included in any redistribution.
  21. *
  22. */
  23. #ifndef _Adafruit_SSD1306_H_
  24. #define _Adafruit_SSD1306_H_
  25. // ONE of the following three lines must be #defined:
  26. // #define SSD1306_128_64 ///< DEPRECTAED: old way to specify 128x64 screen
  27. #define SSD1306_128_32 ///< DEPRECATED: old way to specify 128x32 screen
  28. // #define SSD1306_96_16 ///< DEPRECATED: old way to specify 96x16 screen
  29. // This establishes the screen dimensions in old Adafruit_SSD1306 sketches
  30. // (NEW CODE SHOULD IGNORE THIS, USE THE CONSTRUCTORS THAT ACCEPT WIDTH
  31. // AND HEIGHT ARGUMENTS).
  32. // Uncomment to disable Adafruit splash logo
  33. // #define SSD1306_NO_SPLASH
  34. #if defined(ARDUINO_STM32_FEATHER)
  35. typedef class HardwareSPI SPIClass;
  36. #endif
  37. #include <Adafruit_GFX.h>
  38. #include <SPI.h>
  39. #include <Wire.h>
  40. #if defined(__AVR__)
  41. typedef volatile uint8_t PortReg;
  42. typedef uint8_t PortMask;
  43. #define HAVE_PORTREG
  44. #elif defined(__SAM3X8E__)
  45. typedef volatile RwReg PortReg;
  46. typedef uint32_t PortMask;
  47. #define HAVE_PORTREG
  48. #elif defined(ARDUINO_ARCH_RTTHREAD)
  49. #undef HAVE_PORTREG
  50. #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
  51. !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
  52. typedef volatile uint32_t PortReg;
  53. typedef uint32_t PortMask;
  54. #define HAVE_PORTREG
  55. #endif
  56. /// The following "raw" color names are kept for backwards client compatability
  57. /// They can be disabled by predefining this macro before including the Adafruit
  58. /// header client code will then need to be modified to use the scoped enum
  59. /// values directly
  60. #ifndef NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY
  61. #define BLACK SSD1306_BLACK ///< Draw 'off' pixels
  62. #define WHITE SSD1306_WHITE ///< Draw 'on' pixels
  63. #define INVERSE SSD1306_INVERSE ///< Invert pixels
  64. #endif
  65. /// fit into the SSD1306_ naming scheme
  66. #define SSD1306_BLACK 0 ///< Draw 'off' pixels
  67. #define SSD1306_WHITE 1 ///< Draw 'on' pixels
  68. #define SSD1306_INVERSE 2 ///< Invert pixels
  69. #define SSD1306_MEMORYMODE 0x20 ///< See datasheet
  70. #define SSD1306_COLUMNADDR 0x21 ///< See datasheet
  71. #define SSD1306_PAGEADDR 0x22 ///< See datasheet
  72. #define SSD1306_SETCONTRAST 0x81 ///< See datasheet
  73. #define SSD1306_CHARGEPUMP 0x8D ///< See datasheet
  74. #define SSD1306_SEGREMAP 0xA0 ///< See datasheet
  75. #define SSD1306_DISPLAYALLON_RESUME 0xA4 ///< See datasheet
  76. #define SSD1306_DISPLAYALLON 0xA5 ///< Not currently used
  77. #define SSD1306_NORMALDISPLAY 0xA6 ///< See datasheet
  78. #define SSD1306_INVERTDISPLAY 0xA7 ///< See datasheet
  79. #define SSD1306_SETMULTIPLEX 0xA8 ///< See datasheet
  80. #define SSD1306_DISPLAYOFF 0xAE ///< See datasheet
  81. #define SSD1306_DISPLAYON 0xAF ///< See datasheet
  82. #define SSD1306_COMSCANINC 0xC0 ///< Not currently used
  83. #define SSD1306_COMSCANDEC 0xC8 ///< See datasheet
  84. #define SSD1306_SETDISPLAYOFFSET 0xD3 ///< See datasheet
  85. #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 ///< See datasheet
  86. #define SSD1306_SETPRECHARGE 0xD9 ///< See datasheet
  87. #define SSD1306_SETCOMPINS 0xDA ///< See datasheet
  88. #define SSD1306_SETVCOMDETECT 0xDB ///< See datasheet
  89. #define SSD1306_SETLOWCOLUMN 0x00 ///< Not currently used
  90. #define SSD1306_SETHIGHCOLUMN 0x10 ///< Not currently used
  91. #define SSD1306_SETSTARTLINE 0x40 ///< See datasheet
  92. #define SSD1306_EXTERNALVCC 0x01 ///< External display voltage source
  93. #define SSD1306_SWITCHCAPVCC 0x02 ///< Gen. display voltage from 3.3V
  94. #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 ///< Init rt scroll
  95. #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 ///< Init left scroll
  96. #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 ///< Init diag scroll
  97. #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A ///< Init diag scroll
  98. #define SSD1306_DEACTIVATE_SCROLL 0x2E ///< Stop scroll
  99. #define SSD1306_ACTIVATE_SCROLL 0x2F ///< Start scroll
  100. #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 ///< Set scroll range
  101. // Deprecated size stuff for backwards compatibility with old sketches
  102. #if defined SSD1306_128_64
  103. #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_64 defined
  104. #define SSD1306_LCDHEIGHT 64 ///< DEPRECATED: height w/SSD1306_128_64 defined
  105. #endif
  106. #if defined SSD1306_128_32
  107. #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_32 defined
  108. #define SSD1306_LCDHEIGHT 32 ///< DEPRECATED: height w/SSD1306_128_32 defined
  109. #endif
  110. #if defined SSD1306_96_16
  111. #define SSD1306_LCDWIDTH 96 ///< DEPRECATED: width w/SSD1306_96_16 defined
  112. #define SSD1306_LCDHEIGHT 16 ///< DEPRECATED: height w/SSD1306_96_16 defined
  113. #endif
  114. /*!
  115. @brief Class that stores state and functions for interacting with
  116. SSD1306 OLED displays.
  117. */
  118. class Adafruit_SSD1306 : public Adafruit_GFX {
  119. public:
  120. // NEW CONSTRUCTORS -- recommended for new projects
  121. Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
  122. int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
  123. uint32_t clkAfter = 100000UL);
  124. Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
  125. int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
  126. Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
  127. int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
  128. // DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
  129. Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
  130. int8_t rst_pin, int8_t cs_pin);
  131. Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
  132. Adafruit_SSD1306(int8_t rst_pin = -1);
  133. ~Adafruit_SSD1306(void);
  134. bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
  135. bool reset = true, bool periphBegin = true);
  136. void display(void);
  137. void clearDisplay(void);
  138. void invertDisplay(bool i);
  139. void dim(bool dim);
  140. void drawPixel(int16_t x, int16_t y, uint16_t color);
  141. virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  142. virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  143. void startscrollright(uint8_t start, uint8_t stop);
  144. void startscrollleft(uint8_t start, uint8_t stop);
  145. void startscrolldiagright(uint8_t start, uint8_t stop);
  146. void startscrolldiagleft(uint8_t start, uint8_t stop);
  147. void stopscroll(void);
  148. void ssd1306_command(uint8_t c);
  149. bool getPixel(int16_t x, int16_t y);
  150. uint8_t *getBuffer(void);
  151. protected:
  152. inline void SPIwrite(uint8_t d) __attribute__((always_inline));
  153. void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color);
  154. void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color);
  155. void ssd1306_command1(uint8_t c);
  156. void ssd1306_commandList(const uint8_t *c, uint8_t n);
  157. SPIClass *spi; ///< Initialized during construction when using SPI. See
  158. ///< SPI.cpp, SPI.h
  159. TwoWire *wire; ///< Initialized during construction when using I2C. See
  160. ///< Wire.cpp, Wire.h
  161. uint8_t *buffer; ///< Buffer data used for display buffer. Allocated when
  162. ///< begin method is called.
  163. int8_t i2caddr; ///< I2C address initialized when begin method is called.
  164. int8_t vccstate; ///< VCC selection, set by begin method.
  165. int8_t page_end; ///< not used
  166. int8_t mosiPin; ///< (Master Out Slave In) set when using SPI set during
  167. ///< construction.
  168. int8_t clkPin; ///< (Clock Pin) set when using SPI set during construction.
  169. int8_t dcPin; ///< (Data Pin) set when using SPI set during construction.
  170. int8_t
  171. csPin; ///< (Chip Select Pin) set when using SPI set during construction.
  172. int8_t rstPin; ///< Display reset pin assignment. Set during construction.
  173. #ifdef HAVE_PORTREG
  174. PortReg *mosiPort, *clkPort, *dcPort, *csPort;
  175. PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask;
  176. #endif
  177. #if ARDUINO >= 157
  178. uint32_t wireClk; ///< Wire speed for SSD1306 transfers
  179. uint32_t restoreClk; ///< Wire speed following SSD1306 transfers
  180. #endif
  181. uint8_t contrast; ///< normal contrast setting for this device
  182. #if defined(SPI_HAS_TRANSACTION)
  183. protected:
  184. // Allow sub-class to change
  185. SPISettings spiSettings;
  186. #endif
  187. };
  188. #endif // _Adafruit_SSD1306_H_