takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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     {
00060         // No lock needed in the constructor
00061         gpio_init_in(&gpio, pin);
00062     }
00063 
00064     /** Create a DigitalIn connected to the specified pin
00065      *
00066      *  @param pin DigitalIn pin to connect to
00067      *  @param mode the initial mode of the pin
00068      */
00069     DigitalIn(PinName pin, PinMode mode) : gpio()
00070     {
00071         // No lock needed in the constructor
00072         gpio_init_in_ex(&gpio, pin, mode);
00073     }
00074     /** Read the input, represented as 0 or 1 (int)
00075      *
00076      *  @returns
00077      *    An integer representing the state of the input pin,
00078      *    0 for logical 0, 1 for logical 1
00079      */
00080     int read()
00081     {
00082         // Thread safe / atomic HAL call
00083         return gpio_read(&gpio);
00084     }
00085 
00086     /** Set the input pin mode
00087      *
00088      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00089      */
00090     void mode(PinMode pull)
00091     {
00092         core_util_critical_section_enter();
00093         gpio_mode(&gpio, pull);
00094         core_util_critical_section_exit();
00095     }
00096 
00097     /** Return the output setting, represented as 0 or 1 (int)
00098      *
00099      *  @returns
00100      *    Non zero value if pin is connected to uc GPIO
00101      *    0 if gpio object was initialized with NC
00102      */
00103     int is_connected()
00104     {
00105         // Thread safe / atomic HAL call
00106         return gpio_is_connected(&gpio);
00107     }
00108 
00109     /** An operator shorthand for read()
00110      * \sa DigitalIn::read()
00111      */
00112     operator int()
00113     {
00114         // Underlying read is thread safe
00115         return read();
00116     }
00117 
00118 protected:
00119     gpio_t gpio;
00120 };
00121 
00122 } // namespace mbed
00123 
00124 #endif