an SPI interface the cmu pixy camera
Dependents: robotic_fish_ver_4_9_pixy UMO
pixy.cpp
- Committer:
- sandwich
- Date:
- 2014-06-02
- Revision:
- 0:1b99a0bee9e0
- Child:
- 2:fc86438b206f
File content as of revision 0:1b99a0bee9e0:
#include "pixy.h" pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, Serial* ser) { spi=new SPI(mosi,miso,sclk); //the default spi settings will work fine here spi->format(8,0); spi->frequency(100000); sync=0xAA55; debug=ser; } pixySPI::~pixySPI() { delete spi; } short pixySPI::readTwoBytesLSB() { short out; char read[2]; 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); } } Block pixySPI::getBlock(Serial* debug) { Block out; 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)) { //debug->printf("checksum doesn't add up %d\n", checksum); out.signature=20; //used for invalid signatures } return out; } char pixySPI::getRawData() { return spi->write(0x00); }