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