Mathieu Malone / Mbed 2 deprecated PixyLibrary

Dependencies:   mbed

Dependents:   PixyStereoCam

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers iserial1.h Source File

iserial1.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_H1
00028 #define _ISERIAL_H1
00029 
00030 typedef uint32_t (*SerialCallback1)(uint8_t *data1, uint32_t len1); 
00031 
00032 // circular queue, for receiving data
00033 template <class BufType1> class ReceiveQ1
00034 {
00035 public:
00036     ReceiveQ1(uint32_t size1)
00037     {
00038         m_size1 = size1;
00039         m_buf1 = new BufType1[m_size1];
00040         m_read1 = 0;
00041         m_write1 = 0;
00042         m_produced1 = 0;
00043         m_consumed1 = 0;
00044     }
00045 
00046     ~ReceiveQ1()
00047     {
00048         delete [] m_buf1;
00049     }
00050 
00051     inline int32_t receiveLen1()
00052     {
00053         return m_produced1 - m_consumed1;
00054     }
00055 
00056     inline int32_t freeLen1()
00057     {
00058         return m_size1 - receiveLen1();
00059     }
00060 
00061     inline int read(BufType1 *data1)
00062     {
00063         if (receiveLen1()<=0)
00064             return 0;
00065         *data1 = m_buf1[m_read1++];
00066         m_consumed1++;
00067 
00068         if (m_read1==m_size1)
00069             m_read1 = 0;
00070 
00071         return 1;
00072     }
00073 
00074     inline int write(BufType1 data1)
00075     {
00076         if (freeLen1()<=0)
00077             return 0; 
00078 
00079         m_buf1[m_write1++] = data1;
00080         m_produced1++;
00081 
00082         if (m_write1==m_size1)
00083             m_write1 = 0;
00084 
00085         return 1;
00086     }
00087 
00088     uint32_t m_size1;
00089     BufType1 *m_buf1;
00090     uint32_t m_read1;
00091     uint32_t m_write1;
00092     uint32_t m_produced1;
00093     uint32_t m_consumed1;
00094 };
00095 
00096 
00097 // linear queue, to buffer a chunk and dispense it out
00098 template <class BufType1> class TransmitQ1
00099 {
00100 public:
00101     TransmitQ1(uint32_t size1, SerialCallback1 callback1)
00102     {
00103         m_size1 = size1;
00104         m_buf1 = new BufType1[m_size1];
00105         m_read1 = 0;
00106         m_len1 = 0;
00107         m_callback1 = callback1;
00108     }
00109 
00110     ~TransmitQ1()
00111     {
00112         delete [] m_buf1;
00113     }
00114 
00115     int read(BufType1 *data1)
00116     {
00117         if (m_len1==0)
00118         {
00119             m_len1 = (*m_callback1)((uint8_t *)m_buf1, m_size1*sizeof(BufType1))/sizeof(BufType1);
00120             if (m_len1==0)
00121                 return 0;
00122             m_read1 = 0;
00123         }
00124         *data1 = m_buf1[m_read1++];
00125         m_len1--;
00126 
00127         return 1;
00128     }
00129 
00130     uint32_t m_size1;
00131     BufType1 *m_buf1;
00132     uint32_t m_read1;
00133     uint32_t m_len1;
00134     SerialCallback1 m_callback1;
00135 };
00136 
00137 // virtual interface to a serial device
00138 class Iserial1
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 *buf1, uint32_t len1)
00150     {
00151         return 0;
00152     }
00153     virtual int receiveLen1()
00154     {
00155         return 0;
00156     }
00157     virtual int update1()
00158     {
00159         return 0;
00160     }
00161 };
00162 
00163 #endif