display.c 14 KB

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