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