takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_PORTOUT_H
00017 #define MBED_PORTOUT_H
00018 
00019 #include "platform/platform.h"
00020 
00021 #if defined (DEVICE_PORTOUT) || defined(DOXYGEN_ONLY)
00022 
00023 #include "hal/port_api.h"
00024 #include "platform/mbed_critical.h"
00025 
00026 namespace mbed {
00027 /** \addtogroup drivers */
00028 /** A multiple pin digital out
00029  *
00030  * @note Synchronization level: Interrupt safe
00031  *
00032  * Example:
00033  * @code
00034  * // Toggle all four LEDs
00035  *
00036  * #include "mbed.h"
00037  *
00038  * // LED1 = P1.18  LED2 = P1.20  LED3 = P1.21  LED4 = P1.23
00039  * #define LED_MASK 0x00B40000
00040  *
00041  * PortOut ledport(Port1, LED_MASK);
00042  *
00043  * int main() {
00044  *     while(1) {
00045  *         ledport = LED_MASK;
00046  *         wait(1);
00047  *         ledport = 0;
00048  *         wait(1);
00049  *     }
00050  * }
00051  * @endcode
00052  * @ingroup drivers
00053  */
00054 class PortOut {
00055 public:
00056 
00057     /** Create an PortOut, connected to the specified port
00058      *
00059      *  @param port Port to connect to (Port0-Port5)
00060      *  @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
00061      */
00062     PortOut(PortName port, int mask = 0xFFFFFFFF)
00063     {
00064         core_util_critical_section_enter();
00065         port_init(&_port, port, mask, PIN_OUTPUT);
00066         core_util_critical_section_exit();
00067     }
00068 
00069     /** Write the value to the output port
00070      *
00071      *  @param value An integer specifying a bit to write for every corresponding PortOut pin
00072      */
00073     void write(int value)
00074     {
00075         port_write(&_port, value);
00076     }
00077 
00078     /** Read the value currently output on the port
00079      *
00080      *  @returns
00081      *    An integer with each bit corresponding to associated PortOut pin setting
00082      */
00083     int read()
00084     {
00085         return port_read(&_port);
00086     }
00087 
00088     /** A shorthand for write()
00089      * \sa PortOut::write()
00090      */
00091     PortOut &operator= (int value)
00092     {
00093         write(value);
00094         return *this;
00095     }
00096 
00097     /** A shorthand for read()
00098      * \sa PortOut::read()
00099      */
00100     PortOut &operator= (PortOut &rhs)
00101     {
00102         write(rhs.read());
00103         return *this;
00104     }
00105 
00106     /** A shorthand for read()
00107      * \sa PortOut::read()
00108      */
00109     operator int()
00110     {
00111         return read();
00112     }
00113 
00114 private:
00115     port_t _port;
00116 };
00117 
00118 } // namespace mbed
00119 
00120 #endif
00121 
00122 #endif