Simple USBHost WebCam for EA LPC4088 QSB/LPC1768 test program

Dependencies:   LPC4088-USBHost mbed

EA LPC4088 QSB/LPC1768をUSBホストにしてWebカメラからJPEG画像を読み取るテストプログラムです。

The usage is the same as KL46Z-USBHostC270_example.
使い方はKL46Z-USBHostC270_exampleと同じです。
動作確認: Logitech C270,Logitech Q200R(Qcam Orbit AF)
/media/uploads/va009039/lpc4088-c270-480x360.jpg

Committer:
va009039
Date:
Thu Apr 24 05:38:45 2014 +0000
Revision:
0:c972ee42b455
first commit,sync rev.29.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:c972ee42b455 1 #include "USBHostCam.h"
va009039 0:c972ee42b455 2 RawSerial pc(USBTX, USBRX);
va009039 0:c972ee42b455 3
va009039 0:c972ee42b455 4 #if defined(TARGET_LPC4088)
va009039 0:c972ee42b455 5 #define IMAGE_BUF_SIZE (1024*48)
va009039 0:c972ee42b455 6 #elif defined(TARGET_LPC1768)
va009039 0:c972ee42b455 7 #define IMAGE_BUF_SIZE (1024*24)
va009039 0:c972ee42b455 8 #else
va009039 0:c972ee42b455 9 #error "target error"
va009039 0:c972ee42b455 10 #endif
va009039 0:c972ee42b455 11
va009039 0:c972ee42b455 12 uint8_t image_buf[IMAGE_BUF_SIZE];
va009039 0:c972ee42b455 13
va009039 0:c972ee42b455 14 DigitalOut led1(LED1);
va009039 0:c972ee42b455 15 DigitalOut led2(LED2);
va009039 0:c972ee42b455 16 #define LED_OFF 0
va009039 0:c972ee42b455 17 #define LED_ON 1
va009039 0:c972ee42b455 18
va009039 0:c972ee42b455 19 int main() {
va009039 0:c972ee42b455 20 pc.baud(9600);
va009039 0:c972ee42b455 21 led1 = led2 = LED_OFF;
va009039 0:c972ee42b455 22
va009039 0:c972ee42b455 23 // Logitech C270
va009039 0:c972ee42b455 24 USBHostCam* cam = new USBHostCam(_800x600);
va009039 0:c972ee42b455 25 if (!cam->connect()) {
va009039 0:c972ee42b455 26 error("WebCam not found.\n");
va009039 0:c972ee42b455 27 }
va009039 0:c972ee42b455 28
va009039 0:c972ee42b455 29 int pos = 0;
va009039 0:c972ee42b455 30 int size = 0;
va009039 0:c972ee42b455 31 Timer t;
va009039 0:c972ee42b455 32 for(int seq = 0;;) {
va009039 0:c972ee42b455 33 int c = -1;
va009039 0:c972ee42b455 34 if (pc.readable()) {
va009039 0:c972ee42b455 35 c = pc.getc();
va009039 0:c972ee42b455 36 }
va009039 0:c972ee42b455 37 switch(seq) {
va009039 0:c972ee42b455 38 case 0:
va009039 0:c972ee42b455 39 pc.printf("\nReady, please download JPEG using XMODEM from LPC4088.\n");
va009039 0:c972ee42b455 40 t.start();
va009039 0:c972ee42b455 41 seq++;
va009039 0:c972ee42b455 42 break;
va009039 0:c972ee42b455 43 case 1:
va009039 0:c972ee42b455 44 if (c == 0x15) { // NAK
va009039 0:c972ee42b455 45 size = cam->readJPEG(image_buf, sizeof(image_buf));
va009039 0:c972ee42b455 46 pos = 0;
va009039 0:c972ee42b455 47 seq++;
va009039 0:c972ee42b455 48 }
va009039 0:c972ee42b455 49 break;
va009039 0:c972ee42b455 50 case 2:
va009039 0:c972ee42b455 51 if (pos >= size) {
va009039 0:c972ee42b455 52 pc.putc(0x04); // EOT
va009039 0:c972ee42b455 53 } else {
va009039 0:c972ee42b455 54 pc.putc(0x01); // SOH
va009039 0:c972ee42b455 55 uint8_t block = pos/128 + 1;
va009039 0:c972ee42b455 56 pc.putc(block);
va009039 0:c972ee42b455 57 pc.putc(block ^ 0xff);
va009039 0:c972ee42b455 58 uint8_t checksum = 0x00;
va009039 0:c972ee42b455 59 for(int i = 0; i < 128; i++) {
va009039 0:c972ee42b455 60 uint8_t c = image_buf[pos + i];
va009039 0:c972ee42b455 61 checksum += c;
va009039 0:c972ee42b455 62 pc.putc(c);
va009039 0:c972ee42b455 63 }
va009039 0:c972ee42b455 64 pc.putc(checksum);
va009039 0:c972ee42b455 65 }
va009039 0:c972ee42b455 66 seq++;
va009039 0:c972ee42b455 67 t.reset();
va009039 0:c972ee42b455 68 led2 = !led2;
va009039 0:c972ee42b455 69 break;
va009039 0:c972ee42b455 70 case 3:
va009039 0:c972ee42b455 71 if (c == 0x06) { // ACK
va009039 0:c972ee42b455 72 if (pos >= size) {
va009039 0:c972ee42b455 73 seq = 0;
va009039 0:c972ee42b455 74 } else {
va009039 0:c972ee42b455 75 pos += 128;
va009039 0:c972ee42b455 76 seq--;
va009039 0:c972ee42b455 77 }
va009039 0:c972ee42b455 78 }
va009039 0:c972ee42b455 79 break;
va009039 0:c972ee42b455 80 }
va009039 0:c972ee42b455 81 if (t.read_ms() > 15*1000) { // timeout
va009039 0:c972ee42b455 82 t.reset();
va009039 0:c972ee42b455 83 seq = 0;
va009039 0:c972ee42b455 84 }
va009039 0:c972ee42b455 85 cam->poll();
va009039 0:c972ee42b455 86 }
va009039 0:c972ee42b455 87 }