SH1106.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 USE_IRAM
  18. static char TAG[] = "SH1106";
  19. struct SH1106_Private {
  20. uint8_t *Shadowbuffer;
  21. };
  22. // Functions are not declared to minimize # of lines
  23. static void SetColumnAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  24. // well, unfortunately this driver is 132 colums but most displays are 128...
  25. if (Device->Width != 132) Start += 2;
  26. Device->WriteCommand( Device, 0x10 | (Start >> 4) );
  27. Device->WriteCommand( Device, 0x00 | (Start & 0x0f) );
  28. }
  29. static void SetPageAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  30. Device->WriteCommand( Device, 0xB0 | Start );
  31. }
  32. static void Update( struct GDS_Device* Device ) {
  33. #ifdef SHADOW_BUFFER
  34. struct SH1106_Private *Private = (struct SH1106_Private*) Device->Private;
  35. // not sure the compiler does not have to redo all calculation in for loops, so local it is
  36. int width = Device->Width, rows = Device->Height / 8;
  37. uint8_t *optr = Private->Shadowbuffer, *iptr = Device->Framebuffer;
  38. // by row, find first and last columns that have been updated
  39. for (int r = 0; r < rows; r++) {
  40. uint8_t first = 0, last;
  41. for (int c = 0; c < width; c++) {
  42. if (*iptr != *optr) {
  43. if (!first) first = c + 1;
  44. last = c ;
  45. }
  46. *optr++ = *iptr++;
  47. }
  48. // now update the display by "byte rows"
  49. if (first--) {
  50. SetColumnAddress( Device, first, last );
  51. SetPageAddress( Device, r, r);
  52. Device->WriteData( Device, Private->Shadowbuffer + r*width + first, last - first + 1);
  53. }
  54. }
  55. #else
  56. // SH1106 requires a page-by-page update and has no end Page/Column
  57. for (int i = 0; i < Device->Height / 8 ; i++) {
  58. SH1106_SetPageAddress( Device, i, 0);
  59. SH1106_SetColumnAddress( Device, 0, 0);
  60. SH1106_WriteData( Device, Device->Framebuffer + i*Device->Width, Device->Width );
  61. }
  62. #endif
  63. }
  64. static void SetHFlip( struct GDS_Device* Device, bool On ) { Device->WriteCommand( Device, On ? 0xA1 : 0xA0 ); }
  65. static void SetVFlip( struct GDS_Device *Device, bool On ) { Device->WriteCommand( Device, On ? 0xC8 : 0xC0 ); }
  66. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  67. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  68. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  69. Device->WriteCommand( Device, 0x81 );
  70. Device->WriteCommand( Device, Contrast );
  71. }
  72. static bool Init( struct GDS_Device* Device ) {
  73. Device->FramebufferSize = ( Device->Width * Device->Height ) / 8;
  74. // benchmarks showed little gain to have SPI memory already in IRAL vs letting driver copy
  75. #ifdef SHADOW_BUFFER
  76. struct SH1106_Private *Private = (struct SH1106_Private*) Device->Private;
  77. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  78. NullCheck( Device->Framebuffer, return false );
  79. #ifdef USE_IRAM
  80. if (Device->IF == IF_SPI) Private->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  81. else
  82. #endif
  83. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  84. NullCheck( Private->Shadowbuffer, return false );
  85. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  86. #else // not SHADOW_BUFFER
  87. #ifdef USE_IRAM
  88. // benchmarks showed little gain to have SPI memory already in IRAL vs letting driver copy
  89. if (Device->IF == IF_SPI) Device->Framebuffer = heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  90. else
  91. #endif
  92. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  93. #endif
  94. // need to be off and disable display RAM
  95. Device->DisplayOff( Device );
  96. Device->WriteCommand( Device, 0xA5 );
  97. // charge pump regulator, do direct init
  98. Device->WriteCommand( Device, 0xAD );
  99. Device->WriteCommand( Device, 0x8B );
  100. // COM pins HW config (alternative:EN) - some display might need something difference
  101. Device->WriteCommand( Device, 0xDA );
  102. Device->WriteCommand( Device, 1 << 4);
  103. // MUX Ratio
  104. Device->WriteCommand( Device, 0xA8 );
  105. Device->WriteCommand( Device, Device->Height - 1);
  106. // Display Offset
  107. Device->WriteCommand( Device, 0xD3 );
  108. Device->WriteCommand( Device, 0 );
  109. // Display Start Line
  110. Device->WriteCommand( Device, 0x40 + 0x00 );
  111. Device->SetContrast( Device, 0x7F );
  112. // set flip modes
  113. Device->SetVFlip( Device, false );
  114. Device->SetHFlip( Device, false );
  115. // no Display Inversion
  116. Device->WriteCommand( Device, 0xA6 );
  117. // set Clocks
  118. Device->WriteCommand( Device, 0xD5 );
  119. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  120. // gone with the wind
  121. Device->WriteCommand( Device, 0xA4 );
  122. Device->DisplayOn( Device );
  123. Device->Update( Device );
  124. return true;
  125. }
  126. static const struct GDS_Device SH1106 = {
  127. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  128. .SetVFlip = SetVFlip, .SetHFlip = SetHFlip,
  129. .Update = Update, .Init = Init,
  130. //.DrawPixelFast = GDS_DrawPixelFast,
  131. //.ClearWindow = ClearWindow,
  132. };
  133. struct GDS_Device* SH1106_Detect(char *Driver, struct GDS_Device* Device) {
  134. if (!strcasestr(Driver, "SH1106")) return NULL;
  135. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  136. *Device = SH1106;
  137. Device->Depth = 1;
  138. ESP_LOGI(TAG, "SH1106 driver");
  139. return Device;
  140. }