Skip to content

Commit

Permalink
statusline fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
heeen committed Jul 28, 2015
1 parent b5c1d9a commit 72f0f5b
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 51 deletions.
133 changes: 101 additions & 32 deletions user/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(&timestamp);
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;
Expand All @@ -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() {
}
1 change: 1 addition & 0 deletions user/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
3 changes: 1 addition & 2 deletions user/ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}


Expand Down Expand Up @@ -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);
}
55 changes: 44 additions & 11 deletions user/uplink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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<len) s++;
if(*s != '"') {
tcp_print(conn, "error: expected quoted string.");
return;
}
s++; // skip "
while(dst < 128 && *s !='"' && *s != 0 && dst < len) {
temp[dst] = *s;
dst++; s++;
}
temp[dst] = 0;
os_printf("got %d chars: <%s>\n", dst, temp);
if(*s != '"') {
tcp_print(conn, "error: runaway quoted string.\n");
return;
}
s++; // skip "
while(*s == ' ' && s-data<len) s++;
if(s - data == len) {
tcp_print(conn, "using duration 5s.\n");
duration = 5;
} else {
duration = atoi(s);
os_printf("got duration %d>\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);
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 72f0f5b

Please sign in to comment.