Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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-2019 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 
00026 namespace mbed {
00027 /**
00028  * \defgroup drivers_PortIn PortIn class
00029  * \ingroup drivers-public-api-gpio
00030  * @{
00031  */
00032 
00033 /** A multiple pin digital input
00034  *
00035  * @note Synchronization level: Interrupt safe
00036  *
00037  *  Example:
00038  * @code
00039  * // Turn on an LED if any pins of Port2[0:5] are high
00040  *
00041  * #include "mbed.h"
00042  *
00043  * PortIn     p(Port2, 0x0000003F);  // Port2 pins [0:5] only
00044  * DigitalOut led(LED4);
00045  *
00046  * int main() {
00047  *     while(1) {
00048  *         int pins = p.read();
00049  *         if(pins) {
00050  *             led = 1;
00051  *         } else {
00052  *             led = 0;
00053  *         }
00054  *     }
00055  * }
00056  * @endcode
00057  */
00058 class PortIn {
00059 public:
00060 
00061     /** Create a PortIn, connected to the specified port
00062      *
00063      *  @param port Port to connect to (as defined in target's PortNames.h)
00064      *  @param mask Bitmask defines which port pins should be an input (0 - ignore, 1 - include)
00065         */
00066     PortIn(PortName port, int mask = 0xFFFFFFFF);
00067 
00068     /** Read the value input to the port
00069      *
00070      *  @returns
00071      *    An integer with each bit corresponding to the associated pin value
00072      */
00073     int read()
00074     {
00075         return port_read(&_port);
00076     }
00077 
00078     /** Set the input pin mode
00079      *
00080      *  @param mode PullUp, PullDown, PullNone, OpenDrain
00081      */
00082     void mode(PinMode mode);
00083 
00084     /** A shorthand for read()
00085      */
00086     operator int()
00087     {
00088         return read();
00089     }
00090 
00091 private:
00092     port_t _port;
00093 };
00094 
00095 /** @}*/
00096 
00097 } // namespace mbed
00098 
00099 #endif
00100 
00101 #endif