Mathieu Malone / Mbed 2 deprecated PixyLibrary

Dependencies:   mbed

Dependents:   PixyStereoCam

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pixy1.h Source File

Pixy1.h

00001 //--------------------------------------------------------------------------------------------
00002 //Original Property of: charmedlabs.com/pixystart -> arduino_pixy-x.y.z.zip 
00003 //
00004 //Modifications made by: Mathieu Malone
00005 //Modifications: Modified Arduino code to function with mbed development platform
00006 //Output Method: This program uses "Serial pc(USBTX, USBRX)" in order to allow communication
00007 //               between the mbed platform and putty terminal through USB
00008 //
00009 //Latest update by: Mathieu Malone
00010 //Date of last update: July 24th, 2014
00011 //--------------------------------------------------------------------------------------------
00012 //
00013 // begin license header
00014 //
00015 // This file is part of Pixy CMUcam5 or "Pixy" for short
00016 //
00017 // All Pixy source code is provided under the terms of the
00018 // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
00019 // Those wishing to use Pixy source code, software and/or
00020 // technologies under different licensing terms should contact us at
00021 // cmucam@cs.cmu.edu. Such licensing terms are available for
00022 // all portions of the Pixy codebase presented here.
00023 //
00024 // end license header
00025 //
00026 
00027 /*
00028   Pixy.h - Library for interfacing with Pixy.
00029   Created by Scott Robinson, October 22, 2013.
00030   Released into the public domain.
00031 
00032   06.04.2014 v0.1.3 John Leimon 
00033     + LinkSPI.init() should be called from the setup() 
00034       function instead of being called automatically from
00035       the TPixy<LinkSPI> constructor in global scope. This
00036       is a workaround for a bug (?) in the Arduino DUE in which
00037       calling SPI.begin() from global scope (via a constructor)
00038       inhibits the operation of the Serial peripheral in the
00039       DUE. [As of: Arduino 1.5.6-r2]
00040 */
00041 
00042 #ifndef PIXY_H1
00043 #define PIXY_H1
00044 
00045 #include "TPixy1.h"
00046 #include "SPI1.h"
00047 
00048 
00049 #define PIXY_SYNC_BYTE1              0x5a
00050 #define PIXY_SYNC_BYTE_DATA1         0x5b
00051 #define PIXY_OUTBUF_SIZE1            6
00052 
00053 SPI spi1(p5, p6, p7); // mosi, miso, sclk
00054 
00055 class LinkSPI1
00056 {
00057   public:
00058     void init1()
00059     {
00060       outLen1 = 0;
00061       #ifdef __SAM3X8E__
00062       // DUE clock divider //
00063       SPI1.setClockDivider1(84);
00064       #else
00065       // Default clock divider //
00066       //SPI.setClockDivider(SPI_CLOCK_DIV16);
00067       #endif
00068     }
00069     
00070     uint16_t getWord1()
00071     {
00072       // ordering is different because Pixy is sending 16 bits through SPI 
00073       // instead of 2 bytes in a 16-bit word as with I2C
00074       uint16_t w1;
00075       uint8_t c1, count1 = 0;
00076 
00077       if (outLen1)
00078       {
00079         w1 = spi1.write(PIXY_SYNC_BYTE_DATA1);
00080         count1 = outBuf1[outIndex1++];
00081         if (outIndex1==outLen1)
00082           outLen1 = 0; 
00083       }
00084       else
00085         w1 = spi1.write(PIXY_SYNC_BYTE1);
00086       w1 <<= 8;
00087       c1 = spi1.write(count1);
00088       w1 |= c1;
00089 
00090       return w1;
00091     }
00092 
00093     uint8_t getByte1()
00094     {
00095       return spi1.write(0x00);
00096     }
00097     
00098     int8_t send(uint8_t *data1, uint8_t len1)
00099     {
00100       if (len1>PIXY_OUTBUF_SIZE1 || outLen1!=0)
00101         return -1;
00102       memcpy(outBuf1, data1, len1);
00103       outLen1 = len1;
00104       outIndex1 = 0;
00105       return len1;
00106     }
00107 
00108     void setAddress1(uint8_t addr1)
00109     {
00110       addr_1 = addr1;
00111     }
00112 
00113   private:
00114     uint8_t outBuf1[PIXY_OUTBUF_SIZE1];
00115     uint8_t outLen1;
00116     uint8_t outIndex1;
00117     uint8_t addr_1;
00118 };
00119 
00120 
00121 typedef TPixy1<LinkSPI1> Pixy1;
00122 
00123 #endif
00124 
00125 
00126