cette librairie contient les fonctions de base permettant de detecter un objet avec la camera pixy

Dependents:   pixy_base

Committer:
michelmoulin
Date:
Wed Nov 18 13:12:20 2020 +0000
Revision:
0:a1e10abfbd19
cette version de programme permet d'afficher les coordonnees x et y d'un objet detecte par la camera pixy.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michelmoulin 0:a1e10abfbd19 1 #include "TPixyInterface.h"
michelmoulin 0:a1e10abfbd19 2
michelmoulin 0:a1e10abfbd19 3 PixyInterfaceSPI::PixyInterfaceSPI(SPI* interface) : TPixyInterface(), spi(interface) {}
michelmoulin 0:a1e10abfbd19 4
michelmoulin 0:a1e10abfbd19 5 uint16_t PixyInterfaceSPI::getWord()
michelmoulin 0:a1e10abfbd19 6 {
michelmoulin 0:a1e10abfbd19 7 // ordering is different (big endian) because Pixy is sending 16 bits through SPI
michelmoulin 0:a1e10abfbd19 8 // instead of 2 bytes in a 16-bit word as with I2C
michelmoulin 0:a1e10abfbd19 9 uint16_t w;
michelmoulin 0:a1e10abfbd19 10 if (inQ.read(&w)) {
michelmoulin 0:a1e10abfbd19 11 return w;
michelmoulin 0:a1e10abfbd19 12 }
michelmoulin 0:a1e10abfbd19 13 return getWordHw();
michelmoulin 0:a1e10abfbd19 14 }
michelmoulin 0:a1e10abfbd19 15
michelmoulin 0:a1e10abfbd19 16 uint8_t PixyInterfaceSPI::getByte()
michelmoulin 0:a1e10abfbd19 17 {
michelmoulin 0:a1e10abfbd19 18 //return SPI.transfer(0x00);
michelmoulin 0:a1e10abfbd19 19 return spi->write(0x00);
michelmoulin 0:a1e10abfbd19 20 }
michelmoulin 0:a1e10abfbd19 21
michelmoulin 0:a1e10abfbd19 22 int8_t PixyInterfaceSPI::send(uint8_t *data, uint8_t len)
michelmoulin 0:a1e10abfbd19 23 {
michelmoulin 0:a1e10abfbd19 24 int i;
michelmoulin 0:a1e10abfbd19 25 // check to see if we have enough space in our circular queue
michelmoulin 0:a1e10abfbd19 26 if (outQ.freeLen() < len) {
michelmoulin 0:a1e10abfbd19 27 return -1;
michelmoulin 0:a1e10abfbd19 28 }
michelmoulin 0:a1e10abfbd19 29 for (i = 0; i < len; i++) {
michelmoulin 0:a1e10abfbd19 30 outQ.write(data[i]);
michelmoulin 0:a1e10abfbd19 31 }
michelmoulin 0:a1e10abfbd19 32 flushSend();
michelmoulin 0:a1e10abfbd19 33 return len;
michelmoulin 0:a1e10abfbd19 34 }
michelmoulin 0:a1e10abfbd19 35
michelmoulin 0:a1e10abfbd19 36 void PixyInterfaceSPI::setArg(uint16_t arg) {}
michelmoulin 0:a1e10abfbd19 37
michelmoulin 0:a1e10abfbd19 38 uint16_t PixyInterfaceSPI::getWordHw()
michelmoulin 0:a1e10abfbd19 39 {
michelmoulin 0:a1e10abfbd19 40 // ordering is different (big endian) because Pixy is sending 16 bits through SPI
michelmoulin 0:a1e10abfbd19 41 // instead of 2 bytes in a 16-bit word as with I2C
michelmoulin 0:a1e10abfbd19 42 uint16_t w;
michelmoulin 0:a1e10abfbd19 43 uint8_t c, c_out = 0;
michelmoulin 0:a1e10abfbd19 44
michelmoulin 0:a1e10abfbd19 45 if (outQ.read(&c_out)) {
michelmoulin 0:a1e10abfbd19 46 w = spi->write(PIXY_SYNC_BYTE_DATA);
michelmoulin 0:a1e10abfbd19 47 //w = SPI.transfer(PIXY_SYNC_BYTE_DATA);
michelmoulin 0:a1e10abfbd19 48 } else {
michelmoulin 0:a1e10abfbd19 49 w = spi->write(PIXY_SYNC_BYTE);
michelmoulin 0:a1e10abfbd19 50 //w = SPI.transfer(PIXY_SYNC_BYTE);
michelmoulin 0:a1e10abfbd19 51 }
michelmoulin 0:a1e10abfbd19 52 w <<= 8;
michelmoulin 0:a1e10abfbd19 53 c = spi->write(c_out);
michelmoulin 0:a1e10abfbd19 54 //c = SPI.transfer(cout);
michelmoulin 0:a1e10abfbd19 55 w |= c;
michelmoulin 0:a1e10abfbd19 56 return w;
michelmoulin 0:a1e10abfbd19 57 }
michelmoulin 0:a1e10abfbd19 58
michelmoulin 0:a1e10abfbd19 59 void PixyInterfaceSPI::flushSend()
michelmoulin 0:a1e10abfbd19 60 {
michelmoulin 0:a1e10abfbd19 61 uint16_t w;
michelmoulin 0:a1e10abfbd19 62 while(outQ.len) {
michelmoulin 0:a1e10abfbd19 63 w = getWordHw();
michelmoulin 0:a1e10abfbd19 64 inQ.write(w);
michelmoulin 0:a1e10abfbd19 65 }
michelmoulin 0:a1e10abfbd19 66 }
michelmoulin 0:a1e10abfbd19 67
michelmoulin 0:a1e10abfbd19 68
michelmoulin 0:a1e10abfbd19 69
michelmoulin 0:a1e10abfbd19 70
michelmoulin 0:a1e10abfbd19 71 template <class BufType> CircularQ<BufType>::CircularQ()
michelmoulin 0:a1e10abfbd19 72 {
michelmoulin 0:a1e10abfbd19 73 len = 0;
michelmoulin 0:a1e10abfbd19 74 writeIndex = 0;
michelmoulin 0:a1e10abfbd19 75 readIndex = 0;
michelmoulin 0:a1e10abfbd19 76 }
michelmoulin 0:a1e10abfbd19 77
michelmoulin 0:a1e10abfbd19 78 template <class BufType> bool CircularQ<BufType>::read(BufType *c)
michelmoulin 0:a1e10abfbd19 79 {
michelmoulin 0:a1e10abfbd19 80 if (len) {
michelmoulin 0:a1e10abfbd19 81 *c = buf[readIndex++];
michelmoulin 0:a1e10abfbd19 82 len--;
michelmoulin 0:a1e10abfbd19 83 if (readIndex == PIXY_BUF_SIZE) {
michelmoulin 0:a1e10abfbd19 84 readIndex = 0;
michelmoulin 0:a1e10abfbd19 85 }
michelmoulin 0:a1e10abfbd19 86 return true;
michelmoulin 0:a1e10abfbd19 87 } else {
michelmoulin 0:a1e10abfbd19 88 return false;
michelmoulin 0:a1e10abfbd19 89 }
michelmoulin 0:a1e10abfbd19 90 }
michelmoulin 0:a1e10abfbd19 91
michelmoulin 0:a1e10abfbd19 92 template <class BufType> uint8_t CircularQ<BufType>::freeLen()
michelmoulin 0:a1e10abfbd19 93 {
michelmoulin 0:a1e10abfbd19 94 return PIXY_BUF_SIZE - len;
michelmoulin 0:a1e10abfbd19 95 }
michelmoulin 0:a1e10abfbd19 96
michelmoulin 0:a1e10abfbd19 97 template <class BufType> bool CircularQ<BufType>::write(BufType c)
michelmoulin 0:a1e10abfbd19 98 {
michelmoulin 0:a1e10abfbd19 99 if (freeLen() == 0) {
michelmoulin 0:a1e10abfbd19 100 return false;
michelmoulin 0:a1e10abfbd19 101 }
michelmoulin 0:a1e10abfbd19 102 buf[writeIndex++] = c;
michelmoulin 0:a1e10abfbd19 103 len++;
michelmoulin 0:a1e10abfbd19 104 if (writeIndex == PIXY_BUF_SIZE) {
michelmoulin 0:a1e10abfbd19 105 writeIndex = 0;
michelmoulin 0:a1e10abfbd19 106 }
michelmoulin 0:a1e10abfbd19 107 return true;
michelmoulin 0:a1e10abfbd19 108 }