display.c 13 KB

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