A library for interfacing with the Pixy color recognition camera
Dependents: PixyCamera MbedOS_Robot_Team ManualControlFinal PlayBack ... more
TPixyInterface.cpp@0:ef0e3c67dc5b, 2016-03-14 (annotated)
- Committer:
- swilkins8
- Date:
- Mon Mar 14 00:35:23 2016 +0000
- Revision:
- 0:ef0e3c67dc5b
Completed rewrite of code to expand interfacing options
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
swilkins8 | 0:ef0e3c67dc5b | 1 | #include "TPixyInterface.h" |
swilkins8 | 0:ef0e3c67dc5b | 2 | |
swilkins8 | 0:ef0e3c67dc5b | 3 | PixyInterfaceSPI::PixyInterfaceSPI(SPI* interface) : TPixyInterface(), spi(interface) {} |
swilkins8 | 0:ef0e3c67dc5b | 4 | |
swilkins8 | 0:ef0e3c67dc5b | 5 | void PixyInterfaceSPI::init() |
swilkins8 | 0:ef0e3c67dc5b | 6 | { |
swilkins8 | 0:ef0e3c67dc5b | 7 | spi->frequency(1000000); |
swilkins8 | 0:ef0e3c67dc5b | 8 | } |
swilkins8 | 0:ef0e3c67dc5b | 9 | |
swilkins8 | 0:ef0e3c67dc5b | 10 | uint16_t PixyInterfaceSPI::getWord() |
swilkins8 | 0:ef0e3c67dc5b | 11 | { |
swilkins8 | 0:ef0e3c67dc5b | 12 | // ordering is different (big endian) because Pixy is sending 16 bits through SPI |
swilkins8 | 0:ef0e3c67dc5b | 13 | // instead of 2 bytes in a 16-bit word as with I2C |
swilkins8 | 0:ef0e3c67dc5b | 14 | uint16_t w; |
swilkins8 | 0:ef0e3c67dc5b | 15 | if (inQ.read(&w)) { |
swilkins8 | 0:ef0e3c67dc5b | 16 | return w; |
swilkins8 | 0:ef0e3c67dc5b | 17 | } |
swilkins8 | 0:ef0e3c67dc5b | 18 | return getWordHw(); |
swilkins8 | 0:ef0e3c67dc5b | 19 | } |
swilkins8 | 0:ef0e3c67dc5b | 20 | |
swilkins8 | 0:ef0e3c67dc5b | 21 | uint8_t PixyInterfaceSPI::getByte() |
swilkins8 | 0:ef0e3c67dc5b | 22 | { |
swilkins8 | 0:ef0e3c67dc5b | 23 | //return SPI.transfer(0x00); |
swilkins8 | 0:ef0e3c67dc5b | 24 | return spi->write(0x00); |
swilkins8 | 0:ef0e3c67dc5b | 25 | } |
swilkins8 | 0:ef0e3c67dc5b | 26 | |
swilkins8 | 0:ef0e3c67dc5b | 27 | int8_t PixyInterfaceSPI::send(uint8_t *data, uint8_t len) |
swilkins8 | 0:ef0e3c67dc5b | 28 | { |
swilkins8 | 0:ef0e3c67dc5b | 29 | int i; |
swilkins8 | 0:ef0e3c67dc5b | 30 | // check to see if we have enough space in our circular queue |
swilkins8 | 0:ef0e3c67dc5b | 31 | if (outQ.freeLen() < len) { |
swilkins8 | 0:ef0e3c67dc5b | 32 | return -1; |
swilkins8 | 0:ef0e3c67dc5b | 33 | } |
swilkins8 | 0:ef0e3c67dc5b | 34 | for (i = 0; i < len; i++) { |
swilkins8 | 0:ef0e3c67dc5b | 35 | outQ.write(data[i]); |
swilkins8 | 0:ef0e3c67dc5b | 36 | } |
swilkins8 | 0:ef0e3c67dc5b | 37 | flushSend(); |
swilkins8 | 0:ef0e3c67dc5b | 38 | return len; |
swilkins8 | 0:ef0e3c67dc5b | 39 | } |
swilkins8 | 0:ef0e3c67dc5b | 40 | |
swilkins8 | 0:ef0e3c67dc5b | 41 | void PixyInterfaceSPI::setArg(uint16_t arg) {} |
swilkins8 | 0:ef0e3c67dc5b | 42 | |
swilkins8 | 0:ef0e3c67dc5b | 43 | uint16_t PixyInterfaceSPI::getWordHw() |
swilkins8 | 0:ef0e3c67dc5b | 44 | { |
swilkins8 | 0:ef0e3c67dc5b | 45 | // ordering is different (big endian) because Pixy is sending 16 bits through SPI |
swilkins8 | 0:ef0e3c67dc5b | 46 | // instead of 2 bytes in a 16-bit word as with I2C |
swilkins8 | 0:ef0e3c67dc5b | 47 | uint16_t w; |
swilkins8 | 0:ef0e3c67dc5b | 48 | uint8_t c, c_out = 0; |
swilkins8 | 0:ef0e3c67dc5b | 49 | |
swilkins8 | 0:ef0e3c67dc5b | 50 | if (outQ.read(&c_out)) { |
swilkins8 | 0:ef0e3c67dc5b | 51 | w = spi->write(PIXY_SYNC_BYTE_DATA); |
swilkins8 | 0:ef0e3c67dc5b | 52 | //w = SPI.transfer(PIXY_SYNC_BYTE_DATA); |
swilkins8 | 0:ef0e3c67dc5b | 53 | } else { |
swilkins8 | 0:ef0e3c67dc5b | 54 | w = spi->write(PIXY_SYNC_BYTE); |
swilkins8 | 0:ef0e3c67dc5b | 55 | //w = SPI.transfer(PIXY_SYNC_BYTE); |
swilkins8 | 0:ef0e3c67dc5b | 56 | } |
swilkins8 | 0:ef0e3c67dc5b | 57 | w <<= 8; |
swilkins8 | 0:ef0e3c67dc5b | 58 | c = spi->write(c_out); |
swilkins8 | 0:ef0e3c67dc5b | 59 | //c = SPI.transfer(cout); |
swilkins8 | 0:ef0e3c67dc5b | 60 | w |= c; |
swilkins8 | 0:ef0e3c67dc5b | 61 | return w; |
swilkins8 | 0:ef0e3c67dc5b | 62 | } |
swilkins8 | 0:ef0e3c67dc5b | 63 | |
swilkins8 | 0:ef0e3c67dc5b | 64 | void PixyInterfaceSPI::flushSend() |
swilkins8 | 0:ef0e3c67dc5b | 65 | { |
swilkins8 | 0:ef0e3c67dc5b | 66 | uint16_t w; |
swilkins8 | 0:ef0e3c67dc5b | 67 | while(outQ.len) { |
swilkins8 | 0:ef0e3c67dc5b | 68 | w = getWordHw(); |
swilkins8 | 0:ef0e3c67dc5b | 69 | inQ.write(w); |
swilkins8 | 0:ef0e3c67dc5b | 70 | } |
swilkins8 | 0:ef0e3c67dc5b | 71 | } |
swilkins8 | 0:ef0e3c67dc5b | 72 | |
swilkins8 | 0:ef0e3c67dc5b | 73 | |
swilkins8 | 0:ef0e3c67dc5b | 74 | |
swilkins8 | 0:ef0e3c67dc5b | 75 | |
swilkins8 | 0:ef0e3c67dc5b | 76 | template <class BufType> CircularQ<BufType>::CircularQ() |
swilkins8 | 0:ef0e3c67dc5b | 77 | { |
swilkins8 | 0:ef0e3c67dc5b | 78 | len = 0; |
swilkins8 | 0:ef0e3c67dc5b | 79 | writeIndex = 0; |
swilkins8 | 0:ef0e3c67dc5b | 80 | readIndex = 0; |
swilkins8 | 0:ef0e3c67dc5b | 81 | } |
swilkins8 | 0:ef0e3c67dc5b | 82 | |
swilkins8 | 0:ef0e3c67dc5b | 83 | template <class BufType> bool CircularQ<BufType>::read(BufType *c) |
swilkins8 | 0:ef0e3c67dc5b | 84 | { |
swilkins8 | 0:ef0e3c67dc5b | 85 | if (len) { |
swilkins8 | 0:ef0e3c67dc5b | 86 | *c = buf[readIndex++]; |
swilkins8 | 0:ef0e3c67dc5b | 87 | len--; |
swilkins8 | 0:ef0e3c67dc5b | 88 | if (readIndex == PIXY_BUF_SIZE) { |
swilkins8 | 0:ef0e3c67dc5b | 89 | readIndex = 0; |
swilkins8 | 0:ef0e3c67dc5b | 90 | } |
swilkins8 | 0:ef0e3c67dc5b | 91 | return true; |
swilkins8 | 0:ef0e3c67dc5b | 92 | } else { |
swilkins8 | 0:ef0e3c67dc5b | 93 | return false; |
swilkins8 | 0:ef0e3c67dc5b | 94 | } |
swilkins8 | 0:ef0e3c67dc5b | 95 | } |
swilkins8 | 0:ef0e3c67dc5b | 96 | |
swilkins8 | 0:ef0e3c67dc5b | 97 | template <class BufType> uint8_t CircularQ<BufType>::freeLen() |
swilkins8 | 0:ef0e3c67dc5b | 98 | { |
swilkins8 | 0:ef0e3c67dc5b | 99 | return PIXY_BUF_SIZE - len; |
swilkins8 | 0:ef0e3c67dc5b | 100 | } |
swilkins8 | 0:ef0e3c67dc5b | 101 | |
swilkins8 | 0:ef0e3c67dc5b | 102 | template <class BufType> bool CircularQ<BufType>::write(BufType c) |
swilkins8 | 0:ef0e3c67dc5b | 103 | { |
swilkins8 | 0:ef0e3c67dc5b | 104 | if (freeLen() == 0) { |
swilkins8 | 0:ef0e3c67dc5b | 105 | return false; |
swilkins8 | 0:ef0e3c67dc5b | 106 | } |
swilkins8 | 0:ef0e3c67dc5b | 107 | buf[writeIndex++] = c; |
swilkins8 | 0:ef0e3c67dc5b | 108 | len++; |
swilkins8 | 0:ef0e3c67dc5b | 109 | if (writeIndex == PIXY_BUF_SIZE) { |
swilkins8 | 0:ef0e3c67dc5b | 110 | writeIndex = 0; |
swilkins8 | 0:ef0e3c67dc5b | 111 | } |
swilkins8 | 0:ef0e3c67dc5b | 112 | return true; |
swilkins8 | 0:ef0e3c67dc5b | 113 | } |