/*
 * arduino.c
 *
 * LED Rider
 *
 * Created: 25. 10. 2015 8:28:12
 * Author: Robo Ulbricht
 * www: http://www.arduinoslovakia.eu
 */ 

#include <asf.h>
#include "arduino.h"

const int led[]={PIN_PA11,PIN_PA10,PIN_PA16,PIN_PA17,27/*PIN_PA27*/,PIN_PA25,PIN_PA30,PIN_PA31};
const int ledsize=sizeof(led)/sizeof(int);
int pos=0;
int dir=1;
#define spd 100

///
/// Local setup
///
void rurInit(void)
{
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
for(int i=0;i<ledsize;i++)
  port_pin_set_config(led[i], &pin_conf);
}

///
/// Main loop
///
void rurLoop(void)
{
for(int i=0;i<ledsize;i++)
  {
  // simple moving LED
  digitalWrite(led[i], i==pos ? HIGH : LOW);
  }

pos+=dir;
if(pos==0)
  dir=1; // move to the right
if(pos==ledsize-1)
  dir=-1; // move to the left
delay(spd);
}

///
/// Onboard button
///
bool isOnboardButton(void)
{
return port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE;
}

///
/// Onboard LED on/off
///
void digitalWriteOnboardLed(bool value)
{
port_pin_set_output_level(LED_0_PIN, value ? (LED_0_ACTIVE) : (!LED_0_ACTIVE));
}

///
/// Logical true/false on a pin
///
void digitalWrite(int pin, bool value)
{
port_pin_set_output_level(pin, value);
}  