mbed-os/drivers/DigitalOut.h

154 lines
3.7 KiB
C
Raw Normal View History

/* mbed Microcontroller Library
* Copyright (c) 2006-2019 ARM Limited
2018-11-09 11:22:52 +00:00
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_DIGITALOUT_H
#define MBED_DIGITALOUT_H
#include "platform/platform.h"
#include "hal/gpio_api.h"
namespace mbed {
/**
* \defgroup drivers_DigitalOut DigitalOut class
* \ingroup drivers-public-api-gpio
* @{
*/
/** A digital output, used for setting the state of a pin
*
* @note Synchronization level: Interrupt safe
*
* Example:
* @code
* // Toggle a LED
* #include "mbed.h"
*
* DigitalOut led(LED1);
*
* int main() {
* while(1) {
* led = !led;
2020-03-06 15:51:15 +00:00
* ThisThread::sleep_for(200);
* }
* }
* @endcode
*/
class DigitalOut {
public:
/** Create a DigitalOut connected to the specified pin
*
* @param pin DigitalOut pin to connect to
*/
2018-05-24 15:58:14 +00:00
DigitalOut(PinName pin) : gpio()
{
// No lock needed in the constructor
gpio_init_out(&gpio, pin);
2014-03-04 14:07:30 +00:00
}
/** Create a DigitalOut connected to the specified pin
*
* @param pin DigitalOut pin to connect to
2014-03-04 10:27:30 +00:00
* @param value the initial pin value
*/
2018-05-24 15:58:14 +00:00
DigitalOut(PinName pin, int value) : gpio()
{
// No lock needed in the constructor
gpio_init_out_ex(&gpio, pin, value);
2014-03-04 14:07:30 +00:00
}
/** Set the output, specified as 0 or 1 (int)
*
* @param value An integer specifying the pin output value,
* 0 for logical 0, 1 (or any other non-zero value) for logical 1
*/
2018-05-24 15:58:14 +00:00
void write(int value)
{
// Thread safe / atomic HAL call
gpio_write(&gpio, value);
}
/** Return the output setting, represented as 0 or 1 (int)
*
* @returns
* an integer representing the output setting of the pin,
* 0 for logical 0, 1 for logical 1
*/
2018-05-24 15:58:14 +00:00
int read()
{
// Thread safe / atomic HAL call
return gpio_read(&gpio);
}
/** Return the output setting, represented as 0 or 1 (int)
*
* @returns
* Non zero value if pin is connected to uc GPIO
* 0 if gpio object was initialized with NC
*/
2018-05-24 15:58:14 +00:00
int is_connected()
{
// Thread safe / atomic HAL call
return gpio_is_connected(&gpio);
}
/** A shorthand for write()
* \sa DigitalOut::write()
* @code
* DigitalIn button(BUTTON1);
* DigitalOut led(LED1);
* led = button; // Equivalent to led.write(button.read())
* @endcode
*/
2018-05-24 15:58:14 +00:00
DigitalOut &operator= (int value)
{
// Underlying write is thread safe
write(value);
return *this;
}
/** A shorthand for write() using the assignment operator which copies the
* state from the DigitalOut argument.
* \sa DigitalOut::write()
*/
DigitalOut &operator= (DigitalOut &rhs);
/** A shorthand for read()
* \sa DigitalOut::read()
* @code
* DigitalIn button(BUTTON1);
* DigitalOut led(LED1);
* led = button; // Equivalent to led.write(button.read())
* @endcode
*/
2018-05-24 15:58:14 +00:00
operator int()
{
// Underlying call is thread safe
return read();
}
protected:
2018-11-12 09:33:52 +00:00
#if !defined(DOXYGEN_ONLY)
gpio_t gpio;
2018-11-12 09:33:52 +00:00
#endif //!defined(DOXYGEN_ONLY)
};
/** @}*/
} // namespace mbed
#endif