Rtos API example

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/platform.h"
00020 
00021 #include "hal/gpio_api.h"
00022 #include "platform/mbed_critical.h"
00023 
00024 namespace mbed {
00025 /** \addtogroup drivers */
00026 
00027 /** A digital input, used for reading the state of a pin
00028  *
00029  * @note Synchronization level: Interrupt safe
00030  *
00031  * Example:
00032  * @code
00033  * // Flash an LED while a DigitalIn is true
00034  *
00035  * #include "mbed.h"
00036  *
00037  * DigitalIn enable(p5);
00038  * DigitalOut led(LED1);
00039  *
00040  * int main() {
00041  *     while(1) {
00042  *         if(enable) {
00043  *             led = !led;
00044  *         }
00045  *         wait(0.25);
00046  *     }
00047  * }
00048  * @endcode
00049  * @ingroup drivers
00050  */
00051 class DigitalIn {
00052 
00053 public:
00054     /** Create a DigitalIn connected to the specified pin
00055      *
00056      *  @param pin DigitalIn pin to connect to
00057      */
00058     DigitalIn(PinName pin) : gpio() {
00059         // No lock needed in the constructor
00060         gpio_init_in(&gpio, pin);
00061     }
00062 
00063     /** Create a DigitalIn connected to the specified pin
00064      *
00065      *  @param pin DigitalIn pin to connect to
00066      *  @param mode the initial mode of the pin
00067      */
00068     DigitalIn(PinName pin, PinMode mode) : gpio() {
00069         // No lock needed in the constructor
00070         gpio_init_in_ex(&gpio, pin, mode);
00071     }
00072     /** Read the input, represented as 0 or 1 (int)
00073      *
00074      *  @returns
00075      *    An integer representing the state of the input pin,
00076      *    0 for logical 0, 1 for logical 1
00077      */
00078     int read() {
00079         // Thread safe / atomic HAL call
00080         return gpio_read(&gpio);
00081     }
00082 
00083     /** Set the input pin mode
00084      *
00085      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00086      */
00087     void mode(PinMode pull) {
00088         core_util_critical_section_enter();
00089         gpio_mode(&gpio, pull);
00090         core_util_critical_section_exit();
00091     }
00092 
00093     /** Return the output setting, represented as 0 or 1 (int)
00094      *
00095      *  @returns
00096      *    Non zero value if pin is connected to uc GPIO
00097      *    0 if gpio object was initialized with NC
00098      */
00099     int is_connected() {
00100         // Thread safe / atomic HAL call
00101         return gpio_is_connected(&gpio);
00102     }
00103 
00104     /** An operator shorthand for read()
00105      * \sa DigitalIn::read()
00106      */
00107     operator int() {
00108         // Underlying read is thread safe
00109         return read();
00110     }
00111 
00112 protected:
00113     gpio_t gpio;
00114 };
00115 
00116 } // namespace mbed
00117 
00118 #endif