Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
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_in(&gpio, pin); 00036 } 00037 00038 /** Create a DigitalInOut connected to the specified pin 00039 * 00040 * @param pin DigitalInOut pin to connect to 00041 * @param direction the initial direction of the pin 00042 * @param mode the initial mode of the pin 00043 * @param value the initial value of the pin if is an output 00044 */ 00045 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) { 00046 gpio_init_inout(&gpio, pin, direction, mode, value); 00047 } 00048 00049 /** Set the output, specified as 0 or 1 (int) 00050 * 00051 * @param value An integer specifying the pin output value, 00052 * 0 for logical 0, 1 (or any other non-zero value) for logical 1 00053 */ 00054 void write(int value) { 00055 gpio_write(&gpio, value); 00056 } 00057 00058 /** Return the output setting, represented as 0 or 1 (int) 00059 * 00060 * @returns 00061 * an integer representing the output setting of the pin if it is an output, 00062 * or read the input if set as an input 00063 */ 00064 int read() { 00065 return gpio_read(&gpio); 00066 } 00067 00068 /** Set as an output 00069 */ 00070 void output() { 00071 gpio_dir(&gpio, PIN_OUTPUT); 00072 } 00073 00074 /** Set as an input 00075 */ 00076 void input() { 00077 gpio_dir(&gpio, PIN_INPUT); 00078 } 00079 00080 /** Set the input pin mode 00081 * 00082 * @param mode PullUp, PullDown, PullNone, OpenDrain 00083 */ 00084 void mode(PinMode pull) { 00085 gpio_mode(&gpio, pull); 00086 } 00087 00088 #ifdef MBED_OPERATORS 00089 /** A shorthand for write() 00090 */ 00091 DigitalInOut& operator= (int value) { 00092 write(value); 00093 return *this; 00094 } 00095 00096 DigitalInOut& operator= (DigitalInOut& rhs) { 00097 write(rhs.read()); 00098 return *this; 00099 } 00100 00101 /** A shorthand for read() 00102 */ 00103 operator int() { 00104 return read(); 00105 } 00106 #endif 00107 00108 protected: 00109 gpio_t gpio; 00110 }; 00111 00112 } // namespace mbed 00113 00114 #endif
Generated on Tue Jul 12 2022 21:21:33 by
