The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
145:64910690c574
Release 147 of the mbed library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 128:9bcdf88f62b0 1 /* mbed Microcontroller Library
<> 128:9bcdf88f62b0 2 * Copyright (c) 2006-2013 ARM Limited
<> 128:9bcdf88f62b0 3 *
<> 128:9bcdf88f62b0 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 128:9bcdf88f62b0 5 * you may not use this file except in compliance with the License.
<> 128:9bcdf88f62b0 6 * You may obtain a copy of the License at
<> 128:9bcdf88f62b0 7 *
<> 128:9bcdf88f62b0 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 128:9bcdf88f62b0 9 *
<> 128:9bcdf88f62b0 10 * Unless required by applicable law or agreed to in writing, software
<> 128:9bcdf88f62b0 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 128:9bcdf88f62b0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 128:9bcdf88f62b0 13 * See the License for the specific language governing permissions and
<> 128:9bcdf88f62b0 14 * limitations under the License.
<> 128:9bcdf88f62b0 15 */
<> 128:9bcdf88f62b0 16 #ifndef MBED_DIGITALOUT_H
<> 128:9bcdf88f62b0 17 #define MBED_DIGITALOUT_H
<> 128:9bcdf88f62b0 18
<> 128:9bcdf88f62b0 19 #include "platform/platform.h"
<> 128:9bcdf88f62b0 20 #include "hal/gpio_api.h"
<> 138:093f2bd7b9eb 21 #include "platform/mbed_critical.h"
<> 128:9bcdf88f62b0 22
<> 128:9bcdf88f62b0 23 namespace mbed {
<> 128:9bcdf88f62b0 24 /** \addtogroup drivers */
<> 128:9bcdf88f62b0 25
<> 128:9bcdf88f62b0 26 /** A digital output, used for setting the state of a pin
<> 128:9bcdf88f62b0 27 *
AnnaBridge 145:64910690c574 28 * @note Synchronization level: Interrupt safe
<> 128:9bcdf88f62b0 29 *
<> 128:9bcdf88f62b0 30 * Example:
<> 128:9bcdf88f62b0 31 * @code
<> 128:9bcdf88f62b0 32 * // Toggle a LED
<> 128:9bcdf88f62b0 33 * #include "mbed.h"
<> 128:9bcdf88f62b0 34 *
<> 128:9bcdf88f62b0 35 * DigitalOut led(LED1);
<> 128:9bcdf88f62b0 36 *
<> 128:9bcdf88f62b0 37 * int main() {
<> 128:9bcdf88f62b0 38 * while(1) {
<> 128:9bcdf88f62b0 39 * led = !led;
<> 128:9bcdf88f62b0 40 * wait(0.2);
<> 128:9bcdf88f62b0 41 * }
<> 128:9bcdf88f62b0 42 * }
<> 128:9bcdf88f62b0 43 * @endcode
AnnaBridge 145:64910690c574 44 * @ingroup drivers
<> 128:9bcdf88f62b0 45 */
<> 128:9bcdf88f62b0 46 class DigitalOut {
<> 128:9bcdf88f62b0 47
<> 128:9bcdf88f62b0 48 public:
<> 128:9bcdf88f62b0 49 /** Create a DigitalOut connected to the specified pin
<> 128:9bcdf88f62b0 50 *
<> 128:9bcdf88f62b0 51 * @param pin DigitalOut pin to connect to
<> 128:9bcdf88f62b0 52 */
<> 128:9bcdf88f62b0 53 DigitalOut(PinName pin) : gpio() {
<> 128:9bcdf88f62b0 54 // No lock needed in the constructor
<> 128:9bcdf88f62b0 55 gpio_init_out(&gpio, pin);
<> 128:9bcdf88f62b0 56 }
<> 128:9bcdf88f62b0 57
<> 128:9bcdf88f62b0 58 /** Create a DigitalOut connected to the specified pin
<> 128:9bcdf88f62b0 59 *
<> 128:9bcdf88f62b0 60 * @param pin DigitalOut pin to connect to
<> 128:9bcdf88f62b0 61 * @param value the initial pin value
<> 128:9bcdf88f62b0 62 */
<> 128:9bcdf88f62b0 63 DigitalOut(PinName pin, int value) : gpio() {
<> 128:9bcdf88f62b0 64 // No lock needed in the constructor
<> 128:9bcdf88f62b0 65 gpio_init_out_ex(&gpio, pin, value);
<> 128:9bcdf88f62b0 66 }
<> 128:9bcdf88f62b0 67
<> 128:9bcdf88f62b0 68 /** Set the output, specified as 0 or 1 (int)
<> 128:9bcdf88f62b0 69 *
<> 128:9bcdf88f62b0 70 * @param value An integer specifying the pin output value,
<> 128:9bcdf88f62b0 71 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
<> 128:9bcdf88f62b0 72 */
<> 128:9bcdf88f62b0 73 void write(int value) {
<> 128:9bcdf88f62b0 74 // Thread safe / atomic HAL call
<> 128:9bcdf88f62b0 75 gpio_write(&gpio, value);
<> 128:9bcdf88f62b0 76 }
<> 128:9bcdf88f62b0 77
<> 128:9bcdf88f62b0 78 /** Return the output setting, represented as 0 or 1 (int)
<> 128:9bcdf88f62b0 79 *
<> 128:9bcdf88f62b0 80 * @returns
<> 128:9bcdf88f62b0 81 * an integer representing the output setting of the pin,
<> 128:9bcdf88f62b0 82 * 0 for logical 0, 1 for logical 1
<> 128:9bcdf88f62b0 83 */
<> 128:9bcdf88f62b0 84 int read() {
<> 128:9bcdf88f62b0 85 // Thread safe / atomic HAL call
<> 128:9bcdf88f62b0 86 return gpio_read(&gpio);
<> 128:9bcdf88f62b0 87 }
<> 128:9bcdf88f62b0 88
<> 128:9bcdf88f62b0 89 /** Return the output setting, represented as 0 or 1 (int)
<> 128:9bcdf88f62b0 90 *
<> 128:9bcdf88f62b0 91 * @returns
<> 128:9bcdf88f62b0 92 * Non zero value if pin is connected to uc GPIO
<> 128:9bcdf88f62b0 93 * 0 if gpio object was initialized with NC
<> 128:9bcdf88f62b0 94 */
<> 128:9bcdf88f62b0 95 int is_connected() {
<> 128:9bcdf88f62b0 96 // Thread safe / atomic HAL call
<> 128:9bcdf88f62b0 97 return gpio_is_connected(&gpio);
<> 128:9bcdf88f62b0 98 }
<> 128:9bcdf88f62b0 99
<> 128:9bcdf88f62b0 100 /** A shorthand for write()
AnnaBridge 145:64910690c574 101 * \sa DigitalOut::write()
<> 128:9bcdf88f62b0 102 */
<> 128:9bcdf88f62b0 103 DigitalOut& operator= (int value) {
<> 128:9bcdf88f62b0 104 // Underlying write is thread safe
<> 128:9bcdf88f62b0 105 write(value);
<> 128:9bcdf88f62b0 106 return *this;
<> 128:9bcdf88f62b0 107 }
<> 128:9bcdf88f62b0 108
AnnaBridge 145:64910690c574 109 /** A shorthand for write()
AnnaBridge 145:64910690c574 110 * \sa DigitalOut::write()
AnnaBridge 145:64910690c574 111 */
<> 128:9bcdf88f62b0 112 DigitalOut& operator= (DigitalOut& rhs) {
<> 128:9bcdf88f62b0 113 core_util_critical_section_enter();
<> 128:9bcdf88f62b0 114 write(rhs.read());
<> 128:9bcdf88f62b0 115 core_util_critical_section_exit();
<> 128:9bcdf88f62b0 116 return *this;
<> 128:9bcdf88f62b0 117 }
<> 128:9bcdf88f62b0 118
<> 128:9bcdf88f62b0 119 /** A shorthand for read()
AnnaBridge 145:64910690c574 120 * \sa DigitalOut::read()
<> 128:9bcdf88f62b0 121 */
<> 128:9bcdf88f62b0 122 operator int() {
<> 128:9bcdf88f62b0 123 // Underlying call is thread safe
<> 128:9bcdf88f62b0 124 return read();
<> 128:9bcdf88f62b0 125 }
<> 128:9bcdf88f62b0 126
<> 128:9bcdf88f62b0 127 protected:
<> 128:9bcdf88f62b0 128 gpio_t gpio;
<> 128:9bcdf88f62b0 129 };
<> 128:9bcdf88f62b0 130
<> 128:9bcdf88f62b0 131 } // namespace mbed
<> 128:9bcdf88f62b0 132
<> 128:9bcdf88f62b0 133 #endif