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.
DigitalIn.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_DIGITALIN_H 00017 #define MBED_DIGITALIN_H 00018 00019 #include "platform.h" 00020 00021 #include "gpio_api.h" 00022 #include "critical.h" 00023 00024 namespace mbed { 00025 00026 /** A digital input, used for reading the state of a pin 00027 * 00028 * @Note Synchronization level: Interrupt safe 00029 * 00030 * Example: 00031 * @code 00032 * // Flash an LED while a DigitalIn is true 00033 * 00034 * #include "mbed.h" 00035 * 00036 * DigitalIn enable(p5); 00037 * DigitalOut led(LED1); 00038 * 00039 * int main() { 00040 * while(1) { 00041 * if(enable) { 00042 * led = !led; 00043 * } 00044 * wait(0.25); 00045 * } 00046 * } 00047 * @endcode 00048 */ 00049 class DigitalIn { 00050 00051 public: 00052 /** Create a DigitalIn connected to the specified pin 00053 * 00054 * @param pin DigitalIn pin to connect to 00055 */ 00056 DigitalIn(PinName pin) : gpio() { 00057 // No lock needed in the constructor 00058 gpio_init_in(&gpio, pin); 00059 } 00060 00061 /** Create a DigitalIn connected to the specified pin 00062 * 00063 * @param pin DigitalIn pin to connect to 00064 * @param mode the initial mode of the pin 00065 */ 00066 DigitalIn(PinName pin, PinMode mode) : gpio() { 00067 // No lock needed in the constructor 00068 gpio_init_in_ex(&gpio, pin, mode); 00069 } 00070 /** Read the input, represented as 0 or 1 (int) 00071 * 00072 * @returns 00073 * An integer representing the state of the input pin, 00074 * 0 for logical 0, 1 for logical 1 00075 */ 00076 int read() { 00077 // Thread safe / atomic HAL call 00078 return gpio_read(&gpio); 00079 } 00080 00081 /** Set the input pin mode 00082 * 00083 * @param mode PullUp, PullDown, PullNone, OpenDrain 00084 */ 00085 void mode(PinMode pull) { 00086 core_util_critical_section_enter(); 00087 gpio_mode(&gpio, pull); 00088 core_util_critical_section_exit(); 00089 } 00090 00091 /** Return the output setting, represented as 0 or 1 (int) 00092 * 00093 * @returns 00094 * Non zero value if pin is connected to uc GPIO 00095 * 0 if gpio object was initialized with NC 00096 */ 00097 int is_connected() { 00098 // Thread safe / atomic HAL call 00099 return gpio_is_connected(&gpio); 00100 } 00101 00102 /** An operator shorthand for read() 00103 */ 00104 operator int() { 00105 // Underlying read is thread safe 00106 return read(); 00107 } 00108 00109 protected: 00110 gpio_t gpio; 00111 }; 00112 00113 } // namespace mbed 00114 00115 #endif
Generated on Tue Jul 12 2022 22:19:20 by
