Samuel Pullman / Mbed 2 deprecated CameraTest

Dependencies:   FastPWM MODSERIAL mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Constants.h"
00003 #include "OV7670.h"
00004 #include "FastPWM.h"
00005 #include "spi_ram.h"
00006 #include "MODSERIAL.h"
00007 
00008 #define SIZEX (160)
00009 #define SIZEY (120)
00010 #define SIZE 19200
00011 
00012 OV7670 camera(
00013     p28, p27,           // SDA,SCL(I2C / SCCB)
00014     p21, p22, p23, p24, // XCLK, PCLK, VSYNC, HREF
00015     Port0, 0x07878000); // D0-D7 -> P11-p18
00016     
00017 Serial host(USBTX, USBRX);
00018 MODSERIAL bt(p9, p10, 16384, 512);
00019 DigitalOut status(p20);
00020 int feedMode = 0;
00021 
00022 int hexToInt(int i) {
00023     if(i >= '0' && i <= '9') return i - '0';
00024     else if(i >= 'A' && i <= 'F') return i - 55;
00025     else return 0;
00026 }
00027 
00028 int getHexWord(Serial *ser) {
00029     int b1 = ser->getc();
00030     int b2 = ser->getc();
00031     return (hexToInt(b1) << 4) | hexToInt(b2);
00032 }
00033 
00034 void readLine(Serial *ser, char *data) {
00035     int i=0;
00036     char c = '0';
00037     while(c != '\n') {
00038         if(ser->readable()) {
00039             c = ser->getc();
00040             data[i++] = c;
00041         }
00042     }
00043     data[i] = '\0';
00044 }
00045 
00046 int main() {
00047     host.baud(115200);
00048     bt.baud(230400);
00049     
00050     // Initialize a 12Mhz XCLK for the camera
00051     camera.Init();
00052     
00053     // Reset camera on power up
00054     camera.Reset();
00055     
00056     // Set up for 160*120 pixels RGB565
00057     camera.InitQQVGA();
00058 
00059     char c;
00060     char data[20];
00061     while(1) {
00062         if(bt.readable()) {
00063             c = bt.getc();
00064             switch(c) {
00065                 case REQUEST_PROFILE:
00066                     host.printf("Profile requested\n\r");
00067                     bt.putc(CLIENT_PROFILE);
00068                     bt.putc((TAPCAM_ID >> 4) & 0x0F);
00069                     bt.putc(TAPCAM_ID & 0x0F);
00070                     bt.printf("device\n");
00071                     break;
00072                 case CLIENT_PROFILE: {
00073                     host.printf("Client profile:\n\r");
00074                     int id = bt.getc() << 4;
00075                     id |= bt.getc();
00076                     readLine(&bt, data);
00077                     host.printf("ID: %d\n\r", id);
00078                     host.printf("Type: %s\n\r", data);
00079                     break;
00080                 }
00081                 case FEED_REQUEST:
00082                     host.printf("Feed requested\n\r");
00083                     bt.putc(FEED_START);
00084                     break;
00085                 case FEED_START_ACK:
00086                     host.printf("Feed start acknowledged\n\r");
00087                     feedMode = 1;
00088                     break;
00089                 case FEED_CANCEL:
00090                     host.printf("Feed canceled\n\r");
00091                     feedMode = 0;
00092                     break;
00093             }
00094         }
00095         if(feedMode) {
00096             int bytes = camera.captureImage();
00097             bt.putc(FRAME_READ);
00098             bt.printf(ORIENTATION);
00099             bt.printf("%d\n", bytes);
00100             camera.sendImage(&bt, bytes);
00101             host.printf("Sent %d\n\r", bytes);
00102             wait_ms(100);
00103         }
00104         if(host.readable()) {
00105             c = host.getc();
00106             if(c == 'r') {
00107                 int reg = getHexWord(&host);
00108                 host.printf("%d\n\r", camera.ReadReg(reg));
00109             } else if(c == 'w') {
00110                 int reg = getHexWord(&host);
00111                 int val = getHexWord(&host);
00112                 camera.WriteReg(reg, val);
00113                 host.printf("%d\n\r", camera.ReadReg(reg));
00114             } else if(c == 't') {
00115                 camera.scopeTest(&host);
00116             } else if(c == 'c') {
00117                 status = 1;
00118                 int bytes = camera.captureImage();
00119                 host.printf("%d\n", bytes);
00120                 camera.sendImage(&host, bytes);
00121                 status = 0;
00122             } else if(c == 's') {
00123                 feedMode = false;
00124             } else if(c == 'p') {
00125                 bt.putc(REQUEST_PROFILE);
00126             }
00127         }
00128     }
00129 }
00130