Mistake on this page?
Report an issue in GitHub or email us
DigitalOut.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_DIGITALOUT_H
18 #define MBED_DIGITALOUT_H
19 
20 #include "platform/platform.h"
21 
22 #include "interfaces/InterfaceDigitalOut.h"
23 #include "hal/gpio_api.h"
24 
25 namespace mbed {
26 /**
27  * \defgroup drivers_DigitalOut DigitalOut class
28  * \ingroup drivers-public-api-gpio
29  * @{
30  */
31 
32 /** A digital output, used for setting the state of a pin
33  *
34  * @note Synchronization level: Interrupt safe
35  *
36  * Example:
37  * @code
38  * // Toggle a LED
39  * #include "mbed.h"
40  *
41  * DigitalOut led(LED1);
42  *
43  * int main() {
44  * while(1) {
45  * led = !led;
46  * ThisThread::sleep_for(200);
47  * }
48  * }
49  * @endcode
50  */
52 #ifdef FEATURE_EXPERIMENTAL_API
53  final : public interface::DigitalOut
54 #endif
55 {
56 
57 public:
58  /** Create a DigitalOut connected to the specified pin
59  *
60  * @param pin DigitalOut pin to connect to
61  */
62  DigitalOut(PinName pin) : gpio()
63  {
64  // No lock needed in the constructor
65  gpio_init_out(&gpio, pin);
66  }
67 
68  /** Create a DigitalOut connected to the specified pin
69  *
70  * @param pin DigitalOut pin to connect to
71  * @param value the initial pin value
72  */
73  DigitalOut(PinName pin, int value) : gpio()
74  {
75  // No lock needed in the constructor
76  gpio_init_out_ex(&gpio, pin, value);
77  }
78 
79  /** Set the output, specified as 0 or 1 (int)
80  *
81  * @param value An integer specifying the pin output value,
82  * 0 for logical 0, 1 (or any other non-zero value) for logical 1
83  */
84  void write(int value)
85  {
86  // Thread safe / atomic HAL call
87  gpio_write(&gpio, value);
88  }
89 
90  /** Return the output setting, represented as 0 or 1 (int)
91  *
92  * @returns
93  * an integer representing the output setting of the pin,
94  * 0 for logical 0, 1 for logical 1
95  */
96  int read()
97  {
98  // Thread safe / atomic HAL call
99  return gpio_read(&gpio);
100  }
101 
102  /** Return the output setting, represented as 0 or 1 (int)
103  *
104  * @returns
105  * Non zero value if pin is connected to uc GPIO
106  * 0 if gpio object was initialized with NC
107  */
109  {
110  // Thread safe / atomic HAL call
111  return gpio_is_connected(&gpio);
112  }
113 
114  /** A shorthand for write()
115  * \sa DigitalOut::write()
116  * @code
117  * DigitalIn button(BUTTON1);
118  * DigitalOut led(LED1);
119  * led = button; // Equivalent to led.write(button.read())
120  * @endcode
121  */
122  DigitalOut &operator= (int value)
123  {
124  // Underlying write is thread safe
125  write(value);
126  return *this;
127  }
128 
129  /** A shorthand for write() using the assignment operator which copies the
130  * state from the DigitalOut argument.
131  * \sa DigitalOut::write()
132  */
134 
135  /** A shorthand for read()
136  * \sa DigitalOut::read()
137  * @code
138  * DigitalIn button(BUTTON1);
139  * DigitalOut led(LED1);
140  * led = button; // Equivalent to led.write(button.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
DigitalOut & operator=(int value)
A shorthand for write()
Definition: DigitalOut.h:122
DigitalOut(PinName pin)
Create a DigitalOut connected to the specified pin.
Definition: DigitalOut.h:62
int gpio_is_connected(const gpio_t *obj)
Checks if gpio object is connected (pin was not initialized with NC)
int is_connected()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalOut.h:108
void gpio_write(gpio_t *obj, int value)
Set the output value.
int gpio_read(gpio_t *obj)
Read the input value.
int read()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalOut.h:96
DigitalOut(PinName pin, int value)
Create a DigitalOut connected to the specified pin.
Definition: DigitalOut.h:73
void gpio_init_out(gpio_t *gpio, PinName pin)
Init the output pin as an output, with predefined output value 0.
A digital output, used for setting the state of a pin.
Definition: DigitalOut.h:51
Definition: ATHandler.h:46
void write(int value)
Set the output, specified as 0 or 1 (int)
Definition: DigitalOut.h:84
void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value)
Init the pin as an output and set the output value.
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.