Analog-to-binary input with hysteresis

Dependents:   Hysteresis LAB9_Hysteresis LAB18_StreetLight

Files at this revision

API Documentation at this revision

Comitter:
kayekss
Date:
Fri Dec 20 19:21:57 2013 +0000
Commit message:
First release

Changed in this revision

HysteresisIn.cpp Show annotated file Show diff for this revision Revisions of this file
HysteresisIn.h Show annotated file Show diff for this revision Revisions of this file
--- /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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HysteresisIn.h	Fri Dec 20 19:21:57 2013 +0000
@@ -0,0 +1,26 @@
+// ==================================================== Dec 21 2013, kayeks ==
+// HysteresisIn.h
+// ===========================================================================
+// Analog to binary input class with hysteresis
+//   - For a simple Schmitt-Trigger substitute
+
+#ifndef HYSTERESIS_IN_H_
+#define HYSTERESIS_IN_H_
+
+#include "mbed.h"
+
+class HysteresisIn {
+private:
+    AnalogIn in; 
+    float thresholdHighToLow;
+    float thresholdLowToHigh;
+    bool state;
+    
+public:
+    HysteresisIn(PinName inputPin, float htl, float lth, bool initialState=0);
+    ~HysteresisIn();
+    bool read();
+    void write(bool newState);
+};
+
+#endif