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
Diff: main.cpp
- Revision:
- 1:e384e7146746
- Parent:
- 0:0a6f193d5344
- Child:
- 2:81155674a852
diff -r 0a6f193d5344 -r e384e7146746 main.cpp --- a/main.cpp Sun Jun 10 19:23:12 2012 +0000 +++ b/main.cpp Sun Jun 10 20:30:30 2012 +0000 @@ -7,85 +7,10 @@ DigitalOut led3(LED3); DigitalOut led4(LED4); -LPD8806 strip = LPD8806(32); - -/* 0 - 16 */ -void top_strip(int quantity){ - int i; - - if (quantity == 0) { - // blank it - setPixelsTop(0, 16, 0); - } - - setPixelsTop(0, quantity < 12 ? quantity : 12, strip.Color(0, 127, 0)); - - for (i = 0 ; i < 12 ; i++) { - if (i <= quantity) - strip.setPixelColor(i, 0, 127, 0); - else - strip.setPixelColor(i, 0, 0, 0); - } - i = 12; - if (i <= quantity) - strip.setPixelColor(i, 127, 127, 0); - else - strip.setPixelColor(i, 0, 0, 0); - - i = 13; - if (i <= quantity) - strip.setPixelColor(i, 127, 127, 0); - else - strip.setPixelColor(i, 0, 0, 0); - - i = 14; - if (i <= quantity) - strip.setPixelColor(i, 127, 0, 0); - else - strip.setPixelColor(i, 0, 0, 0); +/* talk to the world */ +Serial pc(USBTX, USBRX); - i = 15; - if (i <= quantity) - strip.setPixelColor(i, 127, 0, 0); - else - strip.setPixelColor(i, 0, 0, 0); - - strip.show(); -} - -void bottom_strip(int quantity){ - int i; - - for (i = 0 ; i < 12 ; i++) { - if (i <= quantity) - strip.setPixelColor(16 + i, 0, 127, 0); - else - strip.setPixelColor(16 + i, 0, 0, 0); - } - i = 12; - if (i <= quantity) - strip.setPixelColor(16 + i, 127, 127, 0); - else - strip.setPixelColor(16 + i, 0, 0, 0); - - i = 13; - if (i <= quantity) - strip.setPixelColor(16 + i, 127, 127, 0); - else - strip.setPixelColor(16 + i, 0, 0, 0); - - i = 14; - if (i <= quantity) - strip.setPixelColor(16 + i, 127, 0, 0); - else - strip.setPixelColor(16 + i, 0, 0, 0); - - i = 15; - if (i <= quantity) - strip.setPixelColor(16 + i, 127, 0, 0); - else - strip.setPixelColor(16 + i, 0, 0, 0); -} +LPD8806 strip = LPD8806(32); void setPixelsTop(int start, int end, int colour) { int i; @@ -103,10 +28,53 @@ } } +/* 0 - 16 */ +void top_strip(int quantity){ + if (quantity < 16) { + // blank unused bits. + setPixelsTop(quantity, 16, 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, 16, 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)); +} + +#define s_looking 1 +#define s_top 2 +#define s_bottom 3 + int main() { - int count = 0; - int c2 = 0; - int i; + int t = 0, b = 0; + int i, state = s_looking, tmp = 0, col = 1; + bool changed = false; + char got; strip.begin(); @@ -119,19 +87,40 @@ while(1) { - if (c2 > 16) c2 = 0; - - led1 = 1; - wait(0.25); - led1 = 0; - count ++; - if (count > 2) + if (pc.readable()) { + got = pc.getc(); + pc.putc(got); // remote echo + + 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\n", t, b); + changed = true; + } else if (got == 'b') { + state = s_bottom; + } else if (got == 't') { + state = s_top; + } else if (got <= '9' and got >= '0') { + tmp += (got - '0') * col; + col = col * 10; + } + } + if (changed) { - top_strip(c2); - bottom_strip(c2); + led1 = 1; + top_strip(t); + bottom_strip(b); strip.show(); - count = 0; - c2 ++; + led1 = 0; } } }