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 #include "hal/gpio_api.h"
22 
23 namespace mbed {
24 /**
25  * \defgroup drivers_DigitalOut DigitalOut class
26  * \ingroup drivers-public-api-gpio
27  * @{
28  */
29 
30 /** A digital output, used for setting the state of a pin
31  *
32  * @note Synchronization level: Interrupt safe
33  *
34  * Example:
35  * @code
36  * // Toggle a LED
37  * #include "mbed.h"
38  *
39  * DigitalOut led(LED1);
40  *
41  * int main() {
42  * while(1) {
43  * led = !led;
44  * ThisThread::sleep_for(200);
45  * }
46  * }
47  * @endcode
48  */
49 class DigitalOut {
50 
51 public:
52  /** Create a DigitalOut connected to the specified pin
53  *
54  * @param pin DigitalOut pin to connect to
55  */
56  DigitalOut(PinName pin) : gpio()
57  {
58  // No lock needed in the constructor
59  gpio_init_out(&gpio, pin);
60  }
61 
62  /** Create a DigitalOut connected to the specified pin
63  *
64  * @param pin DigitalOut pin to connect to
65  * @param value the initial pin value
66  */
67  DigitalOut(PinName pin, int value) : gpio()
68  {
69  // No lock needed in the constructor
70  gpio_init_out_ex(&gpio, pin, value);
71  }
72 
73  /** Set the output, specified as 0 or 1 (int)
74  *
75  * @param value An integer specifying the pin output value,
76  * 0 for logical 0, 1 (or any other non-zero value) for logical 1
77  */
78  void write(int value)
79  {
80  // Thread safe / atomic HAL call
81  gpio_write(&gpio, value);
82  }
83 
84  /** Return the output setting, represented as 0 or 1 (int)
85  *
86  * @returns
87  * an integer representing the output setting of the pin,
88  * 0 for logical 0, 1 for logical 1
89  */
90  int read()
91  {
92  // Thread safe / atomic HAL call
93  return gpio_read(&gpio);
94  }
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  /** A shorthand for write()
109  * \sa DigitalOut::write()
110  * @code
111  * DigitalIn button(BUTTON1);
112  * DigitalOut led(LED1);
113  * led = button; // Equivalent to led.write(button.read())
114  * @endcode
115  */
116  DigitalOut &operator= (int value)
117  {
118  // Underlying write is thread safe
119  write(value);
120  return *this;
121  }
122 
123  /** A shorthand for write() using the assignment operator which copies the
124  * state from the DigitalOut argument.
125  * \sa DigitalOut::write()
126  */
128 
129  /** A shorthand for read()
130  * \sa DigitalOut::read()
131  * @code
132  * DigitalIn button(BUTTON1);
133  * DigitalOut led(LED1);
134  * led = button; // Equivalent to led.write(button.read())
135  * @endcode
136  */
137  operator int()
138  {
139  // Underlying call is thread safe
140  return read();
141  }
142 
143 protected:
144 #if !defined(DOXYGEN_ONLY)
145  gpio_t gpio;
146 #endif //!defined(DOXYGEN_ONLY)
147 };
148 
149 /** @}*/
150 
151 } // namespace mbed
152 
153 #endif
DigitalOut & operator=(int value)
A shorthand for write()
Definition: DigitalOut.h:116
DigitalOut(PinName pin)
Create a DigitalOut connected to the specified pin.
Definition: DigitalOut.h:56
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:102
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:90
DigitalOut(PinName pin, int value)
Create a DigitalOut connected to the specified pin.
Definition: DigitalOut.h:67
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:49
Definition: ATHandler.h:46
void write(int value)
Set the output, specified as 0 or 1 (int)
Definition: DigitalOut.h:78
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.