SSD132x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #define PAGE_BLOCK 1024
  19. #define min(a,b) (((a) < (b)) ? (a) : (b))
  20. static char TAG[] = "SSD132x";
  21. enum { SSD1326, SSD1327 };
  22. struct SSD132x_Private {
  23. uint8_t *iRAM;
  24. uint8_t ReMap, PageSize;
  25. uint8_t Model;
  26. };
  27. static const unsigned char BitReverseTable256[] =
  28. {
  29. 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  30. 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  31. 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  32. 0x0C, 0x8C, 0x4C, 0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 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  44. 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  45. };
  46. // Functions are not declared to minimize # of lines
  47. static void SetColumnAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  48. Device->WriteCommand( Device, 0x15 );
  49. Device->WriteCommand( Device, Start );
  50. Device->WriteCommand( Device, End );
  51. }
  52. static void SetRowAddress( struct GDS_Device* Device, uint8_t Start, uint8_t End ) {
  53. // can be by row, not by page (see Update Optimization)
  54. Device->WriteCommand( Device, 0x75 );
  55. Device->WriteCommand( Device, Start );
  56. Device->WriteCommand( Device, End );
  57. }
  58. static void Update4( struct GDS_Device* Device ) {
  59. struct SSD132x_Private *Private = (struct SSD132x_Private*) Device->Private;
  60. // always update by full lines
  61. SetColumnAddress( Device, 0, Device->Width / 2 - 1);
  62. #ifdef SHADOW_BUFFER
  63. uint16_t *optr = (uint16_t*) Device->Shadowbuffer, *iptr = (uint16_t*) Device->Framebuffer;
  64. bool dirty = false;
  65. for (int r = 0, page = 0; r < Device->Height; r++) {
  66. // look for change and update shadow (cheap optimization = width always / by 2)
  67. for (int c = Device->Width / 2 / 2; --c >= 0;) {
  68. if (*optr != *iptr) {
  69. dirty = true;
  70. *optr = *iptr;
  71. }
  72. iptr++; optr++;
  73. }
  74. // one line done, check for page boundary
  75. if (++page == Private->PageSize) {
  76. if (dirty) {
  77. SetRowAddress( Device, r - page + 1, r );
  78. // own use of IRAM has not proven to be much better than letting SPI do its copy
  79. if (Private->iRAM) {
  80. memcpy(Private->iRAM, Device->Shadowbuffer + (r - page + 1) * Device->Width / 2, page * Device->Width / 2 );
  81. Device->WriteData( Device, Private->iRAM, Device->Width * page / 2 );
  82. } else {
  83. Device->WriteData( Device, Device->Shadowbuffer + (r - page + 1) * Device->Width / 2, page * Device->Width / 2 );
  84. }
  85. dirty = false;
  86. }
  87. page = 0;
  88. }
  89. }
  90. #else
  91. for (int r = 0; r < Device->Height; r += Private->PageSize) {
  92. SetRowAddress( Device, r, r + Private->PageSize - 1 );
  93. if (Private->iRAM) {
  94. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width / 2, Private->PageSize * Device->Width / 2 );
  95. Device->WriteData( Device, Private->iRAM, Private->PageSize * Device->Width / 2 );
  96. } else {
  97. Device->WriteData( Device, Device->Framebuffer + r * Device->Width / 2, Private->PageSize * Device->Width / 2 );
  98. }
  99. }
  100. #endif
  101. }
  102. /*
  103. We have to make a choice here: either we go by row one by one and send lots of
  104. small packets on the serial interface or we do a page of N rows once at least
  105. a change has been detected. So far, choice is to go one-by-one
  106. */
  107. static void Update1( struct GDS_Device* Device ) {
  108. #ifdef SHADOW_BUFFER
  109. // not sure the compiler does not have to redo all calculation in for loops, so local it is
  110. int width = Device->Width / 8, rows = Device->Height;
  111. uint8_t *optr = Device->Shadowbuffer, *iptr = Device->Framebuffer;
  112. // by row, find first and last columns that have been updated
  113. for (int r = 0; r < rows; r++) {
  114. uint8_t first = 0, last;
  115. for (int c = 0; c < width; c++) {
  116. if (*iptr != *optr) {
  117. if (!first) first = c + 1;
  118. last = c ;
  119. }
  120. *optr++ = *iptr++;
  121. }
  122. // now update the display by "byte rows"
  123. if (first--) {
  124. SetColumnAddress( Device, first, last );
  125. SetRowAddress( Device, r, r);
  126. Device->WriteData( Device, Device->Shadowbuffer + r*width + first, last - first + 1);
  127. }
  128. }
  129. #else
  130. // automatic counter and end Row/Column
  131. SetColumnAddress( Device, 0, Device->Width / 8 - 1);
  132. SetRowAddress( Device, 0, Device->Height - 1);
  133. Device->WriteData( Device, Device->Framebuffer, Device->FramebufferSize );
  134. #endif
  135. }
  136. // in 1 bit mode, SSD1326 has a different memory map than SSD1306 and SH1106
  137. static void IRAM_ATTR DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  138. uint32_t XBit = ( X & 0x07 );
  139. uint8_t* FBOffset = Device->Framebuffer + ( ( Y * Device->Width + X ) >> 3 );
  140. if ( Color == GDS_COLOR_XOR ) {
  141. *FBOffset ^= BIT( 7 - XBit );
  142. } else {
  143. // we might be able to save the 7-Xbit using BitRemap (A0 bit 2)
  144. *FBOffset = ( Color == GDS_COLOR_WHITE ) ? *FBOffset | BIT( 7 - XBit ) : *FBOffset & ~BIT( 7 - XBit );
  145. }
  146. }
  147. static void ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  148. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  149. uint8_t Width = Device->Width >> 3;
  150. uint8_t *optr = Device->Framebuffer;
  151. for (int r = y1; r <= y2; r++) {
  152. int c = x1;
  153. // for a row that is not on a boundary, not column opt can be done, so handle all columns on that line
  154. while (c & 0x07 && c <= x2) DrawPixel1Fast( Device, c++, r, Color );
  155. // at this point we are aligned on column boundary
  156. int chunk = (x2 - c + 1) >> 3;
  157. memset(optr + Width * r + (c >> 3), _Color, chunk );
  158. c += chunk * 8;
  159. while (c <= x2) DrawPixel1Fast( Device, c++, r, Color );
  160. }
  161. Device->Dirty = true;
  162. }
  163. static void DrawBitmapCBR(struct GDS_Device* Device, uint8_t *Data, int Width, int Height, int Color ) {
  164. uint8_t *optr = Device->Framebuffer;
  165. if (!Height) Height = Device->Height;
  166. if (!Width) Width = Device->Width;
  167. // just do bitreverse and if BitRemap works, there will be even nothing to do
  168. for (int i = Height * Width >> 3; --i >= 0;) *optr++ = BitReverseTable256[*Data++];
  169. // Dirty is set for us
  170. }
  171. static void SetHFlip( struct GDS_Device* Device, bool On ) {
  172. struct SSD132x_Private *Private = (struct SSD132x_Private*) Device->Private;
  173. if (Private->Model == SSD1326) Private->ReMap = On ? (Private->ReMap | ((1 << 0) | (1 << 2))) : (Private->ReMap & ~((1 << 0) | (1 << 2)));
  174. else Private->ReMap = On ? (Private->ReMap | ((1 << 0) | (1 << 1))) : (Private->ReMap & ~((1 << 0) | (1 << 1)));
  175. Device->WriteCommand( Device, 0xA0 );
  176. Device->WriteCommand( Device, Private->ReMap );
  177. }
  178. static void SetVFlip( struct GDS_Device *Device, bool On ) {
  179. struct SSD132x_Private *Private = (struct SSD132x_Private*) Device->Private;
  180. if (Private->Model == SSD1326) Private->ReMap = On ? (Private->ReMap | (1 << 1)) : (Private->ReMap & ~(1 << 1));
  181. else Private->ReMap = On ? (Private->ReMap | (1 << 4)) : (Private->ReMap & ~(1 << 4));
  182. Device->WriteCommand( Device, 0xA0 );
  183. Device->WriteCommand( Device, Private->ReMap );
  184. }
  185. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  186. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  187. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  188. Device->WriteCommand( Device, 0x81 );
  189. Device->WriteCommand( Device, Contrast );
  190. }
  191. static bool Init( struct GDS_Device* Device ) {
  192. struct SSD132x_Private *Private = (struct SSD132x_Private*) Device->Private;
  193. // find a page size that is not too small is an integer of height
  194. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width / 2));
  195. Private->PageSize = Device->Height / (Device->Height / Private->PageSize) ;
  196. #ifdef USE_IRAM
  197. // let SPI driver allocate memory, it has not proven to be more efficient
  198. if (Device->IF == IF_SPI) Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  199. #endif
  200. Device->FramebufferSize = ( Device->Width * Device->Height ) / 2;
  201. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  202. NullCheck( Device->Framebuffer, return false );
  203. // benchmarks showed little gain to have SPI memory already in IRAM vs letting driver copy
  204. #ifdef SHADOW_BUFFER
  205. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  206. NullCheck( Device->Framebuffer, return false );
  207. #ifdef USE_IRAM
  208. if (Device->IF == IF_SPI) {
  209. if (Device->Depth == 1) {
  210. Device->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  211. } else {
  212. Device->Shadowbuffer = malloc( Device->FramebufferSize );
  213. Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  214. }
  215. } else
  216. #endif
  217. Device->Shadowbuffer = malloc( Device->FramebufferSize );
  218. memset(Device->Shadowbuffer, 0xFF, Device->FramebufferSize);
  219. #else // not SHADOW_BUFFER
  220. #ifdef USE_IRAM
  221. if (Device->IF == IF_SPI) {
  222. if (Device->Depth == 1) {
  223. Device->Framebuffer = heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  224. } else {
  225. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  226. Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  227. }
  228. } else
  229. #endif
  230. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  231. #endif
  232. ESP_LOGI(TAG, "SSD1326/7 with bit depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  233. // need to be off and disable display RAM
  234. Device->DisplayOff( Device );
  235. Device->WriteCommand( Device, 0xA5 );
  236. // need COM split (6)
  237. Private->ReMap = 1 << 6;
  238. // MUX Ratio
  239. Device->WriteCommand( Device, 0xA8 );
  240. Device->WriteCommand( Device, Device->Height - 1);
  241. // Display Offset
  242. Device->WriteCommand( Device, 0xA2 );
  243. Device->WriteCommand( Device, 0 );
  244. // Display Start Line
  245. Device->WriteCommand( Device, 0xA1 );
  246. Device->WriteCommand( Device, 0x00 );
  247. Device->SetContrast( Device, 0x7F );
  248. // set flip modes
  249. Device->SetVFlip( Device, false );
  250. Device->SetHFlip( Device, false );
  251. // no Display Inversion
  252. Device->WriteCommand( Device, 0xA6 );
  253. // set Clocks
  254. Device->WriteCommand( Device, 0xB3 );
  255. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  256. // set Adressing Mode Horizontal
  257. Private->ReMap |= (0 << 2);
  258. // set monotchrome mode if required
  259. if (Device->Depth == 1) Private->ReMap |= (1 << 4);
  260. // write ReMap byte
  261. Device->WriteCommand( Device, 0xA0 );
  262. Device->WriteCommand( Device, Private->ReMap );
  263. // gone with the wind
  264. Device->WriteCommand( Device, 0xA4 );
  265. Device->DisplayOn( Device );
  266. Device->Update( Device );
  267. return true;
  268. }
  269. static const struct GDS_Device SSD132x = {
  270. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  271. .SetVFlip = SetVFlip, .SetHFlip = SetHFlip,
  272. .Update = Update4, .Init = Init,
  273. };
  274. struct GDS_Device* SSD132x_Detect(char *Driver, struct GDS_Device* Device) {
  275. uint8_t Model;
  276. if (!strcasestr(Driver, "SSD1326")) Model = SSD1326;
  277. else if (!strcasestr(Driver, "SSD1327")) Model = SSD1327;
  278. else return NULL;
  279. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  280. *Device = SSD132x;
  281. ((struct SSD132x_Private*) Device->Private)->Model = Model;
  282. sscanf(Driver, "%*[^:]:%c", &Device->Depth);
  283. if (Model == SSD1326 && Device->Depth == 1) {
  284. Device->Update = Update1;
  285. Device->DrawPixelFast = DrawPixel1Fast;
  286. Device->DrawBitmapCBR = DrawBitmapCBR;
  287. Device->ClearWindow = ClearWindow;
  288. } else {
  289. Device->Depth = 4;
  290. }
  291. return Device;
  292. }