gds.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. memset( Device->Framebuffer, Color, Device->FramebufferSize );
  47. }
  48. void GDS_ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  49. // cheap optimization on boundaries
  50. if (x1 == 0 && x2 == Device->Width - 1 && y1 % 8 == 0 && (y2 + 1) % 8 == 0) {
  51. memset( Device->Framebuffer + (y1 / 8) * Device->Width, 0, (y2 - y1 + 1) / 8 * Device->Width );
  52. } else {
  53. for (int y = y1; y <= y2; y++) {
  54. for (int x = x1; x <= x2; x++) {
  55. Device->DrawPixelFast( Device, x, y, Color);
  56. }
  57. }
  58. }
  59. }
  60. void GDS_Update( struct GDS_Device* Device ) {
  61. if (Device->Dirty) Device->Update( Device );
  62. Device->Dirty = false;
  63. }
  64. bool GDS_Reset( struct GDS_Device* Device ) {
  65. if ( Device->RSTPin >= 0 ) {
  66. gpio_set_level( Device->RSTPin, 0 );
  67. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  68. gpio_set_level( Device->RSTPin, 1 );
  69. }
  70. return true;
  71. }
  72. void GDS_SetContrast( struct GDS_Device* Device, uint8_t Contrast ) { Device->SetContrast( Device, Contrast); }
  73. void GDS_SetHFlip( struct GDS_Device* Device, bool On ) { Device->SetHFlip( Device, On ); }
  74. void GDS_SetVFlip( struct GDS_Device* Device, bool On ) { Device->SetVFlip( Device, On ); }
  75. int GDS_GetWidth( struct GDS_Device* Device ) { return Device->Width; }
  76. int GDS_GetHeight( struct GDS_Device* Device ) { return Device->Height; }
  77. void GDS_DisplayOn( struct GDS_Device* Device ) { Device->DisplayOn( Device ); }
  78. void GDS_DisplayOff( struct GDS_Device* Device ) { Device->DisplayOff( Device ); }