Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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-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_DIGITALINOUT_H
00017 #define MBED_DIGITALINOUT_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/output, used for setting or reading a bi-directional pin
00028  *
00029  * @note Synchronization level: Interrupt safe
00030  * @ingroup drivers
00031  */
00032 class DigitalInOut {
00033 
00034 public:
00035     /** Create a DigitalInOut connected to the specified pin
00036      *
00037      *  @param pin DigitalInOut pin to connect to
00038      */
00039     DigitalInOut(PinName pin) : gpio()
00040     {
00041         // No lock needed in the constructor
00042         gpio_init_in(&gpio, pin);
00043     }
00044 
00045     /** Create a DigitalInOut connected to the specified pin
00046      *
00047      *  @param pin DigitalInOut pin to connect to
00048      *  @param direction the initial direction of the pin
00049      *  @param mode the initial mode of the pin
00050      *  @param value the initial value of the pin if is an output
00051      */
00052     DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
00053     {
00054         // No lock needed in the constructor
00055         gpio_init_inout(&gpio, pin, direction, mode, value);
00056     }
00057 
00058     /** Set the output, specified as 0 or 1 (int)
00059      *
00060      *  @param value An integer specifying the pin output value,
00061      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00062      */
00063     void write(int value)
00064     {
00065         // Thread safe / atomic HAL call
00066         gpio_write(&gpio, value);
00067     }
00068 
00069     /** Return the output setting, represented as 0 or 1 (int)
00070      *
00071      *  @returns
00072      *    an integer representing the output setting of the pin if it is an output,
00073      *    or read the input if set as an input
00074      */
00075     int read()
00076     {
00077         // Thread safe / atomic HAL call
00078         return gpio_read(&gpio);
00079     }
00080 
00081     /** Set as an output
00082      */
00083     void output()
00084     {
00085         core_util_critical_section_enter();
00086         gpio_dir(&gpio, PIN_OUTPUT);
00087         core_util_critical_section_exit();
00088     }
00089 
00090     /** Set as an input
00091      */
00092     void input()
00093     {
00094         core_util_critical_section_enter();
00095         gpio_dir(&gpio, PIN_INPUT);
00096         core_util_critical_section_exit();
00097     }
00098 
00099     /** Set the input pin mode
00100      *
00101      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00102      */
00103     void mode(PinMode pull)
00104     {
00105         core_util_critical_section_enter();
00106         gpio_mode(&gpio, pull);
00107         core_util_critical_section_exit();
00108     }
00109 
00110     /** Return the output setting, represented as 0 or 1 (int)
00111      *
00112      *  @returns
00113      *    Non zero value if pin is connected to uc GPIO
00114      *    0 if gpio object was initialized with NC
00115      */
00116     int is_connected()
00117     {
00118         // Thread safe / atomic HAL call
00119         return gpio_is_connected(&gpio);
00120     }
00121 
00122     /** A shorthand for write()
00123      * \sa DigitalInOut::write()
00124      * @code
00125      *      DigitalInOut  inout(PIN);
00126      *      DigitalIn     button(BUTTON1);
00127      *      inout.output();
00128      *
00129      *      inout = button;     // Equivalent to inout.write(button.read())
00130      * @endcode
00131      */
00132     DigitalInOut &operator= (int value)
00133     {
00134         // Underlying write is thread safe
00135         write(value);
00136         return *this;
00137     }
00138 
00139     /**A shorthand for write() using the assignment operator which copies the
00140      * state from the DigitalInOut argument.
00141      * \sa DigitalInOut::write()
00142      */
00143     DigitalInOut &operator= (DigitalInOut &rhs)
00144     {
00145         core_util_critical_section_enter();
00146         write(rhs.read());
00147         core_util_critical_section_exit();
00148         return *this;
00149     }
00150 
00151     /** A shorthand for read()
00152      * \sa DigitalInOut::read()
00153      * @code
00154      *      DigitalInOut inout(PIN);
00155      *      DigitalOut led(LED1);
00156      *
00157      *      inout.input();
00158      *      led = inout;   // Equivalent to led.write(inout.read())
00159      * @endcode
00160      */
00161     operator int()
00162     {
00163         // Underlying call is thread safe
00164         return read();
00165     }
00166 
00167 protected:
00168     #if !defined(DOXYGEN_ONLY)
00169     gpio_t gpio;
00170     #endif //!defined(DOXYGEN_ONLY)
00171 };
00172 
00173 } // namespace mbed
00174 
00175 #endif