diff --git a/user/display.c b/user/display.c index d406f3d..6e5d4d9 100644 --- a/user/display.c +++ b/user/display.c @@ -13,44 +13,73 @@ status_line* create_status(const char* text); void destroy_status(status_line* s); static status_line* head = 0; +static status_line* tail = 0; static status_line* current = 0; +static bool statusline_dirty = false; static os_timer_t display_update_timer; -static int scrollpos = -20; +static int scrollpos = 0; void update_display(void *arg); void dumpstatus(); +void append(status_line* s); + +static char temp[21]; +static bool disabled = false; void statusline(const char* text, unsigned short duration) { status_line* s = create_status(text); - s->next = head; s->until = gettime() + duration; - head = s; + append(s); dumpstatus(); } +void append(status_line* s) { + if(tail) + tail->next = s; + tail = s; + if(!head) + head = s; + statusline_dirty = true; +} + void dumpstatus() { + os_printf("head: %p cur: %p tail: %p\n",head, current, tail); status_line* s = head; + int i = 0; while(s) { + os_printf("%d: %p %s\n",i++,s,s->text); s = s->next; } } status_line* create_status(const char* text){ + os_printf("%s\n", text); status_line* s = (status_line*) os_zalloc(sizeof(status_line)); + if(!s) { + os_printf("status alloc failed!"); + return 0; + } os_memset(s, 0, sizeof(status_line)); int len = os_strlen(text); - s->text = (char*) os_zalloc(len); + s->text = (char*) os_zalloc(len+1); + if(!s->text) { + os_free(s); + os_printf("status alloc failed!"); + return 0; + } s->len = len; os_strcpy(s->text, text); - os_printf("create status: %p %s\n", s, s->text); + os_printf("create status: %p %s %d\n", s, s->text, system_get_free_heap_size()); return s; } void destroy_status(status_line* s) { status_line* i = head; + if(s == tail) tail = 0; + if(s == head) { - head = 0; + head = head->next; } else { // find previous while(i && i->next != s) i = i->next; @@ -64,42 +93,44 @@ void destroy_status(status_line* s) { void start_display() { + disabled = false; os_timer_disarm(&display_update_timer); os_timer_setfn(&display_update_timer, (os_timer_func_t *)update_display, NULL); - os_timer_arm(&display_update_timer, 200, 1); + os_timer_arm(&display_update_timer, 250, 0); statusline("display enabled", 1); } void stop_display() { + disabled = true; os_timer_disarm(&display_update_timer); } void ICACHE_FLASH_ATTR -update_display(void *arg) -{ - time_t timestamp = gettime(); - struct tm *dt = gmtime(×tamp); +update_display_time(time_t t) { + static time_t last; + if(t == last) return; + struct tm *dt = gmtime(&t); bool nightMode = false; + if(dt->tm_hour > 22 || dt->tm_hour < 7) nightMode = true; - char timestr[21]; + + if(nightMode && dt->tm_sec) return; + if(nightMode) - os_sprintf(timestr, "%02d:%02d ", dt->tm_hour, dt->tm_min, dt->tm_sec); + os_sprintf(temp, "%02d:%02d", dt->tm_hour, dt->tm_min); else - os_sprintf(timestr, "%02d:%02d:%02d ", dt->tm_hour, dt->tm_min, dt->tm_sec); - vfd_pos(0,0); - vfd_print(timestr); + os_sprintf(temp, "%02d:%02d:%02d", dt->tm_hour, dt->tm_min, dt->tm_sec); + vfd_pos(0,0); + vfd_print(temp); + last = t; +} - if(timestamp - last_ntp_update > 30*60) { - statusline("fetching network time", 1); - ntp_get_time(); - vfd_pos(18, 0); - vfd_print("?"); - } - - vfd_bars_char(128, rssi); - vfd_pos(19, 0); - uart_tx_one_char(UART1, 128); +bool ICACHE_FLASH_ATTR +update_display_statusline(time_t timestamp) { + bool scrolling = false; + if(!statusline_dirty) + return scrolling; if(!current && head) current = head; @@ -109,23 +140,61 @@ update_display(void *arg) int x = 0; for(x = 0; x < 20; x++) { int i = x + scrollpos; - if(i < 0 || i >= current->len) + if(i == -2 || i == current->len + 1) + uart_tx_one_char(UART1, '*'); + else if(i < 0 || i >= current->len) uart_tx_one_char(UART1, ' '); else uart_tx_one_char(UART1, current->text[i]); } scrollpos ++; - if(scrollpos > current->len) { - dumpstatus(); - scrollpos = -20; + if((current->len <= 20 && scrollpos > 3) + || (current->len > 20 && scrollpos > current->len - 20 + 3)) { + scrollpos = -3; status_line* c = current; current = current->next; + statusline_dirty = true; if(c->until < timestamp) { destroy_status(c); } - dumpstatus(); + } else { + scrolling = true; } } else { - vfd_print("-"); + if(statusline_dirty) { + vfd_print(" "); + statusline_dirty = false; + } } + return scrolling; + +} + +void ICACHE_FLASH_ATTR +update_display(void *arg) +{ + if(disabled) return; + time_t timestamp = gettime(); + bool scrolling = false; + + update_display_time(timestamp); + + vfd_pos(18, 0); + if(timestamp - last_ntp_update > 30*60) { + statusline("fetching network time", 1); + ntp_get_time(); + vfd_print("?"); + } else { + vfd_print(" "); + } + + vfd_bars_char(128, rssi); + uart_tx_one_char(UART1, 128); + + scrolling = update_display_statusline(timestamp); + + os_timer_arm(&display_update_timer, scrolling ? 250 : 1000, 0); +} + +void display_small_update() { } diff --git a/user/display.h b/user/display.h index 9d8766a..83129d3 100644 --- a/user/display.h +++ b/user/display.h @@ -14,5 +14,6 @@ typedef struct status_line { void start_display(); void stop_display(); void statusline(const char* text, unsigned short duration); +void display_small_update(); #endif /* DISPLAY_H */ diff --git a/user/ntp.c b/user/ntp.c index 89a7678..a6907c5 100644 --- a/user/ntp.c +++ b/user/ntp.c @@ -47,7 +47,6 @@ static void ICACHE_FLASH_ATTR ntp_udp_recv(void *arg, char *pdata, unsigned shor ntp_t *ntp; os_timer_disarm(&ntp_timeout); - os_timer_disarm(&tic); // extract ntp time ntp = (ntp_t*)pdata; @@ -67,7 +66,6 @@ static void ICACHE_FLASH_ATTR ntp_udp_recv(void *arg, char *pdata, unsigned shor os_free(pCon); pCon = 0; } - os_timer_arm(&tic, 1000, 1); } @@ -101,4 +99,5 @@ void ICACHE_FLASH_ATTR ntp_get_time() { os_timer_disarm(&tic); os_timer_setfn(&tic, (os_timer_func_t*)tictoc, pCon); + os_timer_arm(&tic, 1000, 1); } diff --git a/user/uplink.c b/user/uplink.c index 56db3d1..dabca83 100644 --- a/user/uplink.c +++ b/user/uplink.c @@ -16,6 +16,7 @@ #include "rboot-ota.h" #include "ntp.h" #include "common.h" +#include "display.h" static struct espconn uplink_conn; static esp_tcp uplink_tcp_conn; @@ -36,7 +37,7 @@ void uplink_ota(); static void ICACHE_FLASH_ATTR tcp_print(struct espconn* con, char* str); void uplink_start() { - os_printf("connecting to the mothership\n"); + statusline("connecting to the mothership",1); os_printf("looking up <%s>\n", mothership_hostname); espconn_gethostbyname(&uplink_conn, mothership_hostname, &mothership_ip, mothership_resolved); @@ -106,7 +107,7 @@ mothership_resolved(const char *name, ip_addr_t *ipaddr, void *arg) espconn_set_keepalive(pespconn, ESPCONN_KEEPCNT, &nKeepaliveParam); espconn_set_opt(pespconn,ESPCONN_KEEPALIVE); - print("connecting!\n"); + statusline("connection...",1); espconn_connect(&uplink_conn); } @@ -118,23 +119,23 @@ static void ICACHE_FLASH_ATTR tcp_print(struct espconn* con, char* str) { } static void ICACHE_FLASH_ATTR uplink_recvCb(void *arg, char *data, unsigned short len) { - char temp[32]; + print("recv\n"); + char temp[128]; struct espconn *conn = (struct espconn *) arg; uart0_tx_buffer(data,len); print("\n"); + if(strncmp(data, "OTA", 3) == 0) { - print("got OTA request\n"); + statusline("got OTA request",0); uplink_ota(); } else if(strncmp(data, "ROM0", 4) == 0) { - print("load rom0 request\n"); rboot_set_current_rom(0); - print("Restarting into rom 0...\n"); + statusline("boot rom 0...",1); tcp_print(conn, "Restarting into rom 0...\n"); system_restart(); } else if(strncmp(data, "ROM1", 4) == 0) { - print("load rom1 request\n"); rboot_set_current_rom(1); - print("Restarting into rom 1...\n"); + statusline("boot rom 1...",1); tcp_print(conn, "Restarting into rom 1...\n"); system_restart(); } else if(strncmp(data, "ping", 4) == 0) { @@ -150,13 +151,45 @@ static void ICACHE_FLASH_ATTR uplink_recvCb(void *arg, char *data, unsigned shor } else if(strncmp(data, "vdd", 3) == 0) { os_sprintf(temp, "VDD=%d\n", readvdd33()); tcp_print(conn, temp); + } else if(strncmp(data, "status", 6) == 0) { + int duration; + char* s=data+6; + int dst=0; + while(*s == ' ' && s-data\n", dst, temp); + if(*s != '"') { + tcp_print(conn, "error: runaway quoted string.\n"); + return; + } + s++; // skip " + while(*s == ' ' && s-data\n", duration); + } + + statusline(temp, duration); + } } static void ICACHE_FLASH_ATTR uplink_connectedCb(void *arg) { - char temp[128]; + char temp[64]; sint8 d; - print("uplink connected\n"); + statusline("connected to mothership.",1); struct espconn *conn=(struct espconn *)arg; char macaddr[6]; wifi_get_macaddr(STATION_IF, macaddr); @@ -182,7 +215,7 @@ static void ICACHE_FLASH_ATTR uplink_reconCb(void *arg, sint8 err) { } static void ICACHE_FLASH_ATTR uplink_disconCb(void *arg) { - print("uplink disconnected\n"); + statusline("mothership disconnected",1); os_timer_disarm(&recon_timer); os_timer_disarm(&alive_timer); os_timer_arm(&recon_timer, 1000, 0); diff --git a/user/user_main.c b/user/user_main.c index a88f9fa..659159d 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -63,7 +63,8 @@ void ICACHE_FLASH_ATTR enable_clock() { //Do nothing function static void ICACHE_FLASH_ATTR user_procTask(os_event_t *events) { - os_delay_us(10); + + display_small_update(); } void ICACHE_FLASH_ATTR @@ -92,7 +93,7 @@ check_ap_joined(void *arg) static uint8_t prev_status = 128; //STATION_IDLE; if(wifi_get_opmode() != STATION_MODE) { - print("switching to station mode\n"); + statusline("switching to station mode",1); ETS_UART_INTR_DISABLE(); wifi_set_opmode(STATION_MODE); ETS_UART_INTR_ENABLE(); diff --git a/user/wifimgr.c b/user/wifimgr.c index 2acfeca..a87195b 100644 --- a/user/wifimgr.c +++ b/user/wifimgr.c @@ -2,12 +2,13 @@ #include "mem.h" #include #include "wificfg.h" +#include "display.h" static void ICACHE_FLASH_ATTR scan_done(void *arg, STATUS status) { if (status != OK) { - os_printf("wifi scan error!\n"); + statusline("wifi scan errorn", 1); return; } @@ -21,7 +22,9 @@ scan_done(void *arg, STATUS status) const char* ssid = wifis[j][0]; const char* pass = wifis[j][1]; if(strcmp(ssid, bss_link->ssid) == 0) { - os_printf("found known network %s\n", ssid); + char temp[128]; + os_sprintf(temp, "found known network %s", ssid); + statusline(temp, 2); struct station_config stationConf; os_bzero(&stationConf, sizeof(struct station_config)); os_strcpy(&stationConf.ssid, ssid); @@ -31,6 +34,7 @@ scan_done(void *arg, STATUS status) wifi_station_set_config(&stationConf); ETS_UART_INTR_ENABLE(); wifi_station_connect(); + break; } } bss_link = bss_link->next.stqe_next; @@ -45,8 +49,8 @@ connect_known_ap() { wifi_set_opmode(STATION_MODE); ETS_UART_INTR_ENABLE(); - os_printf("XXX scanning\n"); + statusline("starting WIFI scan", 1); if(!wifi_station_scan(NULL, scan_done)) { - os_printf("XXX scanning failed!\n"); + statusline("WIFI scan failed!", 1); } }