supported GR-PEACH original: http://developer.mbed.org/users/va009039/code/USBHostC270_example/ The function of Isochronous has moved to USBHost_AddIso library.

Dependencies:   USBHost_custom_Addiso

Fork of USBHostC270_example_GR-PEACH by GR-PEACH_producer_meeting

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostCam.cpp Source File

USBHostCam.cpp

00001 // USBHostCam.cpp
00002 #include "USBHostCam.h"
00003 #include "dbg.h"
00004 
00005 //#define CAM_DEBUG 1
00006 #ifdef CAM_DEBUG
00007 #define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
00008 #else
00009 #define CAM_DBG(...)  while(0);
00010 #endif
00011 
00012 // ------------------ HcControl Register ---------------------
00013 #define  OR_CONTROL_IE                  0x00000008
00014 
00015 CamInfo* getCamInfoList(); // CamInfo.cpp
00016 
00017 USBHostCam::USBHostCam(uint8_t size, uint8_t option, CamInfo* user_caminfo)
00018 {
00019     CAM_DBG("size: %d, option: %d", size, option);
00020     _caminfo_size = size;
00021     _caminfo_option = option;
00022     if (user_caminfo) {
00023         CamInfoList = user_caminfo;
00024     } else {
00025         CamInfoList = getCamInfoList();
00026     }
00027     clearOnResult();
00028     host = USBHost::getHostInst();
00029     m_isoEp = new IsochronousEp;
00030     init();
00031 }
00032 
00033 void USBHostCam::init()
00034 {
00035     CAM_DBG("");
00036     dev_connected = false;
00037     dev = NULL;
00038     cam_intf = -1;
00039     device_found = false;
00040     caminfo_found = false;
00041 }
00042 
00043 bool USBHostCam::connected()
00044 {
00045     return dev_connected;
00046 }
00047 
00048 bool USBHostCam::connect()
00049 {
00050     if (dev_connected) {
00051         return true;
00052     }
00053 
00054     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00055         if ((dev = host->getDevice(i)) != NULL) {
00056             
00057             CAM_DBG("Trying to connect Cam device\r\n");
00058             
00059             if(host->enumerate(dev, this)) {
00060                 break;
00061             }
00062             if (device_found) {
00063                 USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf);
00064                 dev->setName(caminfo->name, cam_intf);
00065                 host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect);
00066                 int addr = dev->getAddress();
00067                 m_isoEp->init(addr, caminfo->en, caminfo->mps, caminfo->frameCount, caminfo->queueLimit);
00068                 uint8_t buf[26];
00069                 memset(buf, 0, sizeof(buf));
00070                 buf[2] = caminfo->formatIndex;
00071                 buf[3] = caminfo->frameIndex;
00072                 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval;
00073                 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
00074                 if (res != USB_TYPE_OK) {
00075                     CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
00076                 }
00077                 res = setInterfaceAlternate(1, caminfo->if_alt);
00078                 if (res != USB_TYPE_OK) {
00079                     CAM_DBG("SET_INTERFACE FAILED");
00080                 }
00081                 for(int i = 0; i < 16; i++) {
00082                     report_cc_count[i] = 0;
00083                     report_ps_cc_count[i] = 0;
00084                 }
00085 
00086                 dev_connected = true;
00087                 return true;
00088             }
00089         }
00090     }
00091     init();
00092     return false;
00093 }
00094 
00095 void USBHostCam::onDisconnect()
00096 {
00097     CAM_DBG("dev_connected: %d", dev_connected);
00098     if (dev_connected) {
00099         m_isoEp->disconnect();
00100         init();
00101     }
00102 }
00103 
00104 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid)
00105 {
00106     CAM_DBG("vid:%04x,pid:%04x", vid, pid);
00107     caminfo = CamInfoList;
00108     while(caminfo->vid != 0) {
00109         if (caminfo->vid == vid && caminfo->pid == pid && 
00110             caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) {
00111             caminfo_found = true;
00112             break;
00113         }
00114         caminfo++;
00115     }
00116 }
00117 
00118 /*virtual*/ bool USBHostCam::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
00119 {
00120     CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
00121     if ((cam_intf == -1) && caminfo_found) {
00122         cam_intf = intf_nb;
00123         device_found = true;
00124         return true;
00125     }
00126     return false;
00127 }
00128 
00129 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00130 {
00131     CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
00132     return false;
00133 }
00134 
00135 #define SEQ_READ_IDOL 0
00136 #define SEQ_READ_EXEC 1
00137 #define SEQ_READ_DONE 2
00138 
00139 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) {
00140     _buf = buf;
00141     _pos = 0;
00142     _size = size;
00143     _seq = SEQ_READ_IDOL;
00144     setOnResult(this, &USBHostCam::callback_motion_jpeg);
00145     Timer timeout_t;
00146     timeout_t.reset();
00147     timeout_t.start();
00148     while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) {
00149         poll();
00150         Thread::wait(1);
00151     } 
00152     return _pos;
00153 }
00154 
00155 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
00156     if (_seq == SEQ_READ_IDOL) {
00157         if (status == JPEG_START) {
00158             _pos = 0;
00159             _seq = SEQ_READ_EXEC;
00160         }
00161     }
00162     if (_seq == SEQ_READ_EXEC) {
00163         if (_pos < _size) {
00164             _buf[_pos++] = c;  
00165         }  
00166         if (status == JPEG_END) {
00167             _seq = SEQ_READ_DONE;
00168         }
00169     }
00170 }
00171 
00172 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
00173         inputPacket(buf, len);
00174 }