Adafruit_GrayOLED.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*!
  2. * @file Adafruit_GrayOLED.cpp
  3. *
  4. * This is documentation for Adafruit's generic library for grayscale
  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. */
  17. #if !defined(__AVR_ATtiny85__) // Not for ATtiny, at all
  18. #include "Adafruit_GrayOLED.h"
  19. #include <Adafruit_GFX.h>
  20. // SOME DEFINES AND STATIC VARIABLES USED INTERNALLY -----------------------
  21. #define grayoled_swap(a, b) \
  22. (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) ///< No-temp-var swap operation
  23. // CONSTRUCTORS, DESTRUCTOR ------------------------------------------------
  24. /*!
  25. @brief Constructor for I2C-interfaced OLED displays.
  26. @param bpp Bits per pixel, 1 for monochrome, 4 for 16-gray
  27. @param w
  28. Display width in pixels
  29. @param h
  30. Display height in pixels
  31. @param twi
  32. Pointer to an existing TwoWire instance (e.g. &Wire, the
  33. microcontroller's primary I2C bus).
  34. @param rst_pin
  35. Reset pin (using Arduino pin numbering), or -1 if not used
  36. (some displays might be wired to share the microcontroller's
  37. reset pin).
  38. @param clkDuring
  39. Speed (in Hz) for Wire transmissions in library calls.
  40. Defaults to 400000 (400 KHz), a known 'safe' value for most
  41. microcontrollers, and meets the OLED datasheet spec.
  42. Some systems can operate I2C faster (800 KHz for ESP32, 1 MHz
  43. for many other 32-bit MCUs), and some (perhaps not all)
  44. Many OLED's can work with this -- so it's optionally be specified
  45. here and is not a default behavior. (Ignored if using pre-1.5.7
  46. Arduino software, which operates I2C at a fixed 100 KHz.)
  47. @param clkAfter
  48. Speed (in Hz) for Wire transmissions following library
  49. calls. Defaults to 100000 (100 KHz), the default Arduino Wire
  50. speed. This is done rather than leaving it at the 'during' speed
  51. because other devices on the I2C bus might not be compatible
  52. with the faster rate. (Ignored if using pre-1.5.7 Arduino
  53. software, which operates I2C at a fixed 100 KHz.)
  54. @note Call the object's begin() function before use -- buffer
  55. allocation is performed there!
  56. */
  57. Adafruit_GrayOLED::Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h,
  58. TwoWire *twi, int8_t rst_pin,
  59. uint32_t clkDuring, uint32_t clkAfter)
  60. : Adafruit_GFX(w, h), i2c_preclk(clkDuring), i2c_postclk(clkAfter),
  61. buffer(NULL), dcPin(-1), csPin(-1), rstPin(rst_pin), _bpp(bpp) {
  62. i2c_dev = NULL;
  63. _theWire = twi;
  64. }
  65. /*!
  66. @brief Constructor for SPI GrayOLED displays, using software (bitbang)
  67. SPI.
  68. @param bpp Bits per pixel, 1 for monochrome, 4 for 16-gray
  69. @param w
  70. Display width in pixels
  71. @param h
  72. Display height in pixels
  73. @param mosi_pin
  74. MOSI (master out, slave in) pin (using Arduino pin numbering).
  75. This transfers serial data from microcontroller to display.
  76. @param sclk_pin
  77. SCLK (serial clock) pin (using Arduino pin numbering).
  78. This clocks each bit from MOSI.
  79. @param dc_pin
  80. Data/command pin (using Arduino pin numbering), selects whether
  81. display is receiving commands (low) or data (high).
  82. @param rst_pin
  83. Reset pin (using Arduino pin numbering), or -1 if not used
  84. (some displays might be wired to share the microcontroller's
  85. reset pin).
  86. @param cs_pin
  87. Chip-select pin (using Arduino pin numbering) for sharing the
  88. bus with other devices. Active low.
  89. @note Call the object's begin() function before use -- buffer
  90. allocation is performed there!
  91. */
  92. Adafruit_GrayOLED::Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h,
  93. int8_t mosi_pin, int8_t sclk_pin,
  94. int8_t dc_pin, int8_t rst_pin,
  95. int8_t cs_pin)
  96. : Adafruit_GFX(w, h), dcPin(dc_pin), csPin(cs_pin), rstPin(rst_pin),
  97. _bpp(bpp) {
  98. spi_dev = new Adafruit_SPIDevice(cs_pin, sclk_pin, -1, mosi_pin, 1000000);
  99. }
  100. /*!
  101. @brief Constructor for SPI GrayOLED displays, using native hardware SPI.
  102. @param bpp Bits per pixel, 1 for monochrome, 4 for 16-gray
  103. @param w
  104. Display width in pixels
  105. @param h
  106. Display height in pixels
  107. @param spi
  108. Pointer to an existing SPIClass instance (e.g. &SPI, the
  109. microcontroller's primary SPI bus).
  110. @param dc_pin
  111. Data/command pin (using Arduino pin numbering), selects whether
  112. display is receiving commands (low) or data (high).
  113. @param rst_pin
  114. Reset pin (using Arduino pin numbering), or -1 if not used
  115. (some displays might be wired to share the microcontroller's
  116. reset pin).
  117. @param cs_pin
  118. Chip-select pin (using Arduino pin numbering) for sharing the
  119. bus with other devices. Active low.
  120. @param bitrate
  121. SPI clock rate for transfers to this display. Default if
  122. unspecified is 8000000UL (8 MHz).
  123. @note Call the object's begin() function before use -- buffer
  124. allocation is performed there!
  125. */
  126. Adafruit_GrayOLED::Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h,
  127. SPIClass *spi, int8_t dc_pin,
  128. int8_t rst_pin, int8_t cs_pin,
  129. uint32_t bitrate)
  130. : Adafruit_GFX(w, h), dcPin(dc_pin), csPin(cs_pin), rstPin(rst_pin),
  131. _bpp(bpp) {
  132. spi_dev = new Adafruit_SPIDevice(cs_pin, bitrate, SPI_BITORDER_MSBFIRST,
  133. SPI_MODE0, spi);
  134. }
  135. /*!
  136. @brief Destructor for Adafruit_GrayOLED object.
  137. */
  138. Adafruit_GrayOLED::~Adafruit_GrayOLED(void) {
  139. if (buffer) {
  140. free(buffer);
  141. buffer = NULL;
  142. }
  143. if (spi_dev)
  144. delete spi_dev;
  145. if (i2c_dev)
  146. delete i2c_dev;
  147. }
  148. // LOW-LEVEL UTILS ---------------------------------------------------------
  149. /*!
  150. @brief Issue single command byte to OLED, using I2C or hard/soft SPI as
  151. needed.
  152. @param c The single byte command
  153. */
  154. void Adafruit_GrayOLED::oled_command(uint8_t c) {
  155. if (i2c_dev) { // I2C
  156. uint8_t buf[2] = {0x00, c}; // Co = 0, D/C = 0
  157. i2c_dev->write(buf, 2);
  158. } else { // SPI (hw or soft) -- transaction started in calling function
  159. digitalWrite(dcPin, LOW);
  160. spi_dev->write(&c, 1);
  161. }
  162. }
  163. // Issue list of commands to GrayOLED
  164. /*!
  165. @brief Issue multiple bytes of commands OLED, using I2C or hard/soft SPI as
  166. needed.
  167. @param c Pointer to the command array
  168. @param n The number of bytes in the command array
  169. @returns True for success on ability to write the data in I2C.
  170. */
  171. bool Adafruit_GrayOLED::oled_commandList(const uint8_t *c, uint8_t n) {
  172. if (i2c_dev) { // I2C
  173. uint8_t dc_byte = 0x00; // Co = 0, D/C = 0
  174. if (!i2c_dev->write((uint8_t *)c, n, true, &dc_byte, 1)) {
  175. return false;
  176. }
  177. } else { // SPI -- transaction started in calling function
  178. digitalWrite(dcPin, LOW);
  179. if (!spi_dev->write((uint8_t *)c, n)) {
  180. return false;
  181. }
  182. }
  183. return true;
  184. }
  185. // ALLOCATE & INIT DISPLAY -------------------------------------------------
  186. /*!
  187. @brief Allocate RAM for image buffer, initialize peripherals and pins.
  188. Note that subclasses must call this before other begin() init
  189. @param addr
  190. I2C address of corresponding oled display.
  191. SPI displays (hardware or software) do not use addresses, but
  192. this argument is still required. Default if unspecified is 0x3C.
  193. @param reset
  194. If true, and if the reset pin passed to the constructor is
  195. valid, a hard reset will be performed before initializing the
  196. display. If using multiple oled displays on the same bus, and
  197. if they all share the same reset pin, you should only pass true
  198. on the first display being initialized, false on all others,
  199. else the already-initialized displays would be reset. Default if
  200. unspecified is true.
  201. @return true on successful allocation/init, false otherwise.
  202. Well-behaved code should check the return value before
  203. proceeding.
  204. @note MUST call this function before any drawing or updates!
  205. */
  206. bool Adafruit_GrayOLED::_init(uint8_t addr, bool reset) {
  207. // attempt to malloc the bitmap framebuffer
  208. if ((!buffer) &&
  209. !(buffer = (uint8_t *)malloc(_bpp * WIDTH * ((HEIGHT + 7) / 8)))) {
  210. return false;
  211. }
  212. // Reset OLED if requested and reset pin specified in constructor
  213. if (reset && (rstPin >= 0)) {
  214. pinMode(rstPin, OUTPUT);
  215. digitalWrite(rstPin, HIGH);
  216. delay(10); // VDD goes high at start, pause
  217. digitalWrite(rstPin, LOW); // Bring reset low
  218. delay(10); // Wait 10 ms
  219. digitalWrite(rstPin, HIGH); // Bring out of reset
  220. delay(10);
  221. }
  222. // Setup pin directions
  223. if (_theWire) { // using I2C
  224. i2c_dev = new Adafruit_I2CDevice(addr, _theWire);
  225. // look for i2c address:
  226. if (!i2c_dev || !i2c_dev->begin()) {
  227. return false;
  228. }
  229. } else { // Using one of the SPI modes, either soft or hardware
  230. if (!spi_dev || !spi_dev->begin()) {
  231. return false;
  232. }
  233. pinMode(dcPin, OUTPUT); // Set data/command pin as output
  234. }
  235. clearDisplay();
  236. // set max dirty window
  237. window_x1 = 0;
  238. window_y1 = 0;
  239. window_x2 = WIDTH - 1;
  240. window_y2 = HEIGHT - 1;
  241. return true; // Success
  242. }
  243. // DRAWING FUNCTIONS -------------------------------------------------------
  244. /*!
  245. @brief Set/clear/invert a single pixel. This is also invoked by the
  246. Adafruit_GFX library in generating many higher-level graphics
  247. primitives.
  248. @param x
  249. Column of display -- 0 at left to (screen width - 1) at right.
  250. @param y
  251. Row of display -- 0 at top to (screen height -1) at bottom.
  252. @param color
  253. Pixel color, one of: MONOOLED_BLACK, MONOOLED_WHITE or
  254. MONOOLED_INVERT.
  255. @note Changes buffer contents only, no immediate effect on display.
  256. Follow up with a call to display(), or with other graphics
  257. commands as needed by one's own application.
  258. */
  259. void Adafruit_GrayOLED::drawPixel(int16_t x, int16_t y, uint16_t color) {
  260. if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
  261. // Pixel is in-bounds. Rotate coordinates if needed.
  262. switch (getRotation()) {
  263. case 1:
  264. grayoled_swap(x, y);
  265. x = WIDTH - x - 1;
  266. break;
  267. case 2:
  268. x = WIDTH - x - 1;
  269. y = HEIGHT - y - 1;
  270. break;
  271. case 3:
  272. grayoled_swap(x, y);
  273. y = HEIGHT - y - 1;
  274. break;
  275. }
  276. // adjust dirty window
  277. window_x1 = min(window_x1, x);
  278. window_y1 = min(window_y1, y);
  279. window_x2 = max(window_x2, x);
  280. window_y2 = max(window_y2, y);
  281. if (_bpp == 1) {
  282. switch (color) {
  283. case MONOOLED_WHITE:
  284. buffer[x + (y / 8) * WIDTH] |= (1 << (y & 7));
  285. break;
  286. case MONOOLED_BLACK:
  287. buffer[x + (y / 8) * WIDTH] &= ~(1 << (y & 7));
  288. break;
  289. case MONOOLED_INVERSE:
  290. buffer[x + (y / 8) * WIDTH] ^= (1 << (y & 7));
  291. break;
  292. }
  293. }
  294. if (_bpp == 4) {
  295. uint8_t *pixelptr = &buffer[x / 2 + (y * WIDTH / 2)];
  296. // Serial.printf("(%d, %d) -> offset %d\n", x, y, x/2 + (y * WIDTH / 2));
  297. if (x % 2 == 0) { // even, left nibble
  298. uint8_t t = pixelptr[0] & 0x0F;
  299. t |= (color & 0xF) << 4;
  300. pixelptr[0] = t;
  301. } else { // odd, right lower nibble
  302. uint8_t t = pixelptr[0] & 0xF0;
  303. t |= color & 0xF;
  304. pixelptr[0] = t;
  305. }
  306. }
  307. }
  308. }
  309. /*!
  310. @brief Clear contents of display buffer (set all pixels to off).
  311. @note Changes buffer contents only, no immediate effect on display.
  312. Follow up with a call to display(), or with other graphics
  313. commands as needed by one's own application.
  314. */
  315. void Adafruit_GrayOLED::clearDisplay(void) {
  316. memset(buffer, 0, _bpp * WIDTH * ((HEIGHT + 7) / 8));
  317. // set max dirty window
  318. window_x1 = 0;
  319. window_y1 = 0;
  320. window_x2 = WIDTH - 1;
  321. window_y2 = HEIGHT - 1;
  322. }
  323. /*!
  324. @brief Return color of a single pixel in display buffer.
  325. @param x
  326. Column of display -- 0 at left to (screen width - 1) at right.
  327. @param y
  328. Row of display -- 0 at top to (screen height -1) at bottom.
  329. @return true if pixel is set (usually MONOOLED_WHITE, unless display invert
  330. mode is enabled), false if clear (MONOOLED_BLACK).
  331. @note Reads from buffer contents; may not reflect current contents of
  332. screen if display() has not been called.
  333. */
  334. bool Adafruit_GrayOLED::getPixel(int16_t x, int16_t y) {
  335. if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
  336. // Pixel is in-bounds. Rotate coordinates if needed.
  337. switch (getRotation()) {
  338. case 1:
  339. grayoled_swap(x, y);
  340. x = WIDTH - x - 1;
  341. break;
  342. case 2:
  343. x = WIDTH - x - 1;
  344. y = HEIGHT - y - 1;
  345. break;
  346. case 3:
  347. grayoled_swap(x, y);
  348. y = HEIGHT - y - 1;
  349. break;
  350. }
  351. return (buffer[x + (y / 8) * WIDTH] & (1 << (y & 7)));
  352. }
  353. return false; // Pixel out of bounds
  354. }
  355. /*!
  356. @brief Get base address of display buffer for direct reading or writing.
  357. @return Pointer to an unsigned 8-bit array, column-major, columns padded
  358. to full byte boundary if needed.
  359. */
  360. uint8_t *Adafruit_GrayOLED::getBuffer(void) { return buffer; }
  361. // OTHER HARDWARE SETTINGS -------------------------------------------------
  362. /*!
  363. @brief Enable or disable display invert mode (white-on-black vs
  364. black-on-white). Handy for testing!
  365. @param i
  366. If true, switch to invert mode (black-on-white), else normal
  367. mode (white-on-black).
  368. @note This has an immediate effect on the display, no need to call the
  369. display() function -- buffer contents are not changed, rather a
  370. different pixel mode of the display hardware is used. When
  371. enabled, drawing MONOOLED_BLACK (value 0) pixels will actually draw
  372. white, MONOOLED_WHITE (value 1) will draw black.
  373. */
  374. void Adafruit_GrayOLED::invertDisplay(bool i) {
  375. oled_command(i ? GRAYOLED_INVERTDISPLAY : GRAYOLED_NORMALDISPLAY);
  376. }
  377. /*!
  378. @brief Adjust the display contrast.
  379. @param level The contrast level from 0 to 0x7F
  380. @note This has an immediate effect on the display, no need to call the
  381. display() function -- buffer contents are not changed.
  382. */
  383. void Adafruit_GrayOLED::setContrast(uint8_t level) {
  384. uint8_t cmd[] = {GRAYOLED_SETCONTRAST, level};
  385. oled_commandList(cmd, 2);
  386. }
  387. #endif /* ATTIN85 not supported */