same

Dependents:   Nucleo_motors

Fork of PixyLibrary by Mathieu Malone

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?

UserRevisionLine numberNew 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 #ifndef _ISERIAL_H2
MBM 0:56a3009221d3 28 #define _ISERIAL_H2
MBM 0:56a3009221d3 29
MBM 0:56a3009221d3 30 typedef uint32_t (*SerialCallback2)(uint8_t *data2, uint32_t len2);
MBM 0:56a3009221d3 31
MBM 0:56a3009221d3 32 // circular queue, for receiving data
MBM 0:56a3009221d3 33 template <class BufType2> class ReceiveQ2
MBM 0:56a3009221d3 34 {
MBM 0:56a3009221d3 35 public:
MBM 0:56a3009221d3 36 ReceiveQ2(uint32_t size2)
MBM 0:56a3009221d3 37 {
MBM 0:56a3009221d3 38 m_size2 = size2;
MBM 0:56a3009221d3 39 m_buf2 = new BufType2[m_size2];
MBM 0:56a3009221d3 40 m_read2 = 0;
MBM 0:56a3009221d3 41 m_write2 = 0;
MBM 0:56a3009221d3 42 m_produced2 = 0;
MBM 0:56a3009221d3 43 m_consumed2 = 0;
MBM 0:56a3009221d3 44 }
MBM 0:56a3009221d3 45
MBM 0:56a3009221d3 46 ~ReceiveQ2()
MBM 0:56a3009221d3 47 {
MBM 0:56a3009221d3 48 delete [] m_buf2;
MBM 0:56a3009221d3 49 }
MBM 0:56a3009221d3 50
MBM 0:56a3009221d3 51 inline int32_t receiveLen2()
MBM 0:56a3009221d3 52 {
MBM 0:56a3009221d3 53 return m_produced2 - m_consumed2;
MBM 0:56a3009221d3 54 }
MBM 0:56a3009221d3 55
MBM 0:56a3009221d3 56 inline int32_t freeLen2()
MBM 0:56a3009221d3 57 {
MBM 0:56a3009221d3 58 return m_size2 - receiveLen2();
MBM 0:56a3009221d3 59 }
MBM 0:56a3009221d3 60
MBM 0:56a3009221d3 61 inline int read(BufType2 *data2)
MBM 0:56a3009221d3 62 {
MBM 0:56a3009221d3 63 if (receiveLen2()<=0)
MBM 0:56a3009221d3 64 return 0;
MBM 0:56a3009221d3 65 *data2 = m_buf2[m_read2++];
MBM 0:56a3009221d3 66 m_consumed2++;
MBM 0:56a3009221d3 67
MBM 0:56a3009221d3 68 if (m_read2==m_size2)
MBM 0:56a3009221d3 69 m_read2 = 0;
MBM 0:56a3009221d3 70
MBM 0:56a3009221d3 71 return 1;
MBM 0:56a3009221d3 72 }
MBM 0:56a3009221d3 73
MBM 0:56a3009221d3 74 inline int write(BufType2 data2)
MBM 0:56a3009221d3 75 {
MBM 0:56a3009221d3 76 if (freeLen2()<=0)
MBM 0:56a3009221d3 77 return 0;
MBM 0:56a3009221d3 78
MBM 0:56a3009221d3 79 m_buf2[m_write2++] = data2;
MBM 0:56a3009221d3 80 m_produced2++;
MBM 0:56a3009221d3 81
MBM 0:56a3009221d3 82 if (m_write2==m_size2)
MBM 0:56a3009221d3 83 m_write2 = 0;
MBM 0:56a3009221d3 84
MBM 0:56a3009221d3 85 return 1;
MBM 0:56a3009221d3 86 }
MBM 0:56a3009221d3 87
MBM 0:56a3009221d3 88 uint32_t m_size2;
MBM 0:56a3009221d3 89 BufType2 *m_buf2;
MBM 0:56a3009221d3 90 uint32_t m_read2;
MBM 0:56a3009221d3 91 uint32_t m_write2;
MBM 0:56a3009221d3 92 uint32_t m_produced2;
MBM 0:56a3009221d3 93 uint32_t m_consumed2;
MBM 0:56a3009221d3 94 };
MBM 0:56a3009221d3 95
MBM 0:56a3009221d3 96
MBM 0:56a3009221d3 97 // linear queue, to buffer a chunk and dispense it out
MBM 0:56a3009221d3 98 template <class BufType2> class TransmitQ2
MBM 0:56a3009221d3 99 {
MBM 0:56a3009221d3 100 public:
MBM 0:56a3009221d3 101 TransmitQ2(uint32_t size2, SerialCallback2 callback2)
MBM 0:56a3009221d3 102 {
MBM 0:56a3009221d3 103 m_size2 = size2;
MBM 0:56a3009221d3 104 m_buf2 = new BufType1[m_size1];
MBM 0:56a3009221d3 105 m_read2 = 0;
MBM 0:56a3009221d3 106 m_len2 = 0;
MBM 0:56a3009221d3 107 m_callback2= callback2;
MBM 0:56a3009221d3 108 }
MBM 0:56a3009221d3 109
MBM 0:56a3009221d3 110 ~TransmitQ2()
MBM 0:56a3009221d3 111 {
MBM 0:56a3009221d3 112 delete [] m_buf2;
MBM 0:56a3009221d3 113 }
MBM 0:56a3009221d3 114
MBM 0:56a3009221d3 115 int read(BufType2 *data2)
MBM 0:56a3009221d3 116 {
MBM 0:56a3009221d3 117 if (m_len2==0)
MBM 0:56a3009221d3 118 {
MBM 0:56a3009221d3 119 m_len2 = (*m_callback2)((uint8_t *)m_buf2, m_size2*sizeof(BufType2))/sizeof(BufType2);
MBM 0:56a3009221d3 120 if (m_len2==0)
MBM 0:56a3009221d3 121 return 0;
MBM 0:56a3009221d3 122 m_read2 = 0;
MBM 0:56a3009221d3 123 }
MBM 0:56a3009221d3 124 *data2 = m_buf2[m_read2++];
MBM 0:56a3009221d3 125 m_len2--;
MBM 0:56a3009221d3 126
MBM 0:56a3009221d3 127 return 1;
MBM 0:56a3009221d3 128 }
MBM 0:56a3009221d3 129
MBM 0:56a3009221d3 130 uint32_t m_size2;
MBM 0:56a3009221d3 131 BufType2 *m_buf2;
MBM 0:56a3009221d3 132 uint32_t m_read2;
MBM 0:56a3009221d3 133 uint32_t m_len2;
MBM 0:56a3009221d3 134 SerialCallback2 m_callback2;
MBM 0:56a3009221d3 135 };
MBM 0:56a3009221d3 136
MBM 0:56a3009221d3 137 // virtual interface to a serial device
MBM 0:56a3009221d3 138 class Iserial2
MBM 0:56a3009221d3 139 {
MBM 0:56a3009221d3 140 public:
MBM 0:56a3009221d3 141 virtual int open()
MBM 0:56a3009221d3 142 {
MBM 0:56a3009221d3 143 return 0;
MBM 0:56a3009221d3 144 }
MBM 0:56a3009221d3 145 virtual int close()
MBM 0:56a3009221d3 146 {
MBM 0:56a3009221d3 147 return 0;
MBM 0:56a3009221d3 148 }
MBM 0:56a3009221d3 149 virtual int receive(uint8_t *buf2, uint32_t len2)
MBM 0:56a3009221d3 150 {
MBM 0:56a3009221d3 151 return 0;
MBM 0:56a3009221d3 152 }
MBM 0:56a3009221d3 153 virtual int receiveLen2()
MBM 0:56a3009221d3 154 {
MBM 0:56a3009221d3 155 return 0;
MBM 0:56a3009221d3 156 }
MBM 0:56a3009221d3 157 virtual int update2()
MBM 0:56a3009221d3 158 {
MBM 0:56a3009221d3 159 return 0;
MBM 0:56a3009221d3 160 }
MBM 0:56a3009221d3 161 };
MBM 0:56a3009221d3 162
MBM 0:56a3009221d3 163 #endif