Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
gpio.hpp
1 /*
2  Title --- gpio.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_GPIO_HPP_
23 #define _RPI_HW_GPIO_HPP_
24 
25 #include <vector>
26 
27 #include <rpi-hw/rpi.hpp>
28 
29 #include <rpi-hw/types.hpp>
30 #include <rpi-hw/exception.hpp>
31 #include <rpi-hw/utils.hpp>
32 
33 #include <rpi-hw/driver/bcm2835.hpp>
34 #include <rpi-hw/driver/io-expander.hpp>
35 
36 #define RESERVED_PINS 30
37 
38 namespace rpihw { // Begin main namespace
39 
40 /*!
41  @class expander_slot
42  @brief Slot to store the I/O expander.
43 */
44 struct expander_slot {
45 
46  //! Constructor method.
47  expander_slot( uint8_t b, driver::io_expander *e ) : pin_base(b), expander(e) {}
48 
49  //! The pin base for the I/O expander.
50  int pin_base;
51 
52  //! Pointer to the I/O expander.
54 };
55 
56 
57 /*!
58  @class gpio
59  @brief Virtual GPIO controller.
60 
61  @example blink.cpp
62 */
63 class gpio {
64 
65 public:
66 
67  //! Returns the singleton instance.
68  static gpio &get();
69 
70  //! Destructor method.
71  virtual ~gpio();
72 
73  /*!
74  @brief Adds a I/O expander to the standard GPIO connector.
75  @param[in] pin_base The user-defined pin base for the I/O expander.
76  @param[in] expander The I/O expander.
77  */
78  template < typename T >
79  void expand( uint8_t pin_base, T &expander );
80 
81  /*!
82  @brief Sets the mode of a GPIO pin.
83  @param[in] pin The GPIO pin.
84  @param[in] mode The GPIO mode.
85  @param[in] pull_mode The pull resistor mode.
86  */
87  void setup( uint8_t pin, uint8_t mode, uint8_t pull_mode = PULL_OFF );
88 
89  /*!
90  @brief Sets the value of a output pin.
91  @param[in] pin The output pin.
92  @param[in] value The value of output pin.
93  */
94  void write( uint8_t pin, bool value );
95 
96  /*!
97  @brief Returns the value of a input pin.
98  @param[in] pin The input pin.
99  @return The value of input pin.
100  */
101  bool read( uint8_t pin );
102 
103  /*!
104  @brief Returns the event state of a GPIO pin.
105  @param[in] pin The GPIO pin.
106  @return The event state.
107  */
108  bool checkEvent( uint8_t pin );
109 
110  /*!
111  @brief Enables/disables the rising edge event on a GPIO pin.
112  @param[in] pin The GPIO pin.
113  @param[in] enabled If \c true enable the rising edge event.
114  */
115  void setRisingEvent( uint8_t pin, bool enabled );
116 
117  /*!
118  @brief Enables/disables the falling edge event on a GPIO pin.
119  @param[in] pin The GPIO pin.
120  @param[in] enabled If \c true enable the falling edge event.
121  */
122  void setFallingEvent( uint8_t pin, bool enabled );
123 
124  /*!
125  @brief Enables/disables the high event on a GPIO pin.
126  @param[in] pin The GPIO pin.
127  @param[in] enabled If \c true enable the high event.
128  */
129  void setHighEvent( uint8_t pin, bool enabled );
130 
131  /*!
132  @brief Enables/disables the low event on a GPIO pin.
133  @param[in] pin The GPIO pin.
134  @param[in] enabled If \c true enable the low event.
135  */
136  void setLowEvent( uint8_t pin, bool enabled );
137 
138  /*!
139  @brief Enables/disables the pull-up/down control on a GPIO pin.
140  @param[in] pin The GPIO pin.
141  @param[in] mode The pull resistor mode.
142  */
143  void setPullUpDown( uint8_t pin, uint8_t mode );
144 
145 private:
146 
147  //! Broadcom BCM2835 controller.
149 
150  //! List of I/O expanders.
151  std::vector< expander_slot > m_expanders;
152 
153  /*!
154  @brief Finds the I/O expander with a specific pin index.
155  @param[in] pin The GPIO pin.
156  @return A pointer to the I/O expander.
157  */
158  expander_slot &findExpander( uint8_t pin );
159 
160  //! Constructor method.
161  gpio();
162 
163  //! Disables the copy constructor.
164  gpio( const gpio &other ) = delete;
165 
166  //! Disables the copy constructor.
167  void operator=( const gpio &other ) = delete;
168 };
169 
170 } // End of main namespace
171 
172 
173 // Include inline methods
174 #include <rpi-hw/gpio-inl.hpp>
175 
176 #endif /* _RPI_HW_GPIO_HPP_ */
void setHighEvent(uint8_t pin, bool enabled)
Enables/disables the high event on a GPIO pin.
void setPullUpDown(uint8_t pin, uint8_t mode)
Enables/disables the pull-up/down control on a GPIO pin.
driver::io_expander * expander
Pointer to the I/O expander.
Definition: gpio.hpp:53
bool read(uint8_t pin)
Returns the value of a input pin.
void operator=(const gpio &other)=delete
Disables the copy constructor.
bool checkEvent(uint8_t pin)
Returns the event state of a GPIO pin.
std::vector< expander_slot > m_expanders
List of I/O expanders.
Definition: gpio.hpp:151
void expand(uint8_t pin_base, T &expander)
Adds a I/O expander to the standard GPIO connector.
Definition: gpio-inl.hpp:29
expander_slot(uint8_t b, driver::io_expander *e)
Constructor method.
Definition: gpio.hpp:47
void setLowEvent(uint8_t pin, bool enabled)
Enables/disables the low event on a GPIO pin.
driver::bcm2835 * m_bcm2835
Broadcom BCM2835 controller.
Definition: gpio.hpp:148
gpio()
Constructor method.
int pin_base
The pin base for the I/O expander.
Definition: gpio.hpp:50
void setup(uint8_t pin, uint8_t mode, uint8_t pull_mode=PULL_OFF)
Sets the mode of a GPIO pin.
Virtual GPIO controller.
Definition: gpio.hpp:63
expander_slot & findExpander(uint8_t pin)
Finds the I/O expander with a specific pin index.
virtual ~gpio()
Destructor method.
void setRisingEvent(uint8_t pin, bool enabled)
Enables/disables the rising edge event on a GPIO pin.
Slot to store the I/O expander.
Definition: gpio.hpp:44
void setFallingEvent(uint8_t pin, bool enabled)
Enables/disables the falling edge event on a GPIO pin.
Broadcom BCM2835 controller.
Definition: bcm2835.hpp:59
void write(uint8_t pin, bool value)
Sets the value of a output pin.