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