Mathieu Malone
/
PixyLibrary
Library used to initialize and to communicate with the CMUcam5 Pixy
Pixy2.h@0:56a3009221d3, 2014-08-12 (annotated)
- Committer:
- MBM
- Date:
- Tue Aug 12 11:01:31 2014 +0000
- Revision:
- 0:56a3009221d3
The PixyLibrary was created from the Arduino libraries for the CMUcam5 Pixy found on http://cmucam.org/projects/cmucam5/files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MBM | 0:56a3009221d3 | 1 | //-------------------------------------------------------------------------------------------- |
MBM | 0:56a3009221d3 | 2 | //Original Property of: charmedlabs.com/pixystart -> arduino_pixy-x.y.z.zip |
MBM | 0:56a3009221d3 | 3 | // |
MBM | 0:56a3009221d3 | 4 | //Modifications made by: Mathieu Malone |
MBM | 0:56a3009221d3 | 5 | //Modifications: Modified Arduino code to function with mbed development platform |
MBM | 0:56a3009221d3 | 6 | //Output Method: This program uses "Serial pc(USBTX, USBRX)" in order to allow communication |
MBM | 0:56a3009221d3 | 7 | // between the mbed platform and putty terminal through USB |
MBM | 0:56a3009221d3 | 8 | // |
MBM | 0:56a3009221d3 | 9 | //Latest update by: Mathieu Malone |
MBM | 0:56a3009221d3 | 10 | //Date of last update: July 24th, 2014 |
MBM | 0:56a3009221d3 | 11 | //-------------------------------------------------------------------------------------------- |
MBM | 0:56a3009221d3 | 12 | // |
MBM | 0:56a3009221d3 | 13 | // begin license header |
MBM | 0:56a3009221d3 | 14 | // |
MBM | 0:56a3009221d3 | 15 | // This file is part of Pixy CMUcam5 or "Pixy" for short |
MBM | 0:56a3009221d3 | 16 | // |
MBM | 0:56a3009221d3 | 17 | // All Pixy source code is provided under the terms of the |
MBM | 0:56a3009221d3 | 18 | // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html). |
MBM | 0:56a3009221d3 | 19 | // Those wishing to use Pixy source code, software and/or |
MBM | 0:56a3009221d3 | 20 | // technologies under different licensing terms should contact us at |
MBM | 0:56a3009221d3 | 21 | // cmucam@cs.cmu.edu. Such licensing terms are available for |
MBM | 0:56a3009221d3 | 22 | // all portions of the Pixy codebase presented here. |
MBM | 0:56a3009221d3 | 23 | // |
MBM | 0:56a3009221d3 | 24 | // end license header |
MBM | 0:56a3009221d3 | 25 | // |
MBM | 0:56a3009221d3 | 26 | |
MBM | 0:56a3009221d3 | 27 | /* |
MBM | 0:56a3009221d3 | 28 | Pixy.h - Library for interfacing with Pixy. |
MBM | 0:56a3009221d3 | 29 | Created by Scott Robinson, October 22, 2013. |
MBM | 0:56a3009221d3 | 30 | Released into the public domain. |
MBM | 0:56a3009221d3 | 31 | |
MBM | 0:56a3009221d3 | 32 | 06.04.2014 v0.1.3 John Leimon |
MBM | 0:56a3009221d3 | 33 | + LinkSPI.init() should be called from the setup() |
MBM | 0:56a3009221d3 | 34 | function instead of being called automatically from |
MBM | 0:56a3009221d3 | 35 | the TPixy<LinkSPI> constructor in global scope. This |
MBM | 0:56a3009221d3 | 36 | is a workaround for a bug (?) in the Arduino DUE in which |
MBM | 0:56a3009221d3 | 37 | calling SPI.begin() from global scope (via a constructor) |
MBM | 0:56a3009221d3 | 38 | inhibits the operation of the Serial peripheral in the |
MBM | 0:56a3009221d3 | 39 | DUE. [As of: Arduino 1.5.6-r2] |
MBM | 0:56a3009221d3 | 40 | */ |
MBM | 0:56a3009221d3 | 41 | |
MBM | 0:56a3009221d3 | 42 | #ifndef PIXY_H2 |
MBM | 0:56a3009221d3 | 43 | #define PIXY_H2 |
MBM | 0:56a3009221d3 | 44 | |
MBM | 0:56a3009221d3 | 45 | #include "TPixy2.h" |
MBM | 0:56a3009221d3 | 46 | #include "SPI2.h" |
MBM | 0:56a3009221d3 | 47 | |
MBM | 0:56a3009221d3 | 48 | |
MBM | 0:56a3009221d3 | 49 | #define PIXY_SYNC_BYTE2 0x5a |
MBM | 0:56a3009221d3 | 50 | #define PIXY_SYNC_BYTE_DATA2 0x5b |
MBM | 0:56a3009221d3 | 51 | #define PIXY_OUTBUF_SIZE2 6 |
MBM | 0:56a3009221d3 | 52 | |
MBM | 0:56a3009221d3 | 53 | SPI spi2(p11, p12, p13); // mosi, miso, sclk |
MBM | 0:56a3009221d3 | 54 | |
MBM | 0:56a3009221d3 | 55 | class LinkSPI2 |
MBM | 0:56a3009221d3 | 56 | { |
MBM | 0:56a3009221d3 | 57 | public: |
MBM | 0:56a3009221d3 | 58 | void init2() |
MBM | 0:56a3009221d3 | 59 | { |
MBM | 0:56a3009221d3 | 60 | outLen2 = 0; |
MBM | 0:56a3009221d3 | 61 | #ifdef __SAM3X8E__ |
MBM | 0:56a3009221d3 | 62 | // DUE clock divider // |
MBM | 0:56a3009221d3 | 63 | SPI.setClockDivider2(84); |
MBM | 0:56a3009221d3 | 64 | #else |
MBM | 0:56a3009221d3 | 65 | // Default clock divider // |
MBM | 0:56a3009221d3 | 66 | //SPI.setClockDivider(SPI_CLOCK_DIV16); |
MBM | 0:56a3009221d3 | 67 | #endif |
MBM | 0:56a3009221d3 | 68 | } |
MBM | 0:56a3009221d3 | 69 | |
MBM | 0:56a3009221d3 | 70 | uint16_t getWord2() |
MBM | 0:56a3009221d3 | 71 | { |
MBM | 0:56a3009221d3 | 72 | // ordering is different because Pixy is sending 16 bits through SPI |
MBM | 0:56a3009221d3 | 73 | // instead of 2 bytes in a 16-bit word as with I2C |
MBM | 0:56a3009221d3 | 74 | uint16_t w2; |
MBM | 0:56a3009221d3 | 75 | uint8_t c2, count2 = 0; |
MBM | 0:56a3009221d3 | 76 | |
MBM | 0:56a3009221d3 | 77 | if (outLen2) |
MBM | 0:56a3009221d3 | 78 | { |
MBM | 0:56a3009221d3 | 79 | w2 = spi2.write(PIXY_SYNC_BYTE_DATA2); |
MBM | 0:56a3009221d3 | 80 | count2 = outBuf2[outIndex2++]; |
MBM | 0:56a3009221d3 | 81 | if (outIndex2==outLen2) |
MBM | 0:56a3009221d3 | 82 | outLen2 = 0; |
MBM | 0:56a3009221d3 | 83 | } |
MBM | 0:56a3009221d3 | 84 | else |
MBM | 0:56a3009221d3 | 85 | w2 = spi2.write(PIXY_SYNC_BYTE2); |
MBM | 0:56a3009221d3 | 86 | w2 <<= 8; |
MBM | 0:56a3009221d3 | 87 | c2 = spi2.write(count2); |
MBM | 0:56a3009221d3 | 88 | w2 |= c2; |
MBM | 0:56a3009221d3 | 89 | |
MBM | 0:56a3009221d3 | 90 | return w2; |
MBM | 0:56a3009221d3 | 91 | } |
MBM | 0:56a3009221d3 | 92 | |
MBM | 0:56a3009221d3 | 93 | uint8_t getByte2() |
MBM | 0:56a3009221d3 | 94 | { |
MBM | 0:56a3009221d3 | 95 | return spi2.write(0x00); |
MBM | 0:56a3009221d3 | 96 | } |
MBM | 0:56a3009221d3 | 97 | |
MBM | 0:56a3009221d3 | 98 | int8_t send(uint8_t *data2, uint8_t len2) |
MBM | 0:56a3009221d3 | 99 | { |
MBM | 0:56a3009221d3 | 100 | if (len2>PIXY_OUTBUF_SIZE2 || outLen2!=0) |
MBM | 0:56a3009221d3 | 101 | return -1; |
MBM | 0:56a3009221d3 | 102 | memcpy(outBuf2, data2, len2); |
MBM | 0:56a3009221d3 | 103 | outLen2 = len2; |
MBM | 0:56a3009221d3 | 104 | outIndex2 = 0; |
MBM | 0:56a3009221d3 | 105 | return len2; |
MBM | 0:56a3009221d3 | 106 | } |
MBM | 0:56a3009221d3 | 107 | |
MBM | 0:56a3009221d3 | 108 | void setAddress2(uint8_t addr2) |
MBM | 0:56a3009221d3 | 109 | { |
MBM | 0:56a3009221d3 | 110 | addr_2 = addr2; |
MBM | 0:56a3009221d3 | 111 | } |
MBM | 0:56a3009221d3 | 112 | |
MBM | 0:56a3009221d3 | 113 | private: |
MBM | 0:56a3009221d3 | 114 | uint8_t outBuf2[PIXY_OUTBUF_SIZE2]; |
MBM | 0:56a3009221d3 | 115 | uint8_t outLen2; |
MBM | 0:56a3009221d3 | 116 | uint8_t outIndex2; |
MBM | 0:56a3009221d3 | 117 | uint8_t addr_2; |
MBM | 0:56a3009221d3 | 118 | }; |
MBM | 0:56a3009221d3 | 119 | |
MBM | 0:56a3009221d3 | 120 | |
MBM | 0:56a3009221d3 | 121 | typedef TPixy2<LinkSPI2> Pixy2; |
MBM | 0:56a3009221d3 | 122 | |
MBM | 0:56a3009221d3 | 123 | #endif |
MBM | 0:56a3009221d3 | 124 | |
MBM | 0:56a3009221d3 | 125 |