Mathieu Malone / Mbed 2 deprecated PixyLibrary

Dependencies:   mbed

Dependents:   PixyStereoCam

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers iserial2.h Source File

iserial2.h

00001 //--------------------------------------------------------------------------------------------
00002 //Original Property of: charmedlabs.com/pixystart -> arduino_pixy-x.y.z.zip 
00003 //
00004 //Modifications made by: Mathieu Malone
00005 //Modifications: Modified Arduino code to function with mbed development platform
00006 //Output Method: This program uses "Serial pc(USBTX, USBRX)" in order to allow communication
00007 //               between the mbed platform and putty terminal through USB
00008 //
00009 //Latest update by: Mathieu Malone
00010 //Date of last update: July 24th, 2014
00011 //--------------------------------------------------------------------------------------------
00012 //
00013 // begin license header
00014 //
00015 // This file is part of Pixy CMUcam5 or "Pixy" for short
00016 //
00017 // All Pixy source code is provided under the terms of the
00018 // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
00019 // Those wishing to use Pixy source code, software and/or
00020 // technologies under different licensing terms should contact us at
00021 // cmucam@cs.cmu.edu. Such licensing terms are available for
00022 // all portions of the Pixy codebase presented here.
00023 //
00024 // end license header
00025 //
00026 
00027 #ifndef _ISERIAL_H2
00028 #define _ISERIAL_H2
00029 
00030 typedef uint32_t (*SerialCallback2)(uint8_t *data2, uint32_t len2); 
00031 
00032 // circular queue, for receiving data
00033 template <class BufType2> class ReceiveQ2
00034 {
00035 public:
00036     ReceiveQ2(uint32_t size2)
00037     {
00038         m_size2 = size2;
00039         m_buf2 = new BufType2[m_size2];
00040         m_read2 = 0;
00041         m_write2 = 0;
00042         m_produced2 = 0;
00043         m_consumed2 = 0;
00044     }
00045 
00046     ~ReceiveQ2()
00047     {
00048         delete [] m_buf2;
00049     }
00050 
00051     inline int32_t receiveLen2()
00052     {
00053         return m_produced2 - m_consumed2;
00054     }
00055 
00056     inline int32_t freeLen2()
00057     {
00058         return m_size2 - receiveLen2();
00059     }
00060 
00061     inline int read(BufType2 *data2)
00062     {
00063         if (receiveLen2()<=0)
00064             return 0;
00065         *data2 = m_buf2[m_read2++];
00066         m_consumed2++;
00067 
00068         if (m_read2==m_size2)
00069             m_read2 = 0;
00070 
00071         return 1;
00072     }
00073 
00074     inline int write(BufType2 data2)
00075     {
00076         if (freeLen2()<=0)
00077             return 0; 
00078 
00079         m_buf2[m_write2++] = data2;
00080         m_produced2++;
00081 
00082         if (m_write2==m_size2)
00083             m_write2 = 0;
00084 
00085         return 1;
00086     }
00087 
00088     uint32_t m_size2;
00089     BufType2 *m_buf2;
00090     uint32_t m_read2;
00091     uint32_t m_write2;
00092     uint32_t m_produced2;
00093     uint32_t m_consumed2;
00094 };
00095 
00096 
00097 // linear queue, to buffer a chunk and dispense it out
00098 template <class BufType2> class TransmitQ2
00099 {
00100 public:
00101     TransmitQ2(uint32_t size2, SerialCallback2 callback2)
00102     {
00103         m_size2 = size2;
00104         m_buf2 = new BufType1[m_size1];
00105         m_read2 = 0;
00106         m_len2 = 0;
00107         m_callback2= callback2;
00108     }
00109 
00110     ~TransmitQ2()
00111     {
00112         delete [] m_buf2;
00113     }
00114 
00115     int read(BufType2 *data2)
00116     {
00117         if (m_len2==0)
00118         {
00119             m_len2 = (*m_callback2)((uint8_t *)m_buf2, m_size2*sizeof(BufType2))/sizeof(BufType2);
00120             if (m_len2==0)
00121                 return 0;
00122             m_read2 = 0;
00123         }
00124         *data2 = m_buf2[m_read2++];
00125         m_len2--;
00126 
00127         return 1;
00128     }
00129 
00130     uint32_t m_size2;
00131     BufType2 *m_buf2;
00132     uint32_t m_read2;
00133     uint32_t m_len2;
00134     SerialCallback2 m_callback2;
00135 };
00136 
00137 // virtual interface to a serial device
00138 class Iserial2
00139 {
00140 public:
00141     virtual int open()
00142     {
00143         return 0;
00144     }
00145     virtual int close()
00146     {
00147         return 0;
00148     }
00149     virtual int receive(uint8_t *buf2, uint32_t len2)
00150     {
00151         return 0;
00152     }
00153     virtual int receiveLen2()
00154     {
00155         return 0;
00156     }
00157     virtual int update2()
00158     {
00159         return 0;
00160     }
00161 };
00162 
00163 #endif