]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/DigitalOut.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / DigitalOut.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_DIGITALOUT_H
17 #define MBED_DIGITALOUT_H
18
19 #include "platform.h"
20 #include "gpio_api.h"
21
22 namespace mbed {
23
24 /** A digital output, used for setting the state of a pin
25  *
26  * Example:
27  * @code
28  * // Toggle a LED
29  * #include "mbed.h"
30  *
31  * DigitalOut led(LED1);
32  *
33  * int main() {
34  *     while(1) {
35  *         led = !led;
36  *         wait(0.2);
37  *     }
38  * }
39  * @endcode
40  */
41 class DigitalOut {
42
43 public:
44     /** Create a DigitalOut connected to the specified pin
45      *
46      *  @param pin DigitalOut pin to connect to
47      */
48     DigitalOut(PinName pin) : gpio() {
49         gpio_init_out(&gpio, pin);
50     }
51
52     /** Create a DigitalOut connected to the specified pin
53      *
54      *  @param pin DigitalOut pin to connect to
55      *  @param value the initial pin value
56      */
57     DigitalOut(PinName pin, int value) : gpio() {
58         gpio_init_out_ex(&gpio, pin, 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         gpio_write(&gpio, value);
68     }
69
70     /** Return the output setting, represented as 0 or 1 (int)
71      *
72      *  @returns
73      *    an integer representing the output setting of the pin,
74      *    0 for logical 0, 1 for logical 1
75      */
76     int read() {
77         return gpio_read(&gpio);
78     }
79
80     /** Return the output setting, represented as 0 or 1 (int)
81      *
82      *  @returns
83      *    Non zero value if pin is connected to uc GPIO
84      *    0 if gpio object was initialized with NC
85      */
86     int is_connected() {
87         return gpio_is_connected(&gpio);
88     }
89
90 #ifdef MBED_OPERATORS
91     /** A shorthand for write()
92      */
93     DigitalOut& operator= (int value) {
94         write(value);
95         return *this;
96     }
97
98     DigitalOut& operator= (DigitalOut& rhs) {
99         write(rhs.read());
100         return *this;
101     }
102
103     /** A shorthand for read()
104      */
105     operator int() {
106         return read();
107     }
108 #endif
109
110 protected:
111     gpio_t gpio;
112 };
113
114 } // namespace mbed
115
116 #endif