-
Notifications
You must be signed in to change notification settings - Fork 127
Control LED switching using GPIO on BeagleBone Black (BBB)
The LEDs on the BBB are on GPIOs 53,54,55,56 pin numbers. To access the GPIO pins, we will use the GPIO class in Dune. The definition of the GPIOs can be looked in DH>src/DUNE/Hardware/Dune.hpp. We will use the following steps to control LEDs.
- Create a new task, say GpioLed. How to create task is describe in Example 1.
- Include the header file
#include <DUNE/Hardware/GPIO.hpp>- The GPIOs have two entities - (1) the direction, either input or output and (2) the value. We will first declare an entity of GPIO class in side the TASK struct as
struct Task: public DUNE::Tasks::Task
{
GPIO* m_out;
//! Constructor.
//! @param[in] name task name.
//! @param[in] ctx context.
Task(const std::string& name, Tasks::Context& ctx):
DUNE::Tasks::Task(name, ctx),
m_out(NULL)
{
bind<IMC::SetLedBrightness>(this);
}In the Task constructor the m_out is initialised with NULL. 4. Include a destructor as
~Task(void)
{
onResourceRelease();
}- Using a GPIO is a resource and we need to acquire the resource before using it. So we will use the onResourceAcquisition method to define the m_out GPIO as
void
onResourceAcquisition(void)
{
m_out = new GPIO(53);
m_out->setDirection(GPIO::GPIO_DIR_OUTPUT);
m_out->setValue(0);
}The new GPIO(53) will open the GPIO pin 53 and set to direction as output direction with value 1. The LED will take a boolean value. Therefore, we set the value as 0 which represents the off-state of the LED. When the value is 1, then the LED is on. 6. Then we will clear the contents on m_out after using the resource. Thus the onResourceRelease method is used as
//! Release resources.
void
onResourceRelease(void)
{
Memory::clear(m_out);
}The Memory class is a dune utility which is used to clear the pointer (m_out in this example). 7. We have now written all the necessary code to acquire the GPIO and release it upon exit. Now, we will manipulate in the main loop to set the value as 1 (turn on), wait for 1 second and then set the value to 0 (turn off). The code is given below.
void
onMain(void)
{
while (!stopping())
{
m_out->setValue(1);
Delay::wait(1.0)
m_out->setValue(0);
Delay::wait(1.0)
}
}