gds.c 8.6 KB

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