Adafruit_SPITFT.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef _ADAFRUIT_SPITFT_
  2. #define _ADAFRUIT_SPITFT_
  3. #if ARDUINO >= 100
  4. #include "Arduino.h"
  5. #include "Print.h"
  6. #else
  7. #include "WProgram.h"
  8. #endif
  9. #include <SPI.h>
  10. #include "Adafruit_GFX.h"
  11. #define USE_FAST_PINIO
  12. #if defined(__AVR__)
  13. typedef volatile uint8_t RwReg;
  14. #elif defined(ARDUINO_STM32_FEATHER)
  15. typedef volatile uint32 RwReg;
  16. #undef USE_FAST_PINIO
  17. #elif defined(__OPENCR__) || defined (__OPENCM904__)
  18. #undef USE_FAST_PINIO
  19. #elif defined(ARDUINO_FEATHER52) || defined(__arm__)
  20. typedef volatile uint32_t RwReg;
  21. #elif defined(ESP32) || defined(ESP8266)
  22. typedef volatile uint32_t RwReg;
  23. #undef USE_FAST_PINIO
  24. #else
  25. #undef USE_FAST_PINIO
  26. #endif
  27. #include "Adafruit_SPITFT_Macros.h"
  28. /// A heavily optimized SPI display subclass of GFX. Manages SPI bitbanging, transactions, DMA, etc! Despite being called SPITFT, the classic SPI data/command interface is also used by OLEDs.
  29. class Adafruit_SPITFT : public Adafruit_GFX {
  30. protected:
  31. public:
  32. Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, int8_t _RST = -1, int8_t _MISO = -1);
  33. Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _RST = -1);
  34. virtual void begin(uint32_t freq) = 0; ///< Virtual begin() function to set SPI frequency, must be overridden in subclass. @param freq Maximum SPI hardware clock speed
  35. void initSPI(uint32_t freq);
  36. // Required Non-Transaction
  37. void drawPixel(int16_t x, int16_t y, uint16_t color);
  38. // Transaction API
  39. void startWrite(void);
  40. void endWrite(void);
  41. void writePixel(int16_t x, int16_t y, uint16_t color);
  42. void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
  43. void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  44. void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  45. // Transaction API not used by GFX
  46. /*!
  47. @brief SPI displays set an address window rectangle for blitting pixels
  48. @param x Top left corner x coordinate
  49. @param y Top left corner x coordinate
  50. @param w Width of window
  51. @param h Height of window
  52. */
  53. virtual void setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h) = 0;
  54. /*!
  55. @brief Write a 2-byte color (must have a transaction in progress)
  56. @param color 16-bit 5-6-5 Color to draw
  57. */
  58. void inline writePixel(uint16_t color) { SPI_WRITE16(color); }
  59. void writePixels(uint16_t * colors, uint32_t len);
  60. void writeColor(uint16_t color, uint32_t len);
  61. void pushColor(uint16_t color);
  62. // Recommended Non-Transaction
  63. void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  64. void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  65. void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
  66. using Adafruit_GFX::drawRGBBitmap; // Check base class first
  67. void drawRGBBitmap(int16_t x, int16_t y,
  68. uint16_t *pcolors, int16_t w, int16_t h);
  69. void invertDisplay(boolean i);
  70. uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
  71. protected:
  72. uint32_t _freq; ///< SPI clock frequency (for hardware SPI)
  73. #if defined (__AVR__) || defined(TEENSYDUINO) || defined (ESP8266) || defined (ESP32)
  74. int8_t _cs, _dc, _rst, _sclk, _mosi, _miso;
  75. #else
  76. int32_t _cs, ///< Arduino pin # for chip-select pin
  77. _dc, ///< Arduino pin # for data-command pin
  78. _rst, ///< Arduino pin # for reset pin
  79. _sclk, ///< Arduino pin # for SPI clock pin
  80. _mosi, ///< Arduino pin # for SPI MOSI pin
  81. _miso; ///< Arduino pin # for SPI MISO pin
  82. #endif
  83. #ifdef USE_FAST_PINIO
  84. volatile RwReg *mosiport, ///< Direct chip register for toggling MOSI with fast bitbang IO
  85. *misoport, ///< Direct chip register for toggling MISO with fast bitbang IO
  86. *clkport, ///< Direct chip register for toggling CLK with fast bitbang IO
  87. *dcport, ///< Direct chip register for toggling DC with fast bitbang IO
  88. *csport; ///< Direct chip register for toggling CS with fast bitbang IO
  89. RwReg mosipinmask, ///< bitmask for turning on/off MOSI with fast register bitbang IO
  90. misopinmask, ///< bitmask for turning on/off MISO with fast register bitbang IO
  91. clkpinmask, ///< bitmask for turning on/off CLK with fast register bitbang IO
  92. cspinmask, ///< bitmask for turning on/off CS with fast register bitbang IO
  93. dcpinmask; ///< bitmask for turning on/off DC with fast register bitbang IO
  94. #endif
  95. void writeCommand(uint8_t cmd);
  96. void spiWrite(uint8_t v);
  97. uint8_t spiRead(void);
  98. uint8_t invertOnCommand = 0, ///< SPI command byte to turn on invert
  99. invertOffCommand = 0; ///< SPI command byte to turn off invert
  100. int16_t _xstart = 0; ///< Many displays don't have pixels starting at (0,0) of the internal framebuffer, this is the x offset from 0 to align
  101. int16_t _ystart = 0; ///< Many displays don't have pixels starting at (0,0) of the internal framebuffer, this is the y offset from 0 to align
  102. };
  103. #endif