SH1122.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <stdio.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include <esp_heap_caps.h>
  13. #include <esp_log.h>
  14. #include "gds.h"
  15. #include "gds_private.h"
  16. #define SHADOW_BUFFER
  17. #define PAGE_BLOCK 1024
  18. #define min(a,b) (((a) < (b)) ? (a) : (b))
  19. static char TAG[] = "SH1122";
  20. struct PrivateSpace {
  21. uint8_t *iRAM, *Shadowbuffer;
  22. uint8_t PageSize;
  23. };
  24. // Functions are not declared to minimize # of lines
  25. static void SetColumnAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  26. Device->WriteCommand( Device, 0x10 | (Start >> 4) );
  27. Device->WriteCommand( Device, 0x00 | (Start & 0x0f) );
  28. }
  29. static void SetRowAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  30. Device->WriteCommand( Device, 0xB0 );
  31. Device->WriteCommand( Device, Start );
  32. }
  33. static void Update( struct GDS_Device* Device ) {
  34. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  35. // RAM is by columns of 4 pixels ...
  36. SetColumnAddress( Device, 0, Device->Width / 4 - 1);
  37. #ifdef SHADOW_BUFFER
  38. uint16_t *optr = (uint16_t*) Private->Shadowbuffer, *iptr = (uint16_t*) Device->Framebuffer;
  39. bool dirty = false;
  40. for (int r = 0, page = 0; r < Device->Height; r++) {
  41. // look for change and update shadow (cheap optimization = width always / by 2)
  42. for (int c = Device->Width / 2 / 2; --c >= 0;) {
  43. if (*optr != *iptr) {
  44. dirty = true;
  45. *optr = *iptr;
  46. }
  47. iptr++; optr++;
  48. }
  49. // one line done, check for page boundary
  50. if (++page == Private->PageSize) {
  51. if (dirty) {
  52. SetRowAddress( Device, r - page + 1, r );
  53. if (Private->iRAM) {
  54. memcpy(Private->iRAM, Private->Shadowbuffer + (r - page + 1) * Device->Width / 2, page * Device->Width / 2 );
  55. Device->WriteData( Device, Private->iRAM, Device->Width * page / 2 );
  56. } else {
  57. Device->WriteData( Device, Private->Shadowbuffer + (r - page + 1) * Device->Width / 2, page * Device->Width / 2);
  58. }
  59. dirty = false;
  60. }
  61. page = 0;
  62. }
  63. }
  64. #else
  65. SetRowAddress( Device, 0, Device->Height - 1 );
  66. for (int r = 0; r < Device->Height; r += Private->PageSize) {
  67. if (Private->iRAM) {
  68. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width / 2, Private->PageSize * Device->Width / 2 );
  69. Device->WriteData( Device, Private->iRAM, Private->PageSize * Device->Width / 2 );
  70. } else {
  71. Device->WriteData( Device, Device->Framebuffer + r * Device->Width / 2, Private->PageSize * Device->Width / 2 );
  72. }
  73. }
  74. #endif
  75. }
  76. static void SetLayout( struct GDS_Device* Device, struct GDS_Layout *Layout ) {
  77. if (Layout->HFlip) {
  78. Device->WriteCommand( Device, 0x40 + 0x20 );
  79. Device->WriteCommand( Device, 0xA1 );
  80. } else {
  81. Device->WriteCommand( Device, 0x40 + 0x00 );
  82. Device->WriteCommand( Device, 0xA0 );
  83. }
  84. Device->WriteCommand( Device, Layout->VFlip ? 0xC8 : 0xC0 );
  85. Device->WriteCommand( Device, Layout->Invert ? 0xA7 : 0xA6 );
  86. }
  87. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  88. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  89. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  90. Device->WriteCommand( Device, 0x81 );
  91. Device->WriteCommand( Device, Contrast );
  92. }
  93. static bool Init( struct GDS_Device* Device ) {
  94. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  95. // find a page size that is not too small is an integer of height
  96. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width / 2));
  97. while (Private->PageSize && Device->Height != (Device->Height / Private->PageSize) * Private->PageSize) Private->PageSize--;
  98. #ifdef SHADOW_BUFFER
  99. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  100. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  101. #endif
  102. // only use iRAM for SPI
  103. if (Device->IF == GDS_IF_SPI) {
  104. Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  105. }
  106. ESP_LOGI(TAG, "SH1122 page %u, iRAM %p", Private->PageSize, Private->iRAM);
  107. // need to be off and disable display RAM
  108. Device->DisplayOff( Device );
  109. Device->WriteCommand( Device, 0xA5 );
  110. // Display Offset
  111. Device->WriteCommand( Device, 0xD3 );
  112. Device->WriteCommand( Device, 0 );
  113. // set flip modes
  114. struct GDS_Layout Layout = { };
  115. Device->SetLayout( Device, &Layout );
  116. // set Clocks => check value
  117. Device->WriteCommand( Device, 0xD5 );
  118. Device->WriteCommand( Device, ( 0x04 << 4 ) | 0x00 );
  119. // MUX Ratio => fixed
  120. Device->WriteCommand( Device, 0xA8 );
  121. Device->WriteCommand( Device, Device->Height - 1);
  122. // no Display Inversion
  123. Device->WriteCommand( Device, 0xA6 );
  124. // gone with the wind
  125. Device->WriteCommand( Device, 0xA4 );
  126. Device->DisplayOn( Device );
  127. Device->Update( Device );
  128. return true;
  129. }
  130. static const struct GDS_Device SH1122 = {
  131. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  132. .SetLayout = SetLayout,
  133. .Update = Update, .Init = Init,
  134. .Mode = GDS_GRAYSCALE, .Depth = 4,
  135. .HighNibble = true,
  136. };
  137. struct GDS_Device* SH1122_Detect(char *Driver, struct GDS_Device* Device) {
  138. if (!strcasestr(Driver, "SH1122")) return NULL;
  139. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  140. *Device = SH1122;
  141. return Device;
  142. }