SSD132x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 PrivateSpace {
  23. uint8_t *iRAM, *Shadowbuffer;
  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. 0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7B, 0xFB,
  43. 0x07, 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 PrivateSpace *Private = (struct PrivateSpace*) 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*) Private->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, Private->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, Private->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. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  110. // not sure the compiler does not have to redo all calculation in for loops, so local it is
  111. int width = Device->Width / 8, rows = Device->Height;
  112. uint8_t *optr = Private->Shadowbuffer, *iptr = Device->Framebuffer;
  113. int CurrentRow = -1, FirstCol = -1, LastCol = -1;
  114. // by row, find first and last columns that have been updated
  115. for (int r = 0; r < rows; r++) {
  116. uint8_t first = 0, last;
  117. for (int c = 0; c < width; c++) {
  118. if (*iptr != *optr) {
  119. if (!first) first = c + 1;
  120. last = c ;
  121. }
  122. *optr++ = *iptr++;
  123. }
  124. // now update the display by "byte rows"
  125. if (first--) {
  126. // only set column when useful, saves a fair bit of CPU
  127. if (first > FirstCol && first <= FirstCol + 4 && last < LastCol && last >= LastCol - 4) {
  128. first = FirstCol;
  129. last = LastCol;
  130. } else {
  131. SetColumnAddress( Device, first, last );
  132. FirstCol = first;
  133. LastCol = last;
  134. }
  135. // Set row only when needed, otherwise let auto-increment work
  136. if (r != CurrentRow) SetRowAddress( Device, r, Device->Height - 1 );
  137. CurrentRow = r + 1;
  138. // actual write
  139. Device->WriteData( Device, Private->Shadowbuffer + r*width + first, last - first + 1 );
  140. }
  141. }
  142. #else
  143. // automatic counter and end Row/Column
  144. SetColumnAddress( Device, 0, Device->Width / 8 - 1);
  145. SetRowAddress( Device, 0, Device->Height - 1);
  146. Device->WriteData( Device, Device->Framebuffer, Device->FramebufferSize );
  147. #endif
  148. }
  149. // in 1 bit mode, SSD1326 has a different memory map than SSD1306 and SH1106
  150. static void IRAM_ATTR _DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  151. uint32_t XBit = ( X & 0x07 );
  152. uint8_t* FBOffset = Device->Framebuffer + ( ( Y * Device->Width + X ) >> 3 );
  153. if ( Color == GDS_COLOR_XOR ) {
  154. *FBOffset ^= BIT( 7 - XBit );
  155. } else {
  156. // we might be able to save the 7-Xbit using BitRemap (A0 bit 2)
  157. *FBOffset = ( Color == GDS_COLOR_BLACK ) ? *FBOffset & ~BIT( XBit ) : *FBOffset | BIT( XBit );
  158. }
  159. }
  160. static void ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  161. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  162. int Width = Device->Width >> 3;
  163. uint8_t *optr = Device->Framebuffer;
  164. for (int r = y1; r <= y2; r++) {
  165. int c = x1;
  166. // for a row that is not on a boundary, not column opt can be done, so handle all columns on that line
  167. while (c & 0x07 && c <= x2) _DrawPixel1Fast( Device, c++, r, Color );
  168. // at this point we are aligned on column boundary
  169. int chunk = (x2 - c + 1) >> 3;
  170. memset(optr + Width * r + (c >> 3), _Color, chunk );
  171. c += chunk * 8;
  172. while (c <= x2) _DrawPixel1Fast( Device, c++, r, Color );
  173. }
  174. }
  175. static void DrawBitmapCBR(struct GDS_Device* Device, uint8_t *Data, int Width, int Height, int Color ) {
  176. if (!Height) Height = Device->Height;
  177. if (!Width) Width = Device->Width;
  178. int DWidth = Device->Width >> 3;
  179. // Two consecutive bits of source data are split over two different bytes of framebuffer
  180. for (int c = 0; c < Width; c++) {
  181. uint8_t shift = c & 0x07, bit = ~(1 << shift);
  182. uint8_t *optr = Device->Framebuffer + (c >> 3);
  183. // we need to linearize code to let compiler better optimize
  184. for (int r = Height >> 3; --r >= 0;) {
  185. uint8_t Byte = BitReverseTable256[*Data++];
  186. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  187. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  188. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  189. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  190. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  191. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  192. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth; Byte >>= 1;
  193. *optr = (*optr & bit) | ((Byte & 0x01) << shift); optr += DWidth;
  194. }
  195. }
  196. }
  197. static void SetLayout( struct GDS_Device* Device, bool HFlip, bool VFlip, bool Rotate ) {
  198. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  199. if (Private->Model == SSD1326) {
  200. Private->ReMap = HFlip ? (Private->ReMap | ((1 << 0) | (1 << 2))) : (Private->ReMap & ~((1 << 0) | (1 << 2)));
  201. Private->ReMap = HFlip ? (Private->ReMap | (1 << 1)) : (Private->ReMap & ~(1 << 1));
  202. } else {
  203. Private->ReMap = VFlip ? (Private->ReMap | ((1 << 0) | (1 << 1))) : (Private->ReMap & ~((1 << 0) | (1 << 1)));
  204. Private->ReMap = VFlip ? (Private->ReMap | (1 << 4)) : (Private->ReMap & ~(1 << 4));
  205. }
  206. Device->WriteCommand( Device, 0xA0 );
  207. Device->WriteCommand( Device, Private->ReMap );
  208. }
  209. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  210. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  211. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  212. Device->WriteCommand( Device, 0x81 );
  213. Device->WriteCommand( Device, Contrast );
  214. }
  215. static bool Init( struct GDS_Device* Device ) {
  216. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  217. // find a page size that is not too small is an integer of height
  218. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width / 2));
  219. while (Private->PageSize && Device->Height != (Device->Height / Private->PageSize) * Private->PageSize) Private->PageSize--;
  220. #ifdef SHADOW_BUFFER
  221. #ifdef USE_IRAM
  222. if (Device->IF == GDS_IF_SPI) {
  223. if (Device->Depth == 1) {
  224. Private->Shadowbuffer = heap_caps_malloc( Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  225. } else {
  226. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  227. Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  228. }
  229. } else
  230. #endif
  231. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  232. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  233. #else
  234. #ifdef USE_IRAM
  235. if (Device->Depth == 4 && Device->IF == GDS_IF_SPI) Private->iRAM = heap_caps_malloc( Private->PageSize * Device->Width / 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  236. #endif
  237. #endif
  238. ESP_LOGI(TAG, "SSD1326/7 with bit depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  239. // need to be off and disable display RAM
  240. Device->DisplayOff( Device );
  241. Device->WriteCommand( Device, 0xA5 );
  242. // need COM split (6)
  243. Private->ReMap = 1 << 6;
  244. // MUX Ratio
  245. Device->WriteCommand( Device, 0xA8 );
  246. Device->WriteCommand( Device, Device->Height - 1);
  247. // Display Offset
  248. Device->WriteCommand( Device, 0xA2 );
  249. Device->WriteCommand( Device, 0 );
  250. // Display Start Line
  251. Device->WriteCommand( Device, 0xA1 );
  252. Device->WriteCommand( Device, 0x00 );
  253. Device->SetContrast( Device, 0x7F );
  254. // set flip modes
  255. Device->SetLayout( Device, false, false, false );
  256. // no Display Inversion
  257. Device->WriteCommand( Device, 0xA6 );
  258. // set Clocks
  259. Device->WriteCommand( Device, 0xB3 );
  260. Device->WriteCommand( Device, ( 0x08 << 4 ) | 0x00 );
  261. // set Adressing Mode Horizontal
  262. Private->ReMap |= (0 << 2);
  263. // set monotchrome mode if required
  264. if (Device->Depth == 1) Private->ReMap |= (1 << 4);
  265. // write ReMap byte
  266. Device->WriteCommand( Device, 0xA0 );
  267. Device->WriteCommand( Device, Private->ReMap );
  268. // gone with the wind
  269. Device->WriteCommand( Device, 0xA4 );
  270. Device->DisplayOn( Device );
  271. Device->Update( Device );
  272. return true;
  273. }
  274. static const struct GDS_Device SSD132x = {
  275. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  276. .SetLayout = SetLayout,
  277. .Update = Update4, .Init = Init,
  278. .Mode = GDS_GRAYSCALE, .Depth = 4,
  279. };
  280. struct GDS_Device* SSD132x_Detect(char *Driver, struct GDS_Device* Device) {
  281. uint8_t Model;
  282. int Depth;
  283. if (strcasestr(Driver, "SSD1326")) Model = SSD1326;
  284. else if (strcasestr(Driver, "SSD1327")) Model = SSD1327;
  285. else return NULL;
  286. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  287. *Device = SSD132x;
  288. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  289. Private->Model = Model;
  290. sscanf(Driver, "%*[^:]:%u", &Depth);
  291. if (Model == SSD1326 && Depth == 1) {
  292. Device->Update = Update1;
  293. Device->DrawPixelFast = _DrawPixel1Fast;
  294. Device->DrawBitmapCBR = DrawBitmapCBR;
  295. Device->ClearWindow = ClearWindow;
  296. Device->Depth = 1;
  297. Device->Mode = GDS_MONO;
  298. #if !defined SHADOW_BUFFER && defined USE_IRAM
  299. Device->Alloc = GDS_ALLOC_IRAM_SPI;
  300. #endif
  301. }
  302. return Device;
  303. }