gds.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <string.h>
  9. #include <ctype.h>
  10. #include <stdint.h>
  11. #include <math.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "driver/gpio.h"
  15. #include "driver/ledc.h"
  16. #include "esp_log.h"
  17. #include "gds.h"
  18. #include "gds_private.h"
  19. #ifdef CONFIG_IDF_TARGET_ESP32S3
  20. #define LEDC_SPEED_MODE LEDC_LOW_SPEED_MODE
  21. #else
  22. #define LEDC_SPEED_MODE LEDC_HIGH_SPEED_MODE
  23. #endif
  24. static struct GDS_Device Display;
  25. static struct GDS_BacklightPWM PWMConfig;
  26. static char TAG[] = "gds";
  27. struct GDS_Device* GDS_AutoDetect( char *Driver, GDS_DetectFunc* DetectFunc[], struct GDS_BacklightPWM* PWM ) {
  28. if (!Driver) return NULL;
  29. if (PWM) PWMConfig = *PWM;
  30. for (int i = 0; DetectFunc[i]; i++) {
  31. if (DetectFunc[i](Driver, &Display)) {
  32. if (PWM && PWM->Init) {
  33. ledc_timer_config_t PWMTimer = {
  34. .duty_resolution = LEDC_TIMER_13_BIT,
  35. .freq_hz = 5000,
  36. .speed_mode = LEDC_SPEED_MODE,
  37. .timer_num = PWMConfig.Timer,
  38. };
  39. ledc_timer_config(&PWMTimer);
  40. }
  41. ESP_LOGD(TAG, "Detected driver %p with PWM %d", &Display, PWM ? PWM->Init : 0);
  42. return &Display;
  43. }
  44. }
  45. return NULL;
  46. }
  47. void GDS_ClearExt(struct GDS_Device* Device, bool full, ...) {
  48. bool commit = true;
  49. if (full) {
  50. GDS_Clear( Device, GDS_COLOR_BLACK );
  51. } else {
  52. va_list args;
  53. va_start(args, full);
  54. commit = va_arg(args, int);
  55. int x1 = va_arg(args, int), y1 = va_arg(args, int), x2 = va_arg(args, int), y2 = va_arg(args, int);
  56. if (x2 < 0) x2 = Device->Width - 1;
  57. if (y2 < 0) y2 = Device->Height - 1;
  58. GDS_ClearWindow( Device, x1, y1, x2, y2, GDS_COLOR_BLACK );
  59. va_end(args);
  60. }
  61. Device->Dirty = true;
  62. if (commit) GDS_Update(Device);
  63. }
  64. void GDS_Clear( struct GDS_Device* Device, int Color ) {
  65. if (Color == GDS_COLOR_BLACK) memset( Device->Framebuffer, 0, Device->FramebufferSize );
  66. else if (Device->Depth == 1) memset( Device->Framebuffer, 0xff, Device->FramebufferSize );
  67. else if (Device->Depth == 4) memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  68. else if (Device->Depth == 8) memset( Device->Framebuffer, Color, Device->FramebufferSize );
  69. else GDS_ClearWindow(Device, 0, 0, -1, -1, Color);
  70. Device->Dirty = true;
  71. }
  72. #define CLEAR_WINDOW(x1,y1,x2,y2,F,W,C,T,N) \
  73. for (int y = y1; y <= y2; y++) { \
  74. T *Ptr = (T*) F + (y * W + x1)*N; \
  75. for (int c = (x2 - x1)*N; c-- >= 0; *Ptr++ = C); \
  76. }
  77. void GDS_ClearWindow( struct GDS_Device* Device, int x1, int y1, int x2, int y2, int Color ) {
  78. // -1 means up to width/height
  79. if (x2 < 0) x2 = Device->Width - 1;
  80. if (y2 < 0) y2 = Device->Height - 1;
  81. // driver can provide own optimized clear window
  82. if (Device->ClearWindow) {
  83. Device->ClearWindow( Device, x1, y1, x2, y2, Color );
  84. } else if (Device->Depth == 1) {
  85. // single shot if we erase all screen
  86. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  87. memset( Device->Framebuffer, Color == GDS_COLOR_BLACK ? 0 : 0xff, Device->FramebufferSize );
  88. } else {
  89. uint8_t _Color = Color == GDS_COLOR_BLACK ? 0: 0xff;
  90. uint8_t Width = Device->Width >> 3;
  91. uint8_t *optr = Device->Framebuffer;
  92. // try to do byte processing as much as possible
  93. for (int r = y1; r <= y2;) {
  94. int c = x1;
  95. // for a row that is not on a boundary, no optimization possible
  96. while (r & 0x07 && r <= y2) {
  97. for (c = x1; c <= x2; c++) Device->DrawPixelFast( Device, c, r, Color );
  98. r++;
  99. }
  100. // go fast if we have more than 8 lines to write
  101. if (r + 8 <= y2) {
  102. memset(optr + Width * r + x1, _Color, x2 - x1 + 1);
  103. r += 8;
  104. } else while (r <= y2) {
  105. for (c = x1; c <= x2; c++) Device->DrawPixelFast( Device, c, r, Color );
  106. r++;
  107. }
  108. }
  109. }
  110. } if (Device->Depth == 4) {
  111. if (x2 - x1 == Device->Width - 1 && y2 - y1 == Device->Height - 1) {
  112. // we assume color is 0..15
  113. memset( Device->Framebuffer, Color | (Color << 4), Device->FramebufferSize );
  114. } else {
  115. uint8_t _Color = Color | (Color << 4);
  116. int Width = Device->Width;
  117. uint8_t *optr = Device->Framebuffer;
  118. // try to do byte processing as much as possible
  119. for (int r = y1; r <= y2; r++) {
  120. int c = x1;
  121. if (c & 0x01) Device->DrawPixelFast( Device, c++, r, Color);
  122. int chunk = (x2 - c + 1) >> 1;
  123. memset(optr + ((r * Width + c) >> 1), _Color, chunk);
  124. if (c + chunk <= x2) Device->DrawPixelFast( Device, x2, r, Color);
  125. }
  126. }
  127. } else if (Device->Depth == 8) {
  128. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint8_t,1);
  129. } else if (Device->Depth == 16) {
  130. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint16_t,1);
  131. } else if (Device->Depth == 24) {
  132. CLEAR_WINDOW(x1,y1,x2,y2,Device->Framebuffer,Device->Width,Color,uint8_t,3);
  133. } else {
  134. for (int y = y1; y <= y2; y++) {
  135. for (int x = x1; x <= x2; x++) {
  136. Device->DrawPixelFast( Device, x, y, Color);
  137. }
  138. }
  139. }
  140. // make sure diplay will do update
  141. Device->Dirty = true;
  142. }
  143. void GDS_Update( struct GDS_Device* Device ) {
  144. if (Device->Dirty) Device->Update( Device );
  145. Device->Dirty = false;
  146. }
  147. bool GDS_Reset( struct GDS_Device* Device ) {
  148. if ( Device->RSTPin >= 0 ) {
  149. gpio_set_level( Device->RSTPin, 0 );
  150. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  151. gpio_set_level( Device->RSTPin, 1 );
  152. }
  153. return true;
  154. }
  155. static void IRAM_ATTR DrawPixel1Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  156. uint32_t YBit = ( Y & 0x07 );
  157. uint8_t* FBOffset;
  158. /*
  159. * We only need to modify the Y coordinate since the pitch
  160. * of the screen is the same as the width.
  161. * Dividing Y by 8 gives us which row the pixel is in but not
  162. * the bit position.
  163. */
  164. Y>>= 3;
  165. FBOffset = Device->Framebuffer + ( ( Y * Device->Width ) + X );
  166. if ( Color == GDS_COLOR_XOR ) {
  167. *FBOffset ^= BIT( YBit );
  168. } else {
  169. *FBOffset = ( Color == GDS_COLOR_BLACK ) ? *FBOffset & ~BIT( YBit ) : *FBOffset | BIT( YBit );
  170. }
  171. }
  172. static void IRAM_ATTR DrawPixel4Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  173. uint8_t* FBOffset = Device->Framebuffer + ( (Y * Device->Width >> 1) + (X >> 1));
  174. *FBOffset = X & 0x01 ? (*FBOffset & 0x0f) | ((Color & 0x0f) << 4) : ((*FBOffset & 0xf0) | (Color & 0x0f));
  175. }
  176. static void IRAM_ATTR DrawPixel4FastHigh( struct GDS_Device* Device, int X, int Y, int Color ) {
  177. uint8_t* FBOffset = Device->Framebuffer + ( (Y * Device->Width >> 1) + (X >> 1));
  178. *FBOffset = X & 0x01 ? ((*FBOffset & 0xf0) | (Color & 0x0f)) : (*FBOffset & 0x0f) | ((Color & 0x0f) << 4);
  179. }
  180. static void IRAM_ATTR DrawPixel8Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  181. Device->Framebuffer[Y * Device->Width + X] = Color;
  182. }
  183. // assumes that Color is 16 bits R..RG..GB..B from MSB to LSB and FB wants 1st serialized byte to start with R
  184. static void IRAM_ATTR DrawPixel16Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  185. uint16_t* FBOffset = (uint16_t*) Device->Framebuffer + Y * Device->Width + X;
  186. *FBOffset = __builtin_bswap16(Color);
  187. }
  188. // assumes that Color is 18 bits RGB from MSB to LSB RRRRRRGGGGGGBBBBBB, so byte[0] is B
  189. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = xxRRRRRR xxGGGGGG xxBBBBBB
  190. static void IRAM_ATTR DrawPixel18Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  191. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  192. *FBOffset++ = Color >> 12; *FBOffset++ = (Color >> 6) & 0x3f; *FBOffset = Color & 0x3f;
  193. }
  194. // assumes that Color is 24 bits RGB from MSB to LSB RRRRRRRRGGGGGGGGBBBBBBBB, so byte[0] is B
  195. // FB is 3-bytes packets and starts with R for serialization so 0,1,2 ... = RRRRRRRR GGGGGGGG BBBBBBBB
  196. static void IRAM_ATTR DrawPixel24Fast( struct GDS_Device* Device, int X, int Y, int Color ) {
  197. uint8_t* FBOffset = Device->Framebuffer + (Y * Device->Width + X) * 3;
  198. *FBOffset++ = Color >> 16; *FBOffset++ = Color >> 8; *FBOffset = Color;
  199. }
  200. bool GDS_Init( struct GDS_Device* Device ) {
  201. if (Device->Depth > 8) Device->FramebufferSize = Device->Width * Device->Height * ((8 + Device->Depth - 1) / 8);
  202. else Device->FramebufferSize = (Device->Width * Device->Height) / (8 / Device->Depth);
  203. // set the proper DrawPixel function if not already set by driver
  204. if (!Device->DrawPixelFast) {
  205. if (Device->Depth == 1) Device->DrawPixelFast = DrawPixel1Fast;
  206. else if (Device->Depth == 4 && Device->HighNibble) Device->DrawPixelFast = DrawPixel4FastHigh;
  207. else if (Device->Depth == 4) Device->DrawPixelFast = DrawPixel4Fast;
  208. else if (Device->Depth == 8) Device->DrawPixelFast = DrawPixel8Fast;
  209. else if (Device->Depth == 16) Device->DrawPixelFast = DrawPixel16Fast;
  210. else if (Device->Depth == 24 && Device->Mode == GDS_RGB666) Device->DrawPixelFast = DrawPixel18Fast;
  211. else if (Device->Depth == 24 && Device->Mode == GDS_RGB888) Device->DrawPixelFast = DrawPixel24Fast;
  212. }
  213. // allocate FB unless explicitely asked not to
  214. if (!(Device->Alloc & GDS_ALLOC_NONE)) {
  215. if ((Device->Alloc & GDS_ALLOC_IRAM) || ((Device->Alloc & GDS_ALLOC_IRAM_SPI) && Device->IF == GDS_IF_SPI)) {
  216. Device->Framebuffer = heap_caps_calloc( 1, Device->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  217. } else {
  218. Device->Framebuffer = calloc( 1, Device->FramebufferSize );
  219. }
  220. NullCheck( Device->Framebuffer, return false );
  221. }
  222. if (Device->Backlight.Pin >= 0) {
  223. Device->Backlight.Channel = PWMConfig.Channel++;
  224. Device->Backlight.PWM = PWMConfig.Max - 1;
  225. ledc_channel_config_t PWMChannel = {
  226. .channel = Device->Backlight.Channel,
  227. .duty = Device->Backlight.PWM,
  228. .gpio_num = Device->Backlight.Pin,
  229. .speed_mode = LEDC_SPEED_MODE,
  230. .hpoint = 0,
  231. .timer_sel = PWMConfig.Timer,
  232. };
  233. ledc_channel_config(&PWMChannel);
  234. }
  235. bool Res = Device->Init( Device );
  236. if (!Res && Device->Framebuffer) free(Device->Framebuffer);
  237. return Res;
  238. }
  239. int GDS_GrayMap( struct GDS_Device* Device, uint8_t Level) {
  240. switch(Device->Mode) {
  241. case GDS_MONO: return Level;
  242. case GDS_GRAYSCALE: return Level >> (8 - Device->Depth);
  243. case GDS_RGB332:
  244. Level >>= 5;
  245. return (Level << 6) | (Level << 3) | (Level >> 1);
  246. case GDS_RGB444:
  247. Level >>= 4;
  248. return (Level << 8) | (Level << 4) | Level;
  249. case GDS_RGB555:
  250. Level >>= 3;
  251. return (Level << 10) | (Level << 5) | Level;
  252. case GDS_RGB565:
  253. Level >>= 2;
  254. return ((Level & ~0x01) << 10) | (Level << 5) | (Level >> 1);
  255. case GDS_RGB666:
  256. Level >>= 2;
  257. return (Level << 12) | (Level << 6) | Level;
  258. case GDS_RGB888:
  259. return (Level << 16) | (Level << 8) | Level;
  260. }
  261. return -1;
  262. }
  263. void GDS_SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  264. if (Device->SetContrast) Device->SetContrast( Device, Contrast );
  265. else if (Device->Backlight.Pin >= 0) {
  266. Device->Backlight.PWM = PWMConfig.Max * powf(Contrast / 255.0, 3);
  267. ledc_set_duty( LEDC_SPEED_MODE, Device->Backlight.Channel, Device->Backlight.PWM );
  268. ledc_update_duty( LEDC_SPEED_MODE, Device->Backlight.Channel );
  269. }
  270. }
  271. void GDS_SetLayout( struct GDS_Device* Device, struct GDS_Layout *Layout ) { if (Device->SetLayout) Device->SetLayout( Device, Layout ); }
  272. void GDS_SetDirty( struct GDS_Device* Device ) { Device->Dirty = true; }
  273. int GDS_GetWidth( struct GDS_Device* Device ) { return Device ? Device->Width : 0; }
  274. void GDS_SetTextWidth( struct GDS_Device* Device, int TextWidth ) { Device->TextWidth = Device && TextWidth && TextWidth < Device->Width ? TextWidth : Device->Width; }
  275. int GDS_GetHeight( struct GDS_Device* Device ) { return Device ? Device->Height : 0; }
  276. int GDS_GetDepth( struct GDS_Device* Device ) { return Device ? Device->Depth : 0; }
  277. int GDS_GetMode( struct GDS_Device* Device ) { return Device ? Device->Mode : 0; }
  278. void GDS_DisplayOn( struct GDS_Device* Device ) { if (Device->DisplayOn) Device->DisplayOn( Device ); }
  279. void GDS_DisplayOff( struct GDS_Device* Device ) { if (Device->DisplayOff) Device->DisplayOff( Device ); }