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