A capacitive touch sensor using a analog input port.

Dependents:   TouchSenseTestProgram

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Thu Aug 15 04:22:11 2013 +0000
Parent:
0:5a0704efe081
Commit message:
Changed the calculation algorithm.

Changed in this revision

TouchSense.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5a0704efe081 -r 08658b9787d8 TouchSense.h
--- a/TouchSense.h	Wed Aug 14 09:08:59 2013 +0000
+++ b/TouchSense.h	Thu Aug 15 04:22:11 2013 +0000
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 #ifndef TOUCHSENSE_H
 #define TOUCHSENSE_H
 
@@ -21,7 +21,8 @@
 #include "gpio_api.h"
 #include "analogin_api.h"
 
-namespace mbed {
+namespace mbed
+{
 
 /**
  * A capacitive touch sensor using analog input port.
@@ -45,42 +46,60 @@
  * }
  * @endcode
  */
-class TouchSense {
+class TouchSense
+{
 
 public:
     /**
      * Create a TouchSense connected to the specified pin
      *
      * @param pin TouchSense pin to connect to
+     * @param thr Threshold (1 to 20)
      */
-    TouchSense(PinName pin) : mypin(pin), calval(0) {
+    TouchSense(PinName pin, int thr = 10) : mypin(pin), calval(0) {
+        for (int i = 0; i < DATA_COUNT; i++) {
+            data[i] = 0;
+        }
+        if (thr < THRMIN) {
+            thr = THRMIN;
+        }
+        if (thr > THRMAX) {
+            thr = THRMAX;
+        }
+        threshold = thr;    
+        execute();
     }
-    
+
     void calibration() {
-        uint16_t f1, f2, f3, f4;
-        f1 = execute();
-        f2 = execute();
-        f3 = execute();
-        f4 = execute();
-        calval = (f1 + f2 + f3 + f4) / 4.0;
+        uint32_t r = 0;
+        for (int i = 0; i < DATA_COUNT; i++) {
+            r += execute();
+            wait_us(100);
+        }
+        r = r / DATA_COUNT;
+        calval = r;
     }
-    
+
     bool sense() {
-        uint16_t f = execute();        
-        return (calval * 3.00) < f;
+        data[index] = execute();
+        index = (index + 1) % DATA_COUNT;
+        uint32_t r = 0;
+        for (int i = 0; i < DATA_COUNT; i++) {
+            r += data[i];
+        }
+        r = r / DATA_COUNT;
+        return (calval * threshold) < r;
     }
 
 private:
 
     uint16_t execute() {
+        uint16_t r;
+        analogin_init(&adc, mypin);
+        r = analogin_read_u16(&adc) >> 4;
         gpio_init(&gpio, mypin, PIN_OUTPUT);
         gpio_write(&gpio, 1);
-        __nop();
-        __nop();
-        __nop();
-        __nop();
-        analogin_init(&adc, mypin);
-        return analogin_read_u16(&adc);
+        return r;
     }
 
 protected:
@@ -88,6 +107,12 @@
     uint16_t calval;
     gpio_t gpio;
     analogin_t adc;
+    static const int DATA_COUNT = 8;
+    uint32_t data[DATA_COUNT];
+    int index;
+    int threshold;
+    static const int THRMIN = 1;
+    static const int THRMAX = 20;
 };
 
 } // namespace mbed