SSD1306.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int CurrentRow = -1, FirstCol = -1, LastCol = -1;
  36. // by row, find first and last columns that have been updated
  37. for (int r = 0; r < rows; r++) {
  38. uint8_t first = 0, last;
  39. for (int c = 0; c < width; c++) {
  40. if (*iptr != *optr) {
  41. if (!first) first = c + 1;
  42. last = c ;
  43. }
  44. *optr++ = *iptr++;
  45. }
  46. // now update the display by "byte rows"
  47. if (first--) {
  48. // only set column when useful, saves a fair bit of CPU
  49. if (first > FirstCol && first <= FirstCol + 4 && last < LastCol && last >= LastCol - 4) {
  50. first = FirstCol;
  51. last = LastCol;
  52. } else {
  53. SetColumnAddress( Device, first, last );
  54. FirstCol = first;
  55. LastCol = last;
  56. }
  57. // Set row only when needed, otherwise let auto-increment work
  58. if (r != CurrentRow) SetPageAddress( Device, r, Device->Height / 8 - 1 );
  59. CurrentRow = r + 1;
  60. // actual write
  61. Device->WriteData( Device, Device->Shadowbuffer + r*width + first, last - first + 1);
  62. }
  63. }
  64. #else
  65. // automatic counter and end Page/Column
  66. SetColumnAddress( Device, 0, Device->Width - 1);
  67. SetPageAddress( Device, 0, Device->Height / 8 - 1);
  68. Device->WriteData( Device, Device->Framebuffer, Device->FramebufferSize );
  69. #endif
  70. }
  71. static void SetHFlip( struct GDS_Device* Device, bool On ) { Device->WriteCommand( Device, On ? 0xA1 : 0xA0 ); }
  72. static void SetVFlip( struct GDS_Device *Device, bool On ) { Device->WriteCommand( Device, On ? 0xC8 : 0xC0 ); }
  73. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  74. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  75. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  76. Device->WriteCommand( Device, 0x81 );
  77. Device->WriteCommand( Device, Contrast );
  78. }
  79. static bool Init( struct GDS_Device* Device ) {
  80. Device->FramebufferSize = ( Device->Width * Device->Height ) / 8;
  81. // benchmarks showed little gain to have SPI memory already in IRAL vs letting driver copy
  82. #ifdef SHADOW_BUFFER
  83. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  84. NullCheck( Device->Framebuffer, return false );
  85. #ifdef USE_IRAM
  86. if (Device->IF == IF_SPI) Device->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  87. else
  88. #endif
  89. Device->Shadowbuffer = malloc( Device->FramebufferSize );
  90. NullCheck( Device->Shadowbuffer, return false );
  91. memset(Device->Shadowbuffer, 0xFF, Device->FramebufferSize);
  92. #else // not SHADOW_BUFFER
  93. #ifdef USE_IRAM
  94. // benchmarks showed little gain to have SPI memory already in IRAL vs letting driver copy
  95. if (Device->IF == IF_SPI) Device->Framebuffer = heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  96. else
  97. #endif
  98. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  99. #endif
  100. // need to be off and disable display RAM
  101. Device->DisplayOff( Device );
  102. Device->WriteCommand( Device, 0xA5 );
  103. // charge pump regulator, do direct init
  104. Device->WriteCommand( Device, 0x8D );
  105. Device->WriteCommand( Device, 0x14 );
  106. // COM pins HW config (alternative:EN if 64, DIS if 32, remap:DIS) - some display might need something different
  107. Device->WriteCommand( Device, 0xDA );
  108. Device->WriteCommand( Device, ((Device->Height == 64 ? 1 : 0) << 4) | (0 < 5) );
  109. // MUX Ratio
  110. Device->WriteCommand( Device, 0xA8 );
  111. Device->WriteCommand( Device, Device->Height - 1);
  112. // Display Offset
  113. Device->WriteCommand( Device, 0xD3 );
  114. Device->WriteCommand( Device, 0 );
  115. // Display Start Line
  116. Device->WriteCommand( Device, 0x40 + 0x00 );
  117. Device->SetContrast( Device, 0x7F );
  118. // set flip modes
  119. Device->SetVFlip( Device, false );
  120. Device->SetHFlip( Device, false );
  121. // no Display Inversion
  122. Device->WriteCommand( Device, 0xA6 );
  123. // set Clocks
  124. Device->WriteCommand( Device, 0xD5 );
  125. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  126. // set Adressing Mode Horizontal
  127. Device->WriteCommand( Device, 0x20 );
  128. Device->WriteCommand( Device, 0 );
  129. // gone with the wind
  130. Device->WriteCommand( Device, 0xA4 );
  131. Device->DisplayOn( Device );
  132. Device->Update( Device );
  133. return true;
  134. }
  135. static const struct GDS_Device SSD1306 = {
  136. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  137. .SetVFlip = SetVFlip, .SetHFlip = SetHFlip,
  138. .Update = Update, .Init = Init,
  139. //.DrawPixelFast = GDS_DrawPixelFast,
  140. //.ClearWindow = ClearWindow,
  141. };
  142. struct GDS_Device* SSD1306_Detect(char *Driver, struct GDS_Device* Device) {
  143. if (!strcasestr(Driver, "SSD1306")) return NULL;
  144. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  145. *Device = SSD1306;
  146. Device->Depth = 1;
  147. ESP_LOGI(TAG, "SSD1306 driver");
  148. return Device;
  149. }