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