]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/DigitalInOut.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / DigitalInOut.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_DIGITALINOUT_H
17 #define MBED_DIGITALINOUT_H
18
19 #include "platform.h"
20
21 #include "gpio_api.h"
22
23 namespace mbed {
24
25 /** A digital input/output, used for setting or reading a bi-directional pin
26  */
27 class DigitalInOut {
28
29 public:
30     /** Create a DigitalInOut connected to the specified pin
31      *
32      *  @param pin DigitalInOut pin to connect to
33      */
34     DigitalInOut(PinName pin) : gpio() {
35         gpio_init_in(&gpio, pin);
36     }
37
38     /** Create a DigitalInOut connected to the specified pin
39      *
40      *  @param pin DigitalInOut pin to connect to
41      *  @param direction the initial direction of the pin
42      *  @param mode the initial mode of the pin
43      *  @param value the initial value of the pin if is an output
44      */
45     DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
46         gpio_init_inout(&gpio, pin, direction, mode, value);
47     }
48
49     /** Set the output, specified as 0 or 1 (int)
50      *
51      *  @param value An integer specifying the pin output value,
52      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
53      */
54     void write(int value) {
55         gpio_write(&gpio, value);
56     }
57
58     /** Return the output setting, represented as 0 or 1 (int)
59      *
60      *  @returns
61      *    an integer representing the output setting of the pin if it is an output,
62      *    or read the input if set as an input
63      */
64     int read() {
65         return gpio_read(&gpio);
66     }
67
68     /** Set as an output
69      */
70     void output() {
71         gpio_dir(&gpio, PIN_OUTPUT);
72     }
73
74     /** Set as an input
75      */
76     void input() {
77         gpio_dir(&gpio, PIN_INPUT);
78     }
79
80     /** Set the input pin mode
81      *
82      *  @param mode PullUp, PullDown, PullNone, OpenDrain
83      */
84     void mode(PinMode pull) {
85         gpio_mode(&gpio, pull);
86     }
87
88     /** Return the output setting, represented as 0 or 1 (int)
89      *
90      *  @returns
91      *    Non zero value if pin is connected to uc GPIO
92      *    0 if gpio object was initialized with NC
93      */
94     int is_connected() {
95         return gpio_is_connected(&gpio);
96     }
97
98 #ifdef MBED_OPERATORS
99     /** A shorthand for write()
100      */
101     DigitalInOut& operator= (int value) {
102         write(value);
103         return *this;
104     }
105
106     DigitalInOut& operator= (DigitalInOut& rhs) {
107         write(rhs.read());
108         return *this;
109     }
110
111     /** A shorthand for read()
112      */
113     operator int() {
114         return read();
115     }
116 #endif
117
118 protected:
119     gpio_t gpio;
120 };
121
122 } // namespace mbed
123
124 #endif