Code: Select all* Author: Jessica Gomez <[email protected]>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, dfrobot FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file
* @ingroup basic
* @brief On board LED blink C++
*
* Blink the on board LED by writing a digital value to an output pin
* using the IO mraa library.
*
* @date 29/09/2015
*/
#include "mraa.hpp"
#include <iostream>
#include <unistd.h>
/*
* On board LED blink C++ example
*
* Demonstrate how to blink the on board LED, writing a digital value to an
* output pin using the MRAA library.
* No external hardware is needed.
*
* - digital out: on board LED
*
* Additional linker flags: none
*/
int main()
{
// select onboard LED pin based on the platform type
// create a GPIO object from MRAA using it
mraa::Platform platform = mraa::getPlatformType();
mraa::Gpio* d_pin = NULL;
switch (platform) {
case mraa::INTEL_GALILEO_GEN1:
d_pin = new mraa::Gpio(3, true, true);
break;
case mraa::INTEL_GALILEO_GEN2:
d_pin = new mraa::Gpio(13, true, false);
break;
case mraa::INTEL_EDISON_FAB_C:
d_pin = new mraa::Gpio(13, true, false);
break;
default:
std::cerr << "Unsupported platform, exiting" << std::endl;
return mraa::ERROR_INVALID_PLATFORM;
}
if (d_pin == NULL) {
std::cerr << "Can't create mraa::Gpio object, exiting" << std::endl;
return mraa::ERROR_UNSPECIFIED;
}
// set the pin as output
if (d_pin->dir(mraa::DIR_OUT) != mraa::SUCCESS) {
std::cerr << "Can't set digital pin as output, exiting" << std::endl;
return MRAA_ERROR_UNSPECIFIED;
}
// loop forever toggling the on board LED every second
for (;;) {
d_pin->write(1);
sleep(1);
d_pin->write(0);
sleep(1);
}
return mraa::SUCCESS;
}