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