display.c 15 KB

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