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