Analog-to-binary input with hysteresis

Dependents:   Hysteresis LAB9_Hysteresis LAB18_StreetLight

Revision:
0:fe43537bd3d6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HysteresisIn.cpp	Fri Dec 20 19:21:57 2013 +0000
@@ -0,0 +1,38 @@
+// ==================================================== Dec 21 2013, kayeks ==
+// HysteresisIn.cpp
+// ===========================================================================
+// Analog to binary input class with hysteresis
+//   - For a simple Schmitt-Trigger substitute
+
+#include "HysteresisIn.h"
+
+/** Constructor of class HysteresisIn */
+HysteresisIn::HysteresisIn(PinName inputPin, float htl, float lth,
+                           bool initialState)
+:
+    in(inputPin)
+{
+    this->state = initialState;
+    this->thresholdHighToLow = htl;
+    this->thresholdLowToHigh = lth;
+}
+
+/** Destructor of class HysteresisIn */
+HysteresisIn::~HysteresisIn() {
+}
+
+/* Read once and decide an output */
+bool HysteresisIn::read() {
+    float val = in;
+    if (this->state && val < this->thresholdHighToLow) {
+        this->state = 0;
+    } else if (!this->state && val > this->thresholdLowToHigh) {
+        this->state = 1;
+    }
+    return this->state;
+}
+
+/* Forcibly set current state */
+void HysteresisIn::write(bool newState) {
+    this->state = newState;
+}