an SPI interface the cmu pixy camera

Dependents:   robotic_fish_ver_4_9_pixy UMO

Committer:
sandwich
Date:
Mon Jun 02 21:02:09 2014 +0000
Revision:
0:1b99a0bee9e0
Child:
2:fc86438b206f
tracks a single object currently

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sandwich 0:1b99a0bee9e0 1 #include "pixy.h"
sandwich 0:1b99a0bee9e0 2
sandwich 0:1b99a0bee9e0 3 pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, Serial* ser)
sandwich 0:1b99a0bee9e0 4 {
sandwich 0:1b99a0bee9e0 5 spi=new SPI(mosi,miso,sclk); //the default spi settings will work fine here
sandwich 0:1b99a0bee9e0 6 spi->format(8,0);
sandwich 0:1b99a0bee9e0 7 spi->frequency(100000);
sandwich 0:1b99a0bee9e0 8 sync=0xAA55;
sandwich 0:1b99a0bee9e0 9 debug=ser;
sandwich 0:1b99a0bee9e0 10 }
sandwich 0:1b99a0bee9e0 11
sandwich 0:1b99a0bee9e0 12 pixySPI::~pixySPI()
sandwich 0:1b99a0bee9e0 13 {
sandwich 0:1b99a0bee9e0 14 delete spi;
sandwich 0:1b99a0bee9e0 15 }
sandwich 0:1b99a0bee9e0 16
sandwich 0:1b99a0bee9e0 17
sandwich 0:1b99a0bee9e0 18 short pixySPI::readTwoBytesLSB()
sandwich 0:1b99a0bee9e0 19 {
sandwich 0:1b99a0bee9e0 20 short out;
sandwich 0:1b99a0bee9e0 21 char read[2];
sandwich 0:1b99a0bee9e0 22 read[0]=spi->write(0x00);
sandwich 0:1b99a0bee9e0 23 read[1]=spi->write(0x00);
sandwich 0:1b99a0bee9e0 24 out=(((short)read[0]) << 8) | read[1];
sandwich 0:1b99a0bee9e0 25 return out;
sandwich 0:1b99a0bee9e0 26 }
sandwich 0:1b99a0bee9e0 27
sandwich 0:1b99a0bee9e0 28
sandwich 0:1b99a0bee9e0 29 //this doesn't work
sandwich 0:1b99a0bee9e0 30 void pixySPI::readNBytes(char* buf, int num)
sandwich 0:1b99a0bee9e0 31 {
sandwich 0:1b99a0bee9e0 32 for (int i=0; i<num; i++) {
sandwich 0:1b99a0bee9e0 33 char byte=spi->write(0x00);
sandwich 0:1b99a0bee9e0 34 debug->printf("%X\n", byte);
sandwich 0:1b99a0bee9e0 35 memcpy(buf+i, &byte, 1);
sandwich 0:1b99a0bee9e0 36 }
sandwich 0:1b99a0bee9e0 37 }
sandwich 0:1b99a0bee9e0 38
sandwich 0:1b99a0bee9e0 39 Block pixySPI::getBlock(Serial* debug)
sandwich 0:1b99a0bee9e0 40 {
sandwich 0:1b99a0bee9e0 41 Block out;
sandwich 0:1b99a0bee9e0 42 uint16_t checksum=0;
sandwich 0:1b99a0bee9e0 43 //first we need to detect the start of a block. They all start with 0xAA55
sandwich 0:1b99a0bee9e0 44 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
sandwich 0:1b99a0bee9e0 45 //debug->printf("looking for valid signature\n");
sandwich 0:1b99a0bee9e0 46 while (memcmp((char*)&sync, frame, 2)!=0) {
sandwich 0:1b99a0bee9e0 47 frame[0]=frame[1]; //move byte down
sandwich 0:1b99a0bee9e0 48 frame[1]=spi->write(0x00); //get next byte.
sandwich 0:1b99a0bee9e0 49 }
sandwich 0:1b99a0bee9e0 50 spi->write(0x00);
sandwich 0:1b99a0bee9e0 51 //ok so we got a valid signature
sandwich 0:1b99a0bee9e0 52 //these didn't end up working
sandwich 0:1b99a0bee9e0 53 //readNBytes((char*)(&checksum), 2); //get the checksum
sandwich 0:1b99a0bee9e0 54 //readNBytes((char*)(&out), sizeof(Block)); //get the rest of the data
sandwich 0:1b99a0bee9e0 55 checksum=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 56 out.signature=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 57 out.x=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 58 out.y=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 59 out.width=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 60 out.height=readTwoBytesLSB();
sandwich 0:1b99a0bee9e0 61
sandwich 0:1b99a0bee9e0 62 if (checksum!=(out.x+out.y+out.signature+out.width+out.height)) {
sandwich 0:1b99a0bee9e0 63 //debug->printf("checksum doesn't add up %d\n", checksum);
sandwich 0:1b99a0bee9e0 64 out.signature=20; //used for invalid signatures
sandwich 0:1b99a0bee9e0 65 }
sandwich 0:1b99a0bee9e0 66 return out;
sandwich 0:1b99a0bee9e0 67 }
sandwich 0:1b99a0bee9e0 68
sandwich 0:1b99a0bee9e0 69 char pixySPI::getRawData()
sandwich 0:1b99a0bee9e0 70 {
sandwich 0:1b99a0bee9e0 71 return spi->write(0x00);
sandwich 0:1b99a0bee9e0 72 }