/*------------------------------------------------------- 
 * 停止(相対時間で指定) / 待機(絶定時間で指定)
 *-------------------------------------------------------*/
#include <xc.h>
#include <stdint.h>
#include "pp.h"
#include "pp-const.h"
#include "ar.h"
#include "ar-stay.h"

/* 停止時間 [mSec] */
static uint16_t t_stay_msec = 0;

/* 停止開始(相対時間) */
void ar_stay_start(ar_staytime_t st)
{
  if (st > AR_STAYTIME_MAX) {
    st = AR_STAYTIME_MAX;
  }
  /* 停止時間: 0 [mSec] ... 51150 [mSec] */
  /*   (p_staytim = 0 ... 1023) */
  t_stay_msec =
    ((uint32_t)p_staytim * st * 50) / (uint32_t)AR_STAYTIME_MAX;
}

/* 停止開始(絶対時間) */
void ar_stay_abs_start(uint8_t t)
{
  t_stay_msec = (uint32_t)t * 100;
}

/* 停止継続 */
int8_t ar_stay_next(void)
{
  if (t_stay_msec < RUN_INTERVAL) {
    return 0; /* 完了 */
  }
  t_stay_msec -= RUN_INTERVAL;
  return 1; /* 継続中 */
}
