![](/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@8:add24f73747b, 2012-10-21 (annotated)
- Committer:
- Jasper
- Date:
- Sun Oct 21 11:27:45 2012 +0000
- Revision:
- 8:add24f73747b
- Parent:
- 7:68a26efc1d02
use MODSERIAL, fix the strip stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jasper | 0:0a6f193d5344 | 1 | #include "mbed.h" |
Jasper | 3:7fca72f96711 | 2 | #include "ctype.h" |
Jasper | 0:0a6f193d5344 | 3 | |
Jasper | 8:add24f73747b | 4 | #include "MODSERIAL.h" |
Jasper | 0:0a6f193d5344 | 5 | #include "LPD8806.h" |
Jasper | 3:7fca72f96711 | 6 | #include "vfd.h" |
Jasper | 0:0a6f193d5344 | 7 | |
Jasper | 0:0a6f193d5344 | 8 | DigitalOut led1(LED1); |
Jasper | 0:0a6f193d5344 | 9 | DigitalOut led2(LED2); |
Jasper | 0:0a6f193d5344 | 10 | DigitalOut led3(LED3); |
Jasper | 0:0a6f193d5344 | 11 | DigitalOut led4(LED4); |
Jasper | 0:0a6f193d5344 | 12 | |
Jasper | 1:e384e7146746 | 13 | /* talk to the world */ |
Jasper | 8:add24f73747b | 14 | MODSERIAL pc(USBTX, USBRX); |
Jasper | 0:0a6f193d5344 | 15 | |
Jasper | 1:e384e7146746 | 16 | LPD8806 strip = LPD8806(32); |
Jasper | 0:0a6f193d5344 | 17 | |
Jasper | 0:0a6f193d5344 | 18 | void setPixelsTop(int start, int end, int colour) { |
Jasper | 0:0a6f193d5344 | 19 | int i; |
Jasper | 0:0a6f193d5344 | 20 | |
Jasper | 0:0a6f193d5344 | 21 | for (i = start; i < end + 1 ; i++) { |
Jasper | 0:0a6f193d5344 | 22 | strip.setPixelColor(i, colour); |
Jasper | 0:0a6f193d5344 | 23 | } |
Jasper | 0:0a6f193d5344 | 24 | } |
Jasper | 0:0a6f193d5344 | 25 | |
Jasper | 0:0a6f193d5344 | 26 | void setPixelsBottom(int start, int end, int colour) { |
Jasper | 0:0a6f193d5344 | 27 | int i; |
Jasper | 0:0a6f193d5344 | 28 | |
Jasper | 0:0a6f193d5344 | 29 | for (i = start; i < end + 1 ; i++) { |
Jasper | 0:0a6f193d5344 | 30 | strip.setPixelColor(16 + i, colour); |
Jasper | 0:0a6f193d5344 | 31 | } |
Jasper | 0:0a6f193d5344 | 32 | } |
Jasper | 0:0a6f193d5344 | 33 | |
Jasper | 1:e384e7146746 | 34 | /* 0 - 16 */ |
Jasper | 1:e384e7146746 | 35 | void top_strip(int quantity){ |
Jasper | 1:e384e7146746 | 36 | if (quantity < 16) { |
Jasper | 1:e384e7146746 | 37 | // blank unused bits. |
Jasper | 4:7087ea3d13c1 | 38 | setPixelsTop(quantity, 15, 0); |
Jasper | 1:e384e7146746 | 39 | } |
Jasper | 1:e384e7146746 | 40 | |
Jasper | 1:e384e7146746 | 41 | if (quantity == 0) return; |
Jasper | 1:e384e7146746 | 42 | |
Jasper | 1:e384e7146746 | 43 | quantity --; |
Jasper | 1:e384e7146746 | 44 | |
Jasper | 1:e384e7146746 | 45 | setPixelsTop(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); |
Jasper | 1:e384e7146746 | 46 | |
Jasper | 1:e384e7146746 | 47 | if (quantity > 11) |
Jasper | 1:e384e7146746 | 48 | setPixelsTop(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); |
Jasper | 1:e384e7146746 | 49 | |
Jasper | 1:e384e7146746 | 50 | if (quantity > 13) |
Jasper | 1:e384e7146746 | 51 | setPixelsTop(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); |
Jasper | 1:e384e7146746 | 52 | } |
Jasper | 1:e384e7146746 | 53 | |
Jasper | 1:e384e7146746 | 54 | void bottom_strip(int quantity){ |
Jasper | 1:e384e7146746 | 55 | if (quantity < 16) { |
Jasper | 1:e384e7146746 | 56 | // blank unused bits. |
Jasper | 4:7087ea3d13c1 | 57 | setPixelsBottom(quantity, 15, 0); |
Jasper | 1:e384e7146746 | 58 | } |
Jasper | 1:e384e7146746 | 59 | |
Jasper | 1:e384e7146746 | 60 | if (quantity == 0) return; |
Jasper | 1:e384e7146746 | 61 | quantity --; |
Jasper | 1:e384e7146746 | 62 | |
Jasper | 1:e384e7146746 | 63 | setPixelsBottom(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); |
Jasper | 1:e384e7146746 | 64 | |
Jasper | 1:e384e7146746 | 65 | if (quantity > 11) |
Jasper | 1:e384e7146746 | 66 | setPixelsBottom(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); |
Jasper | 1:e384e7146746 | 67 | |
Jasper | 1:e384e7146746 | 68 | if (quantity > 13) |
Jasper | 1:e384e7146746 | 69 | setPixelsBottom(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); |
Jasper | 1:e384e7146746 | 70 | } |
Jasper | 1:e384e7146746 | 71 | |
Jasper | 3:7fca72f96711 | 72 | void emf_blue() { |
Jasper | 3:7fca72f96711 | 73 | setPixelsBottom(0, 15, strip.Color(0, 161, 228)); |
Jasper | 3:7fca72f96711 | 74 | setPixelsTop(0, 15, strip.Color(0, 161, 228)); |
Jasper | 3:7fca72f96711 | 75 | } |
Jasper | 3:7fca72f96711 | 76 | |
Jasper | 4:7087ea3d13c1 | 77 | void strip_clear() { |
Jasper | 4:7087ea3d13c1 | 78 | int i; |
Jasper | 4:7087ea3d13c1 | 79 | |
Jasper | 4:7087ea3d13c1 | 80 | for (i = 0 ; i < strip.numPixels() ; i++) { |
Jasper | 4:7087ea3d13c1 | 81 | // clear the strip |
Jasper | 4:7087ea3d13c1 | 82 | strip.setPixelColor(i, 0); |
Jasper | 4:7087ea3d13c1 | 83 | } |
Jasper | 4:7087ea3d13c1 | 84 | } |
Jasper | 4:7087ea3d13c1 | 85 | |
Jasper | 1:e384e7146746 | 86 | #define s_looking 1 |
Jasper | 1:e384e7146746 | 87 | #define s_top 2 |
Jasper | 1:e384e7146746 | 88 | #define s_bottom 3 |
Jasper | 1:e384e7146746 | 89 | |
Jasper | 4:7087ea3d13c1 | 90 | void send_text(const char *s) { |
Jasper | 4:7087ea3d13c1 | 91 | int i; char c; |
Jasper | 4:7087ea3d13c1 | 92 | |
Jasper | 4:7087ea3d13c1 | 93 | for (i = 0, c=s[i]; c != 0; i++, c=s[i]) |
Jasper | 4:7087ea3d13c1 | 94 | vfd_data(c); |
Jasper | 4:7087ea3d13c1 | 95 | } |
Jasper | 4:7087ea3d13c1 | 96 | |
Jasper | 4:7087ea3d13c1 | 97 | void text_centered(const char *s, int hpos) { |
Jasper | 4:7087ea3d13c1 | 98 | |
Jasper | 4:7087ea3d13c1 | 99 | vfd_pos((20 / 2) - (strlen(s)) / 2, hpos); |
Jasper | 4:7087ea3d13c1 | 100 | send_text(s); |
Jasper | 4:7087ea3d13c1 | 101 | } |
Jasper | 4:7087ea3d13c1 | 102 | |
Jasper | 5:24172484b8c5 | 103 | void logo(void) { |
Jasper | 4:7087ea3d13c1 | 104 | const char line0[] = "Welcome to"; |
Jasper | 5:24172484b8c5 | 105 | const char line2[] = "London Hackspace"; |
Jasper | 4:7087ea3d13c1 | 106 | |
Jasper | 4:7087ea3d13c1 | 107 | text_centered(line0, 0); |
Jasper | 4:7087ea3d13c1 | 108 | text_centered(line2, 2); |
Jasper | 4:7087ea3d13c1 | 109 | } |
Jasper | 4:7087ea3d13c1 | 110 | |
Jasper | 5:24172484b8c5 | 111 | int hex_char_to_int(char h) { |
Jasper | 5:24172484b8c5 | 112 | int ret; |
Jasper | 5:24172484b8c5 | 113 | if (h >= '0' || h <= '9') { |
Jasper | 5:24172484b8c5 | 114 | ret = h - 48; |
Jasper | 5:24172484b8c5 | 115 | } else if (h >= 'A' || h <= 'F') { |
Jasper | 5:24172484b8c5 | 116 | ret = (h - 'A') + 10; |
Jasper | 5:24172484b8c5 | 117 | } else if (h >= 'a' || h <= 'f') { |
Jasper | 5:24172484b8c5 | 118 | ret = (h - 'a') + 10; |
Jasper | 5:24172484b8c5 | 119 | } |
Jasper | 5:24172484b8c5 | 120 | return ret; |
Jasper | 5:24172484b8c5 | 121 | } |
Jasper | 5:24172484b8c5 | 122 | |
Jasper | 0:0a6f193d5344 | 123 | int main() { |
Jasper | 4:7087ea3d13c1 | 124 | |
Jasper | 8:add24f73747b | 125 | // setbuf(stdout, NULL); |
Jasper | 3:7fca72f96711 | 126 | |
Jasper | 3:7fca72f96711 | 127 | pc.printf("Hello!\r\n"); |
Jasper | 3:7fca72f96711 | 128 | |
Jasper | 4:7087ea3d13c1 | 129 | wait_ms(10); |
Jasper | 4:7087ea3d13c1 | 130 | vfd_reset(); |
Jasper | 4:7087ea3d13c1 | 131 | wait_ms(10); |
Jasper | 3:7fca72f96711 | 132 | vfd_init(); |
Jasper | 4:7087ea3d13c1 | 133 | wait_ms(10); |
Jasper | 5:24172484b8c5 | 134 | logo(); |
Jasper | 4:7087ea3d13c1 | 135 | wait_ms(10); |
Jasper | 0:0a6f193d5344 | 136 | |
Jasper | 0:0a6f193d5344 | 137 | strip.begin(); |
Jasper | 4:7087ea3d13c1 | 138 | pc.printf("post strip.begin\r\n"); |
Jasper | 4:7087ea3d13c1 | 139 | strip_clear(); |
Jasper | 4:7087ea3d13c1 | 140 | strip.show(); |
Jasper | 4:7087ea3d13c1 | 141 | pc.printf("post strip.show\r\n"); |
Jasper | 0:0a6f193d5344 | 142 | |
Jasper | 4:7087ea3d13c1 | 143 | int tmp = 0; |
Jasper | 4:7087ea3d13c1 | 144 | bool changed = false; |
Jasper | 4:7087ea3d13c1 | 145 | char got; |
Jasper | 4:7087ea3d13c1 | 146 | char buf[128]; |
Jasper | 4:7087ea3d13c1 | 147 | int bufpos = 0; |
Jasper | 4:7087ea3d13c1 | 148 | |
Jasper | 4:7087ea3d13c1 | 149 | enum state {Looking, Number, String, Cmd}; |
Jasper | 5:24172484b8c5 | 150 | enum cmd {None, Top, Bottom, VfdWrite, VfdPos, Emf, VfdReset, VfdClear, SetStrip}; |
Jasper | 4:7087ea3d13c1 | 151 | state state; |
Jasper | 4:7087ea3d13c1 | 152 | cmd cmd; |
Jasper | 4:7087ea3d13c1 | 153 | state = Looking; |
Jasper | 4:7087ea3d13c1 | 154 | cmd = None; |
Jasper | 4:7087ea3d13c1 | 155 | int num = 0; |
Jasper | 0:0a6f193d5344 | 156 | |
Jasper | 0:0a6f193d5344 | 157 | while(1) { |
Jasper | 0:0a6f193d5344 | 158 | |
Jasper | 1:e384e7146746 | 159 | if (pc.readable()) { |
Jasper | 1:e384e7146746 | 160 | got = pc.getc(); |
Jasper | 5:24172484b8c5 | 161 | if (isprint(got)) { |
Jasper | 5:24172484b8c5 | 162 | // pc.putc(got); // remote echo |
Jasper | 5:24172484b8c5 | 163 | } else { |
Jasper | 5:24172484b8c5 | 164 | pc.printf("%02x ", got); |
Jasper | 5:24172484b8c5 | 165 | } |
Jasper | 1:e384e7146746 | 166 | changed = false; |
Jasper | 4:7087ea3d13c1 | 167 | |
Jasper | 4:7087ea3d13c1 | 168 | if (state == Looking) { |
Jasper | 4:7087ea3d13c1 | 169 | switch (got) { |
Jasper | 4:7087ea3d13c1 | 170 | case 't': |
Jasper | 4:7087ea3d13c1 | 171 | state = Number; |
Jasper | 4:7087ea3d13c1 | 172 | num = 0; |
Jasper | 4:7087ea3d13c1 | 173 | cmd = Top; |
Jasper | 4:7087ea3d13c1 | 174 | break; |
Jasper | 4:7087ea3d13c1 | 175 | case 'b': |
Jasper | 4:7087ea3d13c1 | 176 | state = Number; |
Jasper | 4:7087ea3d13c1 | 177 | num = 0; |
Jasper | 4:7087ea3d13c1 | 178 | cmd = Bottom; |
Jasper | 4:7087ea3d13c1 | 179 | break; |
Jasper | 5:24172484b8c5 | 180 | case 'e': // emf blue... |
Jasper | 4:7087ea3d13c1 | 181 | cmd = Emf; |
Jasper | 4:7087ea3d13c1 | 182 | state = Cmd; |
Jasper | 4:7087ea3d13c1 | 183 | break; |
Jasper | 4:7087ea3d13c1 | 184 | case 'r': |
Jasper | 4:7087ea3d13c1 | 185 | cmd = VfdReset; |
Jasper | 4:7087ea3d13c1 | 186 | state = Cmd; |
Jasper | 4:7087ea3d13c1 | 187 | break; |
Jasper | 4:7087ea3d13c1 | 188 | case 'w': |
Jasper | 4:7087ea3d13c1 | 189 | cmd = VfdWrite; |
Jasper | 4:7087ea3d13c1 | 190 | state = String; |
Jasper | 4:7087ea3d13c1 | 191 | bufpos = 0; |
Jasper | 4:7087ea3d13c1 | 192 | break; |
Jasper | 4:7087ea3d13c1 | 193 | case 'p': |
Jasper | 4:7087ea3d13c1 | 194 | cmd = VfdPos; |
Jasper | 4:7087ea3d13c1 | 195 | state = String; |
Jasper | 4:7087ea3d13c1 | 196 | bufpos = 0; |
Jasper | 4:7087ea3d13c1 | 197 | break; |
Jasper | 4:7087ea3d13c1 | 198 | case 'c': |
Jasper | 4:7087ea3d13c1 | 199 | cmd = VfdClear; |
Jasper | 4:7087ea3d13c1 | 200 | state = Cmd; |
Jasper | 4:7087ea3d13c1 | 201 | break; |
Jasper | 5:24172484b8c5 | 202 | case 's': |
Jasper | 5:24172484b8c5 | 203 | cmd = SetStrip; |
Jasper | 5:24172484b8c5 | 204 | state = String; |
Jasper | 5:24172484b8c5 | 205 | bufpos = 0; |
Jasper | 5:24172484b8c5 | 206 | break; |
Jasper | 3:7fca72f96711 | 207 | } |
Jasper | 4:7087ea3d13c1 | 208 | } else { // not looking for a command |
Jasper | 4:7087ea3d13c1 | 209 | // cr or lf terminates a command. |
Jasper | 4:7087ea3d13c1 | 210 | if (got == '\n' || got == '\r') { |
Jasper | 4:7087ea3d13c1 | 211 | switch (cmd) { |
Jasper | 4:7087ea3d13c1 | 212 | case Top: |
Jasper | 4:7087ea3d13c1 | 213 | if (num > 16) |
Jasper | 4:7087ea3d13c1 | 214 | num = 16; |
Jasper | 4:7087ea3d13c1 | 215 | top_strip(num); |
Jasper | 4:7087ea3d13c1 | 216 | changed = true; |
Jasper | 4:7087ea3d13c1 | 217 | break; |
Jasper | 4:7087ea3d13c1 | 218 | case Bottom: |
Jasper | 4:7087ea3d13c1 | 219 | if (num > 16) |
Jasper | 4:7087ea3d13c1 | 220 | num = 16; |
Jasper | 4:7087ea3d13c1 | 221 | bottom_strip(num); |
Jasper | 4:7087ea3d13c1 | 222 | changed = true; |
Jasper | 4:7087ea3d13c1 | 223 | break; |
Jasper | 4:7087ea3d13c1 | 224 | case Emf: |
Jasper | 4:7087ea3d13c1 | 225 | emf_blue(); |
Jasper | 4:7087ea3d13c1 | 226 | changed = true; |
Jasper | 8:add24f73747b | 227 | pc.printf("emf_blue\r\n"); |
Jasper | 4:7087ea3d13c1 | 228 | break; |
Jasper | 4:7087ea3d13c1 | 229 | case VfdReset: |
Jasper | 4:7087ea3d13c1 | 230 | vfd_reset(); |
Jasper | 4:7087ea3d13c1 | 231 | break; |
Jasper | 4:7087ea3d13c1 | 232 | case VfdWrite: |
Jasper | 4:7087ea3d13c1 | 233 | send_text(buf); |
Jasper | 4:7087ea3d13c1 | 234 | bufpos = 0; |
Jasper | 4:7087ea3d13c1 | 235 | break; |
Jasper | 4:7087ea3d13c1 | 236 | case VfdPos: |
Jasper | 4:7087ea3d13c1 | 237 | int v,h; |
Jasper | 4:7087ea3d13c1 | 238 | sscanf(buf, "%d,%d", &h,&v); |
Jasper | 4:7087ea3d13c1 | 239 | if (v > 3) v = 0; |
Jasper | 4:7087ea3d13c1 | 240 | if (h > 19) h = 0; |
Jasper | 4:7087ea3d13c1 | 241 | vfd_pos(h, v); |
Jasper | 4:7087ea3d13c1 | 242 | bufpos = 0; |
Jasper | 4:7087ea3d13c1 | 243 | break; |
Jasper | 4:7087ea3d13c1 | 244 | case VfdClear: |
Jasper | 4:7087ea3d13c1 | 245 | vfd_init(); |
Jasper | 4:7087ea3d13c1 | 246 | break; |
Jasper | 5:24172484b8c5 | 247 | case SetStrip: |
Jasper | 5:24172484b8c5 | 248 | bufpos = 0; |
Jasper | 5:24172484b8c5 | 249 | // buf[0] == t or b for strip |
Jasper | 5:24172484b8c5 | 250 | // next 48 chars == HHH rgb * 16 |
Jasper | 8:add24f73747b | 251 | int p, r, g, b, poff=0; |
Jasper | 7:68a26efc1d02 | 252 | if (buf[0] == 'b') { |
Jasper | 7:68a26efc1d02 | 253 | poff = 16; |
Jasper | 5:24172484b8c5 | 254 | } |
Jasper | 5:24172484b8c5 | 255 | |
Jasper | 8:add24f73747b | 256 | for (p = 0;p < 16 ; p ++) { |
Jasper | 5:24172484b8c5 | 257 | r = hex_char_to_int(buf[(p * 3) + 1]); |
Jasper | 5:24172484b8c5 | 258 | g = hex_char_to_int(buf[(p * 3) + 2]); |
Jasper | 5:24172484b8c5 | 259 | b = hex_char_to_int(buf[(p * 3) + 3]); |
Jasper | 7:68a26efc1d02 | 260 | strip.setPixelColor(p + poff , strip.Color(r,g,b)); |
Jasper | 5:24172484b8c5 | 261 | } |
Jasper | 5:24172484b8c5 | 262 | changed = true; |
Jasper | 5:24172484b8c5 | 263 | |
Jasper | 5:24172484b8c5 | 264 | break; |
Jasper | 4:7087ea3d13c1 | 265 | default: |
Jasper | 8:add24f73747b | 266 | pc.printf("no command given\r\n"); |
Jasper | 4:7087ea3d13c1 | 267 | } |
Jasper | 4:7087ea3d13c1 | 268 | state = Looking; |
Jasper | 4:7087ea3d13c1 | 269 | cmd = None; |
Jasper | 4:7087ea3d13c1 | 270 | } |
Jasper | 4:7087ea3d13c1 | 271 | if (state == Number) { |
Jasper | 4:7087ea3d13c1 | 272 | if (got >= '0' || got <= '9') { |
Jasper | 4:7087ea3d13c1 | 273 | tmp = (got - 48); |
Jasper | 4:7087ea3d13c1 | 274 | num = (num * 10) + tmp; |
Jasper | 4:7087ea3d13c1 | 275 | } else { |
Jasper | 8:add24f73747b | 276 | pc.printf("expected a number, got %c\r\n", got); |
Jasper | 4:7087ea3d13c1 | 277 | } |
Jasper | 4:7087ea3d13c1 | 278 | } else if (state == String) { |
Jasper | 4:7087ea3d13c1 | 279 | if (bufpos < 127) { |
Jasper | 4:7087ea3d13c1 | 280 | buf[bufpos] = got; |
Jasper | 4:7087ea3d13c1 | 281 | bufpos++; |
Jasper | 4:7087ea3d13c1 | 282 | buf[bufpos] = '\0'; |
Jasper | 4:7087ea3d13c1 | 283 | } else { |
Jasper | 8:add24f73747b | 284 | pc.printf("buffer full\r\n!"); |
Jasper | 5:24172484b8c5 | 285 | // XXX state == None here? |
Jasper | 4:7087ea3d13c1 | 286 | } |
Jasper | 4:7087ea3d13c1 | 287 | } |
Jasper | 1:e384e7146746 | 288 | } |
Jasper | 1:e384e7146746 | 289 | } |
Jasper | 1:e384e7146746 | 290 | if (changed) |
Jasper | 0:0a6f193d5344 | 291 | { |
Jasper | 8:add24f73747b | 292 | pc.printf("changed\r\n"); |
Jasper | 2:81155674a852 | 293 | led1 = led1 ? 0 : 1; |
Jasper | 0:0a6f193d5344 | 294 | strip.show(); |
Jasper | 4:7087ea3d13c1 | 295 | changed = false; |
Jasper | 0:0a6f193d5344 | 296 | } |
Jasper | 0:0a6f193d5344 | 297 | } |
Jasper | 0:0a6f193d5344 | 298 | } |