BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

Revision:
2:c10029b87439
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example_captureYUY2.cpp	Tue Dec 11 15:28:00 2012 +0000
@@ -0,0 +1,125 @@
+#if 0
+#include "mbed.h"
+#include "rtos.h"
+#include "BaseUsbHost.h"
+#include "UvcCam.h"
+#include "MyThread.h"
+#include <bitset>
+
+Serial pc(USBTX, USBRX);
+DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
+
+class decodeYUY2 {
+public:
+    decodeYUY2() {
+        xy = 0;
+    }
+protected:
+    void inputPacket(uint8_t* buf, int len) {
+        for(int i = 12; i < len; i += 4) {
+            outputY(xy, buf[i]);
+            outputCb(xy+1, buf[i+1]);
+            outputY(xy+2, buf[i+2]);
+            outputCr(xy+3, buf[i+3]);
+            xy += 4;
+        }
+        if (buf[1]&2) { //EOF
+            xy = 0;
+        }
+    }
+    virtual void outputY(int xy, uint8_t c)=0;
+    virtual void outputCb(int xy, uint8_t c)=0;
+    virtual void outputCr(int xy, uint8_t c)=0;
+    int xy;
+};
+
+class captureYUY2 : public MyThread, public decodeYUY2 {
+public:
+    captureYUY2(BaseUvc* cam) {
+        m_cam = cam;
+        m_cam->setOnResult(this, &captureYUY2::callback);
+    }
+    bitset<160*120> bitmap;
+    uint8_t buf[160];
+protected:
+    virtual void outputY(int xy, uint8_t c) {
+        if (xy < bitmap.size()) {
+            bitmap.set(xy, c > 0x80);
+        }
+        if (xy < sizeof(buf)) {
+            buf[xy] = c;
+        }
+    }
+    virtual void outputCb(int xy, uint8_t c) {}
+    virtual void outputCr(int xy, uint8_t c) {}
+    void callback(uint16_t frame, uint8_t* buf, int len) {
+        inputPacket(buf, len);
+        if (buf[1]&1) { // FID
+            led1 = !led1;
+        }
+        if (buf[1]&2) { // EOF
+            led2 = !led2;
+        }
+    }
+    virtual void run() {
+        while(true) {
+            if (m_cam) {
+                m_cam->poll();
+            }
+        }
+    }
+    BaseUvc* m_cam;
+};
+
+int main() {
+    pc.baud(921600);
+    printf("%s\n", __FILE__);
+
+    BaseUsbHost* usbHost = new BaseUsbHost();
+    UsbHub* hub = new UsbHub();
+    BaseUvc* cam = NULL;
+    for(int i = 1; i <= MAX_HUB_PORT; i++) {
+        ControlEp* ctlEp = hub->GetPortEp(i);
+        if (cam == NULL && UvcCam::check(ctlEp)) {
+            cam = new UvcCam(UVC_YUY2, UVC_160x120, _5FPS, ctlEp); 
+        }
+    }
+    if (cam == NULL) {
+        error("cam not found\n");
+    }
+    captureYUY2* capture = new captureYUY2(cam);
+    capture->set_stack(DEFAULT_STACK_SIZE-128*12);
+    capture->start();
+
+    for(int n = 0; ; n++) {
+        for(int y = 0; y < 120; y+= 6) {
+            for(int x = 0; x < 160; x+= 3) {
+                printf("%c", capture->bitmap[y*160+x] ? '*':'.');
+            }
+            printf("\n");
+        }
+
+        printf("%d captureYUY2 stack used: %d/%d bytes\n", n, capture->stack_used(),  capture->stack_size());
+        printf("CC:"); // conditon code
+        for(int i = 0; i < 16; i++) {
+            printf(" %u", cam->report_cc_count[i]);
+        }
+        printf("\n");
+        printf("PS:"); // Packet status
+        for(int i = 0; i < 16; i++) {
+            printf(" %u", cam->report_ps_cc_count[i]);
+        }
+        printf("\n");
+       
+        for(int i = 0; i < 64; i++) {
+            printf("%02X ", capture->buf[i]);
+            if (i%16==15) {
+                printf("\n");
+            }
+        }
+        Thread::wait(400);
+        led3 = !led3;
+    }
+}
+
+#endif