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