2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
2019-07-11 15:23:39 +00:00
|
|
|
* Copyright (c) 2006-2019 ARM Limited
|
2018-11-09 11:22:52 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/platform.h"
|
|
|
|
#include "hal/gpio_api.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2019-07-11 15:23:39 +00:00
|
|
|
/**
|
|
|
|
* \defgroup drivers_DigitalOut DigitalOut class
|
2019-07-24 15:59:40 +00:00
|
|
|
* \ingroup drivers-public-api-gpio
|
2019-07-11 15:23:39 +00:00
|
|
|
* @{
|
|
|
|
*/
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** A digital output, used for setting the state of a pin
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @note Synchronization level: Interrupt safe
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* 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);
|
2013-02-18 15:32:11 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
class DigitalOut {
|
|
|
|
|
|
|
|
public:
|
2014-03-04 13:09:41 +00:00
|
|
|
/** 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()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// No lock needed in the constructor
|
2014-03-04 13:09:41 +00:00
|
|
|
gpio_init_out(&gpio, pin);
|
2014-03-04 14:07:30 +00:00
|
|
|
}
|
2014-03-04 13:09:41 +00:00
|
|
|
|
2013-02-18 15:32:11 +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
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
DigitalOut(PinName pin, int value) : gpio()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// No lock needed in the constructor
|
2014-03-04 13:09:41 +00:00
|
|
|
gpio_init_out_ex(&gpio, pin, value);
|
2014-03-04 14:07:30 +00:00
|
|
|
}
|
2013-02-18 15:32:11 +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)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// Thread safe / atomic HAL call
|
2013-02-18 15:32:11 +00:00
|
|
|
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()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// Thread safe / atomic HAL call
|
2013-02-18 15:32:11 +00:00
|
|
|
return gpio_read(&gpio);
|
|
|
|
}
|
|
|
|
|
2014-12-19 13:28:47 +00:00
|
|
|
/** 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()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// Thread safe / atomic HAL call
|
2014-12-19 13:28:47 +00:00
|
|
|
return gpio_is_connected(&gpio);
|
|
|
|
}
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
/** A shorthand for write()
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa DigitalOut::write()
|
2018-10-12 19:53:14 +00:00
|
|
|
* @code
|
|
|
|
* DigitalIn button(BUTTON1);
|
|
|
|
* DigitalOut led(LED1);
|
|
|
|
* led = button; // Equivalent to led.write(button.read())
|
|
|
|
* @endcode
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
DigitalOut &operator= (int value)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// Underlying write is thread safe
|
2013-02-18 15:32:11 +00:00
|
|
|
write(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:53:14 +00:00
|
|
|
/** A shorthand for write() using the assignment operator which copies the
|
|
|
|
* state from the DigitalOut argument.
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa DigitalOut::write()
|
|
|
|
*/
|
2019-07-11 15:23:39 +00:00
|
|
|
DigitalOut &operator= (DigitalOut &rhs);
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** A shorthand for read()
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa DigitalOut::read()
|
2018-10-12 19:53:14 +00:00
|
|
|
* @code
|
|
|
|
* DigitalIn button(BUTTON1);
|
|
|
|
* DigitalOut led(LED1);
|
|
|
|
* led = button; // Equivalent to led.write(button.read())
|
|
|
|
* @endcode
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
operator int()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// Underlying call is thread safe
|
2013-02-18 15:32:11 +00:00
|
|
|
return read();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-11-12 09:33:52 +00:00
|
|
|
#if !defined(DOXYGEN_ONLY)
|
2013-02-18 15:32:11 +00:00
|
|
|
gpio_t gpio;
|
2018-11-12 09:33:52 +00:00
|
|
|
#endif //!defined(DOXYGEN_ONLY)
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 15:23:39 +00:00
|
|
|
/** @}*/
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|