Daniel Vizcaya
/
04_RTOS_Embebidos
Entrega 3er corte - sistemas embebidos
mbed-os/drivers/DigitalIn.h@0:6ad07c9019fd, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 00:01:50 2018 +0000
- Revision:
- 0:6ad07c9019fd
Codigo de tales para todos los pasculaes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bethory | 0:6ad07c9019fd | 1 | /* mbed Microcontroller Library |
Bethory | 0:6ad07c9019fd | 2 | * Copyright (c) 2006-2013 ARM Limited |
Bethory | 0:6ad07c9019fd | 3 | * |
Bethory | 0:6ad07c9019fd | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Bethory | 0:6ad07c9019fd | 5 | * you may not use this file except in compliance with the License. |
Bethory | 0:6ad07c9019fd | 6 | * You may obtain a copy of the License at |
Bethory | 0:6ad07c9019fd | 7 | * |
Bethory | 0:6ad07c9019fd | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Bethory | 0:6ad07c9019fd | 9 | * |
Bethory | 0:6ad07c9019fd | 10 | * Unless required by applicable law or agreed to in writing, software |
Bethory | 0:6ad07c9019fd | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Bethory | 0:6ad07c9019fd | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Bethory | 0:6ad07c9019fd | 13 | * See the License for the specific language governing permissions and |
Bethory | 0:6ad07c9019fd | 14 | * limitations under the License. |
Bethory | 0:6ad07c9019fd | 15 | */ |
Bethory | 0:6ad07c9019fd | 16 | #ifndef MBED_DIGITALIN_H |
Bethory | 0:6ad07c9019fd | 17 | #define MBED_DIGITALIN_H |
Bethory | 0:6ad07c9019fd | 18 | |
Bethory | 0:6ad07c9019fd | 19 | #include "platform/platform.h" |
Bethory | 0:6ad07c9019fd | 20 | |
Bethory | 0:6ad07c9019fd | 21 | #include "hal/gpio_api.h" |
Bethory | 0:6ad07c9019fd | 22 | #include "platform/mbed_critical.h" |
Bethory | 0:6ad07c9019fd | 23 | |
Bethory | 0:6ad07c9019fd | 24 | namespace mbed { |
Bethory | 0:6ad07c9019fd | 25 | /** \addtogroup drivers */ |
Bethory | 0:6ad07c9019fd | 26 | |
Bethory | 0:6ad07c9019fd | 27 | /** A digital input, used for reading the state of a pin |
Bethory | 0:6ad07c9019fd | 28 | * |
Bethory | 0:6ad07c9019fd | 29 | * @note Synchronization level: Interrupt safe |
Bethory | 0:6ad07c9019fd | 30 | * |
Bethory | 0:6ad07c9019fd | 31 | * Example: |
Bethory | 0:6ad07c9019fd | 32 | * @code |
Bethory | 0:6ad07c9019fd | 33 | * // Flash an LED while a DigitalIn is true |
Bethory | 0:6ad07c9019fd | 34 | * |
Bethory | 0:6ad07c9019fd | 35 | * #include "mbed.h" |
Bethory | 0:6ad07c9019fd | 36 | * |
Bethory | 0:6ad07c9019fd | 37 | * DigitalIn enable(p5); |
Bethory | 0:6ad07c9019fd | 38 | * DigitalOut led(LED1); |
Bethory | 0:6ad07c9019fd | 39 | * |
Bethory | 0:6ad07c9019fd | 40 | * int main() { |
Bethory | 0:6ad07c9019fd | 41 | * while(1) { |
Bethory | 0:6ad07c9019fd | 42 | * if(enable) { |
Bethory | 0:6ad07c9019fd | 43 | * led = !led; |
Bethory | 0:6ad07c9019fd | 44 | * } |
Bethory | 0:6ad07c9019fd | 45 | * wait(0.25); |
Bethory | 0:6ad07c9019fd | 46 | * } |
Bethory | 0:6ad07c9019fd | 47 | * } |
Bethory | 0:6ad07c9019fd | 48 | * @endcode |
Bethory | 0:6ad07c9019fd | 49 | * @ingroup drivers |
Bethory | 0:6ad07c9019fd | 50 | */ |
Bethory | 0:6ad07c9019fd | 51 | class DigitalIn { |
Bethory | 0:6ad07c9019fd | 52 | |
Bethory | 0:6ad07c9019fd | 53 | public: |
Bethory | 0:6ad07c9019fd | 54 | /** Create a DigitalIn connected to the specified pin |
Bethory | 0:6ad07c9019fd | 55 | * |
Bethory | 0:6ad07c9019fd | 56 | * @param pin DigitalIn pin to connect to |
Bethory | 0:6ad07c9019fd | 57 | */ |
Bethory | 0:6ad07c9019fd | 58 | DigitalIn(PinName pin) : gpio() { |
Bethory | 0:6ad07c9019fd | 59 | // No lock needed in the constructor |
Bethory | 0:6ad07c9019fd | 60 | gpio_init_in(&gpio, pin); |
Bethory | 0:6ad07c9019fd | 61 | } |
Bethory | 0:6ad07c9019fd | 62 | |
Bethory | 0:6ad07c9019fd | 63 | /** Create a DigitalIn connected to the specified pin |
Bethory | 0:6ad07c9019fd | 64 | * |
Bethory | 0:6ad07c9019fd | 65 | * @param pin DigitalIn pin to connect to |
Bethory | 0:6ad07c9019fd | 66 | * @param mode the initial mode of the pin |
Bethory | 0:6ad07c9019fd | 67 | */ |
Bethory | 0:6ad07c9019fd | 68 | DigitalIn(PinName pin, PinMode mode) : gpio() { |
Bethory | 0:6ad07c9019fd | 69 | // No lock needed in the constructor |
Bethory | 0:6ad07c9019fd | 70 | gpio_init_in_ex(&gpio, pin, mode); |
Bethory | 0:6ad07c9019fd | 71 | } |
Bethory | 0:6ad07c9019fd | 72 | /** Read the input, represented as 0 or 1 (int) |
Bethory | 0:6ad07c9019fd | 73 | * |
Bethory | 0:6ad07c9019fd | 74 | * @returns |
Bethory | 0:6ad07c9019fd | 75 | * An integer representing the state of the input pin, |
Bethory | 0:6ad07c9019fd | 76 | * 0 for logical 0, 1 for logical 1 |
Bethory | 0:6ad07c9019fd | 77 | */ |
Bethory | 0:6ad07c9019fd | 78 | int read() { |
Bethory | 0:6ad07c9019fd | 79 | // Thread safe / atomic HAL call |
Bethory | 0:6ad07c9019fd | 80 | return gpio_read(&gpio); |
Bethory | 0:6ad07c9019fd | 81 | } |
Bethory | 0:6ad07c9019fd | 82 | |
Bethory | 0:6ad07c9019fd | 83 | /** Set the input pin mode |
Bethory | 0:6ad07c9019fd | 84 | * |
Bethory | 0:6ad07c9019fd | 85 | * @param pull PullUp, PullDown, PullNone, OpenDrain |
Bethory | 0:6ad07c9019fd | 86 | */ |
Bethory | 0:6ad07c9019fd | 87 | void mode(PinMode pull) { |
Bethory | 0:6ad07c9019fd | 88 | core_util_critical_section_enter(); |
Bethory | 0:6ad07c9019fd | 89 | gpio_mode(&gpio, pull); |
Bethory | 0:6ad07c9019fd | 90 | core_util_critical_section_exit(); |
Bethory | 0:6ad07c9019fd | 91 | } |
Bethory | 0:6ad07c9019fd | 92 | |
Bethory | 0:6ad07c9019fd | 93 | /** Return the output setting, represented as 0 or 1 (int) |
Bethory | 0:6ad07c9019fd | 94 | * |
Bethory | 0:6ad07c9019fd | 95 | * @returns |
Bethory | 0:6ad07c9019fd | 96 | * Non zero value if pin is connected to uc GPIO |
Bethory | 0:6ad07c9019fd | 97 | * 0 if gpio object was initialized with NC |
Bethory | 0:6ad07c9019fd | 98 | */ |
Bethory | 0:6ad07c9019fd | 99 | int is_connected() { |
Bethory | 0:6ad07c9019fd | 100 | // Thread safe / atomic HAL call |
Bethory | 0:6ad07c9019fd | 101 | return gpio_is_connected(&gpio); |
Bethory | 0:6ad07c9019fd | 102 | } |
Bethory | 0:6ad07c9019fd | 103 | |
Bethory | 0:6ad07c9019fd | 104 | /** An operator shorthand for read() |
Bethory | 0:6ad07c9019fd | 105 | * \sa DigitalIn::read() |
Bethory | 0:6ad07c9019fd | 106 | */ |
Bethory | 0:6ad07c9019fd | 107 | operator int() { |
Bethory | 0:6ad07c9019fd | 108 | // Underlying read is thread safe |
Bethory | 0:6ad07c9019fd | 109 | return read(); |
Bethory | 0:6ad07c9019fd | 110 | } |
Bethory | 0:6ad07c9019fd | 111 | |
Bethory | 0:6ad07c9019fd | 112 | protected: |
Bethory | 0:6ad07c9019fd | 113 | gpio_t gpio; |
Bethory | 0:6ad07c9019fd | 114 | }; |
Bethory | 0:6ad07c9019fd | 115 | |
Bethory | 0:6ad07c9019fd | 116 | } // namespace mbed |
Bethory | 0:6ad07c9019fd | 117 | |
Bethory | 0:6ad07c9019fd | 118 | #endif |