gds_private.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. bool HighNibble;
  86. uint8_t Alloc;
  87. uint8_t* Framebuffer;
  88. uint32_t FramebufferSize;
  89. bool Dirty;
  90. // default fonts when using direct draw
  91. const struct GDS_FontDef* Font;
  92. bool FontForceProportional;
  93. bool FontForceMonospace;
  94. // various driver-specific method
  95. // must always provide
  96. bool (*Init)( struct GDS_Device* Device);
  97. void (*Update)( struct GDS_Device* Device );
  98. // may provide if supported
  99. void (*SetContrast)( struct GDS_Device* Device, uint8_t Contrast );
  100. void (*DisplayOn)( struct GDS_Device* Device );
  101. void (*DisplayOff)( struct GDS_Device* Device );
  102. void (*SetLayout)( struct GDS_Device* Device, struct GDS_Layout *Layout );
  103. // must provide for depth other than 1 (vertical) and 4 (may provide for optimization)
  104. void (*DrawPixelFast)( struct GDS_Device* Device, int X, int Y, int Color );
  105. void (*DrawBitmapCBR)(struct GDS_Device* Device, uint8_t *Data, int Width, int Height, int Color );
  106. // may provide for optimization
  107. void (*DrawRGB)( struct GDS_Device* Device, uint8_t *Image,int x, int y, int Width, int Height, int RGB_Mode );
  108. void (*ClearWindow)( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color );
  109. // may provide for tweaking
  110. void (*SPIParams)(int Speed, uint8_t *mode, uint16_t *CS_pre, uint8_t *CS_post);
  111. // interface-specific methods
  112. WriteCommandProc WriteCommand;
  113. WriteDataProc WriteData;
  114. // 32 bytes for whatever the driver wants (should be aligned as it's 32 bits)
  115. uint32_t Private[8];
  116. };
  117. bool GDS_Reset( struct GDS_Device* Device );
  118. bool GDS_Init( struct GDS_Device* Device );
  119. static inline bool IsPixelVisible( struct GDS_Device* Device, int x, int y ) {
  120. bool Result = (
  121. ( x >= 0 ) &&
  122. ( x < Device->Width ) &&
  123. ( y >= 0 ) &&
  124. ( y < Device->Height )
  125. ) ? true : false;
  126. #if CONFIG_GDS_CLIPDEBUG > 0
  127. if ( Result == false ) {
  128. ClipDebug( x, y );
  129. }
  130. #endif
  131. return Result;
  132. }
  133. static inline void IRAM_ATTR DrawPixel( struct GDS_Device* Device, int x, int y, int Color ) {
  134. if ( IsPixelVisible( Device, x, y ) == true ) {
  135. Device->DrawPixelFast( Device, x, y, Color );
  136. }
  137. }
  138. #endif