Einstein Filho
/
MANGUEBAJA2019_BOX
Mangue Baja Box
mbed/DigitalInOut.h@0:0dee8840a1c0, 2019-07-29 (annotated)
- Committer:
- einsteingustavo
- Date:
- Mon Jul 29 20:38:00 2019 +0000
- Revision:
- 0:0dee8840a1c0
Mangue Baja Box
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
einsteingustavo | 0:0dee8840a1c0 | 1 | /* mbed Microcontroller Library |
einsteingustavo | 0:0dee8840a1c0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
einsteingustavo | 0:0dee8840a1c0 | 3 | * |
einsteingustavo | 0:0dee8840a1c0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
einsteingustavo | 0:0dee8840a1c0 | 5 | * you may not use this file except in compliance with the License. |
einsteingustavo | 0:0dee8840a1c0 | 6 | * You may obtain a copy of the License at |
einsteingustavo | 0:0dee8840a1c0 | 7 | * |
einsteingustavo | 0:0dee8840a1c0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
einsteingustavo | 0:0dee8840a1c0 | 9 | * |
einsteingustavo | 0:0dee8840a1c0 | 10 | * Unless required by applicable law or agreed to in writing, software |
einsteingustavo | 0:0dee8840a1c0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
einsteingustavo | 0:0dee8840a1c0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
einsteingustavo | 0:0dee8840a1c0 | 13 | * See the License for the specific language governing permissions and |
einsteingustavo | 0:0dee8840a1c0 | 14 | * limitations under the License. |
einsteingustavo | 0:0dee8840a1c0 | 15 | */ |
einsteingustavo | 0:0dee8840a1c0 | 16 | #ifndef MBED_DIGITALINOUT_H |
einsteingustavo | 0:0dee8840a1c0 | 17 | #define MBED_DIGITALINOUT_H |
einsteingustavo | 0:0dee8840a1c0 | 18 | |
einsteingustavo | 0:0dee8840a1c0 | 19 | #include "platform.h" |
einsteingustavo | 0:0dee8840a1c0 | 20 | |
einsteingustavo | 0:0dee8840a1c0 | 21 | #include "gpio_api.h" |
einsteingustavo | 0:0dee8840a1c0 | 22 | |
einsteingustavo | 0:0dee8840a1c0 | 23 | namespace mbed { |
einsteingustavo | 0:0dee8840a1c0 | 24 | |
einsteingustavo | 0:0dee8840a1c0 | 25 | /** A digital input/output, used for setting or reading a bi-directional pin |
einsteingustavo | 0:0dee8840a1c0 | 26 | */ |
einsteingustavo | 0:0dee8840a1c0 | 27 | class DigitalInOut { |
einsteingustavo | 0:0dee8840a1c0 | 28 | |
einsteingustavo | 0:0dee8840a1c0 | 29 | public: |
einsteingustavo | 0:0dee8840a1c0 | 30 | /** Create a DigitalInOut connected to the specified pin |
einsteingustavo | 0:0dee8840a1c0 | 31 | * |
einsteingustavo | 0:0dee8840a1c0 | 32 | * @param pin DigitalInOut pin to connect to |
einsteingustavo | 0:0dee8840a1c0 | 33 | */ |
einsteingustavo | 0:0dee8840a1c0 | 34 | DigitalInOut(PinName pin) : gpio() { |
einsteingustavo | 0:0dee8840a1c0 | 35 | gpio_init_in(&gpio, pin); |
einsteingustavo | 0:0dee8840a1c0 | 36 | } |
einsteingustavo | 0:0dee8840a1c0 | 37 | |
einsteingustavo | 0:0dee8840a1c0 | 38 | /** Create a DigitalInOut connected to the specified pin |
einsteingustavo | 0:0dee8840a1c0 | 39 | * |
einsteingustavo | 0:0dee8840a1c0 | 40 | * @param pin DigitalInOut pin to connect to |
einsteingustavo | 0:0dee8840a1c0 | 41 | * @param direction the initial direction of the pin |
einsteingustavo | 0:0dee8840a1c0 | 42 | * @param mode the initial mode of the pin |
einsteingustavo | 0:0dee8840a1c0 | 43 | * @param value the initial value of the pin if is an output |
einsteingustavo | 0:0dee8840a1c0 | 44 | */ |
einsteingustavo | 0:0dee8840a1c0 | 45 | DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() { |
einsteingustavo | 0:0dee8840a1c0 | 46 | gpio_init_inout(&gpio, pin, direction, mode, value); |
einsteingustavo | 0:0dee8840a1c0 | 47 | } |
einsteingustavo | 0:0dee8840a1c0 | 48 | |
einsteingustavo | 0:0dee8840a1c0 | 49 | /** Set the output, specified as 0 or 1 (int) |
einsteingustavo | 0:0dee8840a1c0 | 50 | * |
einsteingustavo | 0:0dee8840a1c0 | 51 | * @param value An integer specifying the pin output value, |
einsteingustavo | 0:0dee8840a1c0 | 52 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
einsteingustavo | 0:0dee8840a1c0 | 53 | */ |
einsteingustavo | 0:0dee8840a1c0 | 54 | void write(int value) { |
einsteingustavo | 0:0dee8840a1c0 | 55 | gpio_write(&gpio, value); |
einsteingustavo | 0:0dee8840a1c0 | 56 | } |
einsteingustavo | 0:0dee8840a1c0 | 57 | |
einsteingustavo | 0:0dee8840a1c0 | 58 | /** Return the output setting, represented as 0 or 1 (int) |
einsteingustavo | 0:0dee8840a1c0 | 59 | * |
einsteingustavo | 0:0dee8840a1c0 | 60 | * @returns |
einsteingustavo | 0:0dee8840a1c0 | 61 | * an integer representing the output setting of the pin if it is an output, |
einsteingustavo | 0:0dee8840a1c0 | 62 | * or read the input if set as an input |
einsteingustavo | 0:0dee8840a1c0 | 63 | */ |
einsteingustavo | 0:0dee8840a1c0 | 64 | int read() { |
einsteingustavo | 0:0dee8840a1c0 | 65 | return gpio_read(&gpio); |
einsteingustavo | 0:0dee8840a1c0 | 66 | } |
einsteingustavo | 0:0dee8840a1c0 | 67 | |
einsteingustavo | 0:0dee8840a1c0 | 68 | /** Set as an output |
einsteingustavo | 0:0dee8840a1c0 | 69 | */ |
einsteingustavo | 0:0dee8840a1c0 | 70 | void output() { |
einsteingustavo | 0:0dee8840a1c0 | 71 | gpio_dir(&gpio, PIN_OUTPUT); |
einsteingustavo | 0:0dee8840a1c0 | 72 | } |
einsteingustavo | 0:0dee8840a1c0 | 73 | |
einsteingustavo | 0:0dee8840a1c0 | 74 | /** Set as an input |
einsteingustavo | 0:0dee8840a1c0 | 75 | */ |
einsteingustavo | 0:0dee8840a1c0 | 76 | void input() { |
einsteingustavo | 0:0dee8840a1c0 | 77 | gpio_dir(&gpio, PIN_INPUT); |
einsteingustavo | 0:0dee8840a1c0 | 78 | } |
einsteingustavo | 0:0dee8840a1c0 | 79 | |
einsteingustavo | 0:0dee8840a1c0 | 80 | /** Set the input pin mode |
einsteingustavo | 0:0dee8840a1c0 | 81 | * |
einsteingustavo | 0:0dee8840a1c0 | 82 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
einsteingustavo | 0:0dee8840a1c0 | 83 | */ |
einsteingustavo | 0:0dee8840a1c0 | 84 | void mode(PinMode pull) { |
einsteingustavo | 0:0dee8840a1c0 | 85 | gpio_mode(&gpio, pull); |
einsteingustavo | 0:0dee8840a1c0 | 86 | } |
einsteingustavo | 0:0dee8840a1c0 | 87 | |
einsteingustavo | 0:0dee8840a1c0 | 88 | /** Return the output setting, represented as 0 or 1 (int) |
einsteingustavo | 0:0dee8840a1c0 | 89 | * |
einsteingustavo | 0:0dee8840a1c0 | 90 | * @returns |
einsteingustavo | 0:0dee8840a1c0 | 91 | * Non zero value if pin is connected to uc GPIO |
einsteingustavo | 0:0dee8840a1c0 | 92 | * 0 if gpio object was initialized with NC |
einsteingustavo | 0:0dee8840a1c0 | 93 | */ |
einsteingustavo | 0:0dee8840a1c0 | 94 | int is_connected() { |
einsteingustavo | 0:0dee8840a1c0 | 95 | return gpio_is_connected(&gpio); |
einsteingustavo | 0:0dee8840a1c0 | 96 | } |
einsteingustavo | 0:0dee8840a1c0 | 97 | |
einsteingustavo | 0:0dee8840a1c0 | 98 | #ifdef MBED_OPERATORS |
einsteingustavo | 0:0dee8840a1c0 | 99 | /** A shorthand for write() |
einsteingustavo | 0:0dee8840a1c0 | 100 | */ |
einsteingustavo | 0:0dee8840a1c0 | 101 | DigitalInOut& operator= (int value) { |
einsteingustavo | 0:0dee8840a1c0 | 102 | write(value); |
einsteingustavo | 0:0dee8840a1c0 | 103 | return *this; |
einsteingustavo | 0:0dee8840a1c0 | 104 | } |
einsteingustavo | 0:0dee8840a1c0 | 105 | |
einsteingustavo | 0:0dee8840a1c0 | 106 | DigitalInOut& operator= (DigitalInOut& rhs) { |
einsteingustavo | 0:0dee8840a1c0 | 107 | write(rhs.read()); |
einsteingustavo | 0:0dee8840a1c0 | 108 | return *this; |
einsteingustavo | 0:0dee8840a1c0 | 109 | } |
einsteingustavo | 0:0dee8840a1c0 | 110 | |
einsteingustavo | 0:0dee8840a1c0 | 111 | /** A shorthand for read() |
einsteingustavo | 0:0dee8840a1c0 | 112 | */ |
einsteingustavo | 0:0dee8840a1c0 | 113 | operator int() { |
einsteingustavo | 0:0dee8840a1c0 | 114 | return read(); |
einsteingustavo | 0:0dee8840a1c0 | 115 | } |
einsteingustavo | 0:0dee8840a1c0 | 116 | #endif |
einsteingustavo | 0:0dee8840a1c0 | 117 | |
einsteingustavo | 0:0dee8840a1c0 | 118 | protected: |
einsteingustavo | 0:0dee8840a1c0 | 119 | gpio_t gpio; |
einsteingustavo | 0:0dee8840a1c0 | 120 | }; |
einsteingustavo | 0:0dee8840a1c0 | 121 | |
einsteingustavo | 0:0dee8840a1c0 | 122 | } // namespace mbed |
einsteingustavo | 0:0dee8840a1c0 | 123 | |
einsteingustavo | 0:0dee8840a1c0 | 124 | #endif |
einsteingustavo | 0:0dee8840a1c0 | 125 |