SSD1306.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. struct PrivateSpace {
  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. Device->WriteCommand( Device, 0x21 );
  25. Device->WriteCommand( Device, Start );
  26. Device->WriteCommand( Device, End );
  27. }
  28. static void SetPageAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  29. Device->WriteCommand( Device, 0x22 );
  30. Device->WriteCommand( Device, Start );
  31. Device->WriteCommand( Device, End );
  32. }
  33. static void Update( struct GDS_Device* Device ) {
  34. #ifdef SHADOW_BUFFER
  35. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  36. // not sure the compiler does not have to redo all calculation in for loops, so local it is
  37. int width = Device->Width, pages = Device->Height / 8;
  38. uint8_t *optr = Private->Shadowbuffer, *iptr = Device->Framebuffer;
  39. int CurrentPage = -1, FirstCol = -1, LastCol = -1;
  40. // by row, find first and last columns that have been updated
  41. for (int p = 0; p < pages; p++) {
  42. uint8_t first = 0, last;
  43. for (int c = 0; c < width; c++) {
  44. if (*iptr != *optr) {
  45. if (!first) first = c + 1;
  46. last = c ;
  47. }
  48. *optr++ = *iptr++;
  49. }
  50. // now update the display by "byte rows"
  51. if (first--) {
  52. // only set column when useful, saves a fair bit of CPU
  53. if (first > FirstCol && first <= FirstCol + 4 && last < LastCol && last >= LastCol - 4) {
  54. first = FirstCol;
  55. last = LastCol;
  56. } else {
  57. SetColumnAddress( Device, first, last );
  58. FirstCol = first;
  59. LastCol = last;
  60. }
  61. // Set row only when needed, otherwise let auto-increment work
  62. if (p != CurrentPage) SetPageAddress( Device, p, Device->Height / 8 - 1 );
  63. CurrentPage = p + 1;
  64. // actual write
  65. Device->WriteData( Device, Private->Shadowbuffer + p*width + first, last - first + 1);
  66. }
  67. }
  68. #else
  69. // automatic counter and end Page/Column (we assume Height % 8 == 0)
  70. SetColumnAddress( Device, 0, Device->Width - 1);
  71. SetPageAddress( Device, 0, Device->Height / 8 - 1);
  72. Device->WriteData( Device, Device->Framebuffer, Device->FramebufferSize );
  73. #endif
  74. }
  75. static void SetLayout( struct GDS_Device* Device, bool HFlip, bool VFlip, bool Rotate ) {
  76. Device->WriteCommand( Device, HFlip ? 0xA1 : 0xA0 );
  77. Device->WriteCommand( Device, VFlip ? 0xC8 : 0xC0 );
  78. }
  79. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  80. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  81. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  82. Device->WriteCommand( Device, 0x81 );
  83. Device->WriteCommand( Device, Contrast );
  84. }
  85. static bool Init( struct GDS_Device* Device ) {
  86. #ifdef SHADOW_BUFFER
  87. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  88. #ifdef USE_IRAM
  89. if (Device->IF == GDS_IF_SPI) Private->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  90. else
  91. #endif
  92. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  93. NullCheck( Private->Shadowbuffer, return false );
  94. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  95. #endif
  96. // need to be off and disable display RAM
  97. Device->DisplayOff( Device );
  98. Device->WriteCommand( Device, 0xA5 );
  99. // charge pump regulator, do direct init
  100. Device->WriteCommand( Device, 0x8D );
  101. Device->WriteCommand( Device, 0x14 );
  102. // COM pins HW config (alternative:EN if 64, DIS if 32, remap:DIS) - some display might need something different
  103. Device->WriteCommand( Device, 0xDA );
  104. Device->WriteCommand( Device, ((Device->Height == 64 ? 1 : 0) << 4) | (0 < 5) );
  105. // MUX Ratio
  106. Device->WriteCommand( Device, 0xA8 );
  107. Device->WriteCommand( Device, Device->Height - 1);
  108. // Display Offset
  109. Device->WriteCommand( Device, 0xD3 );
  110. Device->WriteCommand( Device, 0 );
  111. // Display Start Line
  112. Device->WriteCommand( Device, 0x40 + 0x00 );
  113. Device->SetContrast( Device, 0x7F );
  114. // set flip modes
  115. Device->SetLayout( Device, false, false, false);
  116. // no Display Inversion
  117. Device->WriteCommand( Device, 0xA6 );
  118. // set Clocks
  119. Device->WriteCommand( Device, 0xD5 );
  120. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  121. // set Adressing Mode Horizontal
  122. Device->WriteCommand( Device, 0x20 );
  123. Device->WriteCommand( Device, 0 );
  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 SSD1306 = {
  131. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  132. .SetLayout = SetLayout,
  133. .Update = Update, .Init = Init,
  134. .Mode = GDS_MONO, .Depth = 1,
  135. #if !defined SHADOW_BUFFER && defined USE_IRAM
  136. .Alloc = GDS_ALLOC_IRAM_SPI,
  137. #endif
  138. };
  139. struct GDS_Device* SSD1306_Detect(char *Driver, struct GDS_Device* Device) {
  140. if (!strcasestr(Driver, "SSD1306")) return NULL;
  141. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  142. *Device = SSD1306;
  143. ESP_LOGI(TAG, "SSD1306 driver");
  144. return Device;
  145. }