2
0

gds.c 12 KB

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