gds_private.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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;
  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 (*DrawRGB16)( struct GDS_Device* Device, uint16_t *Image,int x, int y, int Width, int Height, int RGB_Mode );
  104. void (*DrawRGB8)( struct GDS_Device* Device, uint8_t *Image, int x, int y, int Width, int Height, int RGB_Mode );
  105. void (*ClearWindow)( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color );
  106. // interface-specific methods
  107. WriteCommandProc WriteCommand;
  108. WriteDataProc WriteData;
  109. // 16 bytes for whatever the driver wants (should be aligned as it's 32 bits)
  110. uint32_t Private[4];
  111. };
  112. bool GDS_Reset( struct GDS_Device* Device );
  113. bool GDS_Init( struct GDS_Device* Device );
  114. static inline bool IsPixelVisible( struct GDS_Device* Device, int x, int y ) {
  115. bool Result = (
  116. ( x >= 0 ) &&
  117. ( x < Device->Width ) &&
  118. ( y >= 0 ) &&
  119. ( y < Device->Height )
  120. ) ? true : false;
  121. #if CONFIG_GDS_CLIPDEBUG > 0
  122. if ( Result == false ) {
  123. ClipDebug( x, y );
  124. }
  125. #endif
  126. return Result;
  127. }
  128. static inline void IRAM_ATTR GDS_DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  129. uint32_t YBit = ( Y & 0x07 );
  130. uint8_t* FBOffset = NULL;
  131. /*
  132. * We only need to modify the Y coordinate since the pitch
  133. * of the screen is the same as the width.
  134. * Dividing Y by 8 gives us which row the pixel is in but not
  135. * the bit position.
  136. */
  137. Y>>= 3;
  138. FBOffset = Device->Framebuffer + ( ( Y * Device->Width ) + X );
  139. if ( Color == GDS_COLOR_XOR ) {
  140. *FBOffset ^= BIT( YBit );
  141. } else {
  142. *FBOffset = ( Color == GDS_COLOR_BLACK ) ? *FBOffset & ~BIT( YBit ) : *FBOffset | BIT( YBit );
  143. }
  144. }
  145. static inline void IRAM_ATTR GDS_DrawPixel4Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  146. uint8_t* FBOffset;
  147. FBOffset = Device->Framebuffer + ( (Y * Device->Width >> 1) + (X >> 1));
  148. *FBOffset = X & 0x01 ? (*FBOffset & 0x0f) | ((Color & 0x0f) << 4) : ((*FBOffset & 0xf0) | (Color & 0x0f));
  149. }
  150. static inline void IRAM_ATTR GDS_DrawPixelFast( struct GDS_Device* Device, int X, int Y, int Color ) {
  151. if (Device->DrawPixelFast) Device->DrawPixelFast( Device, X, Y, Color );
  152. else if (Device->Depth == 4) GDS_DrawPixel4Fast( Device, X, Y, Color);
  153. else if (Device->Depth == 1) GDS_DrawPixel1Fast( Device, X, Y, Color);
  154. }
  155. static inline void IRAM_ATTR GDS_DrawPixel( struct GDS_Device* Device, int x, int y, int Color ) {
  156. if ( IsPixelVisible( Device, x, y ) == true ) {
  157. GDS_DrawPixelFast( Device, x, y, Color );
  158. }
  159. }
  160. #endif