Simple USBHost WebCam test program
Dependencies: F401RE-USBHost mbed
Fork of KL46Z-USBHostC270_example by
WebカメラからJPEG画像を読み取るテストプログラムです。
使い方はKL46Z-USBHostC270_exampleと同じです。
動作確認カメラ: Logitech C270, Logitech C210, Logitech Q200R(Qcam Orbit AF), LifeCam VX-500
Diff: KL46Z_USBHostC270/USBHostCam.cpp
- Revision:
- 2:2a40888db9fc
- Parent:
- 1:22304b8f8395
--- a/KL46Z_USBHostC270/USBHostCam.cpp Tue Jan 28 06:54:16 2014 +0000 +++ b/KL46Z_USBHostC270/USBHostCam.cpp Fri Jan 31 13:50:15 2014 +0000 @@ -1,13 +1,13 @@ // USBHostCam.cpp #include "USBHostCam.h" -//#define CAM_DEBUG 1 -#ifdef CAM_DEBUG -#define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); +#if 0 +#define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else #define CAM_DBG(...) while(0); #endif #define CAM_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0); +#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0); CamInfo* getCamInfoList(); // CamInfo.cpp @@ -23,14 +23,78 @@ } clearOnResult(); host = USBHost::getHostInst(); - setup(); + dev = host->getDevice(0); + ep_iso_in = new USBEndpoint; + init(); } +void USBHostCam::init() +{ + CAM_DBG(""); + dev_connected = false; + dev = NULL; + cam_intf = -1; + device_found = false; + caminfo_found = false; +} + +bool USBHostCam::connected() +{ + return dev_connected; +} + +bool USBHostCam::connect() +{ + if (dev_connected) { + return true; + } + + for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { + if ((dev = host->getDevice(i)) != NULL) { + + CAM_DBG("Trying to connect Cam device\r\n"); + + if(host->enumerate(dev, this)) { + break; + } + if (device_found) { + USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf); + dev->setName(caminfo->name, cam_intf); + //host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect); + int addr = dev->getAddress(); + ep_iso_in->setDevice(dev); + ep_iso_in->setAddress(caminfo->en); + ep_iso_in->setSize(caminfo->mps); + //ep_iso_in->init(addr, caminfo->en, caminfo->mps, caminfo->frameCount, caminfo->queueLimit); + uint8_t buf[26]; + memset(buf, 0, sizeof(buf)); + buf[2] = caminfo->formatIndex; + buf[3] = caminfo->frameIndex; + *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval; + USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf)); + if (res != USB_TYPE_OK) { + CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED"); + } + res = setInterfaceAlternate(1, caminfo->if_alt); + if (res != USB_TYPE_OK) { + CAM_DBG("SET_INTERFACE FAILED"); + } + dev_connected = true; + return true; + } + } + } + init(); + return false; +} + +#if 0 void USBHostCam::setup() { caminfo = CamInfoList; bool found = false; while(caminfo->vid != 0) { - if (caminfo->vid == host->dev.vid && caminfo->pid == host->dev.pid && + if (caminfo->vid == host->getDevice(0)->getVid() && + caminfo->pid == host->getDevice(0)->getPid() && caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { found = true; break; @@ -59,6 +123,39 @@ CAM_DBG("SET_INTERFACE FAILED"); } } +#endif + + +/*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) +{ + CAM_DBG("vid:%04x,pid:%04x", vid, pid); + caminfo = CamInfoList; + while(caminfo->vid != 0) { + if (caminfo->vid == vid && caminfo->pid == pid && + caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { + caminfo_found = true; + break; + } + caminfo++; + } +} + +/*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 +{ + CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); + if ((cam_intf == -1) && caminfo_found) { + cam_intf = intf_nb; + device_found = true; + return true; + } + return false; +} + +/*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used +{ + CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); + return false; +} #define SEQ_READ_IDOL 0 #define SEQ_READ_EXEC 1