You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

186 lines
8.7 KiB

  1. /***************************************************************************************
  2. // The following class creates Sprites in RAM, graphics can then be drawn in the Sprite
  3. // and rendered quickly onto the TFT screen. The class inherits the graphics functions
  4. // from the TFT_eSPI class. Some functions are overridden by this class so that the
  5. // graphics are written to the Sprite rather than the TFT.
  6. ***************************************************************************************/
  7. class TFT_eSprite : public TFT_eSPI {
  8. public:
  9. TFT_eSprite(TFT_eSPI *tft);
  10. // Create a sprite of width x height pixels, return a pointer to the RAM area
  11. // Sketch can cast returned value to (uint16_t*) for 16 bit depth if needed
  12. // RAM required is:
  13. // - 1 bit per pixel for 1 bit colour depth
  14. // - 1 byte per pixel for 8 bit colour
  15. // - 2 bytes per pixel for 16 bit color depth
  16. ~TFT_eSprite(void);
  17. void* createSprite(int16_t width, int16_t height, uint8_t frames = 1);
  18. // Delete the sprite to free up the RAM
  19. void deleteSprite(void);
  20. // Select the frame buffer for graphics write (for 2 colour ePaper and DMA toggle buffer)
  21. // Returns a pointer to the Sprite frame buffer
  22. void* frameBuffer(int8_t f);
  23. // Set or get the colour depth to 4, 8 or 16 bits. Can be used to change depth an existing
  24. // sprite, but clears it to black, returns a new pointer if sprite is re-created.
  25. void* setColorDepth(int8_t b);
  26. int8_t getColorDepth(void);
  27. // Set the palette for a 4 bit depth sprite. Only the first 16 colours in the map are used.
  28. void createPalette(uint16_t *palette, int colors = 16); // Palette in RAM
  29. void createPalette(const uint16_t *palette, int colors = 16); // Palette in FLASH
  30. // Set a single palette index to the given color
  31. void setPaletteColor(uint8_t index, uint16_t color);
  32. // Get the color at the given palette index
  33. uint16_t getPaletteColor(uint8_t index);
  34. // Set foreground and background colours for 1 bit per pixel Sprite
  35. void setBitmapColor(uint16_t fg, uint16_t bg);
  36. void drawPixel(int32_t x, int32_t y, uint32_t color);
  37. void drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t font),
  38. // Fill Sprite with a colour
  39. fillSprite(uint32_t color),
  40. // Define a window to push 16 bit colour pixels into in a raster order
  41. // Colours are converted to the set Sprite colour bit depth
  42. setWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1),
  43. // Push a color (aka singe pixel) to the screen
  44. pushColor(uint32_t color),
  45. // Push len colors (pixels) to the screen
  46. pushColor(uint32_t color, uint16_t len),
  47. // Push a pixel preformatted as a 8 or 16 bit colour (avoids conversion overhead)
  48. writeColor(uint16_t color),
  49. // Set the scroll zone, top left corner at x,y with defined width and height
  50. // The colour (optional, black is default) is used to fill the gap after the scroll
  51. setScrollRect(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t color = TFT_BLACK),
  52. // Scroll the defined zone dx,dy pixels. Negative values left,up, positive right,down
  53. // dy is optional (default is then no up/down scroll).
  54. // The sprite coordinate frame does not move because pixels are moved
  55. scroll(int16_t dx, int16_t dy = 0),
  56. // Draw lines
  57. drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color),
  58. drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color),
  59. drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color),
  60. // Fill a rectangular area with a color (aka draw a filled rectangle)
  61. fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
  62. // Set the sprite text cursor position for print class (does not change the TFT screen cursor)
  63. //setCursor(int16_t x, int16_t y); // Not needed, so uses TFT class function
  64. // Set the coordinate rotation of the Sprite (for 1bpp Sprites only)
  65. // Note: this uses coordinate rotation and is primarily for ePaper which does not support
  66. // CGRAM rotation (like TFT drivers do) within the displays internal hardware
  67. void setRotation(uint8_t rotation);
  68. uint8_t getRotation(void);
  69. // Push a rotated copy of Sprite to TFT with optional transparent colour
  70. bool pushRotated(int16_t angle, int32_t transp = -1); // Using fixed point maths
  71. // Push a rotated copy of Sprite to another different Sprite with optional transparent colour
  72. bool pushRotated(TFT_eSprite *spr, int16_t angle, int32_t transp = -1); // Using fixed point maths
  73. // Set and get the pivot point for this Sprite
  74. void setPivot(int16_t x, int16_t y);
  75. int16_t getPivotX(void),
  76. getPivotY(void);
  77. // Get the TFT bounding box for a rotated copy of this Sprite
  78. bool getRotatedBounds(int16_t angle, int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
  79. // Get the destination Sprite bounding box for a rotated copy of this Sprite
  80. bool getRotatedBounds(TFT_eSprite *spr, int16_t angle, int16_t *min_x, int16_t *min_y,
  81. int16_t *max_x, int16_t *max_y);
  82. // Bounding box support function
  83. void getRotatedBounds(int16_t angle, int16_t w, int16_t h, int16_t xp, int16_t yp,
  84. int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
  85. // Read the colour of a pixel at x,y and return value in 565 format
  86. uint16_t readPixel(int32_t x0, int32_t y0);
  87. // return the numerical value of the pixel at x,y (used when scrolling)
  88. // 16bpp = colour, 8bpp = byte, 4bpp = colour index, 1bpp = 1 or 0
  89. uint16_t readPixelValue(int32_t x, int32_t y);
  90. // Write an image (colour bitmap) to the sprite. Not implemented for _bpp == 4.
  91. void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, uint16_t *data);
  92. void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, const uint16_t *data);
  93. // Swap the byte order for pushImage() - corrects different image endianness
  94. void setSwapBytes(bool swap);
  95. bool getSwapBytes(void);
  96. // Push the sprite to the TFT screen, this fn calls pushImage() in the TFT class.
  97. // Optionally a "transparent" colour can be defined, pixels of that colour will not be rendered
  98. void pushSprite(int32_t x, int32_t y);
  99. void pushSprite(int32_t x, int32_t y, uint16_t transparent);
  100. int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font),
  101. drawChar(uint16_t uniCode, int32_t x, int32_t y);
  102. // Return the width and height of the sprite
  103. int16_t width(void),
  104. height(void);
  105. // Used by print class to print text to cursor position
  106. size_t write(uint8_t);
  107. // Functions associated with anti-aliased fonts
  108. void drawGlyph(uint16_t code);
  109. void printToSprite(String string);
  110. void printToSprite(char *cbuffer, uint16_t len);
  111. int16_t printToSprite(int16_t x, int16_t y, uint16_t index);
  112. private:
  113. TFT_eSPI *_tft;
  114. // Reserve memory for the Sprite and return a pointer
  115. void* callocSprite(int16_t width, int16_t height, uint8_t frames = 1);
  116. protected:
  117. uint8_t _bpp; // bits per pixel (1, 8 or 16)
  118. uint16_t *_img; // pointer to 16 bit sprite
  119. uint8_t *_img8; // pointer to 8 bit sprite
  120. uint8_t *_img4; // pointer to 4 bit sprite (uses color map)
  121. uint8_t *_img8_1; // pointer to frame 1
  122. uint8_t *_img8_2; // pointer to frame 2
  123. uint16_t *_colorMap; // color map: 16 entries, used with 4 bit color map.
  124. int16_t _xpivot; // x pivot point coordinate
  125. int16_t _ypivot; // y pivot point coordinate
  126. int32_t _sinra;
  127. int32_t _cosra;
  128. bool _created; // A Sprite has been created and memory reserved
  129. bool _gFont = false;
  130. // int32_t _icursor_x, _icursor_y;
  131. uint8_t _rotation = 0;
  132. int32_t _xs, _ys, _xe, _ye, _xptr, _yptr; // for setWindow
  133. int32_t _sx, _sy; // x,y for scroll zone
  134. uint32_t _sw, _sh; // w,h for scroll zone
  135. uint32_t _scolor; // gap fill colour for scroll zone
  136. bool _iswapBytes; // Swap the byte order for Sprite pushImage()
  137. int32_t _iwidth, _iheight; // Sprite memory image bit width and height (swapped during rotations)
  138. int32_t _dwidth, _dheight; // Real display width and height (for <8bpp Sprites)
  139. int32_t _bitwidth; // Sprite image bit width for drawPixel (for <8bpp Sprites, not swapped)
  140. };