[241] | 1 | //
|
---|
| 2 | // begin license header
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Pixy CMUcam5 or "Pixy" for short
|
---|
| 5 | //
|
---|
| 6 | // All Pixy source code is provided under the terms of the
|
---|
| 7 | // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
|
---|
| 8 | // Those wishing to use Pixy source code, software and/or
|
---|
| 9 | // technologies under different licensing terms should contact us at
|
---|
| 10 | // cmucam@cs.cmu.edu. Such licensing terms are available for
|
---|
| 11 | // all portions of the Pixy codebase presented here.
|
---|
| 12 | //
|
---|
| 13 | // end license header
|
---|
| 14 | //
|
---|
| 15 | // This file is for defining the SPI-related classes. It's called Pixy.h instead
|
---|
| 16 | // of Pixy_SPI.h because it's the default/recommended communication method
|
---|
| 17 | // with Arduino. This class assumes you are using the ICSP connector to talk to
|
---|
| 18 | // Pixy from your Arduino. For more information go to:
|
---|
| 19 | //
|
---|
| 20 | //http://cmucam.org/projects/cmucam5/wiki/Hooking_up_Pixy_to_a_Microcontroller_(like_an_Arduino)
|
---|
| 21 | //
|
---|
| 22 |
|
---|
| 23 | #ifndef PIXY_H
|
---|
| 24 | #define PIXY_H
|
---|
| 25 |
|
---|
| 26 | #include "TPixy.h"
|
---|
| 27 | #include "SPI.h"
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #define PIXY_SYNC_BYTE 0x5a
|
---|
| 31 | #define PIXY_SYNC_BYTE_DATA 0x5b
|
---|
| 32 | #define PIXY_BUF_SIZE 16
|
---|
| 33 |
|
---|
| 34 | template <class BufType> struct CircularQ
|
---|
| 35 | {
|
---|
| 36 | CircularQ()
|
---|
| 37 | {
|
---|
| 38 | len = 0;
|
---|
| 39 | writeIndex = 0;
|
---|
| 40 | readIndex = 0;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | bool read(BufType *c)
|
---|
| 44 | {
|
---|
| 45 | if (len)
|
---|
| 46 | {
|
---|
| 47 | *c = buf[readIndex++];
|
---|
| 48 | len--;
|
---|
| 49 | if (readIndex==PIXY_BUF_SIZE)
|
---|
| 50 | readIndex = 0;
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 | else
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | uint8_t freeLen()
|
---|
| 58 | {
|
---|
| 59 | return PIXY_BUF_SIZE-len;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | bool write(BufType c)
|
---|
| 63 | {
|
---|
| 64 | if (freeLen()==0)
|
---|
| 65 | return false;
|
---|
| 66 |
|
---|
| 67 | buf[writeIndex++] = c;
|
---|
| 68 | len++;
|
---|
| 69 | if (writeIndex==PIXY_BUF_SIZE)
|
---|
| 70 | writeIndex = 0;
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | BufType buf[PIXY_BUF_SIZE];
|
---|
| 75 | uint8_t len;
|
---|
| 76 | uint8_t writeIndex;
|
---|
| 77 | uint8_t readIndex;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | class LinkSPI
|
---|
| 81 | {
|
---|
| 82 | public:
|
---|
| 83 | void init()
|
---|
| 84 | {
|
---|
| 85 | SPI.begin();
|
---|
| 86 |
|
---|
| 87 | #ifdef ARDUINO_ARCH_SAMD
|
---|
| 88 | // DUE clock divider //
|
---|
| 89 | SPI.setClockDivider(84);
|
---|
| 90 | #else
|
---|
| 91 | // Default clock divider //
|
---|
| 92 | SPI.setClockDivider(SPI_CLOCK_DIV16);
|
---|
| 93 | #endif
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | uint16_t getWord()
|
---|
| 97 | {
|
---|
| 98 | // ordering is different (big endian) because Pixy is sending 16 bits through SPI
|
---|
| 99 | // instead of 2 bytes in a 16-bit word as with I2C
|
---|
| 100 | uint16_t w;
|
---|
| 101 |
|
---|
| 102 | if (inQ.read(&w))
|
---|
| 103 | return w;
|
---|
| 104 |
|
---|
| 105 | return getWordHw();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | uint8_t getByte()
|
---|
| 109 | {
|
---|
| 110 | return SPI.transfer(0x00);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | int8_t send(uint8_t *data, uint8_t len)
|
---|
| 114 | {
|
---|
| 115 | int i;
|
---|
| 116 |
|
---|
| 117 | // check to see if we have enough space in our circular queue
|
---|
| 118 | if (outQ.freeLen()<len)
|
---|
| 119 | return -1;
|
---|
| 120 |
|
---|
| 121 | for (i=0; i<len; i++)
|
---|
| 122 | outQ.write(data[i]);
|
---|
| 123 | flushSend();
|
---|
| 124 | return len;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void setArg(uint16_t arg)
|
---|
| 128 | {
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | private:
|
---|
| 132 | uint16_t getWordHw()
|
---|
| 133 | {
|
---|
| 134 | // ordering is different (big endian) because Pixy is sending 16 bits through SPI
|
---|
| 135 | // instead of 2 bytes in a 16-bit word as with I2C
|
---|
| 136 | uint16_t w;
|
---|
| 137 | uint8_t c, cout = 0;
|
---|
| 138 |
|
---|
| 139 | if (outQ.read(&cout))
|
---|
| 140 | w = SPI.transfer(PIXY_SYNC_BYTE_DATA);
|
---|
| 141 | else
|
---|
| 142 | w = SPI.transfer(PIXY_SYNC_BYTE);
|
---|
| 143 |
|
---|
| 144 | w <<= 8;
|
---|
| 145 | c = SPI.transfer(cout);
|
---|
| 146 | w |= c;
|
---|
| 147 |
|
---|
| 148 | return w;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void flushSend()
|
---|
| 152 | {
|
---|
| 153 | uint16_t w;
|
---|
| 154 | while(outQ.len)
|
---|
| 155 | {
|
---|
| 156 | w = getWordHw();
|
---|
| 157 | inQ.write(w);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | // we need a little circular queues for both directions
|
---|
| 162 | CircularQ<uint8_t> outQ;
|
---|
| 163 | CircularQ<uint16_t> inQ;
|
---|
| 164 | };
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | typedef TPixy<LinkSPI> Pixy;
|
---|
| 168 |
|
---|
| 169 | #endif
|
---|