mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_DIGITALIN_H
00018 #define MBED_DIGITALIN_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #include "hal/gpio_api.h"
00023 #include "platform/mbed_critical.h"
00024 
00025 namespace mbed {
00026 /** \addtogroup drivers */
00027 
00028 /** A digital input, used for reading the state of a pin
00029  *
00030  * @note Synchronization level: Interrupt safe
00031  *
00032  * Example:
00033  * @code
00034  * // Flash an LED while a DigitalIn is true
00035  *
00036  * #include "mbed.h"
00037  *
00038  * DigitalIn enable(p5);
00039  * DigitalOut led(LED1);
00040  *
00041  * int main() {
00042  *     while(1) {
00043  *         if(enable) {
00044  *             led = !led;
00045  *         }
00046  *         wait(0.25);
00047  *     }
00048  * }
00049  * @endcode
00050  * @ingroup drivers
00051  */
00052 class DigitalIn {
00053 
00054 public:
00055     /** Create a DigitalIn connected to the specified pin
00056      *
00057      *  @param pin DigitalIn pin to connect to
00058      */
00059     DigitalIn(PinName pin) : gpio()
00060     {
00061         // No lock needed in the constructor
00062         gpio_init_in(&gpio, pin);
00063     }
00064 
00065     /** Create a DigitalIn connected to the specified pin
00066      *
00067      *  @param pin DigitalIn pin to connect to
00068      *  @param mode the initial mode of the pin
00069      */
00070     DigitalIn(PinName pin, PinMode mode) : gpio()
00071     {
00072         // No lock needed in the constructor
00073         gpio_init_in_ex(&gpio, pin, mode);
00074     }
00075     /** Read the input, represented as 0 or 1 (int)
00076      *
00077      *  @returns
00078      *    An integer representing the state of the input pin,
00079      *    0 for logical 0, 1 for logical 1
00080      */
00081     int read()
00082     {
00083         // Thread safe / atomic HAL call
00084         return gpio_read(&gpio);
00085     }
00086 
00087     /** Set the input pin mode
00088      *
00089      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00090      */
00091     void mode(PinMode pull)
00092     {
00093         core_util_critical_section_enter();
00094         gpio_mode(&gpio, pull);
00095         core_util_critical_section_exit();
00096     }
00097 
00098     /** Return the output setting, represented as 0 or 1 (int)
00099      *
00100      *  @returns
00101      *    Non zero value if pin is connected to uc GPIO
00102      *    0 if gpio object was initialized with NC
00103      */
00104     int is_connected()
00105     {
00106         // Thread safe / atomic HAL call
00107         return gpio_is_connected(&gpio);
00108     }
00109 
00110     /** An operator shorthand for read()
00111      * \sa DigitalIn::read()
00112      * @code
00113      *      DigitalIn  button(BUTTON1);
00114      *      DigitalOut led(LED1);
00115      *      led = button;   // Equivalent to led.write(button.read())
00116      * @endcode
00117      */
00118     operator int()
00119     {
00120         // Underlying read is thread safe
00121         return read();
00122     }
00123 
00124 protected:
00125 #if !defined(DOXYGEN_ONLY)
00126     gpio_t gpio;
00127 #endif //!defined(DOXYGEN_ONLY)
00128 };
00129 
00130 } // namespace mbed
00131 
00132 #endif