Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
base.hpp
1 /*
2  Title --- keypad/base.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_KEYPAD_BASE_HPP_
23 #define _RPI_HW_KEYPAD_BASE_HPP_
24 
25 #include <functional>
26 #include <vector>
27 #include <map>
28 
29 #include <thread>
30 #include <mutex>
31 
32 #include <rpi-hw/types.hpp>
33 #include <rpi-hw/exception.hpp>
34 #include <rpi-hw/math.hpp>
35 #include <rpi-hw/time.hpp>
36 
37 #include <rpi-hw/iface/base.hpp>
38 #include <rpi-hw/iface/input.hpp>
39 
40 namespace rpihw { // Begin main namespace
41 
42 /*!
43  @namespace rpihw::keypad
44  @brief Namespace of the keypads.
45 */
46 
47 namespace keypad { // Begin keypads namespace
48 
49 // Prototypes
50 class base;
51 
52 //! The type of the keypad event listener.
53 typedef std::function< void ( keypad::base & ) > T_EventListener;
54 
55 /*!
56  @class base
57  @brief Generic keypad controller.
58 */
59 class base {
60 
61 public:
62 
63  //! The keymap type.
64  typedef std::map< uint8_t, uint8_t > T_Keymap;
65 
66  /*!
67  @brief Constructor method.
68  @param[in] total Number of the buttons.
69  @param[in] pins Sequence of `uint8_t` containing the input GPIOs.
70  */
71  base( size_t total, std::initializer_list< uint8_t > pins );
72 
73  /*!
74  @brief Constructor method.
75  @param[in] total Number of the buttons.
76  @param[in] pins Sequence of `uint8_t` containing the input GPIOs.
77  @param[in] keymap The keymap vector.
78  */
79  base( size_t total, std::initializer_list< uint8_t > pins, const std::vector< uint8_t > &keymap );
80 
81  /*!
82  @brief Constructor method.
83  @param[in] total Number of the buttons.
84  @param[in] pins Vector containing the input GPIO pins.
85  */
86  base( size_t total, const std::vector< uint8_t > &pins );
87 
88  /*!
89  @brief Constructor method.
90  @param[in] total Number of the buttons.
91  @param[in] pins Vector containing the input GPIO pins.
92  @param[in] keymap The keymap vector.
93  */
94  base( size_t total, const std::vector< uint8_t > &pins, const std::vector< uint8_t > &keymap );
95 
96  //! Destructor method.
97  virtual ~base();
98 
99  /*!
100  @brief Sets the keymap.
101  @param[in] keymap The keymap vector.
102  */
103  virtual void setKeymap( const std::vector< uint8_t > &keymap );
104 
105  /*!
106  @brief Sets the keypad event listener.
107  @param[in] listener The event listener.
108  */
109  virtual void addEventListener( T_EventListener listener );
110 
111  /*!
112  @brief Sets the frequency with which buttons are read.
113  @param[in] frequency The refresh rate in Hz.
114  */
115  virtual void setRefreshRate( float frequency );
116 
117  //! Returns the frequency with which buttons are read.
118  virtual float getRefreshRate() const;
119 
120  /*!
121  @brief Returns a button state.
122  @param[in] index The index position of the input pin.
123  @return The state of the button.
124  */
125  virtual bool state( size_t index ) const;
126 
127  /*!
128  @brief Checks if a button is pressed.
129  @param[in] index The index position of the input pin.
130  @return Return \c true if button is pressed.
131  */
132  virtual bool pressed( size_t index ) const;
133 
134  /*!
135  @brief Checks if a button is released.
136  @param[in] index The index position of the input pin.
137  @return Return \c true if button is released.
138  */
139  virtual bool released( size_t index ) const;
140 
141  //! Returns the list of button states.
142  virtual const std::vector< bool > &state() const;
143 
144  /*!
145  @brief Returns a key state.
146  @param[in] key The key button.
147  @return The state of the button.
148  */
149  virtual bool keyState( uint8_t key ) const;
150 
151  /*!
152  @brief Checks if a key is pressed.
153  @param[in] key The key button.
154  @return Return \c true if button is pressed.
155  */
156  virtual bool keyPressed( uint8_t key ) const;
157 
158  /*!
159  @brief Checks if a key is released.
160  @param[in] key The key button.
161  @return Return \c true if button is released.
162  */
163  virtual bool keyReleased( uint8_t key ) const;
164 
165  //! Returns the list of pressed keys.
166  virtual std::vector< uint8_t > keyState() const;
167 
168  //! Returns the number of keys.
169  virtual size_t numOfKeys() const;
170 
171 protected:
172 
173  //! Number of the keys.
174  size_t m_nkeys;
175 
176  //! Buttons input interface.
178 
179  //! The keymap vector.
181 
182  //! Button states (0 = up, 1 = down).
183  std::vector< bool > m_keystate;
184 
185  //! Pressed buttons (0 = none, 1 = pressed).
186  std::vector< bool > m_pressed;
187 
188  //! Pressed buttons (0 = none, 1 = released).
189  std::vector< bool > m_released;
190 
191  //! The refresh rate.
192  float m_frequency;
193 
194  //! Updating thread.
195  std::thread *m_thread;
196 
197  //! Mutex of the updating thread.
198  std::mutex *m_mutex;
199 
200  //! The keypad event listener.
202 
203  //! Updates the state of buttons (threading function).
204  virtual void update() = 0;
205 };
206 
207 } // End of keypads namespace
208 
209 } // End of main namespace
210 
211 
212 // Include inline methods
213 #include <rpi-hw/keypad/base-inl.hpp>
214 
215 #endif /* _RPI_HW_KEYPAD_BASE_HPP_ */
virtual size_t numOfKeys() const
Returns the number of keys.
Definition: base-inl.hpp:58
base(size_t total, std::initializer_list< uint8_t > pins)
Constructor method.
std::map< uint8_t, uint8_t > T_Keymap
The keymap type.
Definition: base.hpp:64
virtual bool released(size_t index) const
Checks if a button is released.
virtual void addEventListener(T_EventListener listener)
Sets the keypad event listener.
Definition: base-inl.hpp:30
std::thread * m_thread
Updating thread.
Definition: base.hpp:195
std::function< void(keypad::base &) > T_EventListener
The type of the keypad event listener.
Definition: base.hpp:50
virtual bool pressed(size_t index) const
Checks if a button is pressed.
Input interface.
Definition: input.hpp:43
std::mutex * m_mutex
Mutex of the updating thread.
Definition: base.hpp:198
virtual const std::vector< bool > & state() const
Returns the list of button states.
Definition: base-inl.hpp:51
size_t m_nkeys
Number of the keys.
Definition: base.hpp:174
virtual bool keyPressed(uint8_t key) const
Checks if a key is pressed.
Generic keypad controller.
Definition: base.hpp:59
virtual std::vector< uint8_t > keyState() const
Returns the list of pressed keys.
iface::input * m_input
Buttons input interface.
Definition: base.hpp:177
virtual bool keyReleased(uint8_t key) const
Checks if a key is released.
virtual void update()=0
Updates the state of buttons (threading function).
virtual float getRefreshRate() const
Returns the frequency with which buttons are read.
Definition: base-inl.hpp:44
float m_frequency
The refresh rate.
Definition: base.hpp:192
virtual ~base()
Destructor method.
T_EventListener m_event_listener
The keypad event listener.
Definition: base.hpp:201
virtual void setKeymap(const std::vector< uint8_t > &keymap)
Sets the keymap.
std::vector< bool > m_pressed
Pressed buttons (0 = none, 1 = pressed).
Definition: base.hpp:186
T_Keymap m_keymap
The keymap vector.
Definition: base.hpp:180
std::vector< bool > m_released
Pressed buttons (0 = none, 1 = released).
Definition: base.hpp:189
virtual void setRefreshRate(float frequency)
Sets the frequency with which buttons are read.
Definition: base-inl.hpp:37
std::vector< bool > m_keystate
Button states (0 = up, 1 = down).
Definition: base.hpp:183