takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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      */
00108     DigitalOut &operator= (int value)
00109     {
00110         // Underlying write is thread safe
00111         write(value);
00112         return *this;
00113     }
00114 
00115     /** A shorthand for write()
00116      * \sa DigitalOut::write()
00117      */
00118     DigitalOut &operator= (DigitalOut &rhs)
00119     {
00120         core_util_critical_section_enter();
00121         write(rhs.read());
00122         core_util_critical_section_exit();
00123         return *this;
00124     }
00125 
00126     /** A shorthand for read()
00127      * \sa DigitalOut::read()
00128      */
00129     operator int()
00130     {
00131         // Underlying call is thread safe
00132         return read();
00133     }
00134 
00135 protected:
00136     gpio_t gpio;
00137 };
00138 
00139 } // namespace mbed
00140 
00141 #endif