Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitalInOut.h Source File

DigitalInOut.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_DIGITALINOUT_H
00018 #define MBED_DIGITALINOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #include "hal/gpio_api.h"
00023 
00024 namespace mbed {
00025 /**
00026  * \defgroup drivers_DigitalInOut DigitalInOut class
00027  * \ingroup drivers-public-api-gpio
00028  * @{
00029  */
00030 
00031 /** A digital input/output, used for setting or reading a bi-directional pin
00032  *
00033  * @note Synchronization level: Interrupt safe
00034  */
00035 class DigitalInOut {
00036 
00037 public:
00038     /** Create a DigitalInOut connected to the specified pin
00039      *
00040      *  @param pin DigitalInOut pin to connect to
00041      */
00042     DigitalInOut(PinName pin) : gpio()
00043     {
00044         // No lock needed in the constructor
00045         gpio_init_in(&gpio, pin);
00046     }
00047 
00048     /** Create a DigitalInOut connected to the specified pin
00049      *
00050      *  @param pin DigitalInOut pin to connect to
00051      *  @param direction the initial direction of the pin
00052      *  @param mode the initial mode of the pin
00053      *  @param value the initial value of the pin if is an output
00054      */
00055     DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
00056     {
00057         // No lock needed in the constructor
00058         gpio_init_inout(&gpio, pin, direction, mode, value);
00059     }
00060 
00061     /** Set the output, specified as 0 or 1 (int)
00062      *
00063      *  @param value An integer specifying the pin output value,
00064      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00065      */
00066     void write(int value)
00067     {
00068         // Thread safe / atomic HAL call
00069         gpio_write(&gpio, value);
00070     }
00071 
00072     /** Return the output setting, represented as 0 or 1 (int)
00073      *
00074      *  @returns
00075      *    an integer representing the output setting of the pin if it is an output,
00076      *    or read the input if set as an input
00077      */
00078     int read()
00079     {
00080         // Thread safe / atomic HAL call
00081         return gpio_read(&gpio);
00082     }
00083 
00084     /** Set as an output
00085      */
00086     void output();
00087 
00088     /** Set as an input
00089      */
00090     void input();
00091 
00092     /** Set the input pin mode
00093      *
00094      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00095      */
00096     void mode(PinMode pull);
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     /** A shorthand for write()
00111      * \sa DigitalInOut::write()
00112      * @code
00113      *      DigitalInOut  inout(PIN);
00114      *      DigitalIn     button(BUTTON1);
00115      *      inout.output();
00116      *
00117      *      inout = button;     // Equivalent to inout.write(button.read())
00118      * @endcode
00119      */
00120     DigitalInOut &operator= (int value)
00121     {
00122         // Underlying write is thread safe
00123         write(value);
00124         return *this;
00125     }
00126 
00127     /**A shorthand for write() using the assignment operator which copies the
00128      * state from the DigitalInOut argument.
00129      * \sa DigitalInOut::write()
00130      */
00131     DigitalInOut &operator= (DigitalInOut &rhs);
00132 
00133     /** A shorthand for read()
00134      * \sa DigitalInOut::read()
00135      * @code
00136      *      DigitalInOut inout(PIN);
00137      *      DigitalOut led(LED1);
00138      *
00139      *      inout.input();
00140      *      led = inout;   // Equivalent to led.write(inout.read())
00141      * @endcode
00142      */
00143     operator int()
00144     {
00145         // Underlying call is thread safe
00146         return read();
00147     }
00148 
00149 protected:
00150 #if !defined(DOXYGEN_ONLY)
00151     gpio_t gpio;
00152 #endif //!defined(DOXYGEN_ONLY)
00153 };
00154 
00155 /** @}*/
00156 
00157 } // namespace mbed
00158 
00159 #endif