display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. GDS_SetLayout(display, strcasestr(config, "HFlip"), strcasestr(config, "VFlip"), strcasestr(config, "rotate"));
  124. GDS_SetFont(display, &Font_droid_sans_fallback_15x17 );
  125. GDS_TextPos(display, GDS_FONT_MEDIUM, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, welcome);
  126. // start the task that will handle scrolling & counting
  127. displayer.mutex = xSemaphoreCreateMutex();
  128. displayer.by = 2;
  129. displayer.pause = 3600;
  130. displayer.speed = 33;
  131. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "common_displayer", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  132. // set lines for "fixed" text mode
  133. GDS_TextSetFontAuto(display, 1, GDS_FONT_LINE_1, -3);
  134. GDS_TextSetFontAuto(display, 2, GDS_FONT_LINE_2, -3);
  135. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  136. // leave room for artwork is display is horizontal-style
  137. if (strcasestr(displayer.metadata_config, "artwork")) {
  138. displayer.artwork.enable = true;
  139. displayer.artwork.fit = true;
  140. if (height <= 64 && width > height * 2) displayer.artwork.offset = width - height - ARTWORK_BORDER;
  141. PARSE_PARAM(displayer.metadata_config, "artwork", ':', displayer.artwork.fit);
  142. }
  143. }
  144. free(config);
  145. }
  146. /****************************************************************************************
  147. * This is not thread-safe as displayer_task might be in the middle of line drawing
  148. * but it won't crash (I think) and making it thread-safe would be complicated for a
  149. * feature which is secondary (the LMS version of scrolling is thread-safe)
  150. */
  151. static void displayer_task(void *args) {
  152. int scroll_sleep = 0, timer_sleep;
  153. while (1) {
  154. // suspend ourselves if nothing to do
  155. if (displayer.state < DISPLAYER_ACTIVE) {
  156. if (displayer.state == DISPLAYER_IDLE) GDS_TextLine(display, 2, 0, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.string);
  157. vTaskSuspend(NULL);
  158. scroll_sleep = 0;
  159. GDS_ClearExt(display, true);
  160. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_UPDATE, displayer.header);
  161. } else if (displayer.refresh) {
  162. // little trick when switching master while in IDLE and missing it
  163. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.header);
  164. displayer.refresh = false;
  165. }
  166. // we have been waken up before our requested time
  167. if (scroll_sleep <= 10) {
  168. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  169. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  170. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  171. // need to work with local copies as we don't want to suspend caller
  172. int offset = -displayer.offset;
  173. char *string = strdup(displayer.string);
  174. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  175. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  176. xSemaphoreGive(displayer.mutex);
  177. // now display using safe copies, can be lengthy
  178. GDS_TextLine(display, 2, offset, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, string);
  179. free(string);
  180. } else {
  181. scroll_sleep = DEFAULT_SLEEP;
  182. }
  183. }
  184. // handler elapsed track time
  185. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  186. char line[19] = "-", *_line = line + 1; // [-]H:MM:SS / H:MM:SS
  187. TickType_t tick = xTaskGetTickCount();
  188. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  189. if (elapsed >= 1000) {
  190. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  191. displayer.tick = tick;
  192. elapsed = displayer.elapsed += elapsed / 1000;
  193. xSemaphoreGive(displayer.mutex);
  194. // when we have duration but no space, display remaining time
  195. if (displayer.duration.value && !displayer.duration.visible) elapsed = displayer.duration.value - elapsed;
  196. if (elapsed < 3600) sprintf(_line, "%u:%02u", elapsed / 60, elapsed % 60);
  197. else sprintf(_line, "%u:%02u:%02u", (elapsed / 3600) % 100, (elapsed % 3600) / 60, elapsed % 60);
  198. // concatenate if we have room for elapsed / duration
  199. if (displayer.duration.visible) {
  200. strcat(_line, "/");
  201. strcat(_line, displayer.duration.string);
  202. } else if (displayer.duration.value) {
  203. _line--;
  204. }
  205. // just re-write the whole line it's easier
  206. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, displayer.header);
  207. GDS_TextLine(display, 1, GDS_TEXT_RIGHT, GDS_TEXT_UPDATE, _line);
  208. // if we have not received artwork after 5s, display a default icon
  209. if (displayer.artwork.active && !displayer.artwork.updated && tick - displayer.artwork.tick > pdMS_TO_TICKS(5000)) {
  210. ESP_LOGI(TAG, "no artwork received, setting default");
  211. displayer_artwork((uint8_t*) default_artwork);
  212. }
  213. timer_sleep = 1000;
  214. } else timer_sleep = max(1000 - elapsed, 0);
  215. } else timer_sleep = DEFAULT_SLEEP;
  216. // then sleep the min amount of time
  217. int sleep = min(scroll_sleep, timer_sleep);
  218. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  219. scroll_sleep -= sleep;
  220. vTaskDelay(sleep / portTICK_PERIOD_MS);
  221. }
  222. }
  223. /****************************************************************************************
  224. *
  225. */
  226. void displayer_artwork(uint8_t *data) {
  227. if (!displayer.artwork.active) return;
  228. int x = displayer.artwork.offset ? displayer.artwork.offset + ARTWORK_BORDER : 0;
  229. int y = x ? 0 : 32;
  230. GDS_ClearWindow(display, x, y, -1, -1, GDS_COLOR_BLACK);
  231. if (data) {
  232. displayer.artwork.updated = true;
  233. GDS_DrawJPEG(display, data, x, y, GDS_IMAGE_CENTER | (displayer.artwork.fit ? GDS_IMAGE_FIT : 0));
  234. } else {
  235. displayer.artwork.updated = false;
  236. displayer.artwork.tick = xTaskGetTickCount();
  237. }
  238. }
  239. /****************************************************************************************
  240. *
  241. */
  242. void displayer_metadata(char *artist, char *album, char *title) {
  243. char *string = displayer.string, *p;
  244. int len = SCROLLABLE_SIZE;
  245. // need a display!
  246. if (!display) return;
  247. // just do title if there is no config set
  248. if (!displayer.metadata_config) {
  249. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  250. return;
  251. }
  252. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  253. // format metadata parameters and write them directly
  254. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  255. char token[16], *q;
  256. int space = len;
  257. bool skip = false;
  258. displayer.string[0] = '\0';
  259. p = strchr(displayer.metadata_config, '=');
  260. while (p++) {
  261. // find token and copy what's after when reaching last one
  262. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  263. q = strchr(p, ',');
  264. strncat(string, p, q ? min(q - p, space) : space);
  265. break;
  266. }
  267. // copy what's before token (be safe)
  268. if ((q = strchr(p, '%')) == NULL) break;
  269. // skip whatever is after a token if this token is empty
  270. if (!skip) {
  271. strncat(string, p, min(q - p, space));
  272. space = len - strlen(string);
  273. }
  274. // then copy token's content
  275. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  276. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  277. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  278. space = len - strlen(string);
  279. // flag to skip the data following an empty field
  280. if (*p) skip = false;
  281. else skip = true;
  282. // advance to next separator
  283. p = strchr(q + 1, '%');
  284. }
  285. } else {
  286. strncpy(string, title ? title : "", SCROLLABLE_SIZE);
  287. }
  288. // get optional scroll speed & pause
  289. PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
  290. PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
  291. displayer.offset = 0;
  292. utf8_decode(displayer.string);
  293. ESP_LOGI(TAG, "playing %s", displayer.string);
  294. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  295. xSemaphoreGive(displayer.mutex);
  296. }
  297. /****************************************************************************************
  298. *
  299. */
  300. void displayer_scroll(char *string, int speed, int pause) {
  301. // need a display!
  302. if (!display) return;
  303. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  304. if (speed) displayer.speed = speed;
  305. if (pause) displayer.pause = pause;
  306. displayer.offset = 0;
  307. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  308. displayer.string[SCROLLABLE_SIZE] = '\0';
  309. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  310. xSemaphoreGive(displayer.mutex);
  311. }
  312. /****************************************************************************************
  313. *
  314. */
  315. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  316. // need a display!
  317. if (!display) return;
  318. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  319. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  320. if (elapsed >= 0) displayer.elapsed = elapsed / 1000;
  321. if (duration > 0) {
  322. displayer.duration.visible = true;
  323. displayer.duration.value = duration / 1000;
  324. if (displayer.duration.value > 3600) sprintf(displayer.duration.string, "%u:%02u:%02u", (displayer.duration.value / 3600) % 10,
  325. (displayer.duration.value % 3600) / 60, displayer.duration.value % 60);
  326. else sprintf(displayer.duration.string, "%u:%02u", displayer.duration.value / 60, displayer.duration.value % 60);
  327. char *buf;
  328. asprintf(&buf, "%s %s/%s", displayer.header, displayer.duration.string, displayer.duration.string);
  329. if (GDS_GetTextWidth(display, 1, 0, buf) > GDS_GetWidth(display)) {
  330. ESP_LOGW(TAG, "Can't fit duration %s (%d) on screen using elapsed only", buf, GDS_GetTextWidth(display, 1, 0, buf));
  331. displayer.duration.visible = false;
  332. }
  333. free(buf);
  334. } else if (!duration) {
  335. displayer.duration.visible = false;
  336. displayer.duration.value = 0;
  337. }
  338. xSemaphoreGive(displayer.mutex);
  339. }
  340. /****************************************************************************************
  341. * See above comment
  342. */
  343. void displayer_control(enum displayer_cmd_e cmd, ...) {
  344. va_list args;
  345. if (!display) return;
  346. va_start(args, cmd);
  347. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  348. switch(cmd) {
  349. case DISPLAYER_ACTIVATE: {
  350. char *header = va_arg(args, char*);
  351. displayer.artwork.active = displayer.artwork.enable && va_arg(args, int);
  352. strncpy(displayer.header, header, HEADER_SIZE);
  353. displayer.header[HEADER_SIZE] = '\0';
  354. displayer.state = DISPLAYER_ACTIVE;
  355. displayer.timer = false;
  356. displayer.refresh = true;
  357. displayer.string[0] = '\0';
  358. displayer.elapsed = displayer.duration.value = 0;
  359. displayer.duration.visible = false;
  360. displayer.offset = displayer.boundary = 0;
  361. display_bus(&displayer, DISPLAY_BUS_TAKE);
  362. if (displayer.artwork.active) GDS_SetTextWidth(display, displayer.artwork.offset);
  363. vTaskResume(displayer.task);
  364. break;
  365. }
  366. case DISPLAYER_SUSPEND:
  367. // task will display the line 2 from beginning and suspend
  368. displayer.state = DISPLAYER_IDLE;
  369. displayer_artwork(NULL);
  370. display_bus(&displayer, DISPLAY_BUS_GIVE);
  371. break;
  372. case DISPLAYER_SHUTDOWN:
  373. // let the task self-suspend (we might be doing i2c_write)
  374. GDS_SetTextWidth(display, 0);
  375. displayer_artwork(NULL);
  376. displayer.state = DISPLAYER_DOWN;
  377. display_bus(&displayer, DISPLAY_BUS_GIVE);
  378. break;
  379. case DISPLAYER_TIMER_RUN:
  380. if (!displayer.timer) {
  381. display_bus(&displayer, DISPLAY_BUS_TAKE);
  382. displayer.timer = true;
  383. displayer.tick = xTaskGetTickCount();
  384. }
  385. break;
  386. case DISPLAYER_TIMER_PAUSE:
  387. displayer.timer = false;
  388. break;
  389. default:
  390. break;
  391. }
  392. xSemaphoreGive(displayer.mutex);
  393. va_end(args);
  394. }
  395. /****************************************************************************************
  396. *
  397. */
  398. bool display_is_valid_driver(const char * driver){
  399. return display_conf_get_driver_name(driver)!=NULL;
  400. }
  401. /****************************************************************************************
  402. *
  403. */
  404. const char *display_conf_get_driver_name(const char * driver){
  405. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  406. if(strcasestr(driver,known_drivers[i])){
  407. return known_drivers[i];
  408. }
  409. }
  410. return NULL;
  411. }
  412. /****************************************************************************************
  413. *
  414. */
  415. char * display_get_supported_drivers(void){
  416. int total_size = 1;
  417. char * supported_drivers=NULL;
  418. const char * separator = "|";
  419. int separator_len = strlen(separator);
  420. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  421. total_size += strlen(known_drivers[i])+separator_len;
  422. }
  423. total_size+=2;
  424. supported_drivers = malloc(total_size);
  425. memset(supported_drivers,0x00,total_size);
  426. strcat(supported_drivers,"<");
  427. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  428. supported_drivers = strcat(supported_drivers,known_drivers[i]);
  429. supported_drivers = strcat(supported_drivers,separator);
  430. }
  431. strcat(supported_drivers,">");
  432. return supported_drivers;
  433. }