gds_private.h 5.1 KB

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