usbrh.cpp

// usbrh.cpp 2012/2/6
#include "mbed.h"
#include "USBHost.h"
#include "Utils.h"

void usbrh_request(int device)
{
    USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, 0x09, 0x0200, 0, 0, 7);
}

void usbrh_Callback(int device, int endpoint, int status, u8* data, int len, void* userData)
{
    static string old_csv_data;

    float t, rhl, rht;
    unsigned short sot, sorh;
    float d1,d2,c1,c2,c3,t1,t2;

    d1 = -40,1; d2 = 0.01;
    c1 = -2.0468; c2 = 0.0367; c3 = -1.5955/1000000.0;
    t1 = 0.01; t2 = 0.00008;

    //printfBytes("usbrh_Callback", data ,len);
    sorh = data[0] << 8 | data[1];
    sot = data[2] << 8 | data[3];

    t = d1 + d2 * (float)sot;
    rhl = c1 + c2 * (float)sorh + c3 * (float)(sorh * sorh);
    rht = (t - 25.0) * (t1 + t2 * (float)sorh) + rhl;
    if (rht > 99.0) rht = 100.0;

    printf("temperature: %.2f, humidity: %.2f\n", t, rht);
    lcd.locate(0, 0); lcd.printf("t: %.2f", t);
    lcd.locate(0, 1); lcd.printf("h: %.2f", rht);
    wait(10.0);

    char buf[64];
    sprintf(buf, "%.2f,%.2f", t, rht);
    string csv_data = buf; 
    if (old_csv_data != csv_data) {
        old_csv_data = csv_data;
        pachube.PutCsv(pachube_feedID, csv_data);
        printf("Pachube PutCsv[%s]", buf);
        printf("result / response: %d / %d\n", 
        pachube.Result(), pachube.Response());
    }
    USBInterruptTransfer(device, 0x81, usbrh_buf, sizeof(usbrh_buf), usbrh_Callback, userData);
    usbrh_request(device);
}

int OnUsbrhInsert(int device) 
{
    printf("USBRH inserted of %d\n", device);
    USBInterruptTransfer(device, 0x81, usbrh_buf, sizeof(usbrh_buf), usbrh_Callback, (void*)0);
    usbrh_request(device);
    return 0;
}