mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PortIn.h Source File

PortIn.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_PORTIN_H
00018 #define MBED_PORTIN_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_PORTIN || defined(DOXYGEN_ONLY)
00023 
00024 #include "hal/port_api.h"
00025 #include "platform/mbed_critical.h"
00026 
00027 namespace mbed {
00028 /** \addtogroup drivers */
00029 
00030 /** A multiple pin digital input
00031  *
00032  * @note Synchronization level: Interrupt safe
00033  *
00034  *  Example:
00035  * @code
00036  * // Turn on an LED if any pins of Port2[0:5] are high
00037  *
00038  * #include "mbed.h"
00039  *
00040  * PortIn     p(Port2, 0x0000003F);  // Port2 pins [0:5] only
00041  * DigitalOut led(LED4);
00042  *
00043  * int main() {
00044  *     while(1) {
00045  *         int pins = p.read();
00046  *         if(pins) {
00047  *             led = 1;
00048  *         } else {
00049  *             led = 0;
00050  *         }
00051  *     }
00052  * }
00053  * @endcode
00054  * @ingroup drivers
00055  */
00056 class PortIn {
00057 public:
00058 
00059     /** Create a PortIn, connected to the specified port
00060      *
00061      *  @param port Port to connect to (as defined in target's PortNames.h)
00062      *  @param mask Bitmask defines which port pins should be an input (0 - ignore, 1 - include)
00063         */
00064     PortIn(PortName port, int mask = 0xFFFFFFFF)
00065     {
00066         core_util_critical_section_enter();
00067         port_init(&_port, port, mask, PIN_INPUT);
00068         core_util_critical_section_exit();
00069     }
00070 
00071     /** Read the value input to the port
00072      *
00073      *  @returns
00074      *    An integer with each bit corresponding to the associated pin value
00075      */
00076     int read()
00077     {
00078         return port_read(&_port);
00079     }
00080 
00081     /** Set the input pin mode
00082      *
00083      *  @param mode PullUp, PullDown, PullNone, OpenDrain
00084      */
00085     void mode(PinMode mode)
00086     {
00087         core_util_critical_section_enter();
00088         port_mode(&_port, mode);
00089         core_util_critical_section_exit();
00090     }
00091 
00092     /** A shorthand for read()
00093      */
00094     operator int()
00095     {
00096         return read();
00097     }
00098 
00099 private:
00100     port_t _port;
00101 };
00102 
00103 } // namespace mbed
00104 
00105 #endif
00106 
00107 #endif