first release for keyboard

Dependencies:   FATFileSystem2

Dependents:   N64_Output_KB

Fork of F401RE-USBHost by Norimasa Okamoto

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 
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                 ep_iso_in->ohci_init(caminfo->frameCount, caminfo->queueLimit);
00063                 uint8_t buf[26];
00064                 memset(buf, 0, sizeof(buf));
00065                 buf[2] = caminfo->formatIndex;
00066                 buf[3] = caminfo->frameIndex;
00067                 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval;
00068                 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
00069                 if (res != USB_TYPE_OK) {
00070                     USB_ERR("SET_CUR VS_COMMIT_CONTROL FAILED");
00071                 }
00072                 res = setInterfaceAlternate(1, caminfo->if_alt);
00073                 if (res != USB_TYPE_OK) {
00074                     USB_ERR("SET_INTERFACE FAILED");
00075                 }
00076                 dev_connected = true;
00077                 return true;
00078             }
00079         }
00080     }
00081     init();
00082     return false;
00083 }
00084 
00085 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid)
00086 {
00087     CAM_DBG("vid:%04x,pid:%04x", vid, pid);
00088     caminfo = CamInfoList;
00089     while(caminfo->vid != 0) {
00090         if (caminfo->vid == vid && caminfo->pid == pid && 
00091             caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) {
00092             caminfo_found = true;
00093             break;
00094         }
00095         caminfo++;
00096     }
00097 }
00098  
00099 /*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
00100 {
00101     CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
00102     if ((cam_intf == -1) && caminfo_found) {
00103         cam_intf = intf_nb;
00104         device_found = true;
00105         return true;
00106     }
00107     return false;
00108 }
00109  
00110 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00111 {
00112     CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
00113     return false;
00114 }
00115 
00116 #define SEQ_READ_IDLE 0
00117 #define SEQ_READ_EXEC 1
00118 #define SEQ_READ_DONE 2
00119 
00120 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) {
00121     _buf = buf;
00122     _pos = 0;
00123     _size = size;
00124     _seq = SEQ_READ_IDLE;
00125     setOnResult(this, &USBHostCam::callback_motion_jpeg);
00126     Timer timeout_t;
00127     timeout_t.reset();
00128     timeout_t.start();
00129     while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) {
00130         poll();
00131     } 
00132     return _pos;
00133 }
00134 
00135 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
00136     if (_seq == SEQ_READ_IDLE) {
00137         if (status == JPEG_START) {
00138             _pos = 0;
00139             _seq = SEQ_READ_EXEC;
00140         }
00141     }
00142     if (_seq == SEQ_READ_EXEC) {
00143         if (_pos < _size) {
00144             _buf[_pos++] = c;  
00145         }  
00146         if (status == JPEG_END) {
00147             _seq = SEQ_READ_DONE;
00148         }
00149     }
00150 }
00151 
00152 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
00153         inputPacket(buf, len);
00154 }