Mistake on this page?
Report an issue in GitHub or email us
DigitalIn.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2020 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_DIGITALIN_H
18 #define MBED_DIGITALIN_H
19 
20 #include "platform/platform.h"
21 
22 #include "interfaces/InterfaceDigitalIn.h"
23 #include "hal/gpio_api.h"
24 
25 namespace mbed {
26 
27 /**
28  * \defgroup drivers_DigitalIn DigitalIn class
29  * \ingroup drivers-public-api-gpio
30  * @{
31  */
32 
33 /** A digital input, used for reading the state of a pin
34  *
35  * @note Synchronization level: Interrupt safe
36  *
37  * Example:
38  * @code
39  * // Flash an LED while a DigitalIn is true
40  *
41  * #include "mbed.h"
42  *
43  * DigitalIn enable(p5);
44  * DigitalOut led(LED1);
45  *
46  * int main() {
47  * while(1) {
48  * if(enable) {
49  * led = !led;
50  * }
51  * ThisThread::sleep_for(250);
52  * }
53  * }
54  * @endcode
55  */
56 class DigitalIn
57 #ifdef FEATURE_EXPERIMENTAL_API
58  final : public interface::DigitalIn
59 #endif
60 {
61 
62 public:
63  /** Create a DigitalIn connected to the specified pin
64  *
65  * @param pin DigitalIn pin to connect to
66  */
67  DigitalIn(PinName pin) : gpio()
68  {
69  // No lock needed in the constructor
70  gpio_init_in(&gpio, pin);
71  }
72 
73  /** Create a DigitalIn connected to the specified pin
74  *
75  * @param pin DigitalIn pin to connect to
76  * @param mode the initial mode of the pin
77  */
78  DigitalIn(PinName pin, PinMode mode) : gpio()
79  {
80  // No lock needed in the constructor
81  gpio_init_in_ex(&gpio, pin, mode);
82  }
83 
84  /** Class destructor, deinitialize the pin
85  */
87  {
88  gpio_free(&gpio);
89  }
90 
91  /** Read the input, represented as 0 or 1 (int)
92  *
93  * @returns
94  * An integer representing the state of the input pin,
95  * 0 for logical 0, 1 for logical 1
96  */
97  int read()
98  {
99  // Thread safe / atomic HAL call
100  return gpio_read(&gpio);
101  }
102 
103  /** Set the input pin mode
104  *
105  * @param pull PullUp, PullDown, PullNone, OpenDrain
106  */
107  void mode(PinMode pull);
108  /** Return the output setting, represented as 0 or 1 (int)
109  *
110  * @returns
111  * Non zero value if pin is connected to uc GPIO
112  * 0 if gpio object was initialized with NC
113  */
115  {
116  // Thread safe / atomic HAL call
117  return gpio_is_connected(&gpio);
118  }
119 
120  /** An operator shorthand for read()
121  * \sa DigitalIn::read()
122  * @code
123  * DigitalIn button(BUTTON1);
124  * DigitalOut led(LED1);
125  * led = button; // Equivalent to led.write(button.read())
126  * @endcode
127  */
128  operator int()
129  {
130  // Underlying read is thread safe
131  return read();
132  }
133 
134 protected:
135 #if !defined(DOXYGEN_ONLY)
136  gpio_t gpio;
137 #endif //!defined(DOXYGEN_ONLY)
138 };
139 
140 /** @}*/
141 
142 } // namespace mbed
143 
144 #endif
int read()
Read the input, represented as 0 or 1 (int)
Definition: DigitalIn.h:97
DigitalIn(PinName pin)
Create a DigitalIn connected to the specified pin.
Definition: DigitalIn.h:67
void mode(PinMode pull)
Set the input pin mode.
int gpio_is_connected(const gpio_t *obj)
Checks if gpio object is connected (pin was not initialized with NC)
DigitalIn(PinName pin, PinMode mode)
Create a DigitalIn connected to the specified pin.
Definition: DigitalIn.h:78
int gpio_read(gpio_t *obj)
Read the input value.
~DigitalIn()
Class destructor, deinitialize the pin.
Definition: DigitalIn.h:86
int is_connected()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalIn.h:114
A digital input, used for reading the state of a pin.
Definition: DigitalIn.h:56
Definition: ATHandler.h:46
void gpio_init_in(gpio_t *gpio, PinName pin)
Init the input pin and set mode to PullDefault.
void gpio_free(gpio_t *obj)
Releases the GPIO pin.
void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode)
Init the input pin and set the mode.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.