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