![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
testing n-Bed with a Logitech C270 camera
Fork of USBHostC270_example by
main.cpp@9:fecabade834a, 2013-03-16 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 16 13:07:55 2013 +0000
- Revision:
- 9:fecabade834a
- Parent:
- 4:f8a5c8aa895a
- Child:
- 10:387c49b2fc7e
LogitechC270 class driver alpha version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 9:fecabade834a | 1 | // USBHostC270_HelloWorld/main.cpp |
samux | 0:0d68fe822228 | 2 | #include "mbed.h" |
samux | 0:0d68fe822228 | 3 | #include "USBHostMSD.h" |
va009039 | 9:fecabade834a | 4 | #include "USBHostC270.h" |
va009039 | 9:fecabade834a | 5 | #include "decodeMJPEG.h" |
va009039 | 9:fecabade834a | 6 | #include "MyThread.h" |
samux | 0:0d68fe822228 | 7 | |
va009039 | 9:fecabade834a | 8 | #define IMAGE_BUF_SIZE (1024*3) |
va009039 | 9:fecabade834a | 9 | |
va009039 | 9:fecabade834a | 10 | Serial pc(USBTX, USBRX); |
va009039 | 9:fecabade834a | 11 | DigitalOut led1(LED1),led2(LED2),led3(LED3); |
va009039 | 9:fecabade834a | 12 | |
va009039 | 9:fecabade834a | 13 | struct ImageBuffer { |
va009039 | 9:fecabade834a | 14 | int pos; |
va009039 | 9:fecabade834a | 15 | uint8_t buf[IMAGE_BUF_SIZE]; |
va009039 | 9:fecabade834a | 16 | void clear() { pos = 0; } |
va009039 | 9:fecabade834a | 17 | int size() { return pos; } |
va009039 | 9:fecabade834a | 18 | uint8_t get(int pos) { return buf[pos]; } |
va009039 | 9:fecabade834a | 19 | void put(uint8_t c) { |
va009039 | 9:fecabade834a | 20 | if (pos < sizeof(buf)) { |
va009039 | 9:fecabade834a | 21 | buf[pos++] = c; |
va009039 | 9:fecabade834a | 22 | } |
va009039 | 9:fecabade834a | 23 | } |
va009039 | 9:fecabade834a | 24 | }; |
samux | 0:0d68fe822228 | 25 | |
va009039 | 9:fecabade834a | 26 | Mail<ImageBuffer, 1> mail_box; |
va009039 | 9:fecabade834a | 27 | class captureJPEG : public MyThread, public decodeMJPEG { |
va009039 | 9:fecabade834a | 28 | public: |
va009039 | 9:fecabade834a | 29 | captureJPEG(BaseUvc* cam) : m_cam(cam) { |
va009039 | 9:fecabade834a | 30 | m_buf = NULL; |
va009039 | 9:fecabade834a | 31 | m_cam->setOnResult(this, &captureJPEG::callback_motion_jpeg); |
va009039 | 9:fecabade834a | 32 | } |
va009039 | 9:fecabade834a | 33 | private: |
va009039 | 9:fecabade834a | 34 | virtual void outputJPEG(uint8_t c, int status) { |
va009039 | 9:fecabade834a | 35 | if (m_buf == NULL && status == JPEG_START) { |
va009039 | 9:fecabade834a | 36 | m_buf = mail_box.alloc(); |
va009039 | 9:fecabade834a | 37 | if (m_buf) { |
va009039 | 9:fecabade834a | 38 | m_buf->clear(); |
va009039 | 9:fecabade834a | 39 | } |
va009039 | 9:fecabade834a | 40 | } |
va009039 | 9:fecabade834a | 41 | if (m_buf) { |
va009039 | 9:fecabade834a | 42 | m_buf->put(c); |
va009039 | 9:fecabade834a | 43 | if (status == JPEG_END) { |
va009039 | 9:fecabade834a | 44 | mail_box.put(m_buf); |
va009039 | 9:fecabade834a | 45 | m_buf = NULL; |
va009039 | 9:fecabade834a | 46 | led3 = !led3; |
va009039 | 9:fecabade834a | 47 | } |
va009039 | 9:fecabade834a | 48 | } |
va009039 | 9:fecabade834a | 49 | } |
va009039 | 9:fecabade834a | 50 | |
va009039 | 9:fecabade834a | 51 | void callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { |
va009039 | 9:fecabade834a | 52 | inputPacket(buf, len); |
va009039 | 9:fecabade834a | 53 | led1 = buf[1]&1; // FID |
va009039 | 9:fecabade834a | 54 | if (buf[1]&2) { // EOF |
va009039 | 9:fecabade834a | 55 | led2 = !led2; |
va009039 | 9:fecabade834a | 56 | } |
va009039 | 9:fecabade834a | 57 | } |
va009039 | 9:fecabade834a | 58 | |
va009039 | 9:fecabade834a | 59 | virtual void run() { |
va009039 | 9:fecabade834a | 60 | while(true) { |
va009039 | 9:fecabade834a | 61 | if (m_cam) { |
va009039 | 9:fecabade834a | 62 | m_cam->poll(); |
va009039 | 9:fecabade834a | 63 | } |
samux | 1:473f339c54c1 | 64 | } |
va009039 | 9:fecabade834a | 65 | } |
va009039 | 9:fecabade834a | 66 | ImageBuffer* m_buf; |
va009039 | 9:fecabade834a | 67 | BaseUvc* m_cam; |
va009039 | 9:fecabade834a | 68 | }; |
va009039 | 9:fecabade834a | 69 | |
va009039 | 9:fecabade834a | 70 | int main() { |
va009039 | 9:fecabade834a | 71 | pc.baud(921600); |
va009039 | 9:fecabade834a | 72 | printf("%s\n", __FILE__); |
va009039 | 9:fecabade834a | 73 | |
va009039 | 9:fecabade834a | 74 | USBHostMSD* msd = new USBHostMSD("usb"); |
va009039 | 9:fecabade834a | 75 | while(!msd->connect()) { |
va009039 | 9:fecabade834a | 76 | Thread::wait(200); |
va009039 | 9:fecabade834a | 77 | } |
va009039 | 9:fecabade834a | 78 | |
va009039 | 9:fecabade834a | 79 | |
va009039 | 9:fecabade834a | 80 | USBHostC270* cam = new USBHostC270(C270_MJPEG, C270_160x120, _5FPS); |
va009039 | 9:fecabade834a | 81 | while(!cam->connect()) { |
va009039 | 9:fecabade834a | 82 | Thread::wait(200); |
va009039 | 9:fecabade834a | 83 | } |
va009039 | 9:fecabade834a | 84 | |
va009039 | 9:fecabade834a | 85 | captureJPEG* capture = new captureJPEG(cam); |
va009039 | 9:fecabade834a | 86 | capture->set_stack(512); |
va009039 | 9:fecabade834a | 87 | capture->start(); |
va009039 | 9:fecabade834a | 88 | |
va009039 | 9:fecabade834a | 89 | Timer t; |
va009039 | 9:fecabade834a | 90 | t.reset(); |
va009039 | 9:fecabade834a | 91 | t.start(); |
va009039 | 9:fecabade834a | 92 | Timer interval_t; |
va009039 | 9:fecabade834a | 93 | interval_t.reset(); |
va009039 | 9:fecabade834a | 94 | interval_t.start(); |
va009039 | 9:fecabade834a | 95 | int shot = 0; |
va009039 | 9:fecabade834a | 96 | while(1) { |
va009039 | 9:fecabade834a | 97 | osEvent evt = mail_box.get(); |
va009039 | 9:fecabade834a | 98 | if (evt.status == osEventMail) { |
va009039 | 9:fecabade834a | 99 | ImageBuffer *buf = reinterpret_cast<ImageBuffer*>(evt.value.p); |
va009039 | 9:fecabade834a | 100 | if (interval_t.read() > 10) { |
va009039 | 9:fecabade834a | 101 | char path[32]; |
va009039 | 9:fecabade834a | 102 | snprintf(path, sizeof(path), "/usb/image%02d.jpg", shot % 100); |
va009039 | 9:fecabade834a | 103 | printf("%d %s %d bytes\n", shot, path, buf->size()); |
va009039 | 9:fecabade834a | 104 | if (msd->connected()) { |
va009039 | 9:fecabade834a | 105 | FILE* fp = fopen(path, "wb"); |
va009039 | 9:fecabade834a | 106 | if (fp) { |
va009039 | 9:fecabade834a | 107 | for(int i = 0; i < buf->size(); i++) { |
va009039 | 9:fecabade834a | 108 | fputc(buf->get(i), fp); |
va009039 | 9:fecabade834a | 109 | } |
va009039 | 9:fecabade834a | 110 | fclose(fp); |
va009039 | 9:fecabade834a | 111 | } |
va009039 | 9:fecabade834a | 112 | } |
va009039 | 9:fecabade834a | 113 | interval_t.reset(); |
va009039 | 9:fecabade834a | 114 | shot++; |
samux | 1:473f339c54c1 | 115 | } |
va009039 | 9:fecabade834a | 116 | mail_box.free(buf); |
samux | 0:0d68fe822228 | 117 | } |
va009039 | 9:fecabade834a | 118 | if (t.read() > 5) { |
va009039 | 9:fecabade834a | 119 | printf("captureJPEG stack used: %d/%d bytes\n", capture->stack_used(), capture->stack_size()); |
va009039 | 9:fecabade834a | 120 | printf("CC:"); |
va009039 | 9:fecabade834a | 121 | for(int i = 0; i < 16; i++) { |
va009039 | 9:fecabade834a | 122 | printf(" %u", cam->report_cc_count[i]); |
va009039 | 9:fecabade834a | 123 | } |
va009039 | 9:fecabade834a | 124 | printf("\nPS:"); |
va009039 | 9:fecabade834a | 125 | for(int i = 0; i < 16; i++) { |
va009039 | 9:fecabade834a | 126 | printf(" %u", cam->report_ps_cc_count[i]); |
va009039 | 9:fecabade834a | 127 | } |
va009039 | 9:fecabade834a | 128 | printf("\n"); |
va009039 | 9:fecabade834a | 129 | t.reset(); |
va009039 | 9:fecabade834a | 130 | } |
va009039 | 9:fecabade834a | 131 | if (!msd->connected()) { |
va009039 | 9:fecabade834a | 132 | msd->connect(); |
va009039 | 9:fecabade834a | 133 | } |
samux | 0:0d68fe822228 | 134 | } |
samux | 0:0d68fe822228 | 135 | } |