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