Masato YAMANISHI / Mbed 2 deprecated USBHostC210_example

Dependencies:   TextLCD 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 #define MSDFILESYSTEM 0 // 0:SDFileSystem, 1:USBHostMSD
00002 
00003 #if MSDFILESYSTEM
00004 #include "USBHostMSD.h"
00005 #else
00006 #include "SDFileSystem.h"
00007 #endif
00008 #include "USBHostC270.h"
00009 
00010 Serial pc(USBTX, USBRX);
00011 BusOut leds(LED1, LED2, LED3);
00012 #if !MSDFILESYSTEM
00013 SDFileSystem  sd(p5, p6, p7, p8,  "sd");
00014 #endif
00015 
00016 #include "TextLCD.h"
00017 TextLCD     lcd( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
00018 
00019 int main() {
00020     pc.baud(921600);
00021 #if MSDFILESYSTEM
00022     USBHostMSD* msd = new USBHostMSD("usb"); // USB flash drive
00023 #endif
00024     USBHostC270* cam = new USBHostC270(C270_MJPEG, C270_160x120, _5FPS); // Logitech C270
00025     while(!cam->connect()) {
00026         Thread::wait(500);
00027     }
00028 
00029     uint8_t buf[1024*3];
00030     Timer interval_t;
00031     interval_t.reset();
00032     interval_t.start();
00033     int shot = 0;
00034     while(1) {
00035         if (interval_t.read_ms() > 2000) {
00036             printf("start\r\n");
00037             int r = cam->readJPEG(buf, sizeof(buf));
00038             char path[32];
00039 #if MSDFILESYSTEM
00040             snprintf(path, sizeof(path), "/usb/image%02d.jpg", shot % 20);
00041 #else
00042             snprintf(path, sizeof(path), "/sd/image%02d.jpg", shot % 20);
00043 #endif
00044             lcd.locate( 0, 0 );
00045             lcd.printf( path );
00046             printf("%d %s %d bytes\r\n", shot, path, r);
00047 #if MSDFILESYSTEM
00048             if (msd->connected()) 
00049 #endif
00050             {
00051                 FILE* fp = fopen(path, "wb");
00052                 if (fp) {
00053                     lcd.locate( 0, 1 );
00054                     lcd.printf( "writing." );
00055                     printf("write\r\n");
00056                     fwrite(buf, r, 1, fp);
00057                     fclose(fp);
00058                     lcd.locate( 0, 1 );
00059                     lcd.printf( "finish. " );
00060                 }
00061                 shot++;
00062                 leds = shot % 8;
00063             }
00064             interval_t.reset();
00065             printf("end\r\n");
00066         }
00067 #if MSDFILESYSTEM
00068         if (!msd->connected()) msd->connect();
00069 #endif
00070         cam->poll();    
00071     }
00072 }