Mistake on this page?
Report an issue in GitHub or email us
DigitalIn.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 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 "hal/gpio_api.h"
23 
24 namespace mbed {
25 /**
26  * \defgroup drivers_DigitalIn DigitalIn class
27  * \ingroup drivers-public-api-gpio
28  * @{
29  */
30 
31 /** A digital input, used for reading the state of a pin
32  *
33  * @note Synchronization level: Interrupt safe
34  *
35  * Example:
36  * @code
37  * // Flash an LED while a DigitalIn is true
38  *
39  * #include "mbed.h"
40  *
41  * DigitalIn enable(p5);
42  * DigitalOut led(LED1);
43  *
44  * int main() {
45  * while(1) {
46  * if(enable) {
47  * led = !led;
48  * }
49  * wait(0.25);
50  * }
51  * }
52  * @endcode
53  */
54 class DigitalIn {
55 
56 public:
57  /** Create a DigitalIn connected to the specified pin
58  *
59  * @param pin DigitalIn pin to connect to
60  */
61  DigitalIn(PinName pin) : gpio()
62  {
63  // No lock needed in the constructor
64  gpio_init_in(&gpio, pin);
65  }
66 
67  /** Create a DigitalIn connected to the specified pin
68  *
69  * @param pin DigitalIn pin to connect to
70  * @param mode the initial mode of the pin
71  */
72  DigitalIn(PinName pin, PinMode mode) : gpio()
73  {
74  // No lock needed in the constructor
75  gpio_init_in_ex(&gpio, pin, mode);
76  }
77 
78  /** Read the input, represented as 0 or 1 (int)
79  *
80  * @returns
81  * An integer representing the state of the input pin,
82  * 0 for logical 0, 1 for logical 1
83  */
84  int read()
85  {
86  // Thread safe / atomic HAL call
87  return gpio_read(&gpio);
88  }
89 
90  /** Set the input pin mode
91  *
92  * @param pull PullUp, PullDown, PullNone, OpenDrain
93  */
94  void mode(PinMode pull);
95 
96  /** Return the output setting, represented as 0 or 1 (int)
97  *
98  * @returns
99  * Non zero value if pin is connected to uc GPIO
100  * 0 if gpio object was initialized with NC
101  */
103  {
104  // Thread safe / atomic HAL call
105  return gpio_is_connected(&gpio);
106  }
107 
108  /** An operator shorthand for read()
109  * \sa DigitalIn::read()
110  * @code
111  * DigitalIn button(BUTTON1);
112  * DigitalOut led(LED1);
113  * led = button; // Equivalent to led.write(button.read())
114  * @endcode
115  */
116  operator int()
117  {
118  // Underlying read is thread safe
119  return read();
120  }
121 
122 protected:
123 #if !defined(DOXYGEN_ONLY)
124  gpio_t gpio;
125 #endif //!defined(DOXYGEN_ONLY)
126 };
127 
128 /** @}*/
129 
130 } // namespace mbed
131 
132 #endif
int read()
Read the input, represented as 0 or 1 (int)
Definition: DigitalIn.h:84
DigitalIn(PinName pin)
Create a DigitalIn connected to the specified pin.
Definition: DigitalIn.h:61
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:72
int gpio_read(gpio_t *obj)
Read the input value.
int is_connected()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalIn.h:102
A digital input, used for reading the state of a pin.
Definition: DigitalIn.h:54
void gpio_init_in(gpio_t *gpio, PinName pin)
Init the input pin and set mode to PullDefault.
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.