SPI read Pixi Cam V1
Embed:
(wiki syntax)
Show/hide line numbers
pixyUART.cpp
00001 00002 00003 #include "pixyUART.h" 00004 00005 pixyUART::pixyUART(BufferedSerial *com) 00006 { 00007 sync=0xAA55; //start word of a frame coming from the pixy 00008 //blocks=new Block[nBlocks]; //allocate space for the blocks 00009 //numBlocks=nBlocks; 00010 numBlocks=NUM_BLOCKS; 00011 uart = com; 00012 } 00013 00014 pixyUART::~pixyUART() 00015 { 00016 //delete blocks; 00017 } 00018 00019 00020 int8_t pixyUART::readTwoBytesLSB(uint16_t *out) 00021 { 00022 int32_t num = uart->read(buffer, 2); 00023 if(num>0) 00024 { 00025 *out=buffer[0]+256*buffer[1]; 00026 return 1; 00027 } 00028 else 00029 return -1; 00030 } 00031 00032 00033 void pixyUART::capture() 00034 { 00035 memset(blocks, 0, numBlocks*sizeof(Block)); //destroy the old targets 00036 captured_blocks = 0; 00037 Timer ti; 00038 ti.reset(); 00039 ti.start(); 00040 uint8_t got_data; 00041 for (int i=0; i<numBlocks; i++) { 00042 Block* out=&blocks[captured_blocks]; 00043 out->signature=INVALID_BLOCK; 00044 uint16_t checksum=0; 00045 //first we need to detect the start of a block. They all start with 0xAA55 00046 char frame[2]= {0,0}; //this is a 2 byte running frame of what is being recieved. It's like a first-in-last-out queue 00047 //debug->printf("looking for valid signature\n"); 00048 int32_t num = 1; 00049 while(memcmp((char*)&sync, frame, 2)!=0) 00050 { 00051 frame[0] = frame[1]; //move byte down 00052 num = uart->read(&frame[1],1); //get next byte. 00053 //printf("%d ",frame[1]); 00054 if(num<=0) 00055 { 00056 break; 00057 } 00058 } 00059 if(i==0) 00060 ti.reset(); 00061 if(ti.read()>.015) 00062 break; 00063 //printf(" / "); 00064 while(memcmp((char*)&sync, frame, 2)==0) 00065 { 00066 num = uart->read(frame,2); //get next byte. 00067 //printf("%d %d",frame[0],frame[1]); 00068 if(num<=0) 00069 { 00070 break; 00071 } 00072 } 00073 //printf(" . "); 00074 checksum = frame[0]+256*frame[1]; 00075 got_data = readTwoBytesLSB(&out->signature); 00076 if(got_data<0) 00077 break; 00078 got_data = readTwoBytesLSB(&out->x); 00079 if(got_data<0) 00080 break; 00081 got_data = readTwoBytesLSB(&out->y); 00082 if(got_data<0) 00083 break; 00084 got_data = readTwoBytesLSB(&out->width); 00085 if(got_data<0) 00086 break; 00087 got_data = readTwoBytesLSB(&out->height); 00088 if(got_data<0) 00089 break; 00090 //printf("cs: %d i: %d x: %d y: %d w: %d h: %d\r\n",checksum,out->signature,out->x,out->y,out->width,out->height); 00091 if (checksum!=(out->x+out->y+out->signature+out->width+out->height) || checksum==0) { 00092 //debug->printf("checksum doesn't add up %d\n", checksum); 00093 out->signature=INVALID_BLOCK; //used for invalid signatures 00094 } 00095 else 00096 { 00097 captured_blocks++; 00098 } 00099 00100 } 00101 return; 00102 } 00103 00104 Block* pixyUART::getBlocks() 00105 { 00106 return blocks; 00107 } 00108 00109
Generated on Fri Sep 16 2022 13:59:14 by
1.7.2