BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

Committer:
va009039
Date:
Sun Jan 06 11:49:30 2013 +0000
Revision:
4:41ff237a64ec
Parent:
3:6ae9a03a6145
Child:
5:495f7536897b
update library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 3:6ae9a03a6145 1 // BaseUsbHost_example/main.cpp 2013/1/6
va009039 3:6ae9a03a6145 2 #include "mbed.h"
va009039 3:6ae9a03a6145 3 #include "rtos.h"
va009039 3:6ae9a03a6145 4 #include "BaseUsbHost.h"
va009039 3:6ae9a03a6145 5 #include "LogitechC270.h"
va009039 3:6ae9a03a6145 6 #include "LifeCamVX700.h"
va009039 3:6ae9a03a6145 7 #include "UvcCam.h"
va009039 3:6ae9a03a6145 8 #include "UsbFlashDrive.h"
va009039 3:6ae9a03a6145 9 #include "decodeMJPEG.h"
va009039 3:6ae9a03a6145 10 #include "MyThread.h"
va009039 3:6ae9a03a6145 11 #include <string>
va009039 3:6ae9a03a6145 12
va009039 3:6ae9a03a6145 13 #define IMAGE_BUF_SIZE (1024*6)
va009039 3:6ae9a03a6145 14 #define INTERVAL_S 20
va009039 3:6ae9a03a6145 15
va009039 3:6ae9a03a6145 16 Serial pc(USBTX, USBRX);
va009039 3:6ae9a03a6145 17 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
va009039 3:6ae9a03a6145 18
va009039 3:6ae9a03a6145 19 class captureJPEG : public MyThread, public decodeMJPEG {
va009039 3:6ae9a03a6145 20 public:
va009039 3:6ae9a03a6145 21 captureJPEG(BaseUvc* cam) : m_cam(cam) {
va009039 3:6ae9a03a6145 22 m_cam->setOnResult(this, &captureJPEG::callback_motion_jpeg);
va009039 3:6ae9a03a6145 23 interval_t.start();
va009039 3:6ae9a03a6145 24 buf_size = IMAGE_BUF_SIZE;
va009039 3:6ae9a03a6145 25 buf = new uint8_t[buf_size];
va009039 3:6ae9a03a6145 26 pos = 0;
va009039 3:6ae9a03a6145 27 }
va009039 3:6ae9a03a6145 28 Timer interval_t;
va009039 3:6ae9a03a6145 29 private:
va009039 3:6ae9a03a6145 30 virtual void outputJPEG(uint8_t c, int status) {
va009039 3:6ae9a03a6145 31 if (status == JPEG_START) {
va009039 3:6ae9a03a6145 32 pos = 0;
va009039 3:6ae9a03a6145 33 led2 = !led2;
va009039 3:6ae9a03a6145 34 }
va009039 3:6ae9a03a6145 35 if (pos < buf_size) {
va009039 3:6ae9a03a6145 36 buf[pos++] = c;
va009039 3:6ae9a03a6145 37 }
va009039 3:6ae9a03a6145 38 if (status == JPEG_END) {
va009039 3:6ae9a03a6145 39 led3 = !led3;
va009039 3:6ae9a03a6145 40 if (interval_t.read_ms() > INTERVAL_S*1000) {
va009039 3:6ae9a03a6145 41 interval_t.reset();
va009039 3:6ae9a03a6145 42 time_t timestamp = time(NULL);
va009039 3:6ae9a03a6145 43 struct tm* tminfo = localtime(&timestamp);
va009039 3:6ae9a03a6145 44 char tmbuf[64];
va009039 3:6ae9a03a6145 45 strftime(tmbuf, sizeof(tmbuf), "/usb/img%M%S.jpg", tminfo);
va009039 3:6ae9a03a6145 46 string path = tmbuf;
va009039 3:6ae9a03a6145 47 printf("%s %d bytes\n", path.c_str(), pos);
va009039 3:6ae9a03a6145 48 FILE* fp = fopen(path.c_str(), "wb");
va009039 3:6ae9a03a6145 49 if (fp) {
va009039 3:6ae9a03a6145 50 fwrite(buf, pos, 1, fp);
va009039 3:6ae9a03a6145 51 fclose(fp);
va009039 3:6ae9a03a6145 52 }
va009039 3:6ae9a03a6145 53 led4 = !led4;
va009039 3:6ae9a03a6145 54 }
va009039 3:6ae9a03a6145 55 }
va009039 3:6ae9a03a6145 56 }
va009039 3:6ae9a03a6145 57
va009039 3:6ae9a03a6145 58 void callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
va009039 3:6ae9a03a6145 59 inputPacket(buf, len);
va009039 3:6ae9a03a6145 60 led1 = buf[1]&1; // FID
va009039 3:6ae9a03a6145 61 if (buf[1]&2) { // EOF
va009039 3:6ae9a03a6145 62 led2 = !led2;
va009039 3:6ae9a03a6145 63 }
va009039 3:6ae9a03a6145 64 }
va009039 3:6ae9a03a6145 65
va009039 3:6ae9a03a6145 66 virtual void run() {
va009039 3:6ae9a03a6145 67 while(true) {
va009039 3:6ae9a03a6145 68 if (m_cam) {
va009039 3:6ae9a03a6145 69 m_cam->poll();
va009039 3:6ae9a03a6145 70 }
va009039 3:6ae9a03a6145 71 }
va009039 3:6ae9a03a6145 72 }
va009039 3:6ae9a03a6145 73 uint8_t* buf;
va009039 3:6ae9a03a6145 74 int buf_size;
va009039 3:6ae9a03a6145 75 int pos;
va009039 3:6ae9a03a6145 76 BaseUvc* m_cam;
va009039 3:6ae9a03a6145 77 };
va009039 3:6ae9a03a6145 78
va009039 3:6ae9a03a6145 79 void no_memory () {
va009039 3:6ae9a03a6145 80 error("Failed to allocate memory!\n");
va009039 3:6ae9a03a6145 81 }
va009039 3:6ae9a03a6145 82
va009039 3:6ae9a03a6145 83 int main() {
va009039 3:6ae9a03a6145 84 pc.baud(921600);
va009039 3:6ae9a03a6145 85 printf("%s\n", __FILE__);
va009039 3:6ae9a03a6145 86 set_new_handler(no_memory);
va009039 3:6ae9a03a6145 87
va009039 3:6ae9a03a6145 88 BaseUvc* cam = NULL;
va009039 3:6ae9a03a6145 89 UsbFlashDrive* drive = NULL;
va009039 3:6ae9a03a6145 90 BaseUsbHost* usbHost = new BaseUsbHost();
va009039 3:6ae9a03a6145 91 ControlEp* ctlEp = new ControlEp; // root hub
va009039 3:6ae9a03a6145 92 if (!UsbHub::check(ctlEp)) {
va009039 3:6ae9a03a6145 93 error("USB Hub is not connected.\n");
va009039 3:6ae9a03a6145 94 }
va009039 3:6ae9a03a6145 95 UsbHub* hub = new UsbHub(ctlEp);
va009039 3:6ae9a03a6145 96 for(vector<ControlEp*>::iterator it = hub->PortEp.begin(); it != hub->PortEp.end(); ++it) {
va009039 3:6ae9a03a6145 97 if (drive == NULL && UsbFlashDrive::check(*it)) {
va009039 3:6ae9a03a6145 98 drive = new UsbFlashDrive("usb", *it);
va009039 3:6ae9a03a6145 99 } else if (cam == NULL && LogitechC270::check(*it)) {
va009039 3:6ae9a03a6145 100 cam = new LogitechC270(C270_MJPEG, C270_160x120, _5FPS, *it);
va009039 3:6ae9a03a6145 101 } else if (cam == NULL && LifeCamVX700::check(*it)) {
va009039 3:6ae9a03a6145 102 cam = new LifeCamVX700(VX700_160x120, _5FPS, *it);
va009039 3:6ae9a03a6145 103 } else if (cam == NULL && UvcCam::check(*it)) {
va009039 3:6ae9a03a6145 104 cam = new UvcCam(UVC_MJPEG, UVC_160x120, _5FPS, *it);
va009039 3:6ae9a03a6145 105 }
va009039 3:6ae9a03a6145 106 }
va009039 3:6ae9a03a6145 107 if (cam == NULL) {
va009039 3:6ae9a03a6145 108 error("UVC Camera is not connected.\n");
va009039 3:6ae9a03a6145 109 }
va009039 3:6ae9a03a6145 110 if (drive == NULL) {
va009039 3:6ae9a03a6145 111 error("USB flash drive is not connected.\n");
va009039 3:6ae9a03a6145 112 }
va009039 3:6ae9a03a6145 113
va009039 3:6ae9a03a6145 114 captureJPEG* capture = new captureJPEG(cam);
va009039 3:6ae9a03a6145 115 capture->set_stack(DEFAULT_STACK_SIZE+128*4);
va009039 3:6ae9a03a6145 116 capture->start();
va009039 3:6ae9a03a6145 117
va009039 3:6ae9a03a6145 118 for(int n = 0; ; n++) {
va009039 3:6ae9a03a6145 119 printf("%d captureJPEG stack used: %d/%d bytes\n", n, capture->stack_used(), capture->stack_size());
va009039 3:6ae9a03a6145 120 Thread::wait(5*1000);
va009039 3:6ae9a03a6145 121 }
va009039 3:6ae9a03a6145 122 }