BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

Revision:
2:c10029b87439
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example_captureJPEG.cpp	Tue Dec 11 15:28:00 2012 +0000
@@ -0,0 +1,140 @@
+#if 0
+// example_captureJPEG.cpp
+#include "mbed.h"
+#include "rtos.h"
+#include "BaseUsbHost.h"
+#include "LogitechC270.h"
+#include "UvcCam.h"
+#include "UsbFlashDrive.h"
+#include "decodeMJPEG.h"
+#include "MyThread.h"
+
+#define IMAGE_BUF_SIZE (1024*8)
+#define INTERVAL_S 10
+#define SHOT_N 5
+#define BASE_PATH "usb"
+
+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();
+        shot = 0;
+        for(buf_size = IMAGE_BUF_SIZE; ; buf_size -= 128) {
+            buf = new uint8_t[buf_size];
+            if (buf) {
+                break;
+            }
+        }
+        printf("%p buf size: %d\n", buf, buf_size); 
+    }
+    int shot;    
+    Timer interval_t;
+protected:    
+    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();                               
+                led4 = !led4;
+                if (shot < SHOT_N) {
+                    char path[32];
+                    snprintf(path, sizeof(path), "/%s/image%02d.jpg", BASE_PATH, shot);
+                    printf("%d %s %d bytes\n", shot, path, pos);
+                    FILE* fp = fopen(path, "wb");
+                    if (fp) {
+                        fwrite(buf, pos, 1, fp);
+                        fclose(fp);
+                    }
+                    shot++;
+                }
+            }
+        }
+    }
+
+    void callback_motion_jpeg(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();
+            }
+        }
+    }
+    uint8_t* buf;
+    int buf_size;
+    int pos;
+    BaseUvc* m_cam;
+};
+
+int main() {
+    pc.baud(921600);
+    printf("%s\n", __FILE__);
+
+    BaseUvc* cam = NULL;
+    UsbFlashDrive* drive = NULL;
+    BaseUsbHost* usbHost = new BaseUsbHost();
+    UsbHub* hub = new UsbHub();
+    
+    for(int i = 1; i <= MAX_HUB_PORT; i++) {
+        ControlEp* ctlEp = hub->GetPortEp(i);
+        if (drive == NULL && UsbFlashDrive::check(ctlEp)) {
+            drive = new UsbFlashDrive("usb", ctlEp);
+        }
+        if (cam == NULL && LogitechC270::check(ctlEp)) {
+            cam = new LogitechC270(C270_160x120, _5FPS, ctlEp); 
+        }
+        if (cam == NULL && UvcCam::check(ctlEp)) {
+            cam = new UvcCam(UVC_MJPEG, UVC_160x120, _5FPS, ctlEp); 
+        }
+    }
+    if (cam == NULL) {
+        error("UVC cam not found\n");
+    }
+    if (drive == NULL) {
+        error("USB flash drive not found\n");
+    }
+    
+    captureJPEG* capture = new captureJPEG(cam);
+    printf("captureJPEG: %p %d\n", capture, sizeof(captureJPEG));
+    capture->set_stack(DEFAULT_STACK_SIZE+256);
+    capture->start();
+
+    for(int n = 0; ; n++) {
+        printf("%d captureJPEG stack used: %d/%d bytes, interval %d/%d %d/%d sec\n", 
+            n, capture->stack_used(), capture->stack_size(), 
+            capture->shot, SHOT_N, capture->interval_t.read_ms()/1000, INTERVAL_S);
+        printf("CC:");
+        for(int i = 0; i < 16; i++) {
+            printf(" %u", cam->report_cc_count[i]);
+        }
+        printf("\n");
+        printf("PS:");
+        for(int i = 0; i < 16; i++) {
+            printf(" %u", cam->report_ps_cc_count[i]);
+        }
+        printf("\n");
+        Thread::wait(3000);
+    }
+}
+
+#endif