Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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