gds.c 7.1 KB

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