gds.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. for (int y = y1; y <= y2; y++) {
  53. for (int x = x1; x <= x2; x++) {
  54. GDS_DrawPixelFast( Device, x, y, Color);
  55. }
  56. }
  57. return;
  58. // driver can provide own optimized clear window
  59. if (Device->ClearWindow) {
  60. Device->ClearWindow( Device, x1, y1, x2, y2, Color );
  61. } else if (Device->Depth == 1) {
  62. // single shot if we erase all screen
  63. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  64. memset( Device->Framebuffer, Color == GDS_COLOR_BLACK ? 0 : 0xff, Device->FramebufferSize );
  65. } else {
  66. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  67. uint8_t Width = Device->Width;
  68. // try to do byte processing as much as possible
  69. int c;
  70. for (c = x1; c <= x2; c++) {
  71. int r = y1;
  72. while (r & 0x07 && r <= y2) GDS_DrawPixelFast( Device, c, r++, Color);
  73. //for (; (r >> 3) < (y2 >> 3); r++) Device->Framebuffer[(r >> 3) * Width + c] = _Color;
  74. memset(Device->Framebuffer + (r >> 3) * Width + c, _Color, (y2 - r) >> 3);
  75. while (r <= y2) GDS_DrawPixelFast( Device, c, r++, Color);
  76. }
  77. }
  78. } if (Device->Depth == 4) {
  79. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  80. // we assume color is 0..15
  81. memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  82. } else {
  83. uint8_t _Color = Color | (Color << 4);
  84. uint8_t Width = Device->Width;
  85. // try to do byte processing as much as possible
  86. int r;
  87. for (r = y1; r <= y2; r++) {
  88. int c = x1;
  89. if (c & 0x01) GDS_DrawPixelFast( Device, c++, r, Color);
  90. //for (; (c >> 1) < (x2 >> 1); c++) Device->Framebuffer[(r * Width + c) >> 1] = _Color;
  91. memset(Device->Framebuffer + ((r * Width +c) >> 1), _Color, (x2 - c) >> 1);
  92. if (c < x2) GDS_DrawPixelFast( Device, c, r, Color);
  93. }
  94. }
  95. } else {
  96. int y;
  97. for (y = y1; y <= y2; y++) {
  98. int x;
  99. for (x = x1; x <= x2; x++) {
  100. GDS_DrawPixelFast( Device, x, y, Color);
  101. }
  102. }
  103. }
  104. // make sure diplay will do update
  105. Device->Dirty = true;
  106. }
  107. void GDS_Update( struct GDS_Device* Device ) {
  108. if (Device->Dirty) Device->Update( Device );
  109. Device->Dirty = false;
  110. }
  111. bool GDS_Reset( struct GDS_Device* Device ) {
  112. if ( Device->RSTPin >= 0 ) {
  113. gpio_set_level( Device->RSTPin, 0 );
  114. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  115. gpio_set_level( Device->RSTPin, 1 );
  116. }
  117. return true;
  118. }
  119. void GDS_SetContrast( struct GDS_Device* Device, uint8_t Contrast ) { Device->SetContrast( Device, Contrast); }
  120. void GDS_SetHFlip( struct GDS_Device* Device, bool On ) { Device->SetHFlip( Device, On ); }
  121. void GDS_SetVFlip( struct GDS_Device* Device, bool On ) { Device->SetVFlip( Device, On ); }
  122. int GDS_GetWidth( struct GDS_Device* Device ) { return Device->Width; }
  123. int GDS_GetHeight( struct GDS_Device* Device ) { return Device->Height; }
  124. void GDS_DisplayOn( struct GDS_Device* Device ) { Device->DisplayOn( Device ); }
  125. void GDS_DisplayOff( struct GDS_Device* Device ) { Device->DisplayOff( Device ); }