From 1d891dd31390728d7c916fdf7bc729c84fcc2824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 28 Jan 2017 01:42:34 +0100 Subject: [PATCH] added a 20 ms delay before sending WS update, improves performance when user repains entire screen quickly - just 1 update --- user/cgi_sockets.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/user/cgi_sockets.c b/user/cgi_sockets.c index 986a471..e10b35b 100644 --- a/user/cgi_sockets.c +++ b/user/cgi_sockets.c @@ -6,18 +6,15 @@ #include "uart_driver.h" #include "screen.h" -/** - * Broadcast screen state to sockets. - * This is a callback for the Screen module, - * called after each visible screen modification. - */ -void ICACHE_FLASH_ATTR screen_notifyChange() -{ - // TODO cooldown / buffering to reduce nr of such events +static volatile bool timer_running = false; +static ETSTimer notifyTim; + +static void notifyTimCb(void *arg) { + timer_running = false; void *data = NULL; - const int bufsiz = 512; + const int bufsiz = 1024; char buff[bufsiz]; for (int i = 0; i < 20; i++) { httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, &data); @@ -31,6 +28,21 @@ void ICACHE_FLASH_ATTR screen_notifyChange() screenSerializeToBuffer(NULL, bufsiz, &data); } +/** + * Broadcast screen state to sockets. + * This is a callback for the Screen module, + * called after each visible screen modification. + */ +void ICACHE_FLASH_ATTR screen_notifyChange(void) +{ + if (timer_running) return; + + timer_running = true; + os_timer_disarm(¬ifyTim); + os_timer_setfn(¬ifyTim, notifyTimCb, NULL); + os_timer_arm(¬ifyTim, 20, 0); +} + /** Socket received a message */ void ICACHE_FLASH_ATTR updateSockRx(Websock *ws, char *data, int len, int flags) {