gds_private.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #ifndef _GDS_PRIVATE_H_
  9. #define _GDS_PRIVATE_H_
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "esp_attr.h"
  13. #include "gds.h"
  14. #include "gds_err.h"
  15. #define GDS_ALLOC_NONE 0x80
  16. #define GDS_ALLOC_IRAM 0x01
  17. #define GDS_ALLOC_IRAM_SPI 0x02
  18. #define GDS_CLIPDEBUG_NONE 0
  19. #define GDS_CLIPDEBUG_WARNING 1
  20. #define GDS_CLIPDEBUG_ERROR 2
  21. #if CONFIG_GDS_CLIPDEBUG == GDS_CLIPDEBUG_NONE
  22. /*
  23. * Clip silently with no console output.
  24. */
  25. #define ClipDebug( x, y )
  26. #elif CONFIG_GDS_CLIPDEBUG == GDS_CLIPDEBUG_WARNING
  27. /*
  28. * Log clipping to the console as a warning.
  29. */
  30. #define ClipDebug( x, y ) { \
  31. ESP_LOGW( __FUNCTION__, "Line %d: Pixel at %d, %d CLIPPED", __LINE__, x, y ); \
  32. }
  33. #elif CONFIG_GDS_CLIPDEBUG == GDS_CLIPDEBUG_ERROR
  34. /*
  35. * Log clipping as an error to the console.
  36. * Also invokes an abort with stack trace.
  37. */
  38. #define ClipDebug( x, y ) { \
  39. ESP_LOGE( __FUNCTION__, "Line %d: Pixel at %d, %d CLIPPED, ABORT", __LINE__, x, y ); \
  40. abort( ); \
  41. }
  42. #endif
  43. #define GDS_ALWAYS_INLINE __attribute__( ( always_inline ) )
  44. #define MAX_LINES 8
  45. #if ! defined BIT
  46. #define BIT( n ) ( 1 << ( n ) )
  47. #endif
  48. struct GDS_Device;
  49. struct GDS_FontDef;
  50. /*
  51. * These can optionally return a succeed/fail but are as of yet unused in the driver.
  52. */
  53. typedef bool ( *WriteCommandProc ) ( struct GDS_Device* Device, uint8_t Command );
  54. typedef bool ( *WriteDataProc ) ( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength );
  55. struct spi_device_t;
  56. typedef struct spi_device_t* spi_device_handle_t;
  57. #define GDS_IF_SPI 0
  58. #define GDS_IF_I2C 1
  59. struct GDS_Device {
  60. uint8_t IF;
  61. union {
  62. // I2C Specific
  63. struct {
  64. uint8_t Address;
  65. };
  66. // SPI specific
  67. struct {
  68. spi_device_handle_t SPIHandle;
  69. int8_t RSTPin;
  70. int8_t CSPin;
  71. };
  72. };
  73. // cooked text mode
  74. struct {
  75. int16_t Y, Space;
  76. const struct GDS_FontDef* Font;
  77. } Lines[MAX_LINES];
  78. uint16_t Width;
  79. uint16_t Height;
  80. uint8_t Depth, Mode;
  81. uint8_t Alloc;
  82. uint8_t* Framebuffer;
  83. uint16_t FramebufferSize;
  84. bool Dirty;
  85. // default fonts when using direct draw
  86. const struct GDS_FontDef* Font;
  87. bool FontForceProportional;
  88. bool FontForceMonospace;
  89. // various driver-specific method
  90. // must always provide
  91. bool (*Init)( struct GDS_Device* Device);
  92. void (*Update)( struct GDS_Device* Device );
  93. // may provide if supported
  94. void (*SetContrast)( struct GDS_Device* Device, uint8_t Contrast );
  95. void (*DisplayOn)( struct GDS_Device* Device );
  96. void (*DisplayOff)( struct GDS_Device* Device );
  97. void (*SetHFlip)( struct GDS_Device* Device, bool On );
  98. void (*SetVFlip)( struct GDS_Device* Device, bool On );
  99. // must provide for depth other than 1 (vertical) and 4 (may provide for optimization)
  100. void (*DrawPixelFast)( struct GDS_Device* Device, int X, int Y, int Color );
  101. void (*DrawBitmapCBR)(struct GDS_Device* Device, uint8_t *Data, int Width, int Height, int Color );
  102. // may provide for optimization
  103. void (*DrawRGB)( struct GDS_Device* Device, uint8_t *Image,int x, int y, int Width, int Height, int RGB_Mode );
  104. void (*ClearWindow)( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color );
  105. // interface-specific methods
  106. WriteCommandProc WriteCommand;
  107. WriteDataProc WriteData;
  108. // 16 bytes for whatever the driver wants (should be aligned as it's 32 bits)
  109. uint32_t Private[4];
  110. };
  111. bool GDS_Reset( struct GDS_Device* Device );
  112. bool GDS_Init( struct GDS_Device* Device );
  113. static inline bool IsPixelVisible( struct GDS_Device* Device, int x, int y ) {
  114. bool Result = (
  115. ( x >= 0 ) &&
  116. ( x < Device->Width ) &&
  117. ( y >= 0 ) &&
  118. ( y < Device->Height )
  119. ) ? true : false;
  120. #if CONFIG_GDS_CLIPDEBUG > 0
  121. if ( Result == false ) {
  122. ClipDebug( x, y );
  123. }
  124. #endif
  125. return Result;
  126. }
  127. static inline void IRAM_ATTR GDS_DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  128. uint32_t YBit = ( Y & 0x07 );
  129. uint8_t* FBOffset;
  130. /*
  131. * We only need to modify the Y coordinate since the pitch
  132. * of the screen is the same as the width.
  133. * Dividing Y by 8 gives us which row the pixel is in but not
  134. * the bit position.
  135. */
  136. Y>>= 3;
  137. FBOffset = Device->Framebuffer + ( ( Y * Device->Width ) + X );
  138. if ( Color == GDS_COLOR_XOR ) {
  139. *FBOffset ^= BIT( YBit );
  140. } else {
  141. *FBOffset = ( Color == GDS_COLOR_BLACK ) ? *FBOffset & ~BIT( YBit ) : *FBOffset | BIT( YBit );
  142. }
  143. }
  144. static inline void IRAM_ATTR GDS_DrawPixel4Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  145. uint8_t* FBOffset = Device->Framebuffer + ( (Y * Device->Width >> 1) + (X >> 1));
  146. *FBOffset = X & 0x01 ? (*FBOffset & 0x0f) | ((Color & 0x0f) << 4) : ((*FBOffset & 0xf0) | (Color & 0x0f));
  147. }
  148. static inline void IRAM_ATTR GDS_DrawPixel8Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  149. Device->Framebuffer[Y * Device->Width + X] = Color;
  150. }
  151. // assumes that Color is 16 bits R..RG..GB..B from MSB to LSB and FB wants 1st serialized byte to start with R
  152. static inline void IRAM_ATTR GDS_DrawPixel16Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  153. uint16_t* FBOffset = (uint16_t*) Device->Framebuffer + Y * Device->Width + X;
  154. *FBOffset = __builtin_bswap16(Color);
  155. }
  156. // assumes that Color is 18 bits RGB from MSB to LSB RRRRRRGGGGGGBBBBBB, so byte[0] is B
  157. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = xxRRRRRR xxGGGGGG xxBBBBBB
  158. static inline void IRAM_ATTR GDS_DrawPixel18Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  159. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  160. *FBOffset++ = Color >> 12; *FBOffset++ = (Color >> 6) & 0x3f; *FBOffset = Color & 0x3f;
  161. }
  162. // assumes that Color is 24 bits RGB from MSB to LSB RRRRRRRRGGGGGGGGBBBBBBBB, so byte[0] is B
  163. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = RRRRRRRR GGGGGGGG BBBBBBBB
  164. static inline void IRAM_ATTR GDS_DrawPixel24Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  165. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  166. *FBOffset++ = Color >> 16; *FBOffset++ = Color >> 8; *FBOffset = Color;
  167. }
  168. static inline void IRAM_ATTR GDS_DrawPixelFast( struct GDS_Device* Device, int X, int Y, int Color ) {
  169. if (Device->DrawPixelFast) Device->DrawPixelFast( Device, X, Y, Color );
  170. else if (Device->Depth == 4) GDS_DrawPixel4Fast( Device, X, Y, Color );
  171. else if (Device->Depth == 1) GDS_DrawPixel1Fast( Device, X, Y, Color );
  172. else if (Device->Depth == 16) GDS_DrawPixel16Fast( Device, X, Y, Color );
  173. else if (Device->Depth == 24 && Device->Mode == GDS_RGB666) GDS_DrawPixel18Fast( Device, X, Y, Color );
  174. else if (Device->Depth == 24 && Device->Mode == GDS_RGB888) GDS_DrawPixel24Fast( Device, X, Y, Color );
  175. else if (Device->Depth == 8) GDS_DrawPixel8Fast( Device, X, Y, Color );
  176. }
  177. static inline void IRAM_ATTR GDS_DrawPixel( struct GDS_Device* Device, int x, int y, int Color ) {
  178. if ( IsPixelVisible( Device, x, y ) == true ) {
  179. GDS_DrawPixelFast( Device, x, y, Color );
  180. }
  181. }
  182. #endif