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