SH1106.c 4.7 KB

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