display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdint.h>
  11. #include <arpa/inet.h>
  12. #include "esp_log.h"
  13. #include "globdefs.h"
  14. #include "platform_config.h"
  15. #include "tools.h"
  16. #include "display.h"
  17. #include "gds.h"
  18. #include "gds_default_if.h"
  19. #include "gds_draw.h"
  20. #include "gds_text.h"
  21. #include "gds_font.h"
  22. #include "gds_image.h"
  23. static const char *TAG = "display";
  24. #define min(a,b) (((a) < (b)) ? (a) : (b))
  25. #define max(a,b) (((a) > (b)) ? (a) : (b))
  26. #define DISPLAYER_STACK_SIZE (3*1024)
  27. #define SCROLLABLE_SIZE 384
  28. #define HEADER_SIZE 64
  29. #define DEFAULT_SLEEP 3600
  30. #define ARTWORK_BORDER 1
  31. extern const uint8_t default_artwork[] asm("_binary_note_jpg_start");
  32. static EXT_RAM_ATTR struct {
  33. TaskHandle_t task;
  34. SemaphoreHandle_t mutex;
  35. int pause, speed, by;
  36. enum { DISPLAYER_DOWN, DISPLAYER_IDLE, DISPLAYER_ACTIVE } state;
  37. char header[HEADER_SIZE + 1];
  38. char string[SCROLLABLE_SIZE + 1];
  39. int offset, boundary;
  40. char *metadata_config;
  41. bool timer, refresh;
  42. uint32_t elapsed;
  43. struct {
  44. uint32_t value;
  45. char string[8]; // H:MM:SS
  46. bool visible;
  47. } duration;
  48. struct {
  49. bool enable, active;
  50. bool fit;
  51. bool updated;
  52. int tick;
  53. int offset;
  54. } artwork;
  55. TickType_t tick;
  56. } displayer;
  57. static const char *known_drivers[] = {"SH1106",
  58. "SSD1306",
  59. "SSD1322",
  60. "SSD1326",
  61. "SSD1327",
  62. "SSD1675",
  63. "SSD1351",
  64. "ST7735",
  65. "ST7789",
  66. "ILI9341",
  67. NULL
  68. };
  69. static void displayer_task(void *args);
  70. struct GDS_Device *display;
  71. extern GDS_DetectFunc SSD1306_Detect, SSD132x_Detect, SH1106_Detect, SSD1675_Detect, SSD1322_Detect, SSD1351_Detect, ST77xx_Detect, ILI9341_Detect;
  72. GDS_DetectFunc *drivers[] = { SH1106_Detect, SSD1306_Detect, SSD132x_Detect, SSD1675_Detect, SSD1322_Detect, SSD1351_Detect, ST77xx_Detect, ILI9341_Detect, NULL };
  73. /****************************************************************************************
  74. *
  75. */
  76. void display_init(char *welcome) {
  77. bool init = false;
  78. char *config = config_alloc_get_str("display_config", CONFIG_DISPLAY_CONFIG, "N/A");
  79. int width = -1, height = -1, backlight_pin = -1;
  80. char *drivername = strstr(config, "driver");
  81. PARSE_PARAM(config, "width", '=', width);
  82. PARSE_PARAM(config, "height", '=', height);
  83. PARSE_PARAM(config, "back", '=', backlight_pin);
  84. // query drivers to see if we have a match
  85. ESP_LOGI(TAG, "Trying to configure display with %s", config);
  86. if (backlight_pin >= 0) {
  87. struct GDS_BacklightPWM PWMConfig = { .Channel = pwm_system.base_channel++, .Timer = pwm_system.timer, .Max = pwm_system.max, .Init = false };
  88. display = GDS_AutoDetect(drivername, drivers, &PWMConfig);
  89. } else {
  90. display = GDS_AutoDetect(drivername, drivers, NULL);
  91. }
  92. // so far so good
  93. if (display && width > 0 && height > 0) {
  94. int RST_pin = -1;
  95. PARSE_PARAM(config, "reset", '=', RST_pin);
  96. // Detect driver interface
  97. if (strcasestr(config, "I2C") && i2c_system_port != -1) {
  98. int address = 0x3C;
  99. PARSE_PARAM(config, "address", '=', address);
  100. init = true;
  101. GDS_I2CInit( i2c_system_port, -1, -1, i2c_system_speed ) ;
  102. GDS_I2CAttachDevice( display, width, height, address, RST_pin, backlight_pin );
  103. ESP_LOGI(TAG, "Display is I2C on port %u", address);
  104. } else if (strcasestr(config, "SPI") && spi_system_host != -1) {
  105. int CS_pin = -1, speed = 0;
  106. PARSE_PARAM(config, "cs", '=', CS_pin);
  107. PARSE_PARAM(config, "speed", '=', speed);
  108. init = true;
  109. GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
  110. GDS_SPIAttachDevice( display, width, height, CS_pin, RST_pin, backlight_pin, speed );
  111. ESP_LOGI(TAG, "Display is SPI host %u with cs:%d", spi_system_host, CS_pin);
  112. } else {
  113. display = NULL;
  114. ESP_LOGI(TAG, "Unsupported display interface or serial link not configured");
  115. }
  116. } else {
  117. display = NULL;
  118. ESP_LOGW(TAG, "No display driver");
  119. }
  120. if (init) {
  121. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  122. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  123. struct GDS_Layout Layout = {
  124. .HFlip = strcasestr(config, "HFlip"),
  125. .VFlip = strcasestr(config, "VFlip"),
  126. .Rotate = strcasestr(config, "rotate"),
  127. .Invert = strcasestr(config, "invert"),
  128. .ColorSwap = strcasestr(config, "cswap"),
  129. };
  130. GDS_SetLayout(display, &Layout);
  131. GDS_SetFont(display, &Font_droid_sans_fallback_15x17 );
  132. GDS_TextPos(display, GDS_FONT_MEDIUM, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, welcome);
  133. // start the task that will handle scrolling & counting
  134. displayer.mutex = xSemaphoreCreateMutex();
  135. displayer.by = 2;
  136. displayer.pause = 3600;
  137. displayer.speed = 33;
  138. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "common_displayer", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  139. // set lines for "fixed" text mode
  140. GDS_TextSetFontAuto(display, 1, GDS_FONT_LINE_1, -3);
  141. GDS_TextSetFontAuto(display, 2, GDS_FONT_LINE_2, -3);
  142. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  143. // leave room for artwork is display is horizontal-style
  144. if (strcasestr(displayer.metadata_config, "artwork")) {
  145. displayer.artwork.enable = true;
  146. displayer.artwork.fit = true;
  147. if (height <= 64 && width > height * 2) displayer.artwork.offset = width - height - ARTWORK_BORDER;
  148. PARSE_PARAM(displayer.metadata_config, "artwork", ':', displayer.artwork.fit);
  149. }
  150. }
  151. free(config);
  152. }
  153. /****************************************************************************************
  154. * This is not thread-safe as displayer_task might be in the middle of line drawing
  155. * but it won't crash (I think) and making it thread-safe would be complicated for a
  156. * feature which is secondary (the LMS version of scrolling is thread-safe)
  157. */
  158. static void displayer_task(void *args) {
  159. int scroll_sleep = 0, timer_sleep;
  160. while (1) {
  161. // suspend ourselves if nothing to do
  162. if (displayer.state < DISPLAYER_ACTIVE) {
  163. if (displayer.state == DISPLAYER_IDLE) GDS_TextLine(display, 2, 0, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.string);
  164. vTaskSuspend(NULL);
  165. scroll_sleep = 0;
  166. GDS_ClearExt(display, true);
  167. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_UPDATE, displayer.header);
  168. } else if (displayer.refresh) {
  169. // little trick when switching master while in IDLE and missing it
  170. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.header);
  171. displayer.refresh = false;
  172. }
  173. // we have been waken up before our requested time
  174. if (scroll_sleep <= 10) {
  175. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  176. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  177. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  178. // need to work with local copies as we don't want to suspend caller
  179. int offset = -displayer.offset;
  180. char *string = strdup(displayer.string);
  181. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  182. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  183. xSemaphoreGive(displayer.mutex);
  184. // now display using safe copies, can be lengthy
  185. GDS_TextLine(display, 2, offset, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, string);
  186. free(string);
  187. } else {
  188. scroll_sleep = DEFAULT_SLEEP;
  189. }
  190. }
  191. // handler elapsed track time
  192. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  193. char line[19] = "-", *_line = line + 1; // [-]H:MM:SS / H:MM:SS
  194. TickType_t tick = xTaskGetTickCount();
  195. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  196. if (elapsed >= 1000) {
  197. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  198. displayer.tick = tick;
  199. elapsed = displayer.elapsed += elapsed / 1000;
  200. xSemaphoreGive(displayer.mutex);
  201. // when we have duration but no space, display remaining time
  202. if (displayer.duration.value && !displayer.duration.visible) elapsed = displayer.duration.value - elapsed;
  203. if (elapsed < 3600) sprintf(_line, "%u:%02u", elapsed / 60, elapsed % 60);
  204. else sprintf(_line, "%u:%02u:%02u", (elapsed / 3600) % 100, (elapsed % 3600) / 60, elapsed % 60);
  205. // concatenate if we have room for elapsed / duration
  206. if (displayer.duration.visible) {
  207. strcat(_line, "/");
  208. strcat(_line, displayer.duration.string);
  209. } else if (displayer.duration.value) {
  210. _line--;
  211. }
  212. // just re-write the whole line it's easier
  213. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, displayer.header);
  214. GDS_TextLine(display, 1, GDS_TEXT_RIGHT, GDS_TEXT_UPDATE, _line);
  215. // if we have not received artwork after 5s, display a default icon
  216. if (displayer.artwork.active && !displayer.artwork.updated && tick - displayer.artwork.tick > pdMS_TO_TICKS(5000)) {
  217. ESP_LOGI(TAG, "no artwork received, setting default");
  218. displayer_artwork((uint8_t*) default_artwork);
  219. }
  220. timer_sleep = 1000;
  221. } else timer_sleep = max(1000 - elapsed, 0);
  222. } else timer_sleep = DEFAULT_SLEEP;
  223. // then sleep the min amount of time
  224. int sleep = min(scroll_sleep, timer_sleep);
  225. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  226. scroll_sleep -= sleep;
  227. vTaskDelay(sleep / portTICK_PERIOD_MS);
  228. }
  229. }
  230. /****************************************************************************************
  231. *
  232. */
  233. void displayer_artwork(uint8_t *data) {
  234. if (!displayer.artwork.active) return;
  235. int x = displayer.artwork.offset ? displayer.artwork.offset + ARTWORK_BORDER : 0;
  236. int y = x ? 0 : 32;
  237. GDS_ClearWindow(display, x, y, -1, -1, GDS_COLOR_BLACK);
  238. if (data) {
  239. displayer.artwork.updated = true;
  240. GDS_DrawJPEG(display, data, x, y, GDS_IMAGE_CENTER | (displayer.artwork.fit ? GDS_IMAGE_FIT : 0));
  241. } else {
  242. displayer.artwork.updated = false;
  243. displayer.artwork.tick = xTaskGetTickCount();
  244. }
  245. }
  246. /****************************************************************************************
  247. *
  248. */
  249. bool displayer_can_artwork(void) {
  250. return displayer.artwork.active;
  251. }
  252. /****************************************************************************************
  253. *
  254. */
  255. void displayer_metadata(char *artist, char *album, char *title) {
  256. char *string = displayer.string, *p;
  257. int len = SCROLLABLE_SIZE;
  258. // need a display!
  259. if (!display) return;
  260. // just do title if there is no config set
  261. if (!displayer.metadata_config) {
  262. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  263. return;
  264. }
  265. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  266. // format metadata parameters and write them directly
  267. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  268. char token[16], *q;
  269. int space = len;
  270. bool skip = false;
  271. displayer.string[0] = '\0';
  272. p = strchr(displayer.metadata_config, '=');
  273. while (p++) {
  274. // find token and copy what's after when reaching last one
  275. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  276. q = strchr(p, ',');
  277. strncat(string, p, q ? min(q - p, space) : space);
  278. break;
  279. }
  280. // copy what's before token (be safe)
  281. if ((q = strchr(p, '%')) == NULL) break;
  282. // skip whatever is after a token if this token is empty
  283. if (!skip) {
  284. strncat(string, p, min(q - p, space));
  285. space = len - strlen(string);
  286. }
  287. // then copy token's content
  288. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  289. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  290. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  291. space = len - strlen(string);
  292. // flag to skip the data following an empty field
  293. if (*p) skip = false;
  294. else skip = true;
  295. // advance to next separator
  296. p = strchr(q + 1, '%');
  297. }
  298. } else {
  299. strncpy(string, title ? title : "", SCROLLABLE_SIZE);
  300. }
  301. // get optional scroll speed & pause
  302. PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
  303. PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
  304. displayer.offset = 0;
  305. utf8_decode(displayer.string);
  306. ESP_LOGI(TAG, "playing %s", displayer.string);
  307. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  308. xSemaphoreGive(displayer.mutex);
  309. }
  310. /****************************************************************************************
  311. *
  312. */
  313. void displayer_scroll(char *string, int speed, int pause) {
  314. // need a display!
  315. if (!display) return;
  316. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  317. if (speed) displayer.speed = speed;
  318. if (pause) displayer.pause = pause;
  319. displayer.offset = 0;
  320. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  321. displayer.string[SCROLLABLE_SIZE] = '\0';
  322. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  323. xSemaphoreGive(displayer.mutex);
  324. }
  325. /****************************************************************************************
  326. *
  327. */
  328. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  329. // need a display!
  330. if (!display) return;
  331. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  332. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  333. if (elapsed >= 0) displayer.elapsed = elapsed / 1000;
  334. if (duration > 0) {
  335. displayer.duration.visible = true;
  336. displayer.duration.value = duration / 1000;
  337. if (displayer.duration.value > 3600) sprintf(displayer.duration.string, "%u:%02u:%02u", (displayer.duration.value / 3600) % 10,
  338. (displayer.duration.value % 3600) / 60, displayer.duration.value % 60);
  339. else sprintf(displayer.duration.string, "%u:%02u", displayer.duration.value / 60, displayer.duration.value % 60);
  340. char *buf;
  341. asprintf(&buf, "%s %s/%s", displayer.header, displayer.duration.string, displayer.duration.string);
  342. if (GDS_GetTextWidth(display, 1, 0, buf) > GDS_GetWidth(display)) {
  343. ESP_LOGW(TAG, "Can't fit duration %s (%d) on screen using elapsed only", buf, GDS_GetTextWidth(display, 1, 0, buf));
  344. displayer.duration.visible = false;
  345. }
  346. free(buf);
  347. } else if (!duration) {
  348. displayer.duration.visible = false;
  349. displayer.duration.value = 0;
  350. }
  351. xSemaphoreGive(displayer.mutex);
  352. }
  353. /****************************************************************************************
  354. * See above comment
  355. */
  356. void displayer_control(enum displayer_cmd_e cmd, ...) {
  357. va_list args;
  358. if (!display) return;
  359. va_start(args, cmd);
  360. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  361. switch(cmd) {
  362. case DISPLAYER_ACTIVATE: {
  363. char *header = va_arg(args, char*);
  364. displayer.artwork.active = displayer.artwork.enable && va_arg(args, int);
  365. strncpy(displayer.header, header, HEADER_SIZE);
  366. displayer.header[HEADER_SIZE] = '\0';
  367. displayer.state = DISPLAYER_ACTIVE;
  368. displayer.timer = false;
  369. displayer.refresh = true;
  370. displayer.string[0] = '\0';
  371. displayer.elapsed = displayer.duration.value = 0;
  372. displayer.duration.visible = false;
  373. displayer.offset = displayer.boundary = 0;
  374. display_bus(&displayer, DISPLAY_BUS_TAKE);
  375. if (displayer.artwork.active) GDS_SetTextWidth(display, displayer.artwork.offset);
  376. vTaskResume(displayer.task);
  377. break;
  378. }
  379. case DISPLAYER_SUSPEND:
  380. // task will display the line 2 from beginning and suspend
  381. displayer.state = DISPLAYER_IDLE;
  382. displayer_artwork(NULL);
  383. display_bus(&displayer, DISPLAY_BUS_GIVE);
  384. break;
  385. case DISPLAYER_SHUTDOWN:
  386. // let the task self-suspend (we might be doing i2c_write)
  387. GDS_SetTextWidth(display, 0);
  388. displayer_artwork(NULL);
  389. displayer.state = DISPLAYER_DOWN;
  390. display_bus(&displayer, DISPLAY_BUS_GIVE);
  391. break;
  392. case DISPLAYER_TIMER_RUN:
  393. if (!displayer.timer) {
  394. display_bus(&displayer, DISPLAY_BUS_TAKE);
  395. displayer.timer = true;
  396. displayer.tick = xTaskGetTickCount();
  397. }
  398. break;
  399. case DISPLAYER_TIMER_PAUSE:
  400. displayer.timer = false;
  401. break;
  402. default:
  403. break;
  404. }
  405. xSemaphoreGive(displayer.mutex);
  406. va_end(args);
  407. }
  408. /****************************************************************************************
  409. *
  410. */
  411. bool display_is_valid_driver(const char * driver){
  412. return display_conf_get_driver_name(driver)!=NULL;
  413. }
  414. /****************************************************************************************
  415. *
  416. */
  417. const char *display_conf_get_driver_name(const char * driver){
  418. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  419. if(strcasestr(driver,known_drivers[i])){
  420. return known_drivers[i];
  421. }
  422. }
  423. return NULL;
  424. }
  425. /****************************************************************************************
  426. *
  427. */
  428. char * display_get_supported_drivers(void){
  429. int total_size = 1;
  430. char * supported_drivers=NULL;
  431. const char * separator = "|";
  432. int separator_len = strlen(separator);
  433. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  434. total_size += strlen(known_drivers[i])+separator_len;
  435. }
  436. total_size+=2;
  437. supported_drivers = malloc(total_size);
  438. memset(supported_drivers,0x00,total_size);
  439. strcat(supported_drivers,"<");
  440. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  441. supported_drivers = strcat(supported_drivers,known_drivers[i]);
  442. supported_drivers = strcat(supported_drivers,separator);
  443. }
  444. strcat(supported_drivers,">");
  445. return supported_drivers;
  446. }