Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PortOut.h Source File

PortOut.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_PORTOUT_H
00018 #define MBED_PORTOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_PORTOUT || defined(DOXYGEN_ONLY)
00023 
00024 #include "hal/port_api.h"
00025 
00026 namespace mbed {
00027 /**
00028  * \defgroup drivers_PortOut PortOut class
00029  * \ingroup drivers-public-api-gpio
00030  * @{
00031  */
00032 
00033 /** A multiple pin digital output
00034  *
00035  * @note Synchronization level: Interrupt safe
00036  *
00037  * Example:
00038  * @code
00039  * // Toggle all four LEDs
00040  *
00041  * #include "mbed.h"
00042  *
00043  * // LED1 = P1.18  LED2 = P1.20  LED3 = P1.21  LED4 = P1.23
00044  * #define LED_MASK 0x00B40000
00045  *
00046  * PortOut ledport(Port1, LED_MASK);
00047  *
00048  * int main() {
00049  *     while(1) {
00050  *         ledport = LED_MASK;
00051  *         wait(1);
00052  *         ledport = 0;
00053  *         wait(1);
00054  *     }
00055  * }
00056  * @endcode
00057  */
00058 class PortOut {
00059 public:
00060 
00061     /** Create a PortOut, 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 are an output (0 - ignore, 1 - include)
00065      */
00066     PortOut(PortName port, int mask = 0xFFFFFFFF);
00067 
00068     /** Write the value to the output port
00069      *
00070      *  @param value An integer specifying a bit to write for every corresponding PortOut pin
00071      */
00072     void write(int value)
00073     {
00074         port_write(&_port, value);
00075     }
00076 
00077     /** Read the value currently output on the port
00078      *
00079      *  @returns
00080      *    An integer with each bit corresponding to associated pin value
00081      */
00082     int read()
00083     {
00084         return port_read(&_port);
00085     }
00086 
00087     /** A shorthand for write()
00088      * \sa PortOut::write()
00089      */
00090     PortOut &operator= (int value)
00091     {
00092         write(value);
00093         return *this;
00094     }
00095 
00096     /** A shorthand for read()
00097      * \sa PortOut::read()
00098      */
00099     PortOut &operator= (PortOut &rhs)
00100     {
00101         write(rhs.read());
00102         return *this;
00103     }
00104 
00105     /** A shorthand for read()
00106      * \sa PortOut::read()
00107      */
00108     operator int()
00109     {
00110         return read();
00111     }
00112 
00113 private:
00114     port_t _port;
00115 };
00116 
00117 /** @}*/
00118 
00119 } // namespace mbed
00120 
00121 #endif
00122 
00123 #endif