Yihui Xiong
/
Seeed_nRF51822_Motion
initial
mbed-src/api/PortOut.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_PORTOUT_H |
yihui | 0:638edba3adf6 | 17 | #define MBED_PORTOUT_H |
yihui | 0:638edba3adf6 | 18 | |
yihui | 0:638edba3adf6 | 19 | #include "platform.h" |
yihui | 0:638edba3adf6 | 20 | |
yihui | 0:638edba3adf6 | 21 | #if DEVICE_PORTOUT |
yihui | 0:638edba3adf6 | 22 | |
yihui | 0:638edba3adf6 | 23 | #include "port_api.h" |
yihui | 0:638edba3adf6 | 24 | |
yihui | 0:638edba3adf6 | 25 | namespace mbed { |
yihui | 0:638edba3adf6 | 26 | /** A multiple pin digital out |
yihui | 0:638edba3adf6 | 27 | * |
yihui | 0:638edba3adf6 | 28 | * Example: |
yihui | 0:638edba3adf6 | 29 | * @code |
yihui | 0:638edba3adf6 | 30 | * // Toggle all four LEDs |
yihui | 0:638edba3adf6 | 31 | * |
yihui | 0:638edba3adf6 | 32 | * #include "mbed.h" |
yihui | 0:638edba3adf6 | 33 | * |
yihui | 0:638edba3adf6 | 34 | * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23 |
yihui | 0:638edba3adf6 | 35 | * #define LED_MASK 0x00B40000 |
yihui | 0:638edba3adf6 | 36 | * |
yihui | 0:638edba3adf6 | 37 | * PortOut ledport(Port1, LED_MASK); |
yihui | 0:638edba3adf6 | 38 | * |
yihui | 0:638edba3adf6 | 39 | * int main() { |
yihui | 0:638edba3adf6 | 40 | * while(1) { |
yihui | 0:638edba3adf6 | 41 | * ledport = LED_MASK; |
yihui | 0:638edba3adf6 | 42 | * wait(1); |
yihui | 0:638edba3adf6 | 43 | * ledport = 0; |
yihui | 0:638edba3adf6 | 44 | * wait(1); |
yihui | 0:638edba3adf6 | 45 | * } |
yihui | 0:638edba3adf6 | 46 | * } |
yihui | 0:638edba3adf6 | 47 | * @endcode |
yihui | 0:638edba3adf6 | 48 | */ |
yihui | 0:638edba3adf6 | 49 | class PortOut { |
yihui | 0:638edba3adf6 | 50 | public: |
yihui | 0:638edba3adf6 | 51 | |
yihui | 0:638edba3adf6 | 52 | /** Create an PortOut, connected to the specified port |
yihui | 0:638edba3adf6 | 53 | * |
yihui | 0:638edba3adf6 | 54 | * @param port Port to connect to (Port0-Port5) |
yihui | 0:638edba3adf6 | 55 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
yihui | 0:638edba3adf6 | 56 | */ |
yihui | 0:638edba3adf6 | 57 | PortOut(PortName port, int mask = 0xFFFFFFFF) { |
yihui | 0:638edba3adf6 | 58 | port_init(&_port, port, mask, PIN_OUTPUT); |
yihui | 0:638edba3adf6 | 59 | } |
yihui | 0:638edba3adf6 | 60 | |
yihui | 0:638edba3adf6 | 61 | /** Write the value to the output port |
yihui | 0:638edba3adf6 | 62 | * |
yihui | 0:638edba3adf6 | 63 | * @param value An integer specifying a bit to write for every corresponding PortOut pin |
yihui | 0:638edba3adf6 | 64 | */ |
yihui | 0:638edba3adf6 | 65 | void write(int value) { |
yihui | 0:638edba3adf6 | 66 | port_write(&_port, value); |
yihui | 0:638edba3adf6 | 67 | } |
yihui | 0:638edba3adf6 | 68 | |
yihui | 0:638edba3adf6 | 69 | /** Read the value currently output on the port |
yihui | 0:638edba3adf6 | 70 | * |
yihui | 0:638edba3adf6 | 71 | * @returns |
yihui | 0:638edba3adf6 | 72 | * An integer with each bit corresponding to associated PortOut pin setting |
yihui | 0:638edba3adf6 | 73 | */ |
yihui | 0:638edba3adf6 | 74 | int read() { |
yihui | 0:638edba3adf6 | 75 | return port_read(&_port); |
yihui | 0:638edba3adf6 | 76 | } |
yihui | 0:638edba3adf6 | 77 | |
yihui | 0:638edba3adf6 | 78 | /** A shorthand for write() |
yihui | 0:638edba3adf6 | 79 | */ |
yihui | 0:638edba3adf6 | 80 | PortOut& operator= (int value) { |
yihui | 0:638edba3adf6 | 81 | write(value); |
yihui | 0:638edba3adf6 | 82 | return *this; |
yihui | 0:638edba3adf6 | 83 | } |
yihui | 0:638edba3adf6 | 84 | |
yihui | 0:638edba3adf6 | 85 | PortOut& operator= (PortOut& rhs) { |
yihui | 0:638edba3adf6 | 86 | write(rhs.read()); |
yihui | 0:638edba3adf6 | 87 | return *this; |
yihui | 0:638edba3adf6 | 88 | } |
yihui | 0:638edba3adf6 | 89 | |
yihui | 0:638edba3adf6 | 90 | /** A shorthand for read() |
yihui | 0:638edba3adf6 | 91 | */ |
yihui | 0:638edba3adf6 | 92 | operator int() { |
yihui | 0:638edba3adf6 | 93 | return read(); |
yihui | 0:638edba3adf6 | 94 | } |
yihui | 0:638edba3adf6 | 95 | |
yihui | 0:638edba3adf6 | 96 | private: |
yihui | 0:638edba3adf6 | 97 | port_t _port; |
yihui | 0:638edba3adf6 | 98 | }; |
yihui | 0:638edba3adf6 | 99 | |
yihui | 0:638edba3adf6 | 100 | } // namespace mbed |
yihui | 0:638edba3adf6 | 101 | |
yihui | 0:638edba3adf6 | 102 | #endif |
yihui | 0:638edba3adf6 | 103 | |
yihui | 0:638edba3adf6 | 104 | #endif |