![](/media/cache/profiles/f720ec3e5486f090fd382b68e230b435.50x50_q85.jpg)
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
main.cpp
- Committer:
- Jasper
- Date:
- 2012-08-27
- Revision:
- 4:7087ea3d13c1
- Parent:
- 3:7fca72f96711
- Child:
- 5:24172484b8c5
File content as of revision 4:7087ea3d13c1:
#include "mbed.h" #include "ctype.h" #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); /* talk to the world */ Serial pc(USBTX, USBRX); LPD8806 strip = LPD8806(32); void setPixelsTop(int start, int end, int colour) { int i; for (i = start; i < end + 1 ; i++) { strip.setPixelColor(i, colour); } } void setPixelsBottom(int start, int end, int colour) { int i; for (i = start; i < end + 1 ; i++) { strip.setPixelColor(16 + i, colour); } } /* 0 - 16 */ void top_strip(int quantity){ if (quantity < 16) { // blank unused bits. setPixelsTop(quantity, 15, 0); } if (quantity == 0) return; quantity --; setPixelsTop(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); if (quantity > 11) setPixelsTop(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); if (quantity > 13) setPixelsTop(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); } void bottom_strip(int quantity){ if (quantity < 16) { // blank unused bits. setPixelsBottom(quantity, 15, 0); } if (quantity == 0) return; quantity --; setPixelsBottom(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); if (quantity > 11) setPixelsBottom(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); if (quantity > 13) setPixelsBottom(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); } void emf_blue() { setPixelsBottom(0, 15, strip.Color(0, 161, 228)); 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() { setbuf(stdout, NULL); pc.printf("Hello!\r\n"); wait_ms(10); vfd_reset(); wait_ms(10); vfd_init(); 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"); 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(); 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) { if (pc.readable()) { got = pc.getc(); if (isprint(got)) pc.putc(got); // remote echo // vfd_data(got); changed = false; 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 { // 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; strip.show(); changed = false; } // int size = eth.receive(); /* if(size > 0) { eth.read(buf, size); printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\r\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\r\n", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); printf("Size: %d\r\n", size); }*/ } }