WIP: improve weather

Fix Rust warnings etc.
This commit is contained in:
Wladimir J. van der Laan 2019-05-30 20:33:32 +00:00
parent 1235edb40c
commit 76fcc08c46

View File

@ -90,8 +90,8 @@ fn main() -> ! {
io_init(); io_init();
// Configure UARTHS (→host) // Configure UARTHS (→host)
let mut serial = p.UARTHS.constrain(DEFAULT_BAUD.bps(), &clocks); let serial = p.UARTHS.constrain(DEFAULT_BAUD.bps(), &clocks);
let (mut tx, mut rx) = serial.split(); let (mut tx, mut _rx) = serial.split();
let mut debug = Stdout(&mut tx); let mut debug = Stdout(&mut tx);
// Configure UART1 (→WIFI) // Configure UART1 (→WIFI)
@ -103,7 +103,7 @@ fn main() -> ! {
fpioa::set_io_pull(io::WIFI_EN as u8, fpioa::pull::DOWN); fpioa::set_io_pull(io::WIFI_EN as u8, fpioa::pull::DOWN);
gpiohs::set_pin(8, true); gpiohs::set_pin(8, true);
gpiohs::set_direction(8, gpio::direction::OUTPUT); gpiohs::set_direction(8, gpio::direction::OUTPUT);
let mut wifi_serial = p.UART1.constrain(DEFAULT_BAUD.bps(), &clocks); let wifi_serial = p.UART1.constrain(DEFAULT_BAUD.bps(), &clocks);
let (mut wtx, mut wrx) = wifi_serial.split(); let (mut wtx, mut wrx) = wifi_serial.split();
let mut wa = WriteAdapter::new(&mut wtx); let mut wa = WriteAdapter::new(&mut wtx);
@ -116,14 +116,13 @@ fn main() -> ! {
lcd.set_direction(lcd::direction::YX_LRUD); lcd.set_direction(lcd::direction::YX_LRUD);
let mut console: Console = Console::new(); let mut console: Console = Console::new();
let mut i:u16 = 0; writeln!(console, "\x1b[48;2;128;192;255;38;5;0m WEATHER \x1b[0m \x1b[38;2;128;128;128m\x1b[0m").unwrap();
writeln!(console, "\x1b[48;2;128;192;255;38;5;0m WEATHER \x1b[0m \x1b[38;2;128;128;128mfetching...\x1b[0m").unwrap();
// Start off connection process state machine // Start off connection process state machine
sh.start(false); sh.start(false).unwrap();
writeln!(console, "∙ Connecting to AP").unwrap(); writeln!(console, "∙ Connecting to AP").unwrap();
let mut serial_buf = [0u8; 2560]; // 2048 + some let mut serial_buf = [0u8; 8192];
let mut ofs: usize = 0; let mut ofs: usize = 0;
let mut cur_link = 0; let mut cur_link = 0;
@ -135,13 +134,9 @@ fn main() -> ! {
console.dirty = false; console.dirty = false;
} }
// Receive byte into buffer // Receive into buffer
if let Ok(ch) = wrx.read() {
let ofs0 = ofs;
serial_buf[ofs] = ch;
ofs += 1;
let mut lastrecv = mcycle::read(); let mut lastrecv = mcycle::read();
loop { while ofs < serial_buf.len() {
// Read until we stop receiving for a certain duration // Read until we stop receiving for a certain duration
// This is a hack around the fact that in the time that the parser runs, // This is a hack around the fact that in the time that the parser runs,
// more than one FIFO full of characters can be received so characters could be // more than one FIFO full of characters can be received so characters could be
@ -166,7 +161,7 @@ fn main() -> ! {
let tail = &serial_buf[start..ofs]; let tail = &serial_buf[start..ofs];
let erase = match parse_response(tail) { let erase = match parse_response(tail) {
Ok((residue, resp)) => { Ok((residue, resp)) => {
sh.message(&resp, &mut |port, ev, debug| { sh.message(&resp, &mut |port, ev, _debug| {
match ev { match ev {
NetworkEvent::Ready => { NetworkEvent::Ready => {
writeln!(console, "∙ Connected to AP").unwrap(); writeln!(console, "∙ Connected to AP").unwrap();
@ -176,6 +171,10 @@ fn main() -> ! {
NetworkEvent::Error => { NetworkEvent::Error => {
writeln!(console, "∙ Could not connect to AP").unwrap(); writeln!(console, "∙ Could not connect to AP").unwrap();
} }
NetworkEvent::ListenSuccess(ip, port) => {
writeln!(console, "∙ Listening on {}.{}.{}.{}:{}",
ip[0], ip[1], ip[2], ip[3], port).unwrap();
}
NetworkEvent::ConnectionEstablished(link) => { NetworkEvent::ConnectionEstablished(link) => {
if link == cur_link { if link == cur_link {
writeln!(console, "\x1b[38;5;141m[{}]\x1b[0m Sending HTTP request", link).unwrap(); writeln!(console, "\x1b[38;5;141m[{}]\x1b[0m Sending HTTP request", link).unwrap();
@ -203,7 +202,11 @@ fn main() -> ! {
0 0
} }
Err(err) => { Err(err) => {
if tail.len() > 100 {
writeln!(debug, "err: Error([too long ...])").unwrap();
} else {
writeln!(debug, "err: {:?}", err).unwrap(); writeln!(debug, "err: {:?}", err).unwrap();
}
// Erase unparseable data to next line, if line is complete // Erase unparseable data to next line, if line is complete
if let Some(ofs) = tail.iter().position(|&x| x == b'\n') { if let Some(ofs) = tail.iter().position(|&x| x == b'\n') {
ofs + 1 ofs + 1
@ -225,6 +228,11 @@ fn main() -> ! {
serial_buf[i - start] = serial_buf[i]; serial_buf[i - start] = serial_buf[i];
} }
ofs -= start; ofs -= start;
// If the buffer is full and we can't parse *anything*, clear it and start over
if ofs == serial_buf.len() {
writeln!(debug, "Error: buffer was unparseable, dropping buffer").unwrap();
ofs = 0;
} }
/* /*