Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hysteresis.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 23.07.2013 05:09:54 EDT
3 // File: hysteresis.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __HYSTERESIS_HEADER
12 #define __HYSTERESIS_HEADER
13 
14 #include "realization.hpp"
15 #include "../../util/mean_trait.hpp"
16 
17 #ifndef Arduino_h
18  #include <cmath>
19  using std::min;
20  using std::max;
21 #endif
22 
23 namespace ustd {
24  template<typename T, unsigned W, typename _base = identity_filter<T> >
25  class hysteresis_filter: public _base {
26  typedef _base base;
27  public:
28  //------------------- ctors -------------------
29  hysteresis_filter(): val_() {
30  }
31  hysteresis_filter(hysteresis_filter const & arg): val_(arg.val_) {
32  }
33  hysteresis_filter(T const & arg): val_(arg) {
34  }
35  //------------------- ops -------------------
36  hysteresis_filter & operator=(T const & in) {
37  base::operator=(in);
38  val_ = base::value();
39  return (*this);
40  }
41  hysteresis_filter & operator<<(T const & in) {
42  base::operator<<(in);
43 
44  T temp = base::value();
45 
46  if(temp > val_ + (T)W)
47  val_ = temp - W;
48  else
49  if(temp + (T)W < val_)
50  val_ = temp + W;
51 
52  return (*this);
53  }
54  //------------------- converter -------------------
55  T value() const {
56  return val_;
57  }
58  operator T() const {
59  return value();
60  }
61  private:
62  T val_;
63  };
64  //------------------- spez for W == 0 -------------------
65  EMPTY_FILTER(hysteresis, 0)
66  //------------------- realization for filter -------------------
67  template<typename T, typename _base, unsigned W>
68  struct realization<T, tag::hysteresis<W>, _base> {
70  };
71 }//end namespace ustd
72 #endif //__HYSTERESIS_HEADER
Definition: hysteresis.hpp:25
S & operator<<(S &os, oss_class< D, max_buf > const &arg)
Definition: serializer.hpp:158
hysteresis_filter()
Definition: hysteresis.hpp:29
hysteresis_filter & operator=(T const &in)
Definition: hysteresis.hpp:36
#define EMPTY_FILTER(NAME, VALUE)
Definition: realization.hpp:22
hysteresis_filter< T, W, _base > type
Definition: hysteresis.hpp:69
T value() const
Definition: hysteresis.hpp:55
Definition: realization.hpp:19
hysteresis_filter(T const &arg)
Definition: hysteresis.hpp:33
hysteresis_filter & operator<<(T const &in)
Definition: hysteresis.hpp:41
hysteresis_filter(hysteresis_filter const &arg)
Definition: hysteresis.hpp:31