mbed SDK library sources

Fork of mbed-src by mbed official

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.h"
00020 
00021 #include "gpio_api.h"
00022 
00023 namespace mbed {
00024 
00025 /** A digital input/output, used for setting or reading a bi-directional pin
00026  */
00027 class DigitalInOut {
00028 
00029 public:
00030     /** Create a DigitalInOut connected to the specified pin
00031      *
00032      *  @param pin DigitalInOut pin to connect to
00033      */
00034     DigitalInOut(PinName pin) {
00035         gpio_init(&gpio, pin, PIN_INPUT);
00036     }
00037 
00038     /** Set the output, specified as 0 or 1 (int)
00039      *
00040      *  @param value An integer specifying the pin output value,
00041      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00042      */
00043     void write(int value) {
00044         gpio_write(&gpio, value);
00045     }
00046 
00047     /** Return the output setting, represented as 0 or 1 (int)
00048      *
00049      *  @returns
00050      *    an integer representing the output setting of the pin if it is an output,
00051      *    or read the input if set as an input
00052      */
00053     int read() {
00054         return gpio_read(&gpio);
00055     }
00056 
00057     /** Set as an output
00058      */
00059     void output() {
00060         gpio_dir(&gpio, PIN_OUTPUT);
00061     }
00062 
00063     /** Set as an input
00064      */
00065     void input() {
00066         gpio_dir(&gpio, PIN_INPUT);
00067     }
00068 
00069     /** Set the input pin mode
00070      *
00071      *  @param mode PullUp, PullDown, PullNone, OpenDrain
00072      */
00073     void mode(PinMode pull) {
00074         gpio_mode(&gpio, pull);
00075     }
00076 
00077 #ifdef MBED_OPERATORS
00078     /** A shorthand for write()
00079      */
00080     DigitalInOut& operator= (int value) {
00081         write(value);
00082         return *this;
00083     }
00084 
00085     DigitalInOut& operator= (DigitalInOut& rhs) {
00086         write(rhs.read());
00087         return *this;
00088     }
00089 
00090     /** A shorthand for read()
00091      */
00092     operator int() {
00093         return read();
00094     }
00095 #endif
00096 
00097 protected:
00098     gpio_t gpio;
00099 };
00100 
00101 } // namespace mbed
00102 
00103 #endif