The London Hackspace bandwidth meter

Dependencies:   LPD8806 MODSERIAL mbed picojson

See:

main.cpp

Committer:
Jasper
Date:
2012-08-23
Revision:
3:7fca72f96711
Parent:
2:81155674a852
Child:
4:7087ea3d13c1

File content as of revision 3:7fca72f96711:

#include "mbed.h"
#include "ctype.h"

#include "LPD8806.h"
#include "vfd.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

Ethernet eth;

/* 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, 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));
}

void emf_blue() {
        setPixelsBottom(0, 15, strip.Color(0, 161, 228));
        setPixelsTop(0, 15, strip.Color(0, 161, 228));
}

#define s_looking 1
#define s_top 2
#define s_bottom 3

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];

    pc.printf("Hello!\r\n");

    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();*/

    strip.begin();
    
    for (i = 0 ; i < strip.numPixels() ; i++) {
        // clear the strip
        strip.setPixelColor(i, 0);
    }
    
    strip.show();
    
    while(1) {

        if (pc.readable()) {
            got = pc.getc();
            if (isprint(got))
                pc.putc(got); // remote echo
            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]);
                }
            } 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;
            }
        }
        if (changed)
        {
            led1 = led1 ? 0 : 1;
            top_strip(t);
            bottom_strip(b);
            strip.show();
        }

        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);
        }*/

    }
}