gds.c 5.2 KB

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