display.c 17 KB

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