Lcd companion boards support (VKLCD50RTA & VKLCD70RT)

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::plugged()
00049 {
00050     return host->plug_status;
00051 }
00052 
00053 bool USBHostCam::connect()
00054 {
00055     if (dev_connected) {
00056         return true;
00057     }
00058 
00059     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00060         if ((dev = host->getDevice(i)) != NULL) {
00061             
00062             CAM_DBG("Trying to connect Cam device\r\n");
00063             
00064             if(host->enumerate(dev, this)) {
00065                 break;
00066             }
00067             if (device_found) {
00068                 USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf);
00069                 dev->setName(caminfo->name, cam_intf);
00070                 host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect);
00071                 int addr = dev->getAddress();
00072                 m_isoEp->init(addr, caminfo->en, caminfo->mps, caminfo->frameCount, caminfo->queueLimit);
00073                 uint8_t buf[26];
00074                 memset(buf, 0, sizeof(buf));
00075                 buf[2] = caminfo->formatIndex;
00076                 buf[3] = caminfo->frameIndex;
00077                 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval;
00078                 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
00079                 if (res != USB_TYPE_OK) {
00080                     CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
00081                 }
00082                 res = setInterfaceAlternate(1, caminfo->if_alt);
00083                 if (res != USB_TYPE_OK) {
00084                     CAM_DBG("SET_INTERFACE FAILED");
00085                 }
00086                 for(int i = 0; i < 16; i++) {
00087                     report_cc_count[i] = 0;
00088                     report_ps_cc_count[i] = 0;
00089                 }
00090 
00091                 dev_connected = true;
00092                 return true;
00093             }
00094         }
00095     }
00096     init();
00097     return false;
00098 }
00099 
00100 void USBHostCam::onDisconnect()
00101 {
00102     CAM_DBG("dev_connected: %d", dev_connected);
00103     if (dev_connected) {
00104         m_isoEp->disconnect();
00105         init();
00106     }
00107 }
00108 
00109 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid)
00110 {
00111     CAM_DBG("vid:%04x,pid:%04x", vid, pid);
00112     caminfo = CamInfoList;
00113     while(caminfo->vid != 0) {
00114         if (caminfo->vid == vid && caminfo->pid == pid && 
00115             caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) {
00116             caminfo_found = true;
00117             break;
00118         }
00119         caminfo++;
00120     }
00121 }
00122 
00123 /*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
00124 {
00125     CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
00126     if ((cam_intf == -1) && caminfo_found) {
00127         cam_intf = intf_nb;
00128         device_found = true;
00129         return true;
00130     }
00131     return false;
00132 }
00133 
00134 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00135 {
00136     CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
00137     return false;
00138 }
00139 
00140 #define SEQ_READ_IDOL 0
00141 #define SEQ_READ_EXEC 1
00142 #define SEQ_READ_DONE 2
00143 
00144 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) {
00145     _buf = buf;
00146     _pos = 0;
00147     _size = size;
00148     _seq = SEQ_READ_IDOL;
00149     setOnResult(this, &USBHostCam::callback_motion_jpeg);
00150     Timer timeout_t;
00151     timeout_t.reset();
00152     timeout_t.start();
00153     while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) {
00154         poll();
00155         Thread::wait(1);
00156     }
00157     return _pos;
00158 }
00159 
00160 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
00161     if (_seq == SEQ_READ_IDOL) {
00162         if (status == JPEG_START) {
00163             _pos = 0;
00164             _seq = SEQ_READ_EXEC;
00165         }
00166     }
00167     if (_seq == SEQ_READ_EXEC) {
00168         if (_pos < _size) {
00169             _buf[_pos++] = c;  
00170         }  
00171         if (status == JPEG_END) {
00172             _seq = SEQ_READ_DONE;
00173         }
00174     }
00175 }
00176 
00177 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
00178         inputPacket(buf, len);
00179 }