initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew 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_PORTINOUT_H
yihui 0:638edba3adf6 17 #define MBED_PORTINOUT_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_PORTINOUT
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
yihui 0:638edba3adf6 27 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
yihui 0:638edba3adf6 28 */
yihui 0:638edba3adf6 29 class PortInOut {
yihui 0:638edba3adf6 30 public:
yihui 0:638edba3adf6 31
yihui 0:638edba3adf6 32 /** Create an PortInOut, connected to the specified port
yihui 0:638edba3adf6 33 *
yihui 0:638edba3adf6 34 * @param port Port to connect to (Port0-Port5)
yihui 0:638edba3adf6 35 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
yihui 0:638edba3adf6 36 */
yihui 0:638edba3adf6 37 PortInOut(PortName port, int mask = 0xFFFFFFFF) {
yihui 0:638edba3adf6 38 port_init(&_port, port, mask, PIN_INPUT);
yihui 0:638edba3adf6 39 }
yihui 0:638edba3adf6 40
yihui 0:638edba3adf6 41 /** Write the value to the output port
yihui 0:638edba3adf6 42 *
yihui 0:638edba3adf6 43 * @param value An integer specifying a bit to write for every corresponding port pin
yihui 0:638edba3adf6 44 */
yihui 0:638edba3adf6 45 void write(int value) {
yihui 0:638edba3adf6 46 port_write(&_port, value);
yihui 0:638edba3adf6 47 }
yihui 0:638edba3adf6 48
yihui 0:638edba3adf6 49 /** Read the value currently output on the port
yihui 0:638edba3adf6 50 *
yihui 0:638edba3adf6 51 * @returns
yihui 0:638edba3adf6 52 * An integer with each bit corresponding to associated port pin setting
yihui 0:638edba3adf6 53 */
yihui 0:638edba3adf6 54 int read() {
yihui 0:638edba3adf6 55 return port_read(&_port);
yihui 0:638edba3adf6 56 }
yihui 0:638edba3adf6 57
yihui 0:638edba3adf6 58 /** Set as an output
yihui 0:638edba3adf6 59 */
yihui 0:638edba3adf6 60 void output() {
yihui 0:638edba3adf6 61 port_dir(&_port, PIN_OUTPUT);
yihui 0:638edba3adf6 62 }
yihui 0:638edba3adf6 63
yihui 0:638edba3adf6 64 /** Set as an input
yihui 0:638edba3adf6 65 */
yihui 0:638edba3adf6 66 void input() {
yihui 0:638edba3adf6 67 port_dir(&_port, PIN_INPUT);
yihui 0:638edba3adf6 68 }
yihui 0:638edba3adf6 69
yihui 0:638edba3adf6 70 /** Set the input pin mode
yihui 0:638edba3adf6 71 *
yihui 0:638edba3adf6 72 * @param mode PullUp, PullDown, PullNone, OpenDrain
yihui 0:638edba3adf6 73 */
yihui 0:638edba3adf6 74 void mode(PinMode mode) {
yihui 0:638edba3adf6 75 port_mode(&_port, mode);
yihui 0:638edba3adf6 76 }
yihui 0:638edba3adf6 77
yihui 0:638edba3adf6 78 /** A shorthand for write()
yihui 0:638edba3adf6 79 */
yihui 0:638edba3adf6 80 PortInOut& 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 PortInOut& operator= (PortInOut& 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