display.c 13 KB

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