Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Rejestrator
USBHostCam.cpp
00001 // USBHostCam.cpp 00002 #include "USBHostCam.h" 00003 00004 #if 0 00005 #define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); 00006 #else 00007 #define CAM_DBG(...) while(0); 00008 #endif 00009 #define CAM_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0); 00010 00011 CamInfo* getCamInfoList(); // CamInfo.cpp 00012 00013 USBHostCam::USBHostCam(uint8_t size, uint8_t option, CamInfo* user_caminfo) 00014 { 00015 CAM_DBG("size: %d, option: %d", size, option); 00016 _caminfo_size = size; 00017 _caminfo_option = option; 00018 if (user_caminfo) { 00019 CamInfoList = user_caminfo; 00020 } else { 00021 CamInfoList = getCamInfoList(); 00022 } 00023 clearOnResult(); 00024 host = USBHost::getHostInst(); 00025 init(); 00026 } 00027 00028 void USBHostCam::init() 00029 { 00030 CAM_DBG(""); 00031 dev_connected = false; 00032 dev = NULL; 00033 ep_iso_in = NULL; 00034 cam_intf = -1; 00035 device_found = false; 00036 caminfo_found = false; 00037 } 00038 00039 bool USBHostCam::connected() 00040 { 00041 return dev_connected; 00042 } 00043 00044 bool USBHostCam::connect() 00045 { 00046 if (dev_connected) { 00047 return true; 00048 } 00049 00050 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00051 if ((dev = host->getDevice(i)) != NULL) { 00052 00053 CAM_DBG("Trying to connect Cam device\r\n"); 00054 00055 if(host->enumerate(dev, this)) { 00056 break; 00057 } 00058 if (device_found) { 00059 USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf); 00060 ep_iso_in = new USBEndpoint(dev); 00061 ep_iso_in->init(ISOCHRONOUS_ENDPOINT, IN, caminfo->mps, caminfo->en); 00062 uint8_t buf[26]; 00063 memset(buf, 0, sizeof(buf)); 00064 buf[2] = caminfo->formatIndex; 00065 buf[3] = caminfo->frameIndex; 00066 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval; 00067 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf)); 00068 if (res != USB_TYPE_OK) { 00069 USB_ERR("SET_CUR VS_COMMIT_CONTROL FAILED"); 00070 } 00071 res = setInterfaceAlternate(1, caminfo->if_alt); 00072 if (res != USB_TYPE_OK) { 00073 USB_ERR("SET_INTERFACE FAILED"); 00074 } 00075 dev_connected = true; 00076 return true; 00077 } 00078 } 00079 } 00080 init(); 00081 return false; 00082 } 00083 00084 #if 0 00085 void USBHostCam::setup() { 00086 caminfo = CamInfoList; 00087 bool found = false; 00088 while(caminfo->vid != 0) { 00089 if (caminfo->vid == host->getDevice(0)->getVid() && 00090 caminfo->pid == host->getDevice(0)->getPid() && 00091 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { 00092 found = true; 00093 break; 00094 } 00095 caminfo++; 00096 } 00097 if (!found) { 00098 CAM_INFO("caminfo not found."); 00099 exit(1); 00100 } 00101 CAM_INFO("Found: %s", caminfo->name); 00102 00103 ep_iso_in.setAddress(caminfo->en); 00104 ep_iso_in.setSize(caminfo->mps); 00105 uint8_t buf[26]; 00106 memset(buf, 0, sizeof(buf)); 00107 buf[2] = caminfo->formatIndex; 00108 buf[3] = caminfo->frameIndex; 00109 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval; 00110 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf)); 00111 if (res != USB_TYPE_OK) { 00112 USB_ERR("SET_CUR VS_COMMIT_CONTROL FAILED"); 00113 } 00114 res = setInterfaceAlternate(1, caminfo->if_alt); 00115 if (res != USB_TYPE_OK) { 00116 USB_ERR("SET_INTERFACE FAILED"); 00117 } 00118 } 00119 #endif 00120 00121 00122 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) 00123 { 00124 CAM_DBG("vid:%04x,pid:%04x", vid, pid); 00125 caminfo = CamInfoList; 00126 while(caminfo->vid != 0) { 00127 if (caminfo->vid == vid && caminfo->pid == pid && 00128 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { 00129 caminfo_found = true; 00130 break; 00131 } 00132 caminfo++; 00133 } 00134 } 00135 00136 /*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 00137 { 00138 CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); 00139 if ((cam_intf == -1) && caminfo_found) { 00140 cam_intf = intf_nb; 00141 device_found = true; 00142 return true; 00143 } 00144 return false; 00145 } 00146 00147 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00148 { 00149 CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); 00150 return false; 00151 } 00152 00153 #define SEQ_READ_IDLE 0 00154 #define SEQ_READ_EXEC 1 00155 #define SEQ_READ_DONE 2 00156 00157 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) { 00158 _buf = buf; 00159 _pos = 0; 00160 _size = size; 00161 _seq = SEQ_READ_IDLE; 00162 setOnResult(this, &USBHostCam::callback_motion_jpeg); 00163 Timer timeout_t; 00164 timeout_t.reset(); 00165 timeout_t.start(); 00166 while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) { 00167 poll(); 00168 } 00169 return _pos; 00170 } 00171 00172 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG 00173 if (_seq == SEQ_READ_IDLE) { 00174 if (status == JPEG_START) { 00175 _pos = 0; 00176 _seq = SEQ_READ_EXEC; 00177 } 00178 } 00179 if (_seq == SEQ_READ_EXEC) { 00180 if (_pos < _size) { 00181 _buf[_pos++] = c; 00182 } 00183 if (status == JPEG_END) { 00184 _seq = SEQ_READ_DONE; 00185 } 00186 } 00187 } 00188 00189 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { 00190 inputPacket(buf, len); 00191 }
Generated on Sat Jul 30 2022 15:40:23 by
