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