an SPI interface the cmu pixy camera

Dependents:   robotic_fish_ver_4_9_pixy UMO

Revision:
2:fc86438b206f
Parent:
0:1b99a0bee9e0
Child:
5:f54759b26096
--- a/pixy.cpp	Mon Jun 02 21:30:22 2014 +0000
+++ b/pixy.cpp	Tue Jun 03 15:09:31 2014 +0000
@@ -1,24 +1,27 @@
 #include "pixy.h"
 
-pixySPI::pixySPI(PinName mosi, PinName miso, PinName sclk, Serial* ser)
+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);
-    spi->frequency(100000);
-    sync=0xAA55;
-    debug=ser;
+    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];
+    char read[2]= {0,0};
     read[0]=spi->write(0x00);
     read[1]=spi->write(0x00);
     out=(((short)read[0]) << 8) | read[1];
@@ -36,34 +39,49 @@
     }
 }
 
-Block pixySPI::getBlock(Serial* debug)
+void pixySPI::capture()
 {
-    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.
+    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");
     }
-    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;
+    return;
+}
+
+Block* pixySPI::getBlocks()
+{
+    return blocks;
+}
+
+int pixySPI::getNumBlocks()
+{
+    return numBlocks;
 }
 
 char pixySPI::getRawData()