gds.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. * 2020 Philippe G.
  4. *
  5. * This software is released under the MIT License.
  6. * https://opensource.org/licenses/MIT
  7. */
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdint.h>
  11. #include <math.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "driver/gpio.h"
  15. #include "driver/ledc.h"
  16. #include "esp_log.h"
  17. #include "gds.h"
  18. #include "gds_private.h"
  19. #ifdef CONFIG_IDF_TARGET_ESP32S3
  20. #define LEDC_HIGH_SPEED_MODE LEDC_SPEED_MODE_MAX
  21. #endif
  22. static struct GDS_Device Display;
  23. static struct GDS_BacklightPWM PWMConfig;
  24. static char TAG[] = "gds";
  25. struct GDS_Device* GDS_AutoDetect( char *Driver, GDS_DetectFunc* DetectFunc[], struct GDS_BacklightPWM* PWM ) {
  26. if (!Driver) return NULL;
  27. if (PWM) PWMConfig = *PWM;
  28. for (int i = 0; DetectFunc[i]; i++) {
  29. if (DetectFunc[i](Driver, &Display)) {
  30. if (PWM && PWM->Init) {
  31. ledc_timer_config_t PWMTimer = {
  32. .duty_resolution = LEDC_TIMER_13_BIT,
  33. .freq_hz = 5000,
  34. .speed_mode = LEDC_HIGH_SPEED_MODE,
  35. .timer_num = PWMConfig.Timer,
  36. };
  37. ledc_timer_config(&PWMTimer);
  38. }
  39. ESP_LOGD(TAG, "Detected driver %p with PWM %d", &Display, PWM ? PWM->Init : 0);
  40. return &Display;
  41. }
  42. }
  43. return NULL;
  44. }
  45. void GDS_ClearExt(struct GDS_Device* Device, bool full, ...) {
  46. bool commit = true;
  47. if (full) {
  48. GDS_Clear( Device, GDS_COLOR_BLACK );
  49. } else {
  50. va_list args;
  51. va_start(args, full);
  52. commit = va_arg(args, int);
  53. int x1 = va_arg(args, int), y1 = va_arg(args, int), x2 = va_arg(args, int), y2 = va_arg(args, int);
  54. if (x2 < 0) x2 = Device->Width - 1;
  55. if (y2 < 0) y2 = Device->Height - 1;
  56. GDS_ClearWindow( Device, x1, y1, x2, y2, GDS_COLOR_BLACK );
  57. va_end(args);
  58. }
  59. Device->Dirty = true;
  60. if (commit) GDS_Update(Device);
  61. }
  62. void GDS_Clear( struct GDS_Device* Device, int Color ) {
  63. if (Color == GDS_COLOR_BLACK) memset( Device->Framebuffer, 0, Device->FramebufferSize );
  64. else if (Device->Depth == 1) memset( Device->Framebuffer, 0xff, Device->FramebufferSize );
  65. else if (Device->Depth == 4) memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  66. else if (Device->Depth == 8) memset( Device->Framebuffer, Color, Device->FramebufferSize );
  67. else GDS_ClearWindow(Device, 0, 0, -1, -1, Color);
  68. Device->Dirty = true;
  69. }
  70. #define CLEAR_WINDOW(x1,y1,x2,y2,F,W,C,T,N) \
  71. for (int y = y1; y <= y2; y++) { \
  72. T *Ptr = (T*) F + (y * W + x1)*N; \
  73. for (int c = (x2 - x1)*N; c-- >= 0; *Ptr++ = C); \
  74. }
  75. void GDS_ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  76. // -1 means up to width/height
  77. if (x2 < 0) x2 = Device->Width - 1;
  78. if (y2 < 0) y2 = Device->Height - 1;
  79. // driver can provide own optimized clear window
  80. if (Device->ClearWindow) {
  81. Device->ClearWindow( Device, x1, y1, x2, y2, Color );
  82. } else if (Device->Depth == 1) {
  83. // single shot if we erase all screen
  84. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  85. memset( Device->Framebuffer, Color == GDS_COLOR_BLACK ? 0 : 0xff, Device->FramebufferSize );
  86. } else {
  87. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  88. uint8_t Width = Device->Width >> 3;
  89. uint8_t *optr = Device->Framebuffer;
  90. // try to do byte processing as much as possible
  91. for (int r = y1; r <= y2;) {
  92. int c = x1;
  93. // for a row that is not on a boundary, no optimization possible
  94. while (r & 0x07 && r <= y2) {
  95. for (c = x1; c <= x2; c++) DrawPixelFast( Device, c, r, Color );
  96. r++;
  97. }
  98. // go fast if we have more than 8 lines to write
  99. if (r + 8 <= y2) {
  100. memset(optr + Width * r + x1, _Color, x2 - x1 + 1);
  101. r += 8;
  102. } else while (r <= y2) {
  103. for (c = x1; c <= x2; c++) DrawPixelFast( Device, c, r, Color );
  104. r++;
  105. }
  106. }
  107. }
  108. } if (Device->Depth == 4) {
  109. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  110. // we assume color is 0..15
  111. memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  112. } else {
  113. uint8_t _Color = Color | (Color << 4);
  114. int Width = Device->Width;
  115. uint8_t *optr = Device->Framebuffer;
  116. // try to do byte processing as much as possible
  117. for (int r = y1; r <= y2; r++) {
  118. int c = x1;
  119. if (c & 0x01) DrawPixelFast( Device, c++, r, Color);
  120. int chunk = (x2 - c + 1) >> 1;
  121. memset(optr + ((r * Width + c) >> 1), _Color, chunk);
  122. if (c + chunk <= x2) DrawPixelFast( Device, x2, r, Color);
  123. }
  124. }
  125. } else if (Device->Depth == 8) {
  126. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint8_t,1);
  127. } else if (Device->Depth == 16) {
  128. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint16_t,1);
  129. } else if (Device->Depth == 24) {
  130. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint8_t,3);
  131. } else {
  132. for (int y = y1; y <= y2; y++) {
  133. for (int x = x1; x <= x2; x++) {
  134. DrawPixelFast( Device, x, y, Color);
  135. }
  136. }
  137. }
  138. // make sure diplay will do update
  139. Device->Dirty = true;
  140. }
  141. void GDS_Update( struct GDS_Device* Device ) {
  142. if (Device->Dirty) Device->Update( Device );
  143. Device->Dirty = false;
  144. }
  145. bool GDS_Reset( struct GDS_Device* Device ) {
  146. if ( Device->RSTPin >= 0 ) {
  147. gpio_set_level( Device->RSTPin, 0 );
  148. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  149. gpio_set_level( Device->RSTPin, 1 );
  150. }
  151. return true;
  152. }
  153. bool GDS_Init( struct GDS_Device* Device ) {
  154. if (Device->Depth > 8) Device->FramebufferSize = Device->Width * Device->Height * ((8 + Device->Depth - 1) / 8);
  155. else Device->FramebufferSize = (Device->Width * Device->Height) / (8 / Device->Depth);
  156. // allocate FB unless explicitely asked not to
  157. if (!(Device->Alloc & GDS_ALLOC_NONE)) {
  158. if ((Device->Alloc & GDS_ALLOC_IRAM) || ((Device->Alloc & GDS_ALLOC_IRAM_SPI) && Device->IF == GDS_IF_SPI)) {
  159. Device->Framebuffer = heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  160. } else {
  161. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  162. }
  163. NullCheck( Device->Framebuffer, return false );
  164. }
  165. if (Device->Backlight.Pin >= 0) {
  166. Device->Backlight.Channel = PWMConfig.Channel++;
  167. Device->Backlight.PWM = PWMConfig.Max - 1;
  168. ledc_channel_config_t PWMChannel = {
  169. .channel = Device->Backlight.Channel,
  170. .duty = Device->Backlight.PWM,
  171. .gpio_num = Device->Backlight.Pin,
  172. .speed_mode = LEDC_HIGH_SPEED_MODE,
  173. .hpoint = 0,
  174. .timer_sel = PWMConfig.Timer,
  175. };
  176. ledc_channel_config(&PWMChannel);
  177. }
  178. bool Res = Device->Init( Device );
  179. if (!Res && Device->Framebuffer) free(Device->Framebuffer);
  180. return Res;
  181. }
  182. int GDS_GrayMap( struct GDS_Device* Device, uint8_t Level) {
  183. switch(Device->Mode) {
  184. case GDS_MONO: return Level;
  185. case GDS_GRAYSCALE: return Level >> (8 - Device->Depth);
  186. case GDS_RGB332:
  187. Level >>= 5;
  188. return (Level << 6) | (Level << 3) | (Level >> 1);
  189. case GDS_RGB444:
  190. Level >>= 4;
  191. return (Level << 8) | (Level << 4) | Level;
  192. case GDS_RGB555:
  193. Level >>= 3;
  194. return (Level << 10) | (Level << 5) | Level;
  195. case GDS_RGB565:
  196. Level >>= 2;
  197. return ((Level & ~0x01) << 10) | (Level << 5) | (Level >> 1);
  198. case GDS_RGB666:
  199. Level >>= 2;
  200. return (Level << 12) | (Level << 6) | Level;
  201. case GDS_RGB888:
  202. return (Level << 16) | (Level << 8) | Level;
  203. }
  204. return -1;
  205. }
  206. void GDS_SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  207. if (Device->SetContrast) Device->SetContrast( Device, Contrast );
  208. else if (Device->Backlight.Pin >= 0) {
  209. Device->Backlight.PWM = PWMConfig.Max * powf(Contrast / 255.0, 3);
  210. ledc_set_duty( LEDC_HIGH_SPEED_MODE, Device->Backlight.Channel, Device->Backlight.PWM );
  211. ledc_update_duty( LEDC_HIGH_SPEED_MODE, Device->Backlight.Channel );
  212. }
  213. }
  214. void GDS_SetLayout( struct GDS_Device* Device, struct GDS_Layout *Layout ) { if (Device->SetLayout) Device->SetLayout( Device, Layout ); }
  215. void GDS_SetDirty( struct GDS_Device* Device ) { Device->Dirty = true; }
  216. int GDS_GetWidth( struct GDS_Device* Device ) { return Device ? Device->Width : 0; }
  217. void GDS_SetTextWidth( struct GDS_Device* Device, int TextWidth ) { Device->TextWidth = Device && TextWidth && TextWidth < Device->Width ? TextWidth : Device->Width; }
  218. int GDS_GetHeight( struct GDS_Device* Device ) { return Device ? Device->Height : 0; }
  219. int GDS_GetDepth( struct GDS_Device* Device ) { return Device ? Device->Depth : 0; }
  220. int GDS_GetMode( struct GDS_Device* Device ) { return Device ? Device->Mode : 0; }
  221. void GDS_DisplayOn( struct GDS_Device* Device ) { if (Device->DisplayOn) Device->DisplayOn( Device ); }
  222. void GDS_DisplayOff( struct GDS_Device* Device ) { if (Device->DisplayOff) Device->DisplayOff( Device ); }