Adafruit_SSD1306.h 5.6 KB

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