![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
testing n-Bed with a Logitech C270 camera
Fork of USBHostC270_example by
Diff: main.cpp
- Revision:
- 9:fecabade834a
- Parent:
- 4:f8a5c8aa895a
- Child:
- 10:387c49b2fc7e
diff -r 758190c6c455 -r fecabade834a main.cpp --- a/main.cpp Thu Mar 14 14:23:42 2013 +0000 +++ b/main.cpp Sat Mar 16 13:07:55 2013 +0000 @@ -1,50 +1,135 @@ +// USBHostC270_HelloWorld/main.cpp #include "mbed.h" #include "USBHostMSD.h" +#include "USBHostC270.h" +#include "decodeMJPEG.h" +#include "MyThread.h" -DigitalOut led(LED1); +#define IMAGE_BUF_SIZE (1024*3) + +Serial pc(USBTX, USBRX); +DigitalOut led1(LED1),led2(LED2),led3(LED3); + +struct ImageBuffer { + int pos; + uint8_t buf[IMAGE_BUF_SIZE]; + void clear() { pos = 0; } + int size() { return pos; } + uint8_t get(int pos) { return buf[pos]; } + void put(uint8_t c) { + if (pos < sizeof(buf)) { + buf[pos++] = c; + } + } +}; -void msd_task(void const *) { - - USBHostMSD msd("usb"); - int i = 0; - - while(1) { - - // try to connect a MSD device - while(!msd.connect()) { - Thread::wait(500); +Mail<ImageBuffer, 1> mail_box; +class captureJPEG : public MyThread, public decodeMJPEG { +public: + captureJPEG(BaseUvc* cam) : m_cam(cam) { + m_buf = NULL; + m_cam->setOnResult(this, &captureJPEG::callback_motion_jpeg); + } +private: + virtual void outputJPEG(uint8_t c, int status) { + if (m_buf == NULL && status == JPEG_START) { + m_buf = mail_box.alloc(); + if (m_buf) { + m_buf->clear(); + } + } + if (m_buf) { + m_buf->put(c); + if (status == JPEG_END) { + mail_box.put(m_buf); + m_buf = NULL; + led3 = !led3; + } + } + } + + void callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { + inputPacket(buf, len); + led1 = buf[1]&1; // FID + if (buf[1]&2) { // EOF + led2 = !led2; + } + } + + virtual void run() { + while(true) { + if (m_cam) { + m_cam->poll(); + } } - - // in a loop, append a file - // if the device is disconnected, we try to connect it again - while(1) { - - // append a file - FILE * fp = fopen("/usb/test1.txt", "a"); - - if (fp != NULL) { - fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); - printf("Goodbye World!\r\n"); - fclose(fp); - } else { - printf("FILE == NULL\r\n"); + } + ImageBuffer* m_buf; + BaseUvc* m_cam; +}; + +int main() { + pc.baud(921600); + printf("%s\n", __FILE__); + + USBHostMSD* msd = new USBHostMSD("usb"); + while(!msd->connect()) { + Thread::wait(200); + } + + + USBHostC270* cam = new USBHostC270(C270_MJPEG, C270_160x120, _5FPS); + while(!cam->connect()) { + Thread::wait(200); + } + + captureJPEG* capture = new captureJPEG(cam); + capture->set_stack(512); + capture->start(); + + Timer t; + t.reset(); + t.start(); + Timer interval_t; + interval_t.reset(); + interval_t.start(); + int shot = 0; + while(1) { + osEvent evt = mail_box.get(); + if (evt.status == osEventMail) { + ImageBuffer *buf = reinterpret_cast<ImageBuffer*>(evt.value.p); + if (interval_t.read() > 10) { + char path[32]; + snprintf(path, sizeof(path), "/usb/image%02d.jpg", shot % 100); + printf("%d %s %d bytes\n", shot, path, buf->size()); + if (msd->connected()) { + FILE* fp = fopen(path, "wb"); + if (fp) { + for(int i = 0; i < buf->size(); i++) { + fputc(buf->get(i), fp); + } + fclose(fp); + } + } + interval_t.reset(); + shot++; } - - Thread::wait(500); - - // if device disconnected, try to connect again - if (!msd.connected()) - break; + mail_box.free(buf); } - + if (t.read() > 5) { + printf("captureJPEG stack used: %d/%d bytes\n", capture->stack_used(), capture->stack_size()); + printf("CC:"); + for(int i = 0; i < 16; i++) { + printf(" %u", cam->report_cc_count[i]); + } + printf("\nPS:"); + for(int i = 0; i < 16; i++) { + printf(" %u", cam->report_ps_cc_count[i]); + } + printf("\n"); + t.reset(); + } + if (!msd->connected()) { + msd->connect(); + } } } - - -int main() { - Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); - while(1) { - led=!led; - Thread::wait(500); - } -} \ No newline at end of file