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.
Fork of USBHostC270_example by
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 LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable 00086 LPC_USB->HcControl |= OR_CONTROL_IE; // IsochronousEnable 00087 00088 dev_connected = true; 00089 return true; 00090 } 00091 } 00092 } 00093 init(); 00094 return false; 00095 } 00096 00097 void USBHostCam::onDisconnect() 00098 { 00099 CAM_DBG("dev_connected: %d", dev_connected); 00100 if (dev_connected) { 00101 m_isoEp->disconnect(); 00102 init(); 00103 } 00104 } 00105 00106 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) 00107 { 00108 CAM_DBG("vid:%04x,pid:%04x", vid, pid); 00109 caminfo = CamInfoList; 00110 while(caminfo->vid != 0) { 00111 if (caminfo->vid == vid && caminfo->pid == pid && 00112 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { 00113 caminfo_found = true; 00114 break; 00115 } 00116 caminfo++; 00117 } 00118 } 00119 00120 /*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 00121 { 00122 CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); 00123 if ((cam_intf == -1) && caminfo_found) { 00124 cam_intf = intf_nb; 00125 device_found = true; 00126 return true; 00127 } 00128 return false; 00129 } 00130 00131 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00132 { 00133 CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); 00134 return false; 00135 } 00136 00137 #define SEQ_READ_IDOL 0 00138 #define SEQ_READ_EXEC 1 00139 #define SEQ_READ_DONE 2 00140 00141 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) { 00142 _buf = buf; 00143 _pos = 0; 00144 _size = size; 00145 _seq = SEQ_READ_IDOL; 00146 setOnResult(this, &USBHostCam::callback_motion_jpeg); 00147 Timer timeout_t; 00148 timeout_t.reset(); 00149 timeout_t.start(); 00150 while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) { 00151 poll(); 00152 Thread::wait(1); 00153 } 00154 return _pos; 00155 } 00156 00157 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG 00158 if (_seq == SEQ_READ_IDOL) { 00159 if (status == JPEG_START) { 00160 _pos = 0; 00161 _seq = SEQ_READ_EXEC; 00162 } 00163 } 00164 if (_seq == SEQ_READ_EXEC) { 00165 if (_pos < _size) { 00166 _buf[_pos++] = c; 00167 } 00168 if (status == JPEG_END) { 00169 _seq = SEQ_READ_DONE; 00170 } 00171 } 00172 } 00173 00174 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { 00175 inputPacket(buf, len); 00176 }
Generated on Mon Jul 18 2022 19:52:39 by
1.7.2
