BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

main.cpp

Committer:
va009039
Date:
2013-01-06
Revision:
3:6ae9a03a6145
Child:
5:495f7536897b

File content as of revision 3:6ae9a03a6145:

// BaseUsbHost_example/main.cpp 2013/1/6
#include "mbed.h"
#include "rtos.h"
#include "BaseUsbHost.h"
#include "LogitechC270.h"
#include "LifeCamVX700.h"
#include "UvcCam.h"
#include "UsbFlashDrive.h"
#include "decodeMJPEG.h"
#include "MyThread.h"
#include <string>

#define IMAGE_BUF_SIZE (1024*6)
#define INTERVAL_S 20

Serial pc(USBTX, USBRX);
DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);

class captureJPEG : public MyThread, public decodeMJPEG {
public:
    captureJPEG(BaseUvc* cam) : m_cam(cam) {
        m_cam->setOnResult(this, &captureJPEG::callback_motion_jpeg);
        interval_t.start();
        buf_size = IMAGE_BUF_SIZE;
        buf = new uint8_t[buf_size];
        pos = 0;
    }
    Timer interval_t;
private:    
    virtual void outputJPEG(uint8_t c, int status) {
        if (status == JPEG_START) {
            pos = 0;
            led2 = !led2;
        }
        if (pos < buf_size) {
            buf[pos++] = c;
        }
        if (status == JPEG_END) {
            led3 = !led3;
            if (interval_t.read_ms() > INTERVAL_S*1000) {
                interval_t.reset();                               
                time_t timestamp = time(NULL);
                struct tm* tminfo = localtime(&timestamp);
                char tmbuf[64];
                strftime(tmbuf, sizeof(tmbuf), "/usb/img%M%S.jpg", tminfo);
                string path = tmbuf;
                printf("%s %d bytes\n", path.c_str(), pos);
                FILE* fp = fopen(path.c_str(), "wb");
                if (fp) {
                    fwrite(buf, pos, 1, fp);
                    fclose(fp);
                }
                led4 = !led4;
            }
        }
    }

    void callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
        inputPacket(buf, len);
        led1 = buf[1]&1;    // FID
        if (buf[1]&2) {     // EOF
            led2 = !led2;
        }
    }

    virtual void run() {
        while(true) {
            if (m_cam) {
                m_cam->poll();
            }
        }
    }
    uint8_t* buf;
    int buf_size;
    int pos;
    BaseUvc* m_cam;
};

void no_memory () {
  error("Failed to allocate memory!\n");
}

int main() {
    pc.baud(921600);
    printf("%s\n", __FILE__);
    set_new_handler(no_memory);

    BaseUvc* cam = NULL;
    UsbFlashDrive* drive = NULL;
    BaseUsbHost* usbHost = new BaseUsbHost();
    ControlEp* ctlEp = new ControlEp; // root hub
    if (!UsbHub::check(ctlEp)) {
        error("USB Hub is not connected.\n");
    }
    UsbHub* hub = new UsbHub(ctlEp);
    for(vector<ControlEp*>::iterator it = hub->PortEp.begin(); it != hub->PortEp.end(); ++it) {
        if (drive == NULL && UsbFlashDrive::check(*it)) {
            drive = new UsbFlashDrive("usb", *it);
        } else if (cam == NULL && LogitechC270::check(*it)) {
            cam = new LogitechC270(C270_MJPEG, C270_160x120, _5FPS, *it); 
        } else if (cam == NULL && LifeCamVX700::check(*it)) {
            cam = new LifeCamVX700(VX700_160x120, _5FPS, *it); 
        } else if (cam == NULL && UvcCam::check(*it)) {
            cam = new UvcCam(UVC_MJPEG, UVC_160x120, _5FPS, *it); 
        }
    }
    if (cam == NULL) {
        error("UVC Camera is not connected.\n");
    }
    if (drive == NULL) {
        error("USB flash drive is not connected.\n");
    }
    
    captureJPEG* capture = new captureJPEG(cam);
    capture->set_stack(DEFAULT_STACK_SIZE+128*4);
    capture->start();

    for(int n = 0; ; n++) {
        printf("%d captureJPEG stack used: %d/%d bytes\n", n, capture->stack_used(), capture->stack_size());
        Thread::wait(5*1000);
    }
}