SSD1306.c 5.3 KB

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