Adafruit_GrayOLED.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*!
  2. * @file Adafruit_GrayOLED.h
  3. *
  4. * This is part of for Adafruit's GFX library, supplying generic support
  5. * for grayscale 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_GRAYOLED_H_
  24. #define _Adafruit_GRAYOLED_H_
  25. // Not for ATtiny, at all
  26. #if !defined(__AVR_ATtiny85__) && !defined(__AVR_ATtiny84__)
  27. #include <Adafruit_GFX.h>
  28. #include <Adafruit_I2CDevice.h>
  29. #include <Adafruit_SPIDevice.h>
  30. #include <SPI.h>
  31. #include <Wire.h>
  32. #define GRAYOLED_SETCONTRAST 0x81 ///< Generic contrast for almost all OLEDs
  33. #define GRAYOLED_NORMALDISPLAY 0xA6 ///< Generic non-invert for almost all OLEDs
  34. #define GRAYOLED_INVERTDISPLAY 0xA7 ///< Generic invert for almost all OLEDs
  35. #define MONOOLED_BLACK 0 ///< Default black 'color' for monochrome OLEDS
  36. #define MONOOLED_WHITE 1 ///< Default white 'color' for monochrome OLEDS
  37. #define MONOOLED_INVERSE 2 ///< Default inversion command for monochrome OLEDS
  38. /*!
  39. @brief Class that stores state and functions for interacting with
  40. generic grayscale OLED displays.
  41. */
  42. class Adafruit_GrayOLED : public Adafruit_GFX {
  43. public:
  44. Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h, TwoWire *twi = &Wire,
  45. int16_t rst_pin = -1, uint32_t preclk = 400000,
  46. uint32_t postclk = 100000);
  47. Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h, int16_t mosi_pin,
  48. int16_t sclk_pin, int16_t dc_pin, int16_t rst_pin,
  49. int16_t cs_pin);
  50. Adafruit_GrayOLED(uint8_t bpp, uint16_t w, uint16_t h, SPIClass *spi,
  51. int16_t dc_pin, int16_t rst_pin, int16_t cs_pin,
  52. uint32_t bitrate = 8000000UL);
  53. ~Adafruit_GrayOLED(void);
  54. /**
  55. @brief The function that sub-classes define that writes out the buffer to
  56. the display over I2C or SPI
  57. **/
  58. virtual void display(void) = 0;
  59. void clearDisplay(void);
  60. void invertDisplay(bool i);
  61. void setContrast(uint8_t contrastlevel);
  62. void drawPixel(int16_t x, int16_t y, uint16_t color);
  63. bool getPixel(int16_t x, int16_t y);
  64. uint8_t *getBuffer(void);
  65. void oled_command(uint8_t c);
  66. bool oled_commandList(const uint8_t *c, uint8_t n);
  67. protected:
  68. bool _init(uint8_t i2caddr = 0x3C, bool reset = true);
  69. Adafruit_SPIDevice *spi_dev = NULL; ///< The SPI interface BusIO device
  70. Adafruit_I2CDevice *i2c_dev = NULL; ///< The I2C interface BusIO device
  71. int32_t i2c_preclk = 400000, ///< Configurable 'high speed' I2C rate
  72. i2c_postclk = 100000; ///< Configurable 'low speed' I2C rate
  73. uint8_t *buffer = NULL; ///< Internal 1:1 framebuffer of display mem
  74. int16_t window_x1, ///< Dirty tracking window minimum x
  75. window_y1, ///< Dirty tracking window minimum y
  76. window_x2, ///< Dirty tracking window maximum x
  77. window_y2; ///< Dirty tracking window maximum y
  78. int dcPin, ///< The Arduino pin connected to D/C (for SPI)
  79. csPin, ///< The Arduino pin connected to CS (for SPI)
  80. rstPin; ///< The Arduino pin connected to reset (-1 if unused)
  81. uint8_t _bpp = 1; ///< Bits per pixel color for this display
  82. private:
  83. TwoWire *_theWire = NULL; ///< The underlying hardware I2C
  84. };
  85. #endif // end __AVR_ATtiny85__ __AVR_ATtiny84__
  86. #endif // _Adafruit_GrayOLED_H_