testing n-Bed with a Logitech C270 camera

Dependencies:   USBHost mbed

Fork of USBHostC270_example by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "USBHostMSD.h"
00002 #include "USBHostC270.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 BusOut leds(LED1, LED2, LED3);
00006 
00007 int main() {
00008     pc.baud(921600);
00009 
00010     USBHostMSD* msd = new USBHostMSD("usb"); // USB flash drive
00011     USBHostC270* cam = new USBHostC270(C270_MJPEG, C270_160x120, _1FPS); // Logitech C270
00012 
00013     uint8_t buf[1024*3];
00014     Timer interval_t;
00015     interval_t.reset();
00016     interval_t.start();
00017     int shot = 0;
00018     while(1) {
00019         if (interval_t.read() > 10 && cam->connected()) {
00020             int r = cam->readJPEG(buf, sizeof(buf));
00021             char path[32];
00022             snprintf(path, sizeof(path), "/usb/image%02d.jpg", shot % 20);
00023             printf("%d %s %d bytes\n", shot, path, r);
00024             if (msd->connected()) {
00025                 printf("msd connected\n");
00026                 FILE* fp = fopen(path, "wb");
00027                 if (fp) {
00028                     fwrite(buf, r, 1, fp);
00029                     fclose(fp);
00030                 }
00031                 shot++;
00032                 leds = shot % 8;
00033             }
00034             else {
00035                 printf("msd NOT connected\n");
00036                 }
00037             interval_t.reset();
00038         }
00039         if (!msd->connected()) {
00040             msd->connect();
00041             Thread::wait(500);
00042         }
00043         if (!cam->connected()) {
00044             cam->connect();
00045             Thread::wait(500);
00046         } else {
00047             cam->poll();
00048         }
00049     }
00050 }