A library for interfacing with the Pixy color recognition camera

Dependents:   PixyCamera MbedOS_Robot_Team ManualControlFinal PlayBack ... more

Committer:
swilkins8
Date:
Mon Mar 14 00:35:23 2016 +0000
Revision:
0:ef0e3c67dc5b
Child:
3:66df7d295245
Completed rewrite of code to expand interfacing options

Who changed what in which revision?

UserRevisionLine numberNew contents of line
swilkins8 0:ef0e3c67dc5b 1 //
swilkins8 0:ef0e3c67dc5b 2 // begin license header
swilkins8 0:ef0e3c67dc5b 3 //
swilkins8 0:ef0e3c67dc5b 4 // This file is part of Pixy CMUcam5 or "Pixy" for short
swilkins8 0:ef0e3c67dc5b 5 //
swilkins8 0:ef0e3c67dc5b 6 // All Pixy source code is provided under the terms of the
swilkins8 0:ef0e3c67dc5b 7 // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
swilkins8 0:ef0e3c67dc5b 8 // Those wishing to use Pixy source code, software and/or
swilkins8 0:ef0e3c67dc5b 9 // technologies under different licensing terms should contact us at
swilkins8 0:ef0e3c67dc5b 10 // cmucam@cs.cmu.edu. Such licensing terms are available for
swilkins8 0:ef0e3c67dc5b 11 // all portions of the Pixy codebase presented here.
swilkins8 0:ef0e3c67dc5b 12 //
swilkins8 0:ef0e3c67dc5b 13 // end license header
swilkins8 0:ef0e3c67dc5b 14 //
swilkins8 0:ef0e3c67dc5b 15 // This file is for defining the SPI-related classes. It's called Pixy.h instead
swilkins8 0:ef0e3c67dc5b 16 // of Pixy_SPI.h because it's the default/recommended communication method
swilkins8 0:ef0e3c67dc5b 17 // with Arduino. This class assumes you are using the ICSP connector to talk to
swilkins8 0:ef0e3c67dc5b 18 // Pixy from your Arduino. For more information go to:
swilkins8 0:ef0e3c67dc5b 19 //
swilkins8 0:ef0e3c67dc5b 20 //http://cmucam.org/projects/cmucam5/wiki/Hooking_up_Pixy_to_a_Microcontroller_(like_an_Arduino)
swilkins8 0:ef0e3c67dc5b 21 //
swilkins8 0:ef0e3c67dc5b 22
swilkins8 0:ef0e3c67dc5b 23 #ifndef _TPIXY_INTERFACE_H
swilkins8 0:ef0e3c67dc5b 24 #define _TPIXY_INTERFACE_H
swilkins8 0:ef0e3c67dc5b 25
swilkins8 0:ef0e3c67dc5b 26 #include "TPixy.h"
swilkins8 0:ef0e3c67dc5b 27
swilkins8 0:ef0e3c67dc5b 28 #define PIXY_SYNC_BYTE 0x5a
swilkins8 0:ef0e3c67dc5b 29 #define PIXY_SYNC_BYTE_DATA 0x5b
swilkins8 0:ef0e3c67dc5b 30 #define PIXY_BUF_SIZE 16
swilkins8 0:ef0e3c67dc5b 31
swilkins8 0:ef0e3c67dc5b 32 class TPixyInterface
swilkins8 0:ef0e3c67dc5b 33 {
swilkins8 0:ef0e3c67dc5b 34 public:
swilkins8 0:ef0e3c67dc5b 35 TPixyInterface() {}
swilkins8 0:ef0e3c67dc5b 36 virtual void init() = 0;
swilkins8 0:ef0e3c67dc5b 37 virtual int8_t send(uint8_t *data, uint8_t len) = 0;
swilkins8 0:ef0e3c67dc5b 38 virtual void setArg(uint16_t arg) = 0;
swilkins8 0:ef0e3c67dc5b 39 virtual uint16_t getWord() = 0;
swilkins8 0:ef0e3c67dc5b 40 virtual uint8_t getByte() = 0;
swilkins8 0:ef0e3c67dc5b 41 };
swilkins8 0:ef0e3c67dc5b 42
swilkins8 0:ef0e3c67dc5b 43
swilkins8 0:ef0e3c67dc5b 44 template <class BufType> class CircularQ
swilkins8 0:ef0e3c67dc5b 45 {
swilkins8 0:ef0e3c67dc5b 46 public:
swilkins8 0:ef0e3c67dc5b 47 BufType buf[PIXY_BUF_SIZE];
swilkins8 0:ef0e3c67dc5b 48 uint8_t len;
swilkins8 0:ef0e3c67dc5b 49 uint8_t writeIndex;
swilkins8 0:ef0e3c67dc5b 50 uint8_t readIndex;
swilkins8 0:ef0e3c67dc5b 51
swilkins8 0:ef0e3c67dc5b 52 CircularQ();
swilkins8 0:ef0e3c67dc5b 53 bool read(BufType *c);
swilkins8 0:ef0e3c67dc5b 54 uint8_t freeLen();
swilkins8 0:ef0e3c67dc5b 55 bool write(BufType c);
swilkins8 0:ef0e3c67dc5b 56 };
swilkins8 0:ef0e3c67dc5b 57
swilkins8 0:ef0e3c67dc5b 58 class PixyInterfaceSPI : TPixyInterface
swilkins8 0:ef0e3c67dc5b 59 {
swilkins8 0:ef0e3c67dc5b 60 public:
swilkins8 0:ef0e3c67dc5b 61 SPI* spi;
swilkins8 0:ef0e3c67dc5b 62 PixyInterfaceSPI(SPI* interface);
swilkins8 0:ef0e3c67dc5b 63 virtual void init();
swilkins8 0:ef0e3c67dc5b 64 virtual uint16_t getWord();
swilkins8 0:ef0e3c67dc5b 65 virtual uint8_t getByte();
swilkins8 0:ef0e3c67dc5b 66 virtual int8_t send(uint8_t *data, uint8_t len);
swilkins8 0:ef0e3c67dc5b 67 virtual void setArg(uint16_t arg);
swilkins8 0:ef0e3c67dc5b 68
swilkins8 0:ef0e3c67dc5b 69 private:
swilkins8 0:ef0e3c67dc5b 70 // we need a little circular queues for both directions
swilkins8 0:ef0e3c67dc5b 71 CircularQ<uint8_t> outQ;
swilkins8 0:ef0e3c67dc5b 72 CircularQ<uint16_t> inQ;
swilkins8 0:ef0e3c67dc5b 73
swilkins8 0:ef0e3c67dc5b 74 uint16_t getWordHw();
swilkins8 0:ef0e3c67dc5b 75 void flushSend();
swilkins8 0:ef0e3c67dc5b 76 };
swilkins8 0:ef0e3c67dc5b 77
swilkins8 0:ef0e3c67dc5b 78
swilkins8 0:ef0e3c67dc5b 79 /*
swilkins8 0:ef0e3c67dc5b 80 class PixyInterfaceI2C : TPixyInterface
swilkins8 0:ef0e3c67dc5b 81 {
swilkins8 0:ef0e3c67dc5b 82 public:
swilkins8 0:ef0e3c67dc5b 83 SPI* spi;
swilkins8 0:ef0e3c67dc5b 84 PixyInterfaceI2C(I2C* interface);
swilkins8 0:ef0e3c67dc5b 85 virtual void init();
swilkins8 0:ef0e3c67dc5b 86 virtual uint16_t getWord();
swilkins8 0:ef0e3c67dc5b 87 virtual uint8_t getByte();
swilkins8 0:ef0e3c67dc5b 88 virtual int8_t send(uint8_t *data, uint8_t len);
swilkins8 0:ef0e3c67dc5b 89 virtual void setArg(uint16_t arg);
swilkins8 0:ef0e3c67dc5b 90
swilkins8 0:ef0e3c67dc5b 91 private:
swilkins8 0:ef0e3c67dc5b 92 // we need a little circular queues for both directions
swilkins8 0:ef0e3c67dc5b 93 CircularQ<uint8_t> outQ;
swilkins8 0:ef0e3c67dc5b 94 CircularQ<uint16_t> inQ;
swilkins8 0:ef0e3c67dc5b 95
swilkins8 0:ef0e3c67dc5b 96 uint16_t getWordHw();
swilkins8 0:ef0e3c67dc5b 97 void flushSend();
swilkins8 0:ef0e3c67dc5b 98 };*/
swilkins8 0:ef0e3c67dc5b 99
swilkins8 0:ef0e3c67dc5b 100 #endif