BaseUsbHost example program
Dependencies: BaseUsbHost FATFileSystem mbed mbed-rtos
main.cpp
00001 // BaseUsbHost_example/main.cpp 2013/1/25 00002 #include "mbed.h" 00003 #include "rtos.h" 00004 #include "BaseUsbHost.h" 00005 #include "LogitechC270.h" 00006 #include "LifeCamVX700.h" 00007 #include "UvcCam.h" 00008 #include "UsbFlashDrive.h" 00009 #include "decodeMJPEG.h" 00010 #include "MyThread.h" 00011 #include <string> 00012 00013 #define IMAGE_BUF_SIZE (1024*7) 00014 #define INTERVAL_S 30 00015 00016 Serial pc(USBTX, USBRX); 00017 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); 00018 00019 struct ImageBuffer { 00020 int pos; 00021 uint8_t buf[IMAGE_BUF_SIZE]; 00022 void clear() { pos = 0; } 00023 int size() { return pos; } 00024 uint8_t get(int pos) { return buf[pos]; } 00025 void put(uint8_t c) { 00026 if (pos < sizeof(buf)) { 00027 buf[pos++] = c; 00028 } 00029 } 00030 }; 00031 00032 Mail<ImageBuffer, 1> mail_box; 00033 00034 class captureJPEG : public MyThread, public decodeMJPEG { 00035 public: 00036 captureJPEG(BaseUvc* cam) : m_cam(cam) { 00037 m_buf = NULL; 00038 m_cam->setOnResult(this, &captureJPEG::callback_motion_jpeg); 00039 } 00040 private: 00041 virtual void outputJPEG(uint8_t c, int status) { 00042 if (m_buf == NULL && status == JPEG_START) { 00043 m_buf = mail_box.alloc(); 00044 if (m_buf) { 00045 m_buf->clear(); 00046 } 00047 } 00048 if (m_buf) { 00049 m_buf->put(c); 00050 if (status == JPEG_END) { 00051 mail_box.put(m_buf); 00052 m_buf = NULL; 00053 led3 = !led3; 00054 } 00055 } 00056 } 00057 00058 void callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { 00059 inputPacket(buf, len); 00060 led1 = buf[1]&1; // FID 00061 if (buf[1]&2) { // EOF 00062 led2 = !led2; 00063 } 00064 } 00065 00066 virtual void run() { 00067 while(true) { 00068 if (m_cam) { 00069 m_cam->poll(); 00070 } 00071 } 00072 } 00073 ImageBuffer* m_buf; 00074 BaseUvc* m_cam; 00075 }; 00076 00077 void no_memory () { 00078 error("Failed to allocate memory!\n"); 00079 } 00080 00081 int main() { 00082 pc.baud(921600); 00083 printf("%s\n", __FILE__); 00084 set_new_handler(no_memory); 00085 00086 BaseUvc* cam = NULL; 00087 UsbFlashDrive* drive = NULL; 00088 BaseUsbHost* usbHost = new BaseUsbHost(); 00089 ControlEp* ctlEp = new ControlEp; // root hub 00090 if (!UsbHub::check(ctlEp)) { 00091 error("USB Hub is not connected.\n"); 00092 } 00093 UsbHub* hub = new UsbHub(ctlEp); 00094 ctlEp = hub->search<UsbFlashDrive>(); 00095 if (ctlEp) { 00096 drive = new UsbFlashDrive("usb", ctlEp); 00097 } 00098 ctlEp = hub->search<LogitechC270>(); 00099 if (ctlEp) { 00100 //cam = new LogitechC270(C270_MJPEG, C270_160x120, _5FPS, ctlEp); 00101 cam = new LogitechC270(C270_MJPEG, C270_320x176, _5FPS, ctlEp); 00102 } 00103 if (cam == NULL) { 00104 ctlEp = hub->search<LifeCamVX700>(); 00105 if (ctlEp) { 00106 cam = new LifeCamVX700(VX700_160x120, _5FPS, ctlEp); 00107 } 00108 } 00109 if (cam == NULL) { 00110 ctlEp = hub->search<UvcCam>(); 00111 if (ctlEp) { 00112 cam = new UvcCam(UVC_MJPEG, UVC_160x120, _5FPS, ctlEp); 00113 } 00114 } 00115 if (cam == NULL) { 00116 error("UVC Camera is not connected.\n"); 00117 } 00118 if (drive == NULL) { 00119 error("USB flash drive is not connected.\n"); 00120 } 00121 00122 captureJPEG* capture = new captureJPEG(cam); 00123 capture->set_stack(DEFAULT_STACK_SIZE-128*8); 00124 capture->start(); 00125 00126 for(int n = 0; ; n++) { 00127 printf("%d captureJPEG stack used: %d/%d bytes\n", n, capture->stack_used(), capture->stack_size()); 00128 osEvent evt = mail_box.get(); 00129 if (evt.status == osEventMail) { 00130 ImageBuffer *buf = reinterpret_cast<ImageBuffer*>(evt.value.p); 00131 time_t timestamp = time(NULL); 00132 struct tm* tminfo = localtime(×tamp); 00133 char tmbuf[32]; 00134 strftime(tmbuf, sizeof(tmbuf), "/usb/img%M%S.jpg", tminfo); 00135 string path = tmbuf; 00136 printf("%s %d bytes\n", path.c_str(), buf->size()); 00137 FILE* fp = fopen(path.c_str(), "wb"); 00138 if (fp) { 00139 for(int i = 0; i < buf->size(); i++) { 00140 fputc(buf->get(i), fp); 00141 //Thread::yield(); 00142 } 00143 fclose(fp); 00144 } 00145 mail_box.free(buf); 00146 led4 = !led4; 00147 } 00148 00149 printf("CC:"); 00150 for(int i = 0; i < 16; i++) { 00151 printf(" %u", cam->report_cc_count[i]); 00152 } 00153 printf("\nPS:"); 00154 for(int i = 0; i < 16; i++) { 00155 printf(" %u", cam->report_ps_cc_count[i]); 00156 } 00157 printf("\n"); 00158 00159 Thread::wait(INTERVAL_S*1000); 00160 } 00161 }
Generated on Wed Jul 13 2022 00:00:06 by 1.7.2