TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

gdisp_doubleArray.c

Go to the documentation of this file.
00001 
00042 #include <stdio.h>
00043 #include <glib.h>
00044 #include "gdisp_doubleArray.h"
00045 
00046 /*
00047  * Sets the fields of the structure to default values,
00048  * And allocate a sample buffer .
00049  */
00050 DoubleArray_T*
00051 da_newSampleArray (guint maxSamples)
00052 {
00053 
00054   DoubleArray_T *dArray = (DoubleArray_T*)NULL;
00055 
00056   dArray = (DoubleArray_T*)g_malloc0(sizeof(DoubleArray_T));
00057 
00058   dArray->nbSamples  = 0;
00059   dArray->current    = 0;
00060   dArray->first      = 0;
00061   dArray->maxSamples = maxSamples;
00062   dArray->samples    = (gdouble*)g_malloc0(maxSamples * sizeof(gdouble));
00063 
00064   return dArray;
00065 
00066 }
00067 
00068 
00069 /*
00070  * Free memory that has been allocated to this point array.
00071  */
00072 void
00073 da_freeSampleArray (DoubleArray_T* dArray)
00074 {
00075 
00076   g_free (dArray->samples);
00077   g_free (dArray);
00078 
00079 }
00080 
00081 
00082 /*
00083  * Add an array sample element.
00084  */
00085 void
00086 da_addSample (DoubleArray_T *dArray,
00087               gdouble        dValue)
00088 {
00089 
00090   dArray->samples[dArray->current] = dValue;
00091   dArray->current = (dArray->current + 1 ) % dArray->maxSamples;
00092 
00093   /*
00094    * Check end of ring buffer.
00095    */
00096   if (dArray->nbSamples != dArray->maxSamples) {
00097 
00098     dArray->nbSamples++;      
00099     dArray->first = 0;
00100 
00101   }
00102   else {
00103 
00104     dArray->first = dArray->current;
00105 
00106   }
00107 
00108 }
00109 
00110 
00111 /* 
00112  * Accessors on data.
00113  */
00114 guint
00115 da_getFirstIndex (DoubleArray_T *dArray)
00116 {
00117 
00118   return dArray->first;
00119 
00120 }
00121 
00122 guint
00123 da_getCurrentIndex (DoubleArray_T *dArray)
00124 {
00125 
00126   return dArray->current;
00127 
00128 }
00129 
00130 guint
00131 da_getNbSamples (DoubleArray_T *dArray)
00132 {
00133 
00134   return dArray->nbSamples;
00135 
00136 }
00137 
00138 
00139 /*
00140  * Get the samples available from a specific position
00141  * in this tricky circular array.
00142  */
00143 guint 
00144 da_getLeftSamplesFromPos (DoubleArray_T *dArray,
00145                           guint          index)
00146 {
00147 
00148   if (index < dArray->current) {
00149 
00150     return dArray->current - index;
00151 
00152   }
00153   if (index > dArray->current) {
00154 
00155     return dArray->current + dArray->maxSamples - index;
00156 
00157   }
00158 
00159   return dArray->nbSamples;
00160 
00161 }
00162 
00163 
00164 /*
00165  * Debug : Dump the values of the pointer.
00166  */
00167 void
00168 da_printFields (DoubleArray_T *dArray)
00169 {
00170 
00171   printf ("Structure dArray : 0x%X \n", (guint)dArray         );
00172   printf ("\t ->samples     : 0x%X \n", (guint)dArray->samples);
00173   printf ("\t ->nbSampl     : %d   \n", dArray->nbSamples     );
00174   printf ("\t ->maxSample   : %d   \n", dArray->maxSamples    );
00175   printf ("\t ->current     : %d   \n", dArray->current       );
00176   printf ("\t ->first       : %d   \n", dArray->first         );
00177 
00178 }
Framework Home Page.

Beware !! TSP wave is coming...