mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitalIn.h Source File

DigitalIn.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_DIGITALIN_H
00017 #define MBED_DIGITALIN_H
00018 
00019 #include "platform.h"
00020 
00021 #include "gpio_api.h"
00022 
00023 namespace mbed {
00024 
00025 /** A digital input, used for reading the state of a pin
00026  *
00027  * Example:
00028  * @code
00029  * // Flash an LED while a DigitalIn is true
00030  *
00031  * #include "mbed.h"
00032  *
00033  * DigitalIn enable(p5);
00034  * DigitalOut led(LED1);
00035  *
00036  * int main() {
00037  *     while(1) {
00038  *         if(enable) {
00039  *             led = !led;
00040  *         }
00041  *         wait(0.25);
00042  *     }
00043  * }
00044  * @endcode
00045  */
00046 class DigitalIn {
00047 
00048 public:
00049     /** Create a DigitalIn connected to the specified pin
00050      *
00051      *  @param pin DigitalIn pin to connect to
00052      */
00053     DigitalIn(PinName pin) : gpio() {
00054         gpio_init_in(&gpio, pin);
00055     }
00056 
00057     /** Create a DigitalIn connected to the specified pin
00058      *
00059      *  @param pin DigitalIn pin to connect to
00060      *  @param mode the initial mode of the pin
00061      */
00062     DigitalIn(PinName pin, PinMode mode) : gpio() {
00063         gpio_init_in_ex(&gpio, pin, mode);
00064     }
00065     /** Read the input, represented as 0 or 1 (int)
00066      *
00067      *  @returns
00068      *    An integer representing the state of the input pin,
00069      *    0 for logical 0, 1 for logical 1
00070      */
00071     int read() {
00072         return gpio_read(&gpio);
00073     }
00074 
00075     /** Set the input pin mode
00076      *
00077      *  @param mode PullUp, PullDown, PullNone, OpenDrain
00078      */
00079     void mode(PinMode pull) {
00080         gpio_mode(&gpio, pull);
00081     }
00082 
00083     /** Return the output setting, represented as 0 or 1 (int)
00084      *
00085      *  @returns
00086      *    Non zero value if pin is connected to uc GPIO
00087      *    0 if gpio object was initialized with NC
00088      */
00089     int is_connected() {
00090         return gpio_is_connected(&gpio);
00091     }
00092 
00093 #ifdef MBED_OPERATORS
00094     /** An operator shorthand for read()
00095      */
00096     operator int() {
00097         return read();
00098     }
00099 #endif
00100 
00101 protected:
00102     gpio_t gpio;
00103 };
00104 
00105 } // namespace mbed
00106 
00107 #endif