/* Copyright 2016 tgil All Rights Reserved */

#include "nrf_gpio.h"
#include "app_timer.h"
#include "ba.h"
#include "led.h"
#include "io.h"

APP_TIMER_DEF(m_led_app_timer_id);

void led_timeout_handler(void * context)
{
  nrf_gpio_pin_toggle(IO_LED_PIN);
}

void led_init()
{
  nrf_gpio_cfg_output(IO_LED_PIN);
  nrf_gpio_pin_set(IO_LED_PIN);
  app_timer_create(&m_led_app_timer_id, APP_TIMER_MODE_REPEATED, led_timeout_handler);
}

void led_flash(int period)
{
  int ticks;
  ticks = period * 32768 / 2000;
  app_timer_stop(m_led_app_timer_id);
  app_timer_start(m_led_app_timer_id, ticks, (void*)0);
}

void led_on()
{
  app_timer_stop(m_led_app_timer_id);
  nrf_gpio_pin_clear(IO_LED_PIN);
}

void led_off()
{
  app_timer_stop(m_led_app_timer_id);
  nrf_gpio_pin_set(IO_LED_PIN);
}
