test
DigitalOut.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_DIGITALOUT_H |
juansal12 | 0:44931ff4a3ed | 17 | #define MBED_DIGITALOUT_H |
juansal12 | 0:44931ff4a3ed | 18 | |
juansal12 | 0:44931ff4a3ed | 19 | #include "platform.h" |
juansal12 | 0:44931ff4a3ed | 20 | #include "gpio_api.h" |
juansal12 | 0:44931ff4a3ed | 21 | |
juansal12 | 0:44931ff4a3ed | 22 | namespace mbed { |
juansal12 | 0:44931ff4a3ed | 23 | |
juansal12 | 0:44931ff4a3ed | 24 | /** A digital output, used for setting the state of a pin |
juansal12 | 0:44931ff4a3ed | 25 | * |
juansal12 | 0:44931ff4a3ed | 26 | * Example: |
juansal12 | 0:44931ff4a3ed | 27 | * @code |
juansal12 | 0:44931ff4a3ed | 28 | * // Toggle a LED |
juansal12 | 0:44931ff4a3ed | 29 | * #include "mbed.h" |
juansal12 | 0:44931ff4a3ed | 30 | * |
juansal12 | 0:44931ff4a3ed | 31 | * DigitalOut led(LED1); |
juansal12 | 0:44931ff4a3ed | 32 | * |
juansal12 | 0:44931ff4a3ed | 33 | * int main() { |
juansal12 | 0:44931ff4a3ed | 34 | * while(1) { |
juansal12 | 0:44931ff4a3ed | 35 | * led = !led; |
juansal12 | 0:44931ff4a3ed | 36 | * wait(0.2); |
juansal12 | 0:44931ff4a3ed | 37 | * } |
juansal12 | 0:44931ff4a3ed | 38 | * } |
juansal12 | 0:44931ff4a3ed | 39 | * @endcode |
juansal12 | 0:44931ff4a3ed | 40 | */ |
juansal12 | 0:44931ff4a3ed | 41 | class DigitalOut { |
juansal12 | 0:44931ff4a3ed | 42 | |
juansal12 | 0:44931ff4a3ed | 43 | public: |
juansal12 | 0:44931ff4a3ed | 44 | /** Create a DigitalOut connected to the specified pin |
juansal12 | 0:44931ff4a3ed | 45 | * |
juansal12 | 0:44931ff4a3ed | 46 | * @param pin DigitalOut pin to connect to |
juansal12 | 0:44931ff4a3ed | 47 | */ |
juansal12 | 0:44931ff4a3ed | 48 | DigitalOut(PinName pin) : gpio() { |
juansal12 | 0:44931ff4a3ed | 49 | gpio_init_out(&gpio, pin); |
juansal12 | 0:44931ff4a3ed | 50 | } |
juansal12 | 0:44931ff4a3ed | 51 | |
juansal12 | 0:44931ff4a3ed | 52 | /** Create a DigitalOut connected to the specified pin |
juansal12 | 0:44931ff4a3ed | 53 | * |
juansal12 | 0:44931ff4a3ed | 54 | * @param pin DigitalOut pin to connect to |
juansal12 | 0:44931ff4a3ed | 55 | * @param value the initial pin value |
juansal12 | 0:44931ff4a3ed | 56 | */ |
juansal12 | 0:44931ff4a3ed | 57 | DigitalOut(PinName pin, int value) : gpio() { |
juansal12 | 0:44931ff4a3ed | 58 | gpio_init_out_ex(&gpio, pin, value); |
juansal12 | 0:44931ff4a3ed | 59 | } |
juansal12 | 0:44931ff4a3ed | 60 | |
juansal12 | 0:44931ff4a3ed | 61 | /** Set the output, specified as 0 or 1 (int) |
juansal12 | 0:44931ff4a3ed | 62 | * |
juansal12 | 0:44931ff4a3ed | 63 | * @param value An integer specifying the pin output value, |
juansal12 | 0:44931ff4a3ed | 64 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
juansal12 | 0:44931ff4a3ed | 65 | */ |
juansal12 | 0:44931ff4a3ed | 66 | void write(int value) { |
juansal12 | 0:44931ff4a3ed | 67 | gpio_write(&gpio, value); |
juansal12 | 0:44931ff4a3ed | 68 | } |
juansal12 | 0:44931ff4a3ed | 69 | |
juansal12 | 0:44931ff4a3ed | 70 | /** Return the output setting, represented as 0 or 1 (int) |
juansal12 | 0:44931ff4a3ed | 71 | * |
juansal12 | 0:44931ff4a3ed | 72 | * @returns |
juansal12 | 0:44931ff4a3ed | 73 | * an integer representing the output setting of the pin, |
juansal12 | 0:44931ff4a3ed | 74 | * 0 for logical 0, 1 for logical 1 |
juansal12 | 0:44931ff4a3ed | 75 | */ |
juansal12 | 0:44931ff4a3ed | 76 | int read() { |
juansal12 | 0:44931ff4a3ed | 77 | return gpio_read(&gpio); |
juansal12 | 0:44931ff4a3ed | 78 | } |
juansal12 | 0:44931ff4a3ed | 79 | |
juansal12 | 0:44931ff4a3ed | 80 | /** Return the output setting, represented as 0 or 1 (int) |
juansal12 | 0:44931ff4a3ed | 81 | * |
juansal12 | 0:44931ff4a3ed | 82 | * @returns |
juansal12 | 0:44931ff4a3ed | 83 | * Non zero value if pin is connected to uc GPIO |
juansal12 | 0:44931ff4a3ed | 84 | * 0 if gpio object was initialized with NC |
juansal12 | 0:44931ff4a3ed | 85 | */ |
juansal12 | 0:44931ff4a3ed | 86 | int is_connected() { |
juansal12 | 0:44931ff4a3ed | 87 | return gpio_is_connected(&gpio); |
juansal12 | 0:44931ff4a3ed | 88 | } |
juansal12 | 0:44931ff4a3ed | 89 | |
juansal12 | 0:44931ff4a3ed | 90 | #ifdef MBED_OPERATORS |
juansal12 | 0:44931ff4a3ed | 91 | /** A shorthand for write() |
juansal12 | 0:44931ff4a3ed | 92 | */ |
juansal12 | 0:44931ff4a3ed | 93 | DigitalOut& operator= (int value) { |
juansal12 | 0:44931ff4a3ed | 94 | write(value); |
juansal12 | 0:44931ff4a3ed | 95 | return *this; |
juansal12 | 0:44931ff4a3ed | 96 | } |
juansal12 | 0:44931ff4a3ed | 97 | |
juansal12 | 0:44931ff4a3ed | 98 | DigitalOut& operator= (DigitalOut& rhs) { |
juansal12 | 0:44931ff4a3ed | 99 | write(rhs.read()); |
juansal12 | 0:44931ff4a3ed | 100 | return *this; |
juansal12 | 0:44931ff4a3ed | 101 | } |
juansal12 | 0:44931ff4a3ed | 102 | |
juansal12 | 0:44931ff4a3ed | 103 | /** A shorthand for read() |
juansal12 | 0:44931ff4a3ed | 104 | */ |
juansal12 | 0:44931ff4a3ed | 105 | operator int() { |
juansal12 | 0:44931ff4a3ed | 106 | return read(); |
juansal12 | 0:44931ff4a3ed | 107 | } |
juansal12 | 0:44931ff4a3ed | 108 | #endif |
juansal12 | 0:44931ff4a3ed | 109 | |
juansal12 | 0:44931ff4a3ed | 110 | protected: |
juansal12 | 0:44931ff4a3ed | 111 | gpio_t gpio; |
juansal12 | 0:44931ff4a3ed | 112 | }; |
juansal12 | 0:44931ff4a3ed | 113 | |
juansal12 | 0:44931ff4a3ed | 114 | } // namespace mbed |
juansal12 | 0:44931ff4a3ed | 115 | |
juansal12 | 0:44931ff4a3ed | 116 | #endif |