display.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdint.h>
  22. #include <arpa/inet.h>
  23. #include "esp_log.h"
  24. #include "globdefs.h"
  25. #include "config.h"
  26. #include "tools.h"
  27. #include "display.h"
  28. #include "gds.h"
  29. #include "gds_default_if.h"
  30. #include "gds_draw.h"
  31. #include "gds_text.h"
  32. #include "gds_font.h"
  33. static const char *TAG = "display";
  34. #define min(a,b) (((a) < (b)) ? (a) : (b))
  35. #define max(a,b) (((a) > (b)) ? (a) : (b))
  36. #define DISPLAYER_STACK_SIZE (3*1024)
  37. #define SCROLLABLE_SIZE 384
  38. #define HEADER_SIZE 64
  39. #define DEFAULT_SLEEP 3600
  40. static EXT_RAM_ATTR struct {
  41. TaskHandle_t task;
  42. SemaphoreHandle_t mutex;
  43. int pause, speed, by;
  44. enum { DISPLAYER_DOWN, DISPLAYER_IDLE, DISPLAYER_ACTIVE } state;
  45. char header[HEADER_SIZE + 1];
  46. char string[SCROLLABLE_SIZE + 1];
  47. int offset, boundary;
  48. char *metadata_config;
  49. bool timer, refresh;
  50. uint32_t elapsed, duration;
  51. TickType_t tick;
  52. } displayer;
  53. static void displayer_task(void *args);
  54. struct GDS_Device *display;
  55. struct GDS_Device* SSD1306_Detect(char *Driver, struct GDS_Device* Device);
  56. struct GDS_Device* SH1106_Detect(char *Driver, struct GDS_Device* Device);
  57. GDS_DetectFunc drivers[] = { SH1106_Detect, SSD1306_Detect, NULL };
  58. /****************************************************************************************
  59. *
  60. */
  61. void display_init(char *welcome) {
  62. bool init = false;
  63. char *config = config_alloc_get(NVS_TYPE_STR, "display_config");
  64. if (!config) {
  65. ESP_LOGI(TAG, "no display");
  66. return false;
  67. }
  68. int width = -1, height = -1;
  69. char *p, *drivername = strstr(config, "driver");
  70. // query drivers to see if we have a match
  71. ESP_LOGI(TAG, "Trying to configure display with %s", config);
  72. display = GDS_AutoDetect(drivername ? drivername : "SSD1306", drivers);
  73. if ((p = strcasestr(config, "width")) != NULL) width = atoi(strchr(p, '=') + 1);
  74. if ((p = strcasestr(config, "height")) != NULL) height = atoi(strchr(p, '=') + 1);
  75. // so far so good
  76. if (display && width > 0 && height > 0) {
  77. // Detect driver interface
  78. if (strstr(config, "I2C") && i2c_system_port != -1) {
  79. int address = 0x3C;
  80. if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1);
  81. init = true;
  82. GDS_I2CInit( i2c_system_port, -1, -1 ) ;
  83. GDS_I2CAttachDevice( display, width, height, address, -1 );
  84. ESP_LOGI(TAG, "Display is I2C on port %u", address);
  85. } else if (strstr(config, "SPI") && spi_system_host != -1) {
  86. int CS_pin = -1, speed = 0;
  87. if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1);
  88. if ((p = strcasestr(config, "speed")) != NULL) speed = atoi(strchr(p, '=') + 1);
  89. init = true;
  90. GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
  91. GDS_SPIAttachDevice( display, width, height, CS_pin, -1, speed );
  92. ESP_LOGI(TAG, "Display is SPI host %u with cs:%d", spi_system_host, CS_pin);
  93. } else {
  94. display = NULL;
  95. ESP_LOGI(TAG, "Unsupported display interface or serial link not configured");
  96. }
  97. } else {
  98. display = NULL;
  99. ESP_LOGW(TAG, "No display driver");
  100. }
  101. if (init) {
  102. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  103. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  104. GDS_SetHFlip(display, strcasestr(config, "HFlip") ? true : false);
  105. GDS_SetVFlip(display, strcasestr(config, "VFlip") ? true : false);
  106. GDS_SetFont(display, &Font_droid_sans_fallback_15x17 );
  107. GDS_TextPos(display, GDS_FONT_MEDIUM, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, welcome);
  108. // start the task that will handle scrolling & counting
  109. displayer.mutex = xSemaphoreCreateMutex();
  110. displayer.by = 2;
  111. displayer.pause = 3600;
  112. displayer.speed = 33;
  113. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  114. // set lines for "fixed" text mode
  115. GDS_TextSetFontAuto(display, 1, GDS_FONT_LINE_1, -3);
  116. GDS_TextSetFontAuto(display, 2, GDS_FONT_LINE_2, -3);
  117. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  118. }
  119. free(config);
  120. }
  121. /****************************************************************************************
  122. * This is not thread-safe as displayer_task might be in the middle of line drawing
  123. * but it won't crash (I think) and making it thread-safe would be complicated for a
  124. * feature which is secondary (the LMS version of scrolling is thread-safe)
  125. */
  126. static void displayer_task(void *args) {
  127. int scroll_sleep = 0, timer_sleep;
  128. while (1) {
  129. // suspend ourselves if nothing to do
  130. if (displayer.state < DISPLAYER_ACTIVE) {
  131. if (displayer.state == DISPLAYER_IDLE) GDS_TextLine(display, 2, 0, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.string);
  132. vTaskSuspend(NULL);
  133. scroll_sleep = 0;
  134. GDS_ClearExt(display, true);
  135. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_UPDATE, displayer.header);
  136. } else if (displayer.refresh) {
  137. // little trick when switching master while in IDLE and missing it
  138. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.header);
  139. displayer.refresh = false;
  140. }
  141. // we have been waken up before our requested time
  142. if (scroll_sleep <= 10) {
  143. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  144. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  145. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  146. // need to work with local copies as we don't want to suspend caller
  147. int offset = -displayer.offset;
  148. char *string = strdup(displayer.string);
  149. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  150. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  151. xSemaphoreGive(displayer.mutex);
  152. // now display using safe copies, can be lengthy
  153. GDS_TextLine(display, 2, offset, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, string);
  154. free(string);
  155. } else {
  156. scroll_sleep = DEFAULT_SLEEP;
  157. }
  158. }
  159. // handler elapsed track time
  160. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  161. char counter[12];
  162. TickType_t tick = xTaskGetTickCount();
  163. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  164. if (elapsed >= 1000) {
  165. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  166. displayer.tick = tick;
  167. displayer.elapsed += elapsed / 1000;
  168. xSemaphoreGive(displayer.mutex);
  169. if (displayer.elapsed < 3600) sprintf(counter, "%5u:%02u", displayer.elapsed / 60, displayer.elapsed % 60);
  170. else sprintf(counter, "%2u:%02u:%02u", displayer.elapsed / 3600, (displayer.elapsed % 3600) / 60, displayer.elapsed % 60);
  171. GDS_TextLine(display, 1, GDS_TEXT_RIGHT, (GDS_TEXT_CLEAR | GDS_TEXT_CLEAR_EOL) | GDS_TEXT_UPDATE, counter);
  172. timer_sleep = 1000;
  173. } else timer_sleep = max(1000 - elapsed, 0);
  174. } else timer_sleep = DEFAULT_SLEEP;
  175. // then sleep the min amount of time
  176. int sleep = min(scroll_sleep, timer_sleep);
  177. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  178. scroll_sleep -= sleep;
  179. vTaskDelay(sleep / portTICK_PERIOD_MS);
  180. }
  181. }
  182. /****************************************************************************************
  183. *
  184. */
  185. void displayer_metadata(char *artist, char *album, char *title) {
  186. char *string = displayer.string, *p;
  187. int len = SCROLLABLE_SIZE;
  188. // need a display!
  189. if (!display) return;
  190. // just do title if there is no config set
  191. if (!displayer.metadata_config) {
  192. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  193. return;
  194. }
  195. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  196. // format metadata parameters and write them directly
  197. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  198. char token[16], *q;
  199. int space = len;
  200. bool skip = false;
  201. displayer.string[0] = '\0';
  202. p = strchr(displayer.metadata_config, '=');
  203. while (p++) {
  204. // find token and copy what's after when reaching last one
  205. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  206. q = strchr(p, ',');
  207. strncat(string, p, q ? min(q - p, space) : space);
  208. break;
  209. }
  210. // copy what's before token (be safe)
  211. if ((q = strchr(p, '%')) == NULL) break;
  212. // skip whatever is after a token if this token is empty
  213. if (!skip) {
  214. strncat(string, p, min(q - p, space));
  215. space = len - strlen(string);
  216. }
  217. // then copy token's content
  218. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  219. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  220. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  221. space = len - strlen(string);
  222. // flag to skip the data following an empty field
  223. if (*p) skip = false;
  224. else skip = true;
  225. // advance to next separator
  226. p = strchr(q + 1, '%');
  227. }
  228. } else {
  229. strncpy(string, title ? title : "", SCROLLABLE_SIZE);
  230. }
  231. // get optional scroll speed
  232. if ((p = strcasestr(displayer.metadata_config, "speed")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.speed);
  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) {
  243. // need a display!
  244. if (!display) return;
  245. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  246. if (speed) displayer.speed = speed;
  247. displayer.offset = 0;
  248. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  249. displayer.string[SCROLLABLE_SIZE] = '\0';
  250. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  251. xSemaphoreGive(displayer.mutex);
  252. }
  253. /****************************************************************************************
  254. *
  255. */
  256. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  257. // need a display!
  258. if (!display) return;
  259. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  260. if (elapsed >= 0) displayer.elapsed = elapsed / 1000;
  261. if (duration >= 0) displayer.duration = duration / 1000;
  262. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  263. xSemaphoreGive(displayer.mutex);
  264. }
  265. /****************************************************************************************
  266. * See above comment
  267. */
  268. void displayer_control(enum displayer_cmd_e cmd, ...) {
  269. va_list args;
  270. if (!display) return;
  271. va_start(args, cmd);
  272. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  273. switch(cmd) {
  274. case DISPLAYER_ACTIVATE: {
  275. char *header = va_arg(args, char*);
  276. strncpy(displayer.header, header, HEADER_SIZE);
  277. displayer.header[HEADER_SIZE] = '\0';
  278. displayer.state = DISPLAYER_ACTIVE;
  279. displayer.timer = false;
  280. displayer.refresh = true;
  281. displayer.string[0] = '\0';
  282. displayer.elapsed = displayer.duration = 0;
  283. displayer.offset = displayer.boundary = 0;
  284. display_bus(&displayer, DISPLAY_BUS_TAKE);
  285. vTaskResume(displayer.task);
  286. break;
  287. }
  288. case DISPLAYER_SUSPEND:
  289. // task will display the line 2 from beginning and suspend
  290. displayer.state = DISPLAYER_IDLE;
  291. display_bus(&displayer, DISPLAY_BUS_GIVE);
  292. break;
  293. case DISPLAYER_SHUTDOWN:
  294. // let the task self-suspend (we might be doing i2c_write)
  295. displayer.state = DISPLAYER_DOWN;
  296. display_bus(&displayer, DISPLAY_BUS_GIVE);
  297. break;
  298. case DISPLAYER_TIMER_RUN:
  299. if (!displayer.timer) {
  300. displayer.timer = true;
  301. displayer.tick = xTaskGetTickCount();
  302. }
  303. break;
  304. case DISPLAYER_TIMER_PAUSE:
  305. displayer.timer = false;
  306. break;
  307. default:
  308. break;
  309. }
  310. xSemaphoreGive(displayer.mutex);
  311. va_end(args);
  312. }