gds.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 (Device->Depth == 1) Color = Color == GDS_COLOR_BLACK ? 0 : 0xff;
  47. else if (Device->Depth == 4) Color = Color | (Color << 4);
  48. memset( Device->Framebuffer, Color, Device->FramebufferSize );
  49. Device->Dirty = true;
  50. }
  51. void GDS_ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  52. // -1 means up to width/height
  53. if (x2 < 0) x2 = Device->Width - 1;
  54. if (y2 < 0) y2 = Device->Height - 1;
  55. // driver can provide own optimized clear window
  56. if (Device->ClearWindow) {
  57. Device->ClearWindow( Device, x1, y1, x2, y2, Color );
  58. } else if (Device->Depth == 1) {
  59. // single shot if we erase all screen
  60. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  61. memset( Device->Framebuffer, Color == GDS_COLOR_BLACK ? 0 : 0xff, Device->FramebufferSize );
  62. } else {
  63. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  64. uint8_t Width = Device->Width >> 3;
  65. uint8_t *optr = Device->Framebuffer;
  66. // try to do byte processing as much as possible
  67. for (int r = y1; r <= y2;) {
  68. int c = x1;
  69. // for a row that is not on a boundary, no optimization possible
  70. while (r & 0x07 && r <= y2) {
  71. for (c = x1; c <= x2; c++) GDS_DrawPixelFast( Device, c, r, Color );
  72. r++;
  73. }
  74. // go fast if we have more than 8 lines to write
  75. if (r + 8 <= y2) {
  76. memset(optr + Width * r + x1, _Color, x2 - x1 + 1);
  77. r += 8;
  78. } else while (r <= y2) {
  79. for (c = x1; c <= x2; c++) GDS_DrawPixelFast( Device, c, r, Color );
  80. r++;
  81. }
  82. }
  83. }
  84. } if (Device->Depth == 4) {
  85. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  86. // we assume color is 0..15
  87. memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  88. } else {
  89. uint8_t _Color = Color | (Color << 4);
  90. int Width = Device->Width;
  91. uint8_t *optr = Device->Framebuffer;
  92. // try to do byte processing as much as possible
  93. for (int r = y1; r <= y2; r++) {
  94. int c = x1;
  95. if (c & 0x01) GDS_DrawPixelFast( Device, c++, r, Color);
  96. int chunk = (x2 - c + 1) >> 1;
  97. memset(optr + ((r * Width + c) >> 1), _Color, chunk);
  98. if (c + chunk <= x2) GDS_DrawPixelFast( Device, x2, r, Color);
  99. }
  100. }
  101. } else {
  102. for (int y = y1; y <= y2; y++) {
  103. for (int x = x1; x <= x2; x++) {
  104. GDS_DrawPixelFast( Device, x, y, Color);
  105. }
  106. }
  107. }
  108. // make sure diplay will do update
  109. Device->Dirty = true;
  110. }
  111. void GDS_Update( struct GDS_Device* Device ) {
  112. if (Device->Dirty) Device->Update( Device );
  113. Device->Dirty = false;
  114. }
  115. bool GDS_Reset( struct GDS_Device* Device ) {
  116. if ( Device->RSTPin >= 0 ) {
  117. gpio_set_level( Device->RSTPin, 0 );
  118. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  119. gpio_set_level( Device->RSTPin, 1 );
  120. }
  121. return true;
  122. }
  123. bool GDS_Init( struct GDS_Device* Device ) {
  124. Device->FramebufferSize = (Device->Width * Device->Height) / (8 / Device->Depth);
  125. // allocate FB unless explicitely asked not to
  126. if (!(Device->Alloc & GDS_ALLOC_NONE)) {
  127. if ((Device->Alloc & GDS_ALLOC_IRAM) || ((Device->Alloc & GDS_ALLOC_IRAM_SPI) && Device->IF == GDS_IF_SPI)) {
  128. heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  129. } else {
  130. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  131. }
  132. NullCheck( Device->Framebuffer, return false );
  133. }
  134. bool Res = Device->Init( Device );
  135. if (!Res) free(Device->Framebuffer);
  136. return Res;
  137. }
  138. void GDS_SetContrast( struct GDS_Device* Device, uint8_t Contrast ) { if (Device->SetContrast) Device->SetContrast( Device, Contrast); }
  139. void GDS_SetHFlip( struct GDS_Device* Device, bool On ) { if (Device->SetHFlip) Device->SetHFlip( Device, On ); }
  140. void GDS_SetVFlip( struct GDS_Device* Device, bool On ) { if (Device->SetVFlip) Device->SetVFlip( Device, On ); }
  141. void GDS_SetDirty( struct GDS_Device* Device ) { Device->Dirty = true; }
  142. int GDS_GetWidth( struct GDS_Device* Device ) { return Device->Width; }
  143. int GDS_GetHeight( struct GDS_Device* Device ) { return Device->Height; }
  144. int GDS_GetDepth( struct GDS_Device* Device ) { return Device->Depth; }
  145. void GDS_DisplayOn( struct GDS_Device* Device ) { if (Device->DisplayOn) Device->DisplayOn( Device ); }
  146. void GDS_DisplayOff( struct GDS_Device* Device ) { if (Device->DisplayOff) Device->DisplayOff( Device ); }