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_PORTIN_H
yihui 0:638edba3adf6 17 #define MBED_PORTIN_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_PORTIN
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 input
yihui 0:638edba3adf6 28 *
yihui 0:638edba3adf6 29 * Example:
yihui 0:638edba3adf6 30 * @code
yihui 0:638edba3adf6 31 * // Switch on an LED if any of mbed pins 21-26 is high
yihui 0:638edba3adf6 32 *
yihui 0:638edba3adf6 33 * #include "mbed.h"
yihui 0:638edba3adf6 34 *
yihui 0:638edba3adf6 35 * PortIn p(Port2, 0x0000003F); // p21-p26
yihui 0:638edba3adf6 36 * DigitalOut ind(LED4);
yihui 0:638edba3adf6 37 *
yihui 0:638edba3adf6 38 * int main() {
yihui 0:638edba3adf6 39 * while(1) {
yihui 0:638edba3adf6 40 * int pins = p.read();
yihui 0:638edba3adf6 41 * if(pins) {
yihui 0:638edba3adf6 42 * ind = 1;
yihui 0:638edba3adf6 43 * } else {
yihui 0:638edba3adf6 44 * ind = 0;
yihui 0:638edba3adf6 45 * }
yihui 0:638edba3adf6 46 * }
yihui 0:638edba3adf6 47 * }
yihui 0:638edba3adf6 48 * @endcode
yihui 0:638edba3adf6 49 */
yihui 0:638edba3adf6 50 class PortIn {
yihui 0:638edba3adf6 51 public:
yihui 0:638edba3adf6 52
yihui 0:638edba3adf6 53 /** Create an PortIn, connected to the specified port
yihui 0:638edba3adf6 54 *
yihui 0:638edba3adf6 55 * @param port Port to connect to (Port0-Port5)
yihui 0:638edba3adf6 56 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
yihui 0:638edba3adf6 57 */
yihui 0:638edba3adf6 58 PortIn(PortName port, int mask = 0xFFFFFFFF) {
yihui 0:638edba3adf6 59 port_init(&_port, port, mask, PIN_INPUT);
yihui 0:638edba3adf6 60 }
yihui 0:638edba3adf6 61
yihui 0:638edba3adf6 62 /** Read the value currently output on the port
yihui 0:638edba3adf6 63 *
yihui 0:638edba3adf6 64 * @returns
yihui 0:638edba3adf6 65 * An integer with each bit corresponding to associated port pin setting
yihui 0:638edba3adf6 66 */
yihui 0:638edba3adf6 67 int read() {
yihui 0:638edba3adf6 68 return port_read(&_port);
yihui 0:638edba3adf6 69 }
yihui 0:638edba3adf6 70
yihui 0:638edba3adf6 71 /** Set the input pin mode
yihui 0:638edba3adf6 72 *
yihui 0:638edba3adf6 73 * @param mode PullUp, PullDown, PullNone, OpenDrain
yihui 0:638edba3adf6 74 */
yihui 0:638edba3adf6 75 void mode(PinMode mode) {
yihui 0:638edba3adf6 76 port_mode(&_port, mode);
yihui 0:638edba3adf6 77 }
yihui 0:638edba3adf6 78
yihui 0:638edba3adf6 79 /** A shorthand for read()
yihui 0:638edba3adf6 80 */
yihui 0:638edba3adf6 81 operator int() {
yihui 0:638edba3adf6 82 return read();
yihui 0:638edba3adf6 83 }
yihui 0:638edba3adf6 84
yihui 0:638edba3adf6 85 private:
yihui 0:638edba3adf6 86 port_t _port;
yihui 0:638edba3adf6 87 };
yihui 0:638edba3adf6 88
yihui 0:638edba3adf6 89 } // namespace mbed
yihui 0:638edba3adf6 90
yihui 0:638edba3adf6 91 #endif
yihui 0:638edba3adf6 92
yihui 0:638edba3adf6 93 #endif