SHIO

Fork of mbed-stm32l0/l1-src by lzbp li

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.h"
00020 #include "gpio_api.h"
00021 
00022 namespace mbed {
00023 
00024 /** A digital output, used for setting the state of a pin
00025  *
00026  * Example:
00027  * @code
00028  * // Toggle a LED
00029  * #include "mbed.h"
00030  *
00031  * DigitalOut led(LED1);
00032  *
00033  * int main() {
00034  *     while(1) {
00035  *         led = !led;
00036  *         wait(0.2);
00037  *     }
00038  * }
00039  * @endcode
00040  */
00041 class DigitalOut {
00042 
00043 public:
00044     /** Create a DigitalOut connected to the specified pin
00045      *
00046      *  @param pin DigitalOut pin to connect to
00047      */
00048     DigitalOut(PinName pin) : gpio() {
00049         gpio_init_out(&gpio, pin);
00050     }
00051 
00052     /** Create a DigitalOut connected to the specified pin
00053      *
00054      *  @param pin DigitalOut pin to connect to
00055      *  @param value the initial pin value
00056      */
00057     DigitalOut(PinName pin, int value) : gpio() {
00058         gpio_init_out_ex(&gpio, pin, value);
00059     }
00060 
00061     /** Set the output, specified as 0 or 1 (int)
00062      *
00063      *  @param value An integer specifying the pin output value,
00064      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00065      */
00066     void write(int value) {
00067         gpio_write(&gpio, value);
00068     }
00069 
00070     /** Return the output setting, represented as 0 or 1 (int)
00071      *
00072      *  @returns
00073      *    an integer representing the output setting of the pin,
00074      *    0 for logical 0, 1 for logical 1
00075      */
00076     int read() {
00077         return gpio_read(&gpio);
00078     }
00079 
00080     /** Return the output setting, represented as 0 or 1 (int)
00081      *
00082      *  @returns
00083      *    Non zero value if pin is connected to uc GPIO
00084      *    0 if gpio object was initialized with NC
00085      */
00086     int is_connected() {
00087         return gpio_is_connected(&gpio);
00088     }
00089 
00090 #ifdef MBED_OPERATORS
00091     /** A shorthand for write()
00092      */
00093     DigitalOut& operator= (int value) {
00094         write(value);
00095         return *this;
00096     }
00097 
00098     DigitalOut& operator= (DigitalOut& rhs) {
00099         write(rhs.read());
00100         return *this;
00101     }
00102 
00103     /** A shorthand for read()
00104      */
00105     operator int() {
00106         return read();
00107     }
00108 #endif
00109 
00110 protected:
00111     gpio_t gpio;
00112 };
00113 
00114 } // namespace mbed
00115 
00116 #endif