![](/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@3:7fca72f96711, 2012-08-23 (annotated)
- Committer:
- Jasper
- Date:
- Thu Aug 23 00:17:04 2012 +0000
- Revision:
- 3:7fca72f96711
- Parent:
- 2:81155674a852
- Child:
- 4:7087ea3d13c1
got the vfd working
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 | 0:0a6f193d5344 | 4 | #include "LPD8806.h" |
Jasper | 3:7fca72f96711 | 5 | #include "vfd.h" |
Jasper | 0:0a6f193d5344 | 6 | |
Jasper | 0:0a6f193d5344 | 7 | DigitalOut led1(LED1); |
Jasper | 0:0a6f193d5344 | 8 | DigitalOut led2(LED2); |
Jasper | 0:0a6f193d5344 | 9 | DigitalOut led3(LED3); |
Jasper | 0:0a6f193d5344 | 10 | DigitalOut led4(LED4); |
Jasper | 0:0a6f193d5344 | 11 | |
Jasper | 3:7fca72f96711 | 12 | Ethernet eth; |
Jasper | 3:7fca72f96711 | 13 | |
Jasper | 1:e384e7146746 | 14 | /* talk to the world */ |
Jasper | 1:e384e7146746 | 15 | Serial pc(USBTX, USBRX); |
Jasper | 0:0a6f193d5344 | 16 | |
Jasper | 1:e384e7146746 | 17 | LPD8806 strip = LPD8806(32); |
Jasper | 0:0a6f193d5344 | 18 | |
Jasper | 0:0a6f193d5344 | 19 | void setPixelsTop(int start, int end, int colour) { |
Jasper | 0:0a6f193d5344 | 20 | int i; |
Jasper | 0:0a6f193d5344 | 21 | |
Jasper | 0:0a6f193d5344 | 22 | for (i = start; i < end + 1 ; i++) { |
Jasper | 0:0a6f193d5344 | 23 | strip.setPixelColor(i, colour); |
Jasper | 0:0a6f193d5344 | 24 | } |
Jasper | 0:0a6f193d5344 | 25 | } |
Jasper | 0:0a6f193d5344 | 26 | |
Jasper | 0:0a6f193d5344 | 27 | void setPixelsBottom(int start, int end, int colour) { |
Jasper | 0:0a6f193d5344 | 28 | int i; |
Jasper | 0:0a6f193d5344 | 29 | |
Jasper | 0:0a6f193d5344 | 30 | for (i = start; i < end + 1 ; i++) { |
Jasper | 0:0a6f193d5344 | 31 | strip.setPixelColor(16 + i, colour); |
Jasper | 0:0a6f193d5344 | 32 | } |
Jasper | 0:0a6f193d5344 | 33 | } |
Jasper | 0:0a6f193d5344 | 34 | |
Jasper | 1:e384e7146746 | 35 | /* 0 - 16 */ |
Jasper | 1:e384e7146746 | 36 | void top_strip(int quantity){ |
Jasper | 1:e384e7146746 | 37 | if (quantity < 16) { |
Jasper | 1:e384e7146746 | 38 | // blank unused bits. |
Jasper | 1:e384e7146746 | 39 | setPixelsTop(quantity, 16, 0); |
Jasper | 1:e384e7146746 | 40 | } |
Jasper | 1:e384e7146746 | 41 | |
Jasper | 1:e384e7146746 | 42 | if (quantity == 0) return; |
Jasper | 1:e384e7146746 | 43 | |
Jasper | 1:e384e7146746 | 44 | quantity --; |
Jasper | 1:e384e7146746 | 45 | |
Jasper | 1:e384e7146746 | 46 | setPixelsTop(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); |
Jasper | 1:e384e7146746 | 47 | |
Jasper | 1:e384e7146746 | 48 | if (quantity > 11) |
Jasper | 1:e384e7146746 | 49 | setPixelsTop(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); |
Jasper | 1:e384e7146746 | 50 | |
Jasper | 1:e384e7146746 | 51 | if (quantity > 13) |
Jasper | 1:e384e7146746 | 52 | setPixelsTop(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); |
Jasper | 1:e384e7146746 | 53 | } |
Jasper | 1:e384e7146746 | 54 | |
Jasper | 1:e384e7146746 | 55 | void bottom_strip(int quantity){ |
Jasper | 1:e384e7146746 | 56 | if (quantity < 16) { |
Jasper | 1:e384e7146746 | 57 | // blank unused bits. |
Jasper | 1:e384e7146746 | 58 | setPixelsBottom(quantity, 16, 0); |
Jasper | 1:e384e7146746 | 59 | } |
Jasper | 1:e384e7146746 | 60 | |
Jasper | 1:e384e7146746 | 61 | if (quantity == 0) return; |
Jasper | 1:e384e7146746 | 62 | quantity --; |
Jasper | 1:e384e7146746 | 63 | |
Jasper | 1:e384e7146746 | 64 | setPixelsBottom(0, quantity < 12 ? quantity : 11, strip.Color(0, 127, 0)); |
Jasper | 1:e384e7146746 | 65 | |
Jasper | 1:e384e7146746 | 66 | if (quantity > 11) |
Jasper | 1:e384e7146746 | 67 | setPixelsBottom(12, quantity < 14 ? quantity : 14, strip.Color(127, 127, 0)); |
Jasper | 1:e384e7146746 | 68 | |
Jasper | 1:e384e7146746 | 69 | if (quantity > 13) |
Jasper | 1:e384e7146746 | 70 | setPixelsBottom(14, quantity < 16 ? quantity : 16, strip.Color(127, 0, 0)); |
Jasper | 1:e384e7146746 | 71 | } |
Jasper | 1:e384e7146746 | 72 | |
Jasper | 3:7fca72f96711 | 73 | void emf_blue() { |
Jasper | 3:7fca72f96711 | 74 | setPixelsBottom(0, 15, strip.Color(0, 161, 228)); |
Jasper | 3:7fca72f96711 | 75 | setPixelsTop(0, 15, strip.Color(0, 161, 228)); |
Jasper | 3:7fca72f96711 | 76 | } |
Jasper | 3:7fca72f96711 | 77 | |
Jasper | 1:e384e7146746 | 78 | #define s_looking 1 |
Jasper | 1:e384e7146746 | 79 | #define s_top 2 |
Jasper | 1:e384e7146746 | 80 | #define s_bottom 3 |
Jasper | 1:e384e7146746 | 81 | |
Jasper | 0:0a6f193d5344 | 82 | int main() { |
Jasper | 1:e384e7146746 | 83 | int t = 0, b = 0; |
Jasper | 1:e384e7146746 | 84 | int i, state = s_looking, tmp = 0, col = 1; |
Jasper | 1:e384e7146746 | 85 | bool changed = false; |
Jasper | 1:e384e7146746 | 86 | char got; |
Jasper | 3:7fca72f96711 | 87 | char buf[0x600]; |
Jasper | 3:7fca72f96711 | 88 | char mad[6]; |
Jasper | 3:7fca72f96711 | 89 | |
Jasper | 3:7fca72f96711 | 90 | pc.printf("Hello!\r\n"); |
Jasper | 3:7fca72f96711 | 91 | |
Jasper | 3:7fca72f96711 | 92 | vfd_init(); |
Jasper | 3:7fca72f96711 | 93 | wait_ms(1); |
Jasper | 3:7fca72f96711 | 94 | |
Jasper | 3:7fca72f96711 | 95 | for (i = 'a'; i < 'a' + 25 ; i++){ |
Jasper | 3:7fca72f96711 | 96 | vfd_data(i); |
Jasper | 3:7fca72f96711 | 97 | } |
Jasper | 3:7fca72f96711 | 98 | for (i = 'a'; i < 'a' + 25 ; i++){ |
Jasper | 3:7fca72f96711 | 99 | vfd_data(i); |
Jasper | 3:7fca72f96711 | 100 | } |
Jasper | 3:7fca72f96711 | 101 | for (i = 'a'; i < 'a' + 25 ; i++){ |
Jasper | 3:7fca72f96711 | 102 | vfd_data(i); |
Jasper | 3:7fca72f96711 | 103 | } |
Jasper | 3:7fca72f96711 | 104 | |
Jasper | 3:7fca72f96711 | 105 | /* eth.init(); |
Jasper | 3:7fca72f96711 | 106 | eth.connect(); |
Jasper | 3:7fca72f96711 | 107 | printf("IP Address is %s\n", eth.getIPAddress()); |
Jasper | 3:7fca72f96711 | 108 | eth.disconnect();*/ |
Jasper | 0:0a6f193d5344 | 109 | |
Jasper | 0:0a6f193d5344 | 110 | strip.begin(); |
Jasper | 0:0a6f193d5344 | 111 | |
Jasper | 0:0a6f193d5344 | 112 | for (i = 0 ; i < strip.numPixels() ; i++) { |
Jasper | 0:0a6f193d5344 | 113 | // clear the strip |
Jasper | 0:0a6f193d5344 | 114 | strip.setPixelColor(i, 0); |
Jasper | 0:0a6f193d5344 | 115 | } |
Jasper | 0:0a6f193d5344 | 116 | |
Jasper | 0:0a6f193d5344 | 117 | strip.show(); |
Jasper | 0:0a6f193d5344 | 118 | |
Jasper | 0:0a6f193d5344 | 119 | while(1) { |
Jasper | 0:0a6f193d5344 | 120 | |
Jasper | 1:e384e7146746 | 121 | if (pc.readable()) { |
Jasper | 1:e384e7146746 | 122 | got = pc.getc(); |
Jasper | 3:7fca72f96711 | 123 | if (isprint(got)) |
Jasper | 3:7fca72f96711 | 124 | pc.putc(got); // remote echo |
Jasper | 3:7fca72f96711 | 125 | vfd_data(got); |
Jasper | 1:e384e7146746 | 126 | changed = false; |
Jasper | 1:e384e7146746 | 127 | |
Jasper | 1:e384e7146746 | 128 | if (got == '\n' || got == '\r') { |
Jasper | 1:e384e7146746 | 129 | if (state == s_top) |
Jasper | 1:e384e7146746 | 130 | t = tmp; |
Jasper | 1:e384e7146746 | 131 | if (state == s_bottom) |
Jasper | 1:e384e7146746 | 132 | b = tmp; |
Jasper | 1:e384e7146746 | 133 | state = s_looking; |
Jasper | 1:e384e7146746 | 134 | tmp = 0; |
Jasper | 1:e384e7146746 | 135 | col = 1; |
Jasper | 1:e384e7146746 | 136 | if (t > 16) t = 16; |
Jasper | 1:e384e7146746 | 137 | if (b > 16) b = 16; |
Jasper | 3:7fca72f96711 | 138 | printf("t: %d b: %d\r\n", t, b); |
Jasper | 1:e384e7146746 | 139 | changed = true; |
Jasper | 3:7fca72f96711 | 140 | printf("link: %d\r\n", eth.link()); |
Jasper | 3:7fca72f96711 | 141 | if (eth.link()) { |
Jasper | 3:7fca72f96711 | 142 | eth.address(mad); |
Jasper | 3:7fca72f96711 | 143 | printf("mymac: %02X:%02X:%02X:%02X:%02X:%02X\r\n", |
Jasper | 3:7fca72f96711 | 144 | mad[0], mad[1], mad[2], mad[3], mad[4], mad[5]); |
Jasper | 3:7fca72f96711 | 145 | } |
Jasper | 1:e384e7146746 | 146 | } else if (got == 'b') { |
Jasper | 1:e384e7146746 | 147 | state = s_bottom; |
Jasper | 1:e384e7146746 | 148 | } else if (got == 't') { |
Jasper | 1:e384e7146746 | 149 | state = s_top; |
Jasper | 3:7fca72f96711 | 150 | } else if (got == 'e') { |
Jasper | 3:7fca72f96711 | 151 | emf_blue(); |
Jasper | 3:7fca72f96711 | 152 | strip.show(); |
Jasper | 1:e384e7146746 | 153 | } else if (got <= '9' and got >= '0') { |
Jasper | 1:e384e7146746 | 154 | tmp += (got - '0') * col; |
Jasper | 1:e384e7146746 | 155 | col = col * 10; |
Jasper | 1:e384e7146746 | 156 | } |
Jasper | 1:e384e7146746 | 157 | } |
Jasper | 1:e384e7146746 | 158 | if (changed) |
Jasper | 0:0a6f193d5344 | 159 | { |
Jasper | 2:81155674a852 | 160 | led1 = led1 ? 0 : 1; |
Jasper | 1:e384e7146746 | 161 | top_strip(t); |
Jasper | 1:e384e7146746 | 162 | bottom_strip(b); |
Jasper | 0:0a6f193d5344 | 163 | strip.show(); |
Jasper | 0:0a6f193d5344 | 164 | } |
Jasper | 3:7fca72f96711 | 165 | |
Jasper | 3:7fca72f96711 | 166 | int size = eth.receive(); |
Jasper | 3:7fca72f96711 | 167 | /* if(size > 0) { |
Jasper | 3:7fca72f96711 | 168 | eth.read(buf, size); |
Jasper | 3:7fca72f96711 | 169 | printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\r\n", |
Jasper | 3:7fca72f96711 | 170 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
Jasper | 3:7fca72f96711 | 171 | printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\r\n", |
Jasper | 3:7fca72f96711 | 172 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); |
Jasper | 3:7fca72f96711 | 173 | printf("Size: %d\r\n", size); |
Jasper | 3:7fca72f96711 | 174 | }*/ |
Jasper | 3:7fca72f96711 | 175 | |
Jasper | 0:0a6f193d5344 | 176 | } |
Jasper | 0:0a6f193d5344 | 177 | } |