Mistake on this page?
Report an issue in GitHub or email us
DigitalInOut.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_DIGITALINOUT_H
18 #define MBED_DIGITALINOUT_H
19 
20 #include "platform/platform.h"
21 
22 #include "hal/gpio_api.h"
23 
24 namespace mbed {
25 /**
26  * \defgroup drivers_DigitalInOut DigitalInOut class
27  * \ingroup drivers-public-api-gpio
28  * @{
29  */
30 
31 /** A digital input/output, used for setting or reading a bi-directional pin
32  *
33  * @note Synchronization level: Interrupt safe
34  */
35 class DigitalInOut {
36 
37 public:
38  /** Create a DigitalInOut connected to the specified pin
39  *
40  * @param pin DigitalInOut pin to connect to
41  */
42  DigitalInOut(PinName pin) : gpio()
43  {
44  // No lock needed in the constructor
45  gpio_init_in(&gpio, pin);
46  }
47 
48  /** Create a DigitalInOut connected to the specified pin
49  *
50  * @param pin DigitalInOut pin to connect to
51  * @param direction the initial direction of the pin
52  * @param mode the initial mode of the pin
53  * @param value the initial value of the pin if is an output
54  */
55  DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
56  {
57  // No lock needed in the constructor
58  gpio_init_inout(&gpio, pin, direction, mode, value);
59  }
60 
61  /** Set the output, specified as 0 or 1 (int)
62  *
63  * @param value An integer specifying the pin output value,
64  * 0 for logical 0, 1 (or any other non-zero value) for logical 1
65  */
66  void write(int value)
67  {
68  // Thread safe / atomic HAL call
69  gpio_write(&gpio, value);
70  }
71 
72  /** Return the output setting, represented as 0 or 1 (int)
73  *
74  * @returns
75  * an integer representing the output setting of the pin if it is an output,
76  * or read the input if set as an input
77  */
78  int read()
79  {
80  // Thread safe / atomic HAL call
81  return gpio_read(&gpio);
82  }
83 
84  /** Set as an output
85  */
86  void output();
87 
88  /** Set as an input
89  */
90  void input();
91 
92  /** Set the input pin mode
93  *
94  * @param pull PullUp, PullDown, PullNone, OpenDrain
95  */
96  void mode(PinMode pull);
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  /** A shorthand for write()
111  * \sa DigitalInOut::write()
112  * @code
113  * DigitalInOut inout(PIN);
114  * DigitalIn button(BUTTON1);
115  * inout.output();
116  *
117  * inout = button; // Equivalent to inout.write(button.read())
118  * @endcode
119  */
121  {
122  // Underlying write is thread safe
123  write(value);
124  return *this;
125  }
126 
127  /**A shorthand for write() using the assignment operator which copies the
128  * state from the DigitalInOut argument.
129  * \sa DigitalInOut::write()
130  */
132 
133  /** A shorthand for read()
134  * \sa DigitalInOut::read()
135  * @code
136  * DigitalInOut inout(PIN);
137  * DigitalOut led(LED1);
138  *
139  * inout.input();
140  * led = inout; // Equivalent to led.write(inout.read())
141  * @endcode
142  */
143  operator int()
144  {
145  // Underlying call is thread safe
146  return read();
147  }
148 
149 protected:
150 #if !defined(DOXYGEN_ONLY)
151  gpio_t gpio;
152 #endif //!defined(DOXYGEN_ONLY)
153 };
154 
155 /** @}*/
156 
157 } // namespace mbed
158 
159 #endif
void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value)
Init the pin to be in/out.
void output()
Set as an output.
void input()
Set as an input.
DigitalInOut & operator=(int value)
A shorthand for write()
Definition: DigitalInOut.h:120
A digital input/output, used for setting or reading a bi-directional pin.
Definition: DigitalInOut.h:35
int gpio_is_connected(const gpio_t *obj)
Checks if gpio object is connected (pin was not initialized with NC)
DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value)
Create a DigitalInOut connected to the specified pin.
Definition: DigitalInOut.h:55
void write(int value)
Set the output, specified as 0 or 1 (int)
Definition: DigitalInOut.h:66
void gpio_write(gpio_t *obj, int value)
Set the output value.
int read()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalInOut.h:78
int gpio_read(gpio_t *obj)
Read the input value.
int is_connected()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalInOut.h:104
void mode(PinMode pull)
Set the input pin mode.
DigitalInOut(PinName pin)
Create a DigitalInOut connected to the specified pin.
Definition: DigitalInOut.h:42
void gpio_init_in(gpio_t *gpio, PinName pin)
Init the input pin and set mode to PullDefault.
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.