an SPI interface the cmu pixy camera

Dependents:   robotic_fish_ver_4_9_pixy UMO

Revision:
5:f54759b26096
Parent:
2:fc86438b206f
Child:
6:4bf8d39fc5ce
--- a/pixy.cpp	Tue Jun 03 15:29:26 2014 +0000
+++ b/pixy.cpp	Thu Jun 05 00:12:48 2014 +0000
@@ -1,19 +1,18 @@
 #include "pixy.h"
 
-pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, int nBlocks, Serial* ser)
+pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, int nBlocks)
+    :spi(mosi,miso,sclk)
 {
-    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
+    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;
+    //blocks=new Block[nBlocks]; //allocate space for the blocks
+    //numBlocks=nBlocks;
+    numBlocks=NUM_BLOCKS;
 }
 
 pixySPI::~pixySPI()
 {
-    delete spi;
     delete blocks;
 }
 
@@ -22,8 +21,8 @@
 {
     short out;
     char read[2]= {0,0};
-    read[0]=spi->write(0x00);
-    read[1]=spi->write(0x00);
+    read[0]=spi.write(0x00);
+    read[1]=spi.write(0x00);
     out=(((short)read[0]) << 8) | read[1];
     return out;
 }
@@ -33,16 +32,16 @@
 void pixySPI::readNBytes(char* buf, int num)
 {
     for (int i=0; i<num; i++) {
-        char byte=spi->write(0x00);
-        debug->printf("%X\n", byte);
+        char byte=spi.write(0x00);
         memcpy(buf+i, &byte, 1);
     }
 }
 
 void pixySPI::capture()
 {
-    memset(blocks, 0, numBlocks*sizeof(Block));
+    memset(blocks, 0, numBlocks*sizeof(Block)); //destroy the old targets
     for (int i=0; i<numBlocks; ++i) {
+        // printf("block %d\n", i);
         Block* out=&blocks[i];
         out->signature=INVALID_BLOCK;
         uint16_t checksum=0;
@@ -51,9 +50,9 @@
         //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.
+            frame[1]=spi.write(0x00); //get next byte.
         }
-        spi->write(0x00);
+        spi.write(0x00);
         //ok so we got a valid signature
         //these didn't end up working
         //readNBytes((char*)(&checksum), 2); //get the checksum
@@ -69,7 +68,9 @@
             //debug->printf("checksum doesn't add up %d\n", checksum);
             out->signature=INVALID_BLOCK; //used for invalid signatures
         }
-        //debug->printf("found block\n");
+        //printf("found block\n");
+        bestX=blocks[0].x;
+        bestY=blocks[0].y;
     }
     return;
 }
@@ -84,7 +85,27 @@
     return numBlocks;
 }
 
+int pixySPI::getBestX()
+{
+    if (bestX>640)
+        return 640;
+    else if (bestX<0)
+        return 0;
+    else
+        return bestX;
+}
+
+int pixySPI::getBestY()
+{
+    if (bestY>400)
+        return 400;
+    else if (bestY<0)
+        return 0;
+    else
+        return bestY;
+}
+
 char pixySPI::getRawData()
 {
-    return spi->write(0x00);
+    return spi.write(0x00);
 }
\ No newline at end of file