gds.c 8.3 KB

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