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