gds_private.h 5.2 KB

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