2
0

gds_private.h 5.0 KB

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