Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
button.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 18.06.2013 17:31:25 EDT
3 // File: button.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 __BUTTON_HEADER
12 #define __BUTTON_HEADER
13 
14 #include "clock.hpp"
15 
16 #include <Arduino.h>
17 #include "../ustd/type_traits.hpp"
18 #include "pin_concept.hpp"
19 
20 namespace state {
21  enum button_enum : uint8_t {
23  , pressed = 2
24  , falling = 4
25  , rising = 8
27  , auto_repeat = 16
28  };
29  uint8_t const auto_rate = 30; //auto_repeat per second
30  uint16_t const auto_delay = 500; //in millis
31 }//end namespace state
32 
33 namespace tool {
34  template<typename pin_concept, bool pressed_state = HIGH, uint16_t _auto_rate = state::auto_rate, uint16_t _auto_delay = state::auto_delay>
35  class button_class {
36  typedef uint8_t state_type;
37  public:
38  //------------------- ctors -------------------
39  button_class(): start_(0), state_(state::released), old_read_(!pressed_state) {
40  if(pressed_state)
41  pin_.mode(INPUT);
42  else
43  pin_.mode(INPUT_PULLUP);
44  }
45  template<typename T>
46  button_class(T & t): start_(0), state_(state::released), old_read_(!pressed_state), pin_(t) {
47  if(pressed_state)
48  pin_.mode(INPUT);
49  else
50  pin_.mode(INPUT_PULLUP);
51  }
52  operator bool() const {
53  return state_ == state::pressed;
54  }
55  private:
56  //------------------- update implementation -------------------
57  void update_impl(bool const & read) {
58  if(state_ & (state::falling | state::auto_repeat)) {
59  state_ = state::pressed;
60  return;
61  }
62  if(state_ & state::rising) {
63  state_ = state::released;
64  return;
65  }
66  if(state_ & state::pressed) {
67  if((tool::clock.micros() - start_press_) > (1000000.0 / _auto_rate)) {
68  start_press_ = tool::clock.micros();
70  }
71  }
72 
73  if(read != old_read_) {
74  if(start_ == 0)
75  start_ = tool::clock.micros();
76  else
77  if(tool::clock.micros() - start_ > 2000u) {
78  state_ <<= 2; //brings pressed -> rising and released -> falling
79  start_press_ = tool::clock.micros() + _auto_delay * 1000.0 - (1000000.0 / _auto_rate);
80  old_read_ = read;
81  }
82  }
83  else
84  start_ = 0;
85  }
86  public:
87  //------------------- update -------------------
88  template<typename T = void>
90  update_impl(pin_.read());
91  }
92  template<typename T = void>
93  typename ustd::enable_if<ustd::is_same<pin_concept, fake>::value, T>::type update(bool const & read) {
94  update_impl(read);
95  }
96  //------------------- getter -------------------
97  state_type state() const {
98  return state_;
99  }
100  //------------------- compare operator -------------------
101  bool operator==(state_type const & arg) {
102  return arg & state_;
103  }
104  private:
105  double start_;
106  double start_press_;
107  state_type state_;
108  bool old_read_;
109 
110  pin_concept pin_;
111  };
112 
113 }//end namespace tool
114 
115 #endif //__BUTTON_HEADER
uint16_t const auto_delay
Definition: button.hpp:30
button_class(T &t)
Definition: button.hpp:46
Definition: button.hpp:25
Definition: type_traits.hpp:228
class tool::clock_class clock
uint64_t micros() const
Definition: clock.hpp:32
ustd::enable_if<!ustd::is_same< pin_concept, fake >::value, T >::type update()
Definition: button.hpp:89
Definition: button.hpp:26
Definition: button.hpp:24
Definition: button.hpp:22
uint8_t const auto_rate
Definition: button.hpp:29
button_enum
Definition: button.hpp:21
button_class()
Definition: button.hpp:39
state_type state() const
Definition: button.hpp:97
ustd::enable_if< ustd::is_same< pin_concept, fake >::value, T >::type update(bool const &read)
Definition: button.hpp:93
Definition: button.hpp:23
bool operator==(state_type const &arg)
Definition: button.hpp:101
Definition: button.hpp:35
Definition: button.hpp:27