same
Fork of PixyLibrary by
Pixy1.h@1:4b14159ab9bb, 2015-04-13 (annotated)
- Committer:
- johnylafleur
- Date:
- Mon Apr 13 12:32:52 2015 +0000
- Revision:
- 1:4b14159ab9bb
- Parent:
- 0:56a3009221d3
Same library
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_H1 |
MBM | 0:56a3009221d3 | 43 | #define PIXY_H1 |
MBM | 0:56a3009221d3 | 44 | |
MBM | 0:56a3009221d3 | 45 | #include "TPixy1.h" |
MBM | 0:56a3009221d3 | 46 | #include "SPI1.h" |
MBM | 0:56a3009221d3 | 47 | |
MBM | 0:56a3009221d3 | 48 | |
MBM | 0:56a3009221d3 | 49 | #define PIXY_SYNC_BYTE1 0x5a |
MBM | 0:56a3009221d3 | 50 | #define PIXY_SYNC_BYTE_DATA1 0x5b |
MBM | 0:56a3009221d3 | 51 | #define PIXY_OUTBUF_SIZE1 6 |
MBM | 0:56a3009221d3 | 52 | |
MBM | 0:56a3009221d3 | 53 | SPI spi1(p5, p6, p7); // mosi, miso, sclk |
MBM | 0:56a3009221d3 | 54 | |
MBM | 0:56a3009221d3 | 55 | class LinkSPI1 |
MBM | 0:56a3009221d3 | 56 | { |
MBM | 0:56a3009221d3 | 57 | public: |
MBM | 0:56a3009221d3 | 58 | void init1() |
MBM | 0:56a3009221d3 | 59 | { |
MBM | 0:56a3009221d3 | 60 | outLen1 = 0; |
MBM | 0:56a3009221d3 | 61 | #ifdef __SAM3X8E__ |
MBM | 0:56a3009221d3 | 62 | // DUE clock divider // |
MBM | 0:56a3009221d3 | 63 | SPI1.setClockDivider1(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 getWord1() |
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 w1; |
MBM | 0:56a3009221d3 | 75 | uint8_t c1, count1 = 0; |
MBM | 0:56a3009221d3 | 76 | |
MBM | 0:56a3009221d3 | 77 | if (outLen1) |
MBM | 0:56a3009221d3 | 78 | { |
MBM | 0:56a3009221d3 | 79 | w1 = spi1.write(PIXY_SYNC_BYTE_DATA1); |
MBM | 0:56a3009221d3 | 80 | count1 = outBuf1[outIndex1++]; |
MBM | 0:56a3009221d3 | 81 | if (outIndex1==outLen1) |
MBM | 0:56a3009221d3 | 82 | outLen1 = 0; |
MBM | 0:56a3009221d3 | 83 | } |
MBM | 0:56a3009221d3 | 84 | else |
MBM | 0:56a3009221d3 | 85 | w1 = spi1.write(PIXY_SYNC_BYTE1); |
MBM | 0:56a3009221d3 | 86 | w1 <<= 8; |
MBM | 0:56a3009221d3 | 87 | c1 = spi1.write(count1); |
MBM | 0:56a3009221d3 | 88 | w1 |= c1; |
MBM | 0:56a3009221d3 | 89 | |
MBM | 0:56a3009221d3 | 90 | return w1; |
MBM | 0:56a3009221d3 | 91 | } |
MBM | 0:56a3009221d3 | 92 | |
MBM | 0:56a3009221d3 | 93 | uint8_t getByte1() |
MBM | 0:56a3009221d3 | 94 | { |
MBM | 0:56a3009221d3 | 95 | return spi1.write(0x00); |
MBM | 0:56a3009221d3 | 96 | } |
MBM | 0:56a3009221d3 | 97 | |
MBM | 0:56a3009221d3 | 98 | int8_t send(uint8_t *data1, uint8_t len1) |
MBM | 0:56a3009221d3 | 99 | { |
MBM | 0:56a3009221d3 | 100 | if (len1>PIXY_OUTBUF_SIZE1 || outLen1!=0) |
MBM | 0:56a3009221d3 | 101 | return -1; |
MBM | 0:56a3009221d3 | 102 | memcpy(outBuf1, data1, len1); |
MBM | 0:56a3009221d3 | 103 | outLen1 = len1; |
MBM | 0:56a3009221d3 | 104 | outIndex1 = 0; |
MBM | 0:56a3009221d3 | 105 | return len1; |
MBM | 0:56a3009221d3 | 106 | } |
MBM | 0:56a3009221d3 | 107 | |
MBM | 0:56a3009221d3 | 108 | void setAddress1(uint8_t addr1) |
MBM | 0:56a3009221d3 | 109 | { |
MBM | 0:56a3009221d3 | 110 | addr_1 = addr1; |
MBM | 0:56a3009221d3 | 111 | } |
MBM | 0:56a3009221d3 | 112 | |
MBM | 0:56a3009221d3 | 113 | private: |
MBM | 0:56a3009221d3 | 114 | uint8_t outBuf1[PIXY_OUTBUF_SIZE1]; |
MBM | 0:56a3009221d3 | 115 | uint8_t outLen1; |
MBM | 0:56a3009221d3 | 116 | uint8_t outIndex1; |
MBM | 0:56a3009221d3 | 117 | uint8_t addr_1; |
MBM | 0:56a3009221d3 | 118 | }; |
MBM | 0:56a3009221d3 | 119 | |
MBM | 0:56a3009221d3 | 120 | |
MBM | 0:56a3009221d3 | 121 | typedef TPixy1<LinkSPI1> Pixy1; |
MBM | 0:56a3009221d3 | 122 | |
MBM | 0:56a3009221d3 | 123 | #endif |
MBM | 0:56a3009221d3 | 124 | |
MBM | 0:56a3009221d3 | 125 | |
MBM | 0:56a3009221d3 | 126 |