This lib is supposed to be used as a sensor's calibration or control program. This makes Cubic Spline Model from some sample plots(sets of (value, voltage)), and then discretize the model (dividing the range of voltage into some steps) in order to use the calibrated model data without getting the INVERSE function.

Revision:
20:0453919a76b2
Parent:
18:b046bba4ffa7
Child:
21:cd20537290f9
--- a/TRP105F_Spline.cpp	Thu Jun 30 15:23:41 2016 +0000
+++ b/TRP105F_Spline.cpp	Thu Jun 30 16:45:30 2016 +0000
@@ -34,6 +34,7 @@
     :_useType(AsMODULE)
     ,_ai(AnalogIn(DEFALT_AI_PIN))
 {
+    _snum = 0;
     _Sample_Num = 5;
     _Sample_Set = (VDset *)malloc(_Sample_Num * sizeof(VDset));
     _u_spline   = (double*)malloc(_Sample_Num * sizeof(double));
@@ -50,6 +51,7 @@
     :_useType(AsMODULE)
     ,_ai(AnalogIn(DEFALT_AI_PIN))
 {
+    _snum = 0;
     if(arg_num > _ENUM) _Sample_Num = _ENUM;
     else _Sample_Num = arg_num;
 
@@ -69,6 +71,7 @@
     :_useType(arg_type)
     ,_ai(AnalogIn(DEFALT_AI_PIN))
 {
+    _snum = 0;
     if(arg_num > _ENUM) _Sample_Num = _ENUM;
     else _Sample_Num = arg_num;
 
@@ -89,6 +92,7 @@
     :_ai(AnalogIn(pin))
 
 {
+    _snum = 0;
     if(arg_num > _ENUM) _Sample_Num = _ENUM;
     else _Sample_Num = arg_num;
 
@@ -109,6 +113,7 @@
     :_useType(arg_type)
     ,_ai(AnalogIn(pin))
 {
+    _snum = 0;
     if(arg_num > _ENUM) _Sample_Num = _ENUM;
     else _Sample_Num = arg_num;
 
@@ -189,35 +194,34 @@
 
 void TRP105FS::_setSample(unsigned short arg_x, unsigned short arg_y)
 {
-    static unsigned int snum = 0;
     unsigned int num;
     int     tmp;
     VDset tmp_set[_ENUM];   // for bucket sort
 
     // Increment it if this function called.
-    snum++;
+    _snum++;
 #ifdef DEBUG
-    g_Serial_Signal.printf("snum       : %d\n", snum);
+    g_Serial_Signal.printf("_snum       : %d\n", _snum);
     g_Serial_Signal.printf("(%d,%d)\n",arg_x, arg_y);
 #endif
 
     //  fit to smaller
-    if (snum < _Sample_Num) {
-        num = snum;
+    if (_snum < _Sample_Num) {
+        num = _snum;
     } else {
-        //  To reclloc memories if snum is bigger than _Sample_Num.
-        //  When realloc is failed, snum is back to porevius.
-        VDset*  tmp_Set = (VDset *)realloc(_Sample_Set, snum * sizeof(VDset));
-        double* tmp__u_ = (double*)realloc(_u_spline,   snum * sizeof(double));
-        if (tmp_set != NULL && tmp__u_ != NULL) {
+        //  To reclloc memories if _snum is bigger than _Sample_Num.
+        //  When realloc is failed, _snum is back to porevius.
+        VDset*  tmp_Set = (VDset *)realloc(_Sample_Set, _snum * sizeof(VDset));
+        double* tmp__u_ = (double*)realloc(_u_spline,   _snum * sizeof(double));
+        if (tmp_Set != NULL && tmp__u_ != NULL) {
             _Sample_Set = tmp_Set;
             _u_spline   = tmp__u_;
-            num = _Sample_Num = snum;
+            num = _Sample_Num = _snum;
         } else {
-            snum--;
-            num = snum = _Sample_Num ;
+            _snum--;
+            num = _snum = _Sample_Num ;
 #ifdef DEBUG
-            g_Serial_Signal.printf("failed to realloc\n", snum);
+            g_Serial_Signal.printf("failed to realloc\n", _snum);
 #endif
         }
     }
@@ -251,7 +255,7 @@
 #endif
     //  substruct tmp from number of sample.
     _Sample_Num -= tmp;
-    snum -= tmp;
+    _snum -= tmp;
 #ifdef DEBUG
     g_Serial_Signal.printf("-----------------\n");
     g_Serial_Signal.printf("  _Sample_num: %d\n", _Sample_Num );