display.c 17 KB

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