The London Hackspace bandwidth meter
Dependencies: LPD8806 MODSERIAL mbed picojson
See:
- https://github.com/londonhackspace/net-o-meter
- http://wiki.london.hackspace.org.uk/view/Projects/BandwidthMeter
Revision 4:7087ea3d13c1, committed 2012-08-27
- Comitter:
- Jasper
- Date:
- Mon Aug 27 01:32:52 2012 +0000
- Parent:
- 3:7fca72f96711
- Child:
- 5:24172484b8c5
- Commit message:
- much fiddlign later, better cmd processor on the serial interface, give up on netcode for the moment
Changed in this revision
--- a/main.cpp Thu Aug 23 00:17:04 2012 +0000 +++ b/main.cpp Mon Aug 27 01:32:52 2012 +0000 @@ -4,13 +4,19 @@ #include "LPD8806.h" #include "vfd.h" +/* +#include "EthernetNetIf.h" +#include "HTTPServer.h" + +EthernetNetIf eth; +HTTPServer svr; +*/ + DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); -Ethernet eth; - /* talk to the world */ Serial pc(USBTX, USBRX); @@ -36,7 +42,7 @@ void top_strip(int quantity){ if (quantity < 16) { // blank unused bits. - setPixelsTop(quantity, 16, 0); + setPixelsTop(quantity, 15, 0); } if (quantity == 0) return; @@ -55,7 +61,7 @@ void bottom_strip(int quantity){ if (quantity < 16) { // blank unused bits. - setPixelsBottom(quantity, 16, 0); + setPixelsBottom(quantity, 15, 0); } if (quantity == 0) return; @@ -75,46 +81,140 @@ setPixelsTop(0, 15, strip.Color(0, 161, 228)); } +void strip_clear() { + int i; + + for (i = 0 ; i < strip.numPixels() ; i++) { + // clear the strip + strip.setPixelColor(i, 0); + } +} + #define s_looking 1 #define s_top 2 #define s_bottom 3 +void send_text(const char *s) { + int i; char c; + + for (i = 0, c=s[i]; c != 0; i++, c=s[i]) + vfd_data(c); +} + +void text_centered(const char *s, int hpos) { + + vfd_pos((20 / 2) - (strlen(s)) / 2, hpos); + send_text(s); +} + +void emf_logo(void) { + const char line0[] = "Welcome to"; + const char line2[] = "EMFCamp!"; + + text_centered(line0, 0); + text_centered(line2, 2); +} + int main() { - int t = 0, b = 0; - int i, state = s_looking, tmp = 0, col = 1; - bool changed = false; - char got; - char buf[0x600]; - char mad[6]; + + setbuf(stdout, NULL); pc.printf("Hello!\r\n"); + wait_ms(10); + vfd_reset(); + wait_ms(10); vfd_init(); - wait_ms(1); - - for (i = 'a'; i < 'a' + 25 ; i++){ - vfd_data(i); - } - for (i = 'a'; i < 'a' + 25 ; i++){ - vfd_data(i); - } - for (i = 'a'; i < 'a' + 25 ; i++){ - vfd_data(i); - } - -/* eth.init(); - eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); - eth.disconnect();*/ + wait_ms(10); + emf_logo(); + wait_ms(10); strip.begin(); + pc.printf("post strip.begin\r\n"); + strip_clear(); + strip.show(); + pc.printf("post strip.show\r\n"); + + wait_ms(500); + emf_blue(); + strip.show(); + + wait_ms(500); + strip_clear(); + strip.show(); + + wait_ms(500); + emf_blue(); + strip.show(); + + wait_ms(500); + strip_clear(); + strip.show(); + + vfd_init(); + + /* print our mac address & ip (when/if we get it). */ + +/* + EthernetErr ethErr = eth.setup(); + + if(ethErr) + { + printf("Error %d in setup.\n", ethErr); + return -1; + } + printf("Setup OK\n"); +// printf("link: %d\r\n", eth.link()); +// if (eth.link()) { +// eth.address(mad); +// printf("mymac: %02X:%02X:%02X:%02X:%02X:%02X\r\n", +// mad[0], mad[1], mad[2], mad[3], mad[4], mad[5]); +// } + + svr.addHandler<SimpleHandler>("/"); //Default handler + svr.addHandler<SimpleHandler>("/hello"); + svr.bind(80); + + printf("Listening...\n"); - for (i = 0 ; i < strip.numPixels() ; i++) { - // clear the strip - strip.setPixelColor(i, 0); + Timer tm; + tm.start(); + //Listen indefinitely + while(true) + { + Net::poll(); + if(tm.read() > .5) + { +// vfd_data('.'); + tm.start(); } + } +*/ + +/* if (ret != 0) { + sprintf(buf, "unable to get ip address!"); + printf("%s\r\n", buf); + send_text(buf); + } else { + ret = sprintf(buf, "IP Address is %s", eth.getIPAddress()); + printf("%s : %d\r\n", buf, ret); + send_text(buf); + }*/ +// eth.disconnect(); - strip.show(); + int tmp = 0; + bool changed = false; + char got; + char buf[128]; + int bufpos = 0; + + enum state {Looking, Number, String, Cmd}; + enum cmd {None, Top, Bottom, VfdWrite, VfdPos, Emf, VfdReset, VfdClear}; + state state; + cmd cmd; + state = Looking; + cmd = None; + int num = 0; while(1) { @@ -122,48 +222,116 @@ got = pc.getc(); if (isprint(got)) pc.putc(got); // remote echo - vfd_data(got); +// vfd_data(got); changed = false; - - if (got == '\n' || got == '\r') { - if (state == s_top) - t = tmp; - if (state == s_bottom) - b = tmp; - state = s_looking; - tmp = 0; - col = 1; - if (t > 16) t = 16; - if (b > 16) b = 16; - printf("t: %d b: %d\r\n", t, b); - changed = true; - printf("link: %d\r\n", eth.link()); - if (eth.link()) { - eth.address(mad); - printf("mymac: %02X:%02X:%02X:%02X:%02X:%02X\r\n", - mad[0], mad[1], mad[2], mad[3], mad[4], mad[5]); + + if (state == Looking) { + switch (got) { + case 't': + state = Number; + num = 0; + cmd = Top; + break; + case 'b': + state = Number; + num = 0; + cmd = Bottom; + break; + case 'e': + cmd = Emf; + state = Cmd; + break; + case 'r': + cmd = VfdReset; + state = Cmd; + break; + case 'w': + cmd = VfdWrite; + state = String; + bufpos = 0; + break; + case 'p': + cmd = VfdPos; + state = String; + bufpos = 0; + break; + case 'c': + cmd = VfdClear; + state = Cmd; + break; } - } else if (got == 'b') { - state = s_bottom; - } else if (got == 't') { - state = s_top; - } else if (got == 'e') { - emf_blue(); - strip.show(); - } else if (got <= '9' and got >= '0') { - tmp += (got - '0') * col; - col = col * 10; + } else { // not looking for a command + // cr or lf terminates a command. + if (got == '\n' || got == '\r') { + switch (cmd) { + case Top: + if (num > 16) + num = 16; + top_strip(num); + changed = true; + break; + case Bottom: + if (num > 16) + num = 16; + bottom_strip(num); + changed = true; + break; + case Emf: + emf_blue(); + changed = true; + printf("emf_blue\r\n"); + break; + case VfdReset: + vfd_reset(); + break; + case VfdWrite: + send_text(buf); + bufpos = 0; + break; + case VfdPos: + int v,h; + sscanf(buf, "%d,%d", &h,&v); + if (v > 3) v = 0; + if (h > 19) h = 0; + vfd_pos(h, v); + bufpos = 0; + break; + case VfdClear: + vfd_init(); + break; + default: + printf("no command given\r\n"); + } + state = Looking; + cmd = None; + } + if (state == Number) { + if (got >= '0' || got <= '9') { + tmp = (got - 48); + num = (num * 10) + tmp; + } else { + printf("expected a number, got %c\r\n", got); + } + } else if (state == String) { + if (bufpos < 127) { + buf[bufpos] = got; + bufpos++; + buf[bufpos] = '\0'; + } else { + printf("buffer full\r\n!"); + } + } } } if (changed) { + printf("changed\r\n"); led1 = led1 ? 0 : 1; - top_strip(t); - bottom_strip(b); strip.show(); + changed = false; } - int size = eth.receive(); +// int size = eth.receive(); /* if(size > 0) { eth.read(buf, size); printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
--- a/vfd.cpp Thu Aug 23 00:17:04 2012 +0000 +++ b/vfd.cpp Mon Aug 27 01:32:52 2012 +0000 @@ -14,6 +14,8 @@ #include "mbed.h" #include "vfd.h" +#include "Stream.h" +#include "FunctionPointer.h" // InturruptIn busy_intr(p27) @@ -27,6 +29,8 @@ bool c_a0; bool c_blank; +bool vfd_dead = 0; + void vfd_shift_clock(void) { vfd_p_clock = 0; wait_us(100); @@ -34,7 +38,7 @@ wait_us(100); } -void vfd_doit(int data, int mode, int wr, int ss) { +void vfd_doit(int data, int mode, int wr, int ss, int reset) { int i, bit; vfd_p_data = 0; // nc @@ -42,7 +46,7 @@ vfd_p_data = 0; // nc vfd_shift_clock(); - vfd_p_data = 1; // reset, 0 = in reset + vfd_p_data = reset; // reset, 0 = in reset vfd_shift_clock(); vfd_p_data = c_blank; // Blank, 0 = blanked @@ -73,14 +77,25 @@ } /* mode == 1 if command */ -void vfd_send(int data, int mode) +void vfd_send(int data, int mode, int force) { data = data & 0xff; c_data = data; c_blank = 1; - if (vfd_p_busy) + if (mode == 0) + printf("%02x ", data); + + if (vfd_dead && ! force) { + printf("vfd dead (?), not sending.\r\n"); + return; + } + if (force) + printf("forcing vfd for 0x%02x %d\r\n", data, mode); + + if (vfd_p_busy) { printf("pre, busy: %d\r\n", vfd_p_busy.read()); + } if (vfd_p_busy) { @@ -88,23 +103,32 @@ if (vfd_p_busy) { printf("still busy :(\r\n"); - return; + printf("resetting\r\n"); + if (!force) + vfd_reset(); + if (vfd_p_busy) { + printf("still busy after reset!\r\n"); + if (!force) + return; + } + if (!force) + return; } } /* wr,ss */ - vfd_doit(data, mode, 0, 0); + vfd_doit(data, mode, 0, 0, 1); wait_us(1); - vfd_doit(data, mode, 1, 0); + vfd_doit(data, mode, 1, 0, 1); wait_us(1); - vfd_doit(data, mode, 1, 1); + vfd_doit(data, mode, 1, 1, 1); wait_us(1); if (vfd_p_busy) printf("post1, busy: %d\r\n", vfd_p_busy.read()); - vfd_doit(data, mode, 0, 0); - wait_ms(1); // loop and wait for not busy + vfd_doit(data, mode, 0, 0, 1); + wait_ms(4); // loop and wait for not busy if (vfd_p_busy) printf("post2, busy: %d\r\n", vfd_p_busy.read()); @@ -115,15 +139,20 @@ } } -void vfd_command(int data) { - vfd_send(data, 1); +void vfd_command(int data, int force) { + vfd_send(data, 1, force); } void vfd_data(int data) { - vfd_send(data, 0); + if ((data & 0xff) == 0x7f) { + data = 0x20; + printf("changed data to %02x\r\n", data); + } + vfd_send(data, 0, 0); } void vfd_init(void) { + vfd_data(0x11); // normal mode vfd_data(0x0c); // clear vfd_data(0x1b); // esc vfd_data(0); // v pos @@ -138,4 +167,69 @@ /* unblank */ } +void vfd_pos(int h, int v) { + if (v > 3 || v < 0) + return; + if (h > 19 || h < 0) + return; + vfd_data(0x1b); + vfd_data(v); + vfd_data(h); +} +void vfd_reset(void) { + int i, data, mode; + + data = 0; mode = 1; + + printf("reset sent, busy: %d\r\n", vfd_p_busy.read()); + + /* everything low, includeing reset */ + for (i = 0 ; i < 16; i++) { + vfd_p_data = 0; + vfd_shift_clock(); + } + vfd_p_latch = 0; + wait_us(10); + vfd_p_latch = 1; + + vfd_doit(data, mode, 0, 0, 1); + wait_us(1); + vfd_doit(data, mode, 1, 0, 1); + wait_us(1); + vfd_doit(data, mode, 1, 1, 1); + wait_us(1); + + if (vfd_p_busy) + printf("post1, busy: %d\r\n", vfd_p_busy.read()); + + vfd_doit(data, mode, 0, 0, 1); + wait_ms(1); // loop and wait for not busy + + printf("reset sent, busy: %d\r\n", vfd_p_busy.read()); + wait_ms(10); + if (vfd_p_busy) { + printf("still busy, not calling vfd_init()\r\n"); + vfd_reset_cmd(); + printf("post reset_cmd\r\n"); + vfd_dead = 1; + wait_ms(10); + } else { + if (!vfd_dead) + vfd_init(); + } +} + +void vfd_reset_cmd(void) { + vfd_command(0xff, 1); +} + +/* +class Vfd : Public Stream { + public: + + +} +*/ + +
--- a/vfd.h Thu Aug 23 00:17:04 2012 +0000 +++ b/vfd.h Mon Aug 27 01:32:52 2012 +0000 @@ -4,4 +4,8 @@ void vfd_blank(void); void vfd_status(void); void vfd_init(void); +void vfd_pos(int h, int v); +void vfd_reset(void); +void vfd_reset_cmd(void); +