testing n-Bed with a Logitech C270 camera

Dependencies:   USBHost mbed

Fork of USBHostC270_example by Norimasa Okamoto

Revision:
10:387c49b2fc7e
Parent:
9:fecabade834a
Child:
11:6a8eef89eb22
--- a/USBHostC270/USBHostC270.cpp	Sat Mar 16 13:07:55 2013 +0000
+++ b/USBHostC270/USBHostC270.cpp	Sun Mar 17 13:22:13 2013 +0000
@@ -1,14 +1,24 @@
 #include "USBHostC270.h"
 #include "dbg.h"
 
+//#define C270_DEBUG 1
+#ifdef C270_DEBUG
+#define C270_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
+#else
+#define C270_DBG(...)  while(0);
+#endif
+
 // ------------------ HcControl Register ---------------------
 #define  OR_CONTROL_IE                  0x00000008
 
 USBHostC270::USBHostC270(int formatIndex, int frameIndex, uint32_t interval)
 {
+    C270_DBG("formatIndex: %d, frameIndex: %d, interval: %d", formatIndex, frameIndex, interval);
     _formatIndex = formatIndex;
     _frameIndex = frameIndex;
     _interval = interval;
+    m_isoEp = NULL;
+    clearOnResult();
     host = USBHost::getHostInst();
     init();
 }
@@ -21,18 +31,17 @@
     c270_intf = -1;
     c270_device_found = false;
     c270_vid_pid_found = false;
-    clearOnResult();
 }
 
 bool USBHostC270::connected()
 {
-    C270_DBG("");
+    C270_DBG("dev_connected: %d", dev_connected);
     return dev_connected;
 }
 
 bool USBHostC270::connect()
 {
-    C270_DBG("");
+    C270_DBG("dev_connected: %d", dev_connected);
     if (dev_connected) {
         return true;
     }
@@ -48,7 +57,7 @@
             if (c270_device_found) {
                 USB_INFO("New C270 device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, c270_intf);
                 dev->setName("C270", c270_intf);
-                host->registerDriver(dev, c270_intf, this, &USBHostC270::init);
+                host->registerDriver(dev, c270_intf, this, &USBHostC270::onDisconnect);
                 int addr = dev->getAddress();
                 m_isoEp = new IsochronousEp(addr, C270_EN, C270_MPS);
                 uint8_t buf[26];
@@ -80,6 +89,16 @@
     return false;
 }
 
+void USBHostC270::onDisconnect()
+{
+    C270_DBG("dev_connected: %d", dev_connected);
+    // TODO
+    if (m_isoEp) {
+       m_isoEp->disconnect();
+    }
+    init();
+}
+
 /*virtual*/ void USBHostC270::setVidPid(uint16_t vid, uint16_t pid)
 {
     C270_DBG("vid:%04x,pid:%04x", vid, pid);
@@ -95,6 +114,7 @@
     C270_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
     if ((c270_intf == -1) && c270_vid_pid_found) {
         c270_intf = intf_nb;
+        c270_vid_pid_found = false;
         c270_device_found = true;
         return true;
     }
@@ -107,23 +127,42 @@
     return false;
 }
 
-USB_TYPE USBHostC270::setInterfaceAlternate(uint8_t intf, uint8_t alt)
-{
-    C270_DBG("intf:%d, alt:%d", intf, alt);
-    return host->controlWrite(dev, USB_HOST_TO_DEVICE | USB_RECIPIENT_INTERFACE,
-                                   SET_INTERFACE, alt, intf, NULL, 0);
+#define SEQ_READ_IDOL 0
+#define SEQ_READ_EXEC 1
+#define SEQ_READ_DONE 2
+
+int USBHostC270::readJPEG(uint8_t* buf, int size, int timeout_ms) {
+    _buf = buf;
+    _pos = 0;
+    _size = size;
+    _seq = SEQ_READ_IDOL;
+    setOnResult(this, &USBHostC270::callback_motion_jpeg);
+    Timer timeout_t;
+    timeout_t.reset();
+    timeout_t.start();
+    while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) {
+        poll(timeout_ms);
+    } 
+    return _pos;
 }
 
-USB_TYPE USBHostC270::Control(int req, int cs, int index, uint8_t* buf, int size)
-{
-    C270_DBG("req:%d,cs:%d,index:%d", req, cs,index);
-    if (req == SET_CUR) {    
-        return host->controlWrite(dev,
-                    USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
-                    req, cs<<8, index, buf, size);
+/* virtual */ void USBHostC270::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
+    if (_seq == SEQ_READ_IDOL) {
+        if (status == JPEG_START) {
+            _pos = 0;
+            _seq = SEQ_READ_EXEC;
+        }
     }
-    return host->controlRead(dev,
-                USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
-                req, cs<<8, index, buf, size);
+    if (_seq == SEQ_READ_EXEC) {
+        if (_pos < _size) {
+            _buf[_pos++] = c;  
+        }  
+        if (status == JPEG_END) {
+            _seq = SEQ_READ_DONE;
+        }
+    }
 }
 
+void USBHostC270::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
+        inputPacket(buf, len);
+}