Simple USBHost C270 example

Dependencies:   F401RE-USBHost mbed

/media/uploads/va009039/k64f-c270.jpg

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Fri Aug 08 14:08:44 2014 +0000
Commit message:
first commit

Changed in this revision

F401RE-USBHost.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 5ca8a4347f67 F401RE-USBHost.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/F401RE-USBHost.lib	Fri Aug 08 14:08:44 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/va009039/code/F401RE-USBHost/#47978c25c9b8
diff -r 000000000000 -r 5ca8a4347f67 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 08 14:08:44 2014 +0000
@@ -0,0 +1,79 @@
+#include "USBHostCam.h"
+RawSerial pc(USBTX, USBRX);
+
+uint8_t image_buf[1024*64];
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+#define LED_OFF 1
+#define LED_ON  0
+
+int main() {
+    pc.baud(9600);
+    led1 = led2 = LED_OFF;
+
+    // Logitech C270
+    USBHostCam* cam = new USBHostCam(_800x448);
+    if (!cam->connect()) {
+        error("WebCam not found.\n");
+    }
+
+    int pos = 0; 
+    int size = 0;
+    Timer t;
+    for(int seq = 0;;) {
+        int c = -1;
+        if (pc.readable()) {
+            c = pc.getc();
+        }    
+        switch(seq) {
+            case 0:
+                pc.printf("\nReady, please download JPEG using XMODEM/Checksum.\n");
+                t.start();
+                seq++;
+                break;
+            case 1:
+                if (c == 0x15) { // NAK
+                    size = cam->readJPEG(image_buf, sizeof(image_buf));
+                    pos = 0;
+                    seq++;
+                }
+                break;
+            case 2:
+                if (pos >= size) {
+                    pc.putc(0x04); // EOT
+                } else {
+                    pc.putc(0x01); // SOH
+                    uint8_t block = pos/128 + 1;
+                    pc.putc(block);
+                    pc.putc(block ^ 0xff);
+                    uint8_t checksum = 0x00;
+                    for(int i = 0; i < 128; i++) {
+                        uint8_t c = image_buf[pos + i];
+                        checksum += c;
+                        pc.putc(c);
+                    }
+                    pc.putc(checksum);
+                }
+                seq++;
+                t.reset();
+                led2 = !led2;
+                break;
+            case 3:
+                if (c == 0x06) { // ACK
+                    if (pos >= size) {
+                        seq = 0;
+                    } else {
+                        pos += 128;
+                        seq--;
+                    }
+                }
+                break;
+        }
+        if (t.read_ms() > 15*1000) { // timeout
+            t.reset();
+            seq = 0;
+        }
+        cam->poll();
+    }
+}
+
diff -r 000000000000 -r 5ca8a4347f67 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Aug 08 14:08:44 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804
\ No newline at end of file