gds_private.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. int8_t RSTPin;
  62. struct {
  63. int8_t Pin, Channel;
  64. int PWM;
  65. } Backlight;
  66. union {
  67. // I2C Specific
  68. struct {
  69. uint8_t Address;
  70. };
  71. // SPI specific
  72. struct {
  73. spi_device_handle_t SPIHandle;
  74. int8_t CSPin;
  75. };
  76. };
  77. // cooked text mode
  78. struct {
  79. int16_t Y, Space;
  80. const struct GDS_FontDef* Font;
  81. } Lines[MAX_LINES];
  82. uint16_t Width, TextWidth;
  83. uint16_t Height;
  84. uint8_t Depth, Mode;
  85. uint8_t Alloc;
  86. uint8_t* Framebuffer;
  87. uint32_t FramebufferSize;
  88. bool Dirty;
  89. // default fonts when using direct draw
  90. const struct GDS_FontDef* Font;
  91. bool FontForceProportional;
  92. bool FontForceMonospace;
  93. // various driver-specific method
  94. // must always provide
  95. bool (*Init)( struct GDS_Device* Device);
  96. void (*Update)( struct GDS_Device* Device );
  97. // may provide if supported
  98. void (*SetContrast)( struct GDS_Device* Device, uint8_t Contrast );
  99. void (*DisplayOn)( struct GDS_Device* Device );
  100. void (*DisplayOff)( struct GDS_Device* Device );
  101. void (*SetLayout)( struct GDS_Device* Device, struct GDS_Layout *Layout );
  102. // must provide for depth other than 1 (vertical) and 4 (may provide for optimization)
  103. void (*DrawPixelFast)( struct GDS_Device* Device, int X, int Y, int Color );
  104. void (*DrawBitmapCBR)(struct GDS_Device* Device, uint8_t *Data, int Width, int Height, int Color );
  105. // may provide for optimization
  106. void (*DrawRGB)( struct GDS_Device* Device, uint8_t *Image,int x, int y, int Width, int Height, int RGB_Mode );
  107. void (*ClearWindow)( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color );
  108. // may provide for tweaking
  109. void (*SPIParams)(int Speed, uint8_t *mode, uint8_t *CS_pre, uint8_t *CS_post);
  110. // interface-specific methods
  111. WriteCommandProc WriteCommand;
  112. WriteDataProc WriteData;
  113. // 32 bytes for whatever the driver wants (should be aligned as it's 32 bits)
  114. uint32_t Private[8];
  115. };
  116. bool GDS_Reset( struct GDS_Device* Device );
  117. bool GDS_Init( struct GDS_Device* Device );
  118. static inline bool IsPixelVisible( struct GDS_Device* Device, int x, int y ) {
  119. bool Result = (
  120. ( x >= 0 ) &&
  121. ( x < Device->Width ) &&
  122. ( y >= 0 ) &&
  123. ( y < Device->Height )
  124. ) ? true : false;
  125. #if CONFIG_GDS_CLIPDEBUG > 0
  126. if ( Result == false ) {
  127. ClipDebug( x, y );
  128. }
  129. #endif
  130. return Result;
  131. }
  132. static inline void DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  133. uint32_t YBit = ( Y & 0x07 );
  134. uint8_t* FBOffset;
  135. /*
  136. * We only need to modify the Y coordinate since the pitch
  137. * of the screen is the same as the width.
  138. * Dividing Y by 8 gives us which row the pixel is in but not
  139. * the bit position.
  140. */
  141. Y>>= 3;
  142. FBOffset = Device->Framebuffer + ( ( Y * Device->Width ) + X );
  143. if ( Color == GDS_COLOR_XOR ) {
  144. *FBOffset ^= BIT( YBit );
  145. } else {
  146. *FBOffset = ( Color == GDS_COLOR_BLACK ) ? *FBOffset & ~BIT( YBit ) : *FBOffset | BIT( YBit );
  147. }
  148. }
  149. static inline void DrawPixel4Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  150. uint8_t* FBOffset = Device->Framebuffer + ( (Y * Device->Width >> 1) + (X >> 1));
  151. *FBOffset = X & 0x01 ? (*FBOffset & 0x0f) | ((Color & 0x0f) << 4) : ((*FBOffset & 0xf0) | (Color & 0x0f));
  152. }
  153. static inline void DrawPixel8Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  154. Device->Framebuffer[Y * Device->Width + X] = Color;
  155. }
  156. // assumes that Color is 16 bits R..RG..GB..B from MSB to LSB and FB wants 1st serialized byte to start with R
  157. static inline void DrawPixel16Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  158. uint16_t* FBOffset = (uint16_t*) Device->Framebuffer + Y * Device->Width + X;
  159. *FBOffset = __builtin_bswap16(Color);
  160. }
  161. // assumes that Color is 18 bits RGB from MSB to LSB RRRRRRGGGGGGBBBBBB, so byte[0] is B
  162. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = xxRRRRRR xxGGGGGG xxBBBBBB
  163. static inline void DrawPixel18Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  164. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  165. *FBOffset++ = Color >> 12; *FBOffset++ = (Color >> 6) & 0x3f; *FBOffset = Color & 0x3f;
  166. }
  167. // assumes that Color is 24 bits RGB from MSB to LSB RRRRRRRRGGGGGGGGBBBBBBBB, so byte[0] is B
  168. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = RRRRRRRR GGGGGGGG BBBBBBBB
  169. static inline void DrawPixel24Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  170. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  171. *FBOffset++ = Color >> 16; *FBOffset++ = Color >> 8; *FBOffset = Color;
  172. }
  173. static inline void IRAM_ATTR DrawPixelFast( struct GDS_Device* Device, int X, int Y, int Color ) {
  174. if (Device->DrawPixelFast) Device->DrawPixelFast( Device, X, Y, Color );
  175. else if (Device->Depth == 4) DrawPixel4Fast( Device, X, Y, Color );
  176. else if (Device->Depth == 1) DrawPixel1Fast( Device, X, Y, Color );
  177. else if (Device->Depth == 16) DrawPixel16Fast( Device, X, Y, Color );
  178. else if (Device->Depth == 24 && Device->Mode == GDS_RGB666) DrawPixel18Fast( Device, X, Y, Color );
  179. else if (Device->Depth == 24 && Device->Mode == GDS_RGB888) DrawPixel24Fast( Device, X, Y, Color );
  180. else if (Device->Depth == 8) DrawPixel8Fast( Device, X, Y, Color );
  181. }
  182. static inline void IRAM_ATTR DrawPixel( struct GDS_Device* Device, int x, int y, int Color ) {
  183. if ( IsPixelVisible( Device, x, y ) == true ) {
  184. DrawPixelFast( Device, x, y, Color );
  185. }
  186. }
  187. #endif