SSD1306.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, struct GDS_Layout *Layout ) {
  76. Device->WriteCommand( Device, Layout->HFlip ? 0xA1 : 0xA0 );
  77. Device->WriteCommand( Device, Layout->VFlip ? 0xC8 : 0xC0 );
  78. Device->WriteCommand( Device, Layout->Invert ? 0xA7 : 0xA6 );
  79. }
  80. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  81. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  82. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  83. Device->WriteCommand( Device, 0x81 );
  84. Device->WriteCommand( Device, Contrast );
  85. }
  86. static bool Init( struct GDS_Device* Device ) {
  87. #ifdef SHADOW_BUFFER
  88. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  89. #ifdef USE_IRAM
  90. if (Device->IF == GDS_IF_SPI) Private->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  91. else
  92. #endif
  93. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  94. NullCheck( Private->Shadowbuffer, return false );
  95. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  96. #endif
  97. // need to be off and disable display RAM
  98. Device->DisplayOff( Device );
  99. Device->WriteCommand( Device, 0xA5 );
  100. // charge pump regulator, do direct init
  101. Device->WriteCommand( Device, 0x8D );
  102. Device->WriteCommand( Device, 0x14 );
  103. // COM pins HW config (alternative:EN if 64, DIS if 32, remap:DIS) - some display might need something different
  104. Device->WriteCommand( Device, 0xDA );
  105. Device->WriteCommand( Device, ((Device->Height == 64 ? 1 : 0) << 4) | (0 < 5) );
  106. // MUX Ratio
  107. Device->WriteCommand( Device, 0xA8 );
  108. Device->WriteCommand( Device, Device->Height - 1);
  109. // Display Offset
  110. Device->WriteCommand( Device, 0xD3 );
  111. Device->WriteCommand( Device, 0 );
  112. // Display Start Line
  113. Device->WriteCommand( Device, 0x40 + 0x00 );
  114. Device->SetContrast( Device, 0x7F );
  115. // set flip modes
  116. struct GDS_Layout Layout = { };
  117. Device->SetLayout( Device, &Layout );
  118. // no Display Inversion
  119. Device->WriteCommand( Device, 0xA6 );
  120. // set Clocks
  121. Device->WriteCommand( Device, 0xD5 );
  122. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  123. // set Adressing Mode Horizontal
  124. Device->WriteCommand( Device, 0x20 );
  125. Device->WriteCommand( Device, 0 );
  126. // gone with the wind
  127. Device->WriteCommand( Device, 0xA4 );
  128. Device->DisplayOn( Device );
  129. Device->Update( Device );
  130. return true;
  131. }
  132. static const struct GDS_Device SSD1306 = {
  133. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  134. .SetLayout = SetLayout,
  135. .Update = Update, .Init = Init,
  136. .Mode = GDS_MONO, .Depth = 1,
  137. #if !defined SHADOW_BUFFER && defined USE_IRAM
  138. .Alloc = GDS_ALLOC_IRAM_SPI,
  139. #endif
  140. };
  141. struct GDS_Device* SSD1306_Detect(char *Driver, struct GDS_Device* Device) {
  142. if (!strcasestr(Driver, "SSD1306")) return NULL;
  143. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  144. *Device = SSD1306;
  145. ESP_LOGI(TAG, "SSD1306 driver");
  146. return Device;
  147. }