The London Hackspace bandwidth meter

Dependencies:   LPD8806 MODSERIAL mbed picojson

See:

main.cpp

Committer:
Jasper
Date:
2012-06-10
Revision:
1:e384e7146746
Parent:
0:0a6f193d5344
Child:
2:81155674a852

File content as of revision 1:e384e7146746:

#include "mbed.h"

#include "LPD8806.h"

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, 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 t = 0, b = 0;
    int i, state = s_looking, tmp = 0, col = 1;
    bool changed = false;
    char got;

    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();
            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)
        {
            led1 = 1;
            top_strip(t);
            bottom_strip(b);
            strip.show();
            led1 = 0;
        }
    }
}