an SPI interface the cmu pixy camera
Dependents: robotic_fish_ver_4_9_pixy UMO
pixy.cpp
- Committer:
- sandwich
- Date:
- 2014-06-03
- Revision:
- 2:fc86438b206f
- Parent:
- 0:1b99a0bee9e0
- Child:
- 5:f54759b26096
File content as of revision 2:fc86438b206f:
#include "pixy.h" pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, int nBlocks, Serial* ser) { spi=new SPI(mosi,miso,sclk); //the default spi settings will work fine here spi->format(8,0); //8bits mode 0 spi->frequency(100000); //set the frequency in Hz sync=0xAA55; //start word of a frame coming from the pixy debug=ser; //debug connection blocks=new Block[nBlocks]; //allocate space for the blocks numBlocks=nBlocks; } pixySPI::~pixySPI() { delete spi; delete blocks; } short pixySPI::readTwoBytesLSB() { short out; char read[2]= {0,0}; read[0]=spi->write(0x00); read[1]=spi->write(0x00); out=(((short)read[0]) << 8) | read[1]; return out; } //this doesn't work void pixySPI::readNBytes(char* buf, int num) { for (int i=0; i<num; i++) { char byte=spi->write(0x00); debug->printf("%X\n", byte); memcpy(buf+i, &byte, 1); } } void pixySPI::capture() { memset(blocks, 0, numBlocks*sizeof(Block)); for (int i=0; i<numBlocks; ++i) { Block* out=&blocks[i]; out->signature=INVALID_BLOCK; uint16_t checksum=0; //first we need to detect the start of a block. They all start with 0xAA55 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 //debug->printf("looking for valid signature\n"); while (memcmp((char*)&sync, frame, 2)!=0) { frame[0]=frame[1]; //move byte down frame[1]=spi->write(0x00); //get next byte. } spi->write(0x00); //ok so we got a valid signature //these didn't end up working //readNBytes((char*)(&checksum), 2); //get the checksum //readNBytes((char*)(&out), sizeof(Block)); //get the rest of the data checksum=readTwoBytesLSB(); out->signature=readTwoBytesLSB(); out->x=readTwoBytesLSB(); out->y=readTwoBytesLSB(); out->width=readTwoBytesLSB(); out->height=readTwoBytesLSB(); if (checksum!=(out->x+out->y+out->signature+out->width+out->height) || checksum==0) { //debug->printf("checksum doesn't add up %d\n", checksum); out->signature=INVALID_BLOCK; //used for invalid signatures } //debug->printf("found block\n"); } return; } Block* pixySPI::getBlocks() { return blocks; } int pixySPI::getNumBlocks() { return numBlocks; } char pixySPI::getRawData() { return spi->write(0x00); }