changeset 0:f2ccfa3f20ad default tip

more complete version from magic rattle
author ark
date 2017-06-28 00:48 +0200
parents
children
files TiltSwitch.cpp TiltSwitch.h library.properties
diffstat 3 files changed, 162 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TiltSwitch.cpp	Wed Jun 28 00:48:53 2017 +0200
@@ -0,0 +1,72 @@
+#include "TiltSwitch.h"
+
+/**
+    see TiltSwitch.h for description.
+*/
+    
+TiltSwitch::TiltSwitch() {}
+
+
+void TiltSwitch::setup(byte pin, bool pu, float secs) {
+    this->pin = pin;
+    this->isPullup = pu;
+    this->dus = 1000000*secs;
+    pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT);
+    this->isOn = this->readState();
+    this->act = 0.0;
+}
+
+
+void TiltSwitch::update(uint32_t us) {
+    bool s = this->readState();
+
+    // calculate rate of change per secs seconds
+    if(this->isOn != s) {
+        this->ref = this->act + 1.0;
+        this->refUs = us;
+        this->act = this->ref;
+    }
+    else {
+        uint32_t dt = (us - this->refUs);
+        if(dt >= this->dus) {
+            this->act = 0.0;
+            // guard against overflow
+            this->refUs = us - this->dus;
+        } else {
+            this->act = this->ref * ((float)(this->dus-dt)/(float)this->dus);
+        }
+    }
+    this->isOn = s;
+    
+    // calculate average to determine up/down
+    // if collected MAX_SAMPLES already, simulate dropping a sample
+    if(this->num == MAX_SAMPLES) {
+        this->sum -= this->avg;
+        this->num--;
+    }
+    // add current sample
+    this->sum += (s ? 1.0 : 0.0);
+    this->num++;
+    this->avg = this->sum/this->num;
+    // if average is closer to 0 or 1 than 1/MAX_SAMPLES, meaning that it's
+    // less than one sample away from the stable bounds, truncate 
+    if(this->num == MAX_SAMPLES) {
+        float e = 1/(float)MAX_SAMPLES-.001;
+        if(this->avg < e) {
+            this->avg = 0.0;
+            this->sum = 0.0;
+        }
+        else if(this->avg > 1.0-e) {
+            this->avg = 1.0;
+            this->sum = 1.0 * MAX_SAMPLES;
+        }
+    }
+}
+
+
+bool TiltSwitch::readState() {
+    bool s = digitalRead(this->pin);
+    if(this->isPullup) s = !s;
+    return s;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TiltSwitch.h	Wed Jun 28 00:48:53 2017 +0200
@@ -0,0 +1,81 @@
+#ifndef TiltSwitch_h
+#define TiltSwitch_h
+
+#include <Arduino.h>
+
+
+/**
+    utility class for interpreting tilt switch state.
+
+    calculates intensity of movement expressed as number of state changes per
+    _secs_ seconds.
+
+    calculates up/down state as approximated by avg.
+
+
+    (c) 2017 arkadiusz@wobblyhead.net
+*/
+class TiltSwitch {
+    public: 
+        // raw switch input
+        bool isOn;
+        // movement intensity
+        float act;
+        // averaged state
+        float avg;
+
+
+        /**
+            create an uninitialised TiltSwitch.
+            setup() must be called after this to make it usable.
+        */
+        TiltSwitch();
+
+        /**
+            setup switch instance.
+            @param {byte} pin   button input pin
+            @param {bool} pu    if true, button uses internal pullup
+            @param {float} secs     seconds to average over
+        */
+        void setup(byte pin, bool pu, float secs = 1.0);
+
+        /**
+            check pin and update switch state.
+            @param {unsigned long} us   current micros() value
+        */
+        void update(unsigned long us);
+
+
+        /**
+            number of samples to average for _avg_.
+        */
+        #ifdef __AVR_ATtiny85__
+            const uint32_t MAX_SAMPLES   = 5;
+        #else
+            const uint32_t MAX_SAMPLES   = 20;
+        #endif
+
+
+    private: 
+        byte pin;
+        bool isPullup;
+
+        // act calculation
+        uint32_t dus;
+        float ref;
+        uint32_t refUs;
+
+        // avg calculation
+        float sum;
+        int num;
+
+
+        /**
+            read pin state, translate to on/off state.
+            @return {bool} true if buton is pressed
+        */
+        bool readState();
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/library.properties	Wed Jun 28 00:48:53 2017 +0200
@@ -0,0 +1,9 @@
+name=TiltSwitch
+version=1.0.0
+author=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net>
+maintainer=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net>
+sentence=Tilt switch wrapper
+paragraph=Tilt switch state interpretation
+category=Signal Input/Output
+url=https://hg.wobblyhead.net/arduino/TiltSwitch
+architectures=*