Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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