]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/DigitalIn.h
Add a qwerty layer
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / DigitalIn.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_DIGITALIN_H
17 #define MBED_DIGITALIN_H
18
19 #include "platform.h"
20
21 #include "gpio_api.h"
22
23 namespace mbed {
24
25 /** A digital input, used for reading the state of a pin
26  *
27  * Example:
28  * @code
29  * // Flash an LED while a DigitalIn is true
30  *
31  * #include "mbed.h"
32  *
33  * DigitalIn enable(p5);
34  * DigitalOut led(LED1);
35  *
36  * int main() {
37  *     while(1) {
38  *         if(enable) {
39  *             led = !led;
40  *         }
41  *         wait(0.25);
42  *     }
43  * }
44  * @endcode
45  */
46 class DigitalIn {
47
48 public:
49     /** Create a DigitalIn connected to the specified pin
50      *
51      *  @param pin DigitalIn pin to connect to
52      */
53     DigitalIn(PinName pin) : gpio() {
54         gpio_init_in(&gpio, pin);
55     }
56
57     /** Create a DigitalIn connected to the specified pin
58      *
59      *  @param pin DigitalIn pin to connect to
60      *  @param mode the initial mode of the pin
61      */
62     DigitalIn(PinName pin, PinMode mode) : gpio() {
63         gpio_init_in_ex(&gpio, pin, mode);
64     }
65     /** Read the input, represented as 0 or 1 (int)
66      *
67      *  @returns
68      *    An integer representing the state of the input pin,
69      *    0 for logical 0, 1 for logical 1
70      */
71     int read() {
72         return gpio_read(&gpio);
73     }
74
75     /** Set the input pin mode
76      *
77      *  @param mode PullUp, PullDown, PullNone, OpenDrain
78      */
79     void mode(PinMode pull) {
80         gpio_mode(&gpio, pull);
81     }
82
83     /** Return the output setting, represented as 0 or 1 (int)
84      *
85      *  @returns
86      *    Non zero value if pin is connected to uc GPIO
87      *    0 if gpio object was initialized with NC
88      */
89     int is_connected() {
90         return gpio_is_connected(&gpio);
91     }
92
93 #ifdef MBED_OPERATORS
94     /** An operator shorthand for read()
95      */
96     operator int() {
97         return read();
98     }
99 #endif
100
101 protected:
102     gpio_t gpio;
103 };
104
105 } // namespace mbed
106
107 #endif