Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitalOut.h Source File

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