mbed library sources for GR-PEACH rev.B.

Fork of mbed-src by mbed official

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.h"
00020 
00021 #if DEVICE_PORTOUT
00022 
00023 #include "port_api.h"
00024 
00025 namespace mbed {
00026 /** A multiple pin digital out
00027  *
00028  * Example:
00029  * @code
00030  * // Toggle all four LEDs
00031  *
00032  * #include "mbed.h"
00033  *
00034  * // LED1 = P1.18  LED2 = P1.20  LED3 = P1.21  LED4 = P1.23
00035  * #define LED_MASK 0x00B40000
00036  *
00037  * PortOut ledport(Port1, LED_MASK);
00038  *
00039  * int main() {
00040  *     while(1) {
00041  *         ledport = LED_MASK;
00042  *         wait(1);
00043  *         ledport = 0;
00044  *         wait(1);
00045  *     }
00046  * }
00047  * @endcode
00048  */
00049 class PortOut {
00050 public:
00051 
00052     /** Create an PortOut, connected to the specified port
00053      *
00054      *  @param port Port to connect to (Port0-Port5)
00055      *  @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
00056      */
00057     PortOut(PortName port, int mask = 0xFFFFFFFF) {
00058         port_init(&_port, port, mask, PIN_OUTPUT);
00059     }
00060 
00061     /** Write the value to the output port
00062      *
00063      *  @param value An integer specifying a bit to write for every corresponding PortOut pin
00064      */
00065     void write(int value) {
00066         port_write(&_port, value);
00067     }
00068 
00069     /** Read the value currently output on the port
00070      *
00071      *  @returns
00072      *    An integer with each bit corresponding to associated PortOut pin setting
00073      */
00074     int read() {
00075         return port_read(&_port);
00076     }
00077 
00078     /** A shorthand for write()
00079      */
00080     PortOut& operator= (int value) {
00081         write(value);
00082         return *this;
00083     }
00084 
00085     PortOut& operator= (PortOut& rhs) {
00086         write(rhs.read());
00087         return *this;
00088     }
00089 
00090     /** A shorthand for read()
00091      */
00092     operator int() {
00093         return read();
00094     }
00095 
00096 private:
00097     port_t _port;
00098 };
00099 
00100 } // namespace mbed
00101 
00102 #endif
00103 
00104 #endif