Rtos API example

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         // No lock needed in the constructor
00055         gpio_init_out(&gpio, pin);
00056     }
00057 
00058     /** Create a DigitalOut connected to the specified pin
00059      *
00060      *  @param pin DigitalOut pin to connect to
00061      *  @param value the initial pin value
00062      */
00063     DigitalOut(PinName pin, int value) : gpio() {
00064         // No lock needed in the constructor
00065         gpio_init_out_ex(&gpio, pin, value);
00066     }
00067 
00068     /** Set the output, specified as 0 or 1 (int)
00069      *
00070      *  @param value An integer specifying the pin output value,
00071      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00072      */
00073     void write(int value) {
00074         // Thread safe / atomic HAL call
00075         gpio_write(&gpio, value);
00076     }
00077 
00078     /** Return the output setting, represented as 0 or 1 (int)
00079      *
00080      *  @returns
00081      *    an integer representing the output setting of the pin,
00082      *    0 for logical 0, 1 for logical 1
00083      */
00084     int read() {
00085         // Thread safe / atomic HAL call
00086         return gpio_read(&gpio);
00087     }
00088 
00089     /** Return the output setting, represented as 0 or 1 (int)
00090      *
00091      *  @returns
00092      *    Non zero value if pin is connected to uc GPIO
00093      *    0 if gpio object was initialized with NC
00094      */
00095     int is_connected() {
00096         // Thread safe / atomic HAL call
00097         return gpio_is_connected(&gpio);
00098     }
00099 
00100     /** A shorthand for write()
00101      * \sa DigitalOut::write()
00102      */
00103     DigitalOut& operator= (int value) {
00104         // Underlying write is thread safe
00105         write(value);
00106         return *this;
00107     }
00108 
00109     /** A shorthand for write()
00110      * \sa DigitalOut::write()
00111      */
00112     DigitalOut& operator= (DigitalOut& rhs) {
00113         core_util_critical_section_enter();
00114         write(rhs.read());
00115         core_util_critical_section_exit();
00116         return *this;
00117     }
00118 
00119     /** A shorthand for read()
00120      * \sa DigitalOut::read()
00121      */
00122     operator int() {
00123         // Underlying call is thread safe
00124         return read();
00125     }
00126 
00127 protected:
00128     gpio_t gpio;
00129 };
00130 
00131 } // namespace mbed
00132 
00133 #endif