SPKT
api/PortOut.h@0:e87aa4c49e95, 2019-06-04 (annotated)
- Committer:
- phungductung
- Date:
- Tue Jun 04 21:51:46 2019 +0000
- Revision:
- 0:e87aa4c49e95
libray
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phungductung | 0:e87aa4c49e95 | 1 | /* mbed Microcontroller Library |
phungductung | 0:e87aa4c49e95 | 2 | * Copyright (c) 2006-2013 ARM Limited |
phungductung | 0:e87aa4c49e95 | 3 | * |
phungductung | 0:e87aa4c49e95 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
phungductung | 0:e87aa4c49e95 | 5 | * you may not use this file except in compliance with the License. |
phungductung | 0:e87aa4c49e95 | 6 | * You may obtain a copy of the License at |
phungductung | 0:e87aa4c49e95 | 7 | * |
phungductung | 0:e87aa4c49e95 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
phungductung | 0:e87aa4c49e95 | 9 | * |
phungductung | 0:e87aa4c49e95 | 10 | * Unless required by applicable law or agreed to in writing, software |
phungductung | 0:e87aa4c49e95 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
phungductung | 0:e87aa4c49e95 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
phungductung | 0:e87aa4c49e95 | 13 | * See the License for the specific language governing permissions and |
phungductung | 0:e87aa4c49e95 | 14 | * limitations under the License. |
phungductung | 0:e87aa4c49e95 | 15 | */ |
phungductung | 0:e87aa4c49e95 | 16 | #ifndef MBED_PORTOUT_H |
phungductung | 0:e87aa4c49e95 | 17 | #define MBED_PORTOUT_H |
phungductung | 0:e87aa4c49e95 | 18 | |
phungductung | 0:e87aa4c49e95 | 19 | #include "platform.h" |
phungductung | 0:e87aa4c49e95 | 20 | |
phungductung | 0:e87aa4c49e95 | 21 | #if DEVICE_PORTOUT |
phungductung | 0:e87aa4c49e95 | 22 | |
phungductung | 0:e87aa4c49e95 | 23 | #include "port_api.h" |
phungductung | 0:e87aa4c49e95 | 24 | |
phungductung | 0:e87aa4c49e95 | 25 | namespace mbed { |
phungductung | 0:e87aa4c49e95 | 26 | /** A multiple pin digital out |
phungductung | 0:e87aa4c49e95 | 27 | * |
phungductung | 0:e87aa4c49e95 | 28 | * Example: |
phungductung | 0:e87aa4c49e95 | 29 | * @code |
phungductung | 0:e87aa4c49e95 | 30 | * // Toggle all four LEDs |
phungductung | 0:e87aa4c49e95 | 31 | * |
phungductung | 0:e87aa4c49e95 | 32 | * #include "mbed.h" |
phungductung | 0:e87aa4c49e95 | 33 | * |
phungductung | 0:e87aa4c49e95 | 34 | * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23 |
phungductung | 0:e87aa4c49e95 | 35 | * #define LED_MASK 0x00B40000 |
phungductung | 0:e87aa4c49e95 | 36 | * |
phungductung | 0:e87aa4c49e95 | 37 | * PortOut ledport(Port1, LED_MASK); |
phungductung | 0:e87aa4c49e95 | 38 | * |
phungductung | 0:e87aa4c49e95 | 39 | * int main() { |
phungductung | 0:e87aa4c49e95 | 40 | * while(1) { |
phungductung | 0:e87aa4c49e95 | 41 | * ledport = LED_MASK; |
phungductung | 0:e87aa4c49e95 | 42 | * wait(1); |
phungductung | 0:e87aa4c49e95 | 43 | * ledport = 0; |
phungductung | 0:e87aa4c49e95 | 44 | * wait(1); |
phungductung | 0:e87aa4c49e95 | 45 | * } |
phungductung | 0:e87aa4c49e95 | 46 | * } |
phungductung | 0:e87aa4c49e95 | 47 | * @endcode |
phungductung | 0:e87aa4c49e95 | 48 | */ |
phungductung | 0:e87aa4c49e95 | 49 | class PortOut { |
phungductung | 0:e87aa4c49e95 | 50 | public: |
phungductung | 0:e87aa4c49e95 | 51 | |
phungductung | 0:e87aa4c49e95 | 52 | /** Create an PortOut, connected to the specified port |
phungductung | 0:e87aa4c49e95 | 53 | * |
phungductung | 0:e87aa4c49e95 | 54 | * @param port Port to connect to (Port0-Port5) |
phungductung | 0:e87aa4c49e95 | 55 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
phungductung | 0:e87aa4c49e95 | 56 | */ |
phungductung | 0:e87aa4c49e95 | 57 | PortOut(PortName port, int mask = 0xFFFFFFFF) { |
phungductung | 0:e87aa4c49e95 | 58 | port_init(&_port, port, mask, PIN_OUTPUT); |
phungductung | 0:e87aa4c49e95 | 59 | } |
phungductung | 0:e87aa4c49e95 | 60 | |
phungductung | 0:e87aa4c49e95 | 61 | /** Write the value to the output port |
phungductung | 0:e87aa4c49e95 | 62 | * |
phungductung | 0:e87aa4c49e95 | 63 | * @param value An integer specifying a bit to write for every corresponding PortOut pin |
phungductung | 0:e87aa4c49e95 | 64 | */ |
phungductung | 0:e87aa4c49e95 | 65 | void write(int value) { |
phungductung | 0:e87aa4c49e95 | 66 | port_write(&_port, value); |
phungductung | 0:e87aa4c49e95 | 67 | } |
phungductung | 0:e87aa4c49e95 | 68 | |
phungductung | 0:e87aa4c49e95 | 69 | /** Read the value currently output on the port |
phungductung | 0:e87aa4c49e95 | 70 | * |
phungductung | 0:e87aa4c49e95 | 71 | * @returns |
phungductung | 0:e87aa4c49e95 | 72 | * An integer with each bit corresponding to associated PortOut pin setting |
phungductung | 0:e87aa4c49e95 | 73 | */ |
phungductung | 0:e87aa4c49e95 | 74 | int read() { |
phungductung | 0:e87aa4c49e95 | 75 | return port_read(&_port); |
phungductung | 0:e87aa4c49e95 | 76 | } |
phungductung | 0:e87aa4c49e95 | 77 | |
phungductung | 0:e87aa4c49e95 | 78 | /** A shorthand for write() |
phungductung | 0:e87aa4c49e95 | 79 | */ |
phungductung | 0:e87aa4c49e95 | 80 | PortOut& operator= (int value) { |
phungductung | 0:e87aa4c49e95 | 81 | write(value); |
phungductung | 0:e87aa4c49e95 | 82 | return *this; |
phungductung | 0:e87aa4c49e95 | 83 | } |
phungductung | 0:e87aa4c49e95 | 84 | |
phungductung | 0:e87aa4c49e95 | 85 | PortOut& operator= (PortOut& rhs) { |
phungductung | 0:e87aa4c49e95 | 86 | write(rhs.read()); |
phungductung | 0:e87aa4c49e95 | 87 | return *this; |
phungductung | 0:e87aa4c49e95 | 88 | } |
phungductung | 0:e87aa4c49e95 | 89 | |
phungductung | 0:e87aa4c49e95 | 90 | /** A shorthand for read() |
phungductung | 0:e87aa4c49e95 | 91 | */ |
phungductung | 0:e87aa4c49e95 | 92 | operator int() { |
phungductung | 0:e87aa4c49e95 | 93 | return read(); |
phungductung | 0:e87aa4c49e95 | 94 | } |
phungductung | 0:e87aa4c49e95 | 95 | |
phungductung | 0:e87aa4c49e95 | 96 | private: |
phungductung | 0:e87aa4c49e95 | 97 | port_t _port; |
phungductung | 0:e87aa4c49e95 | 98 | }; |
phungductung | 0:e87aa4c49e95 | 99 | |
phungductung | 0:e87aa4c49e95 | 100 | } // namespace mbed |
phungductung | 0:e87aa4c49e95 | 101 | |
phungductung | 0:e87aa4c49e95 | 102 | #endif |
phungductung | 0:e87aa4c49e95 | 103 | |
phungductung | 0:e87aa4c49e95 | 104 | #endif |