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