/*------------------------------------------------------- 
 * センサ入力 (センサ・ポイントインターフェースボード)
 *-------------------------------------------------------*/
#include <xc.h>
#include <stdint.h>
#include "mcr.h"
#include "io.h"
#include "ar.h"
#include "if-toq.h"
#include "if.h"
#include "i2c.h"
#include "i2c-mcp23017.h"

/* 検出有無のまとめ: 1=あるセンサで検出有り, 0=検出無し */
static int8_t if_sn_detected = 0;

/*-------------------------------------------------------
 * 初期化 
 *-------------------------------------------------------*/
void if_sensor_init(void)
{
  /* ポート初期値 */
  uint8_t caddr = 0;
  for ( ; caddr < IF_MAX_MODULES_TYPE_A; caddr++) {
    /*(ポート割付依存)*/
    if_rd_gpa[caddr] = (unsigned)if_rd_gpa[caddr] & ~0xFF;
    if_rd_gpb[caddr] = (unsigned)if_rd_gpb[caddr] & ~0x03;
  }
}

/*-------------------------------------------------------
 * センサ読み取り値の全クリア
 *-------------------------------------------------------*/
void if_sensor_reset(void)
{
  if_sn_detected = 0;
  if_sensor_read();
  if_sensor_init();
}

/*-------------------------------------------------------
 * センサ読み取り 
 *-------------------------------------------------------*/
void if_sensor_read(void)
{
  if_sn_detected = 0;
  uint8_t caddr = 0;
  for ( ; caddr < IF_MAX_MODULES_TYPE_A; caddr++) {
    CLRWDT();
    if (if_module[caddr] != 0) {
      /* ポート読み出し */
      uint8_t vga = i2c_mcp23017_a_read(caddr);
      uint8_t vgb = i2c_mcp23017_b_read(caddr);
      /* IOC読み出し */
      uint8_t via
        = i2c_mcp23017_reg_read(caddr, I2C_MCP23017_REG_INTCAPA);
      uint8_t vib
        = i2c_mcp23017_reg_read(caddr, I2C_MCP23017_REG_INTCAPB);
      /* 検出->値1, 非検出->値0 */
      uint8_t a = (unsigned)(~vga | via) & 0xFF; /*(ポート割付依存)*/
      uint8_t b = (unsigned)(~vgb | vib) & 0x03; /*(ポート割付依存)*/
      /* 検出値の更新 */
      if_rd_gpa[caddr] |= a;
      if_rd_gpb[caddr] |= b;
      /* 検出有無のまとめ */
      if_sn_detected |= ((a | b) != 0);
    }
  }
}

/*-------------------------------------------------------
 * センサ読み取り値を返す (読出し後にクリア) 
 *-------------------------------------------------------*/
int8_t if_sensor_get(if_sn_id sn)
{
  if ((sn == IF_SN_NULL) || (IF_MAX_SENSORS < sn)) {
    return 0;
  }
  /* 該当インターフェースボードのアドレスとポート番号
   * caddr := (sn-1) / N_SENSORS_PER_MOD 
   *     g := (sn-1) % N_SENSORS_PER_MOD */
  uint8_t g = (uint8_t)(sn - 1);
  uint8_t caddr = 0;
  while (g >= N_SENSORS_PER_MOD) {
    g -= N_SENSORS_PER_MOD;
    caddr++;
  }
  /* センサ読み出し情報を取得 */
  int8_t val = 0;
  if (if_module[caddr] != 0) {
    /* 対応ビットの読み取りとクリア (ポート割付依存) */
    const uint8_t bit_a[8] = { /* ポートA: センサ 1..8 */
      0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
    const uint8_t bit_b[8] = { /* ポートB: センサ 9,10 */
      0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    if (g < 8) {
      val = ((if_rd_gpa[caddr] & bit_a[(unsigned)g])   != 0);
      if_rd_gpa[caddr] &= (unsigned)~bit_a[(unsigned)g];
    } else {
      val = ((if_rd_gpb[caddr] & bit_b[(unsigned)g-8]) != 0);
      if_rd_gpb[caddr] &= (unsigned)~bit_b[(unsigned)g-8];    
    }
  }
  return val;
}

/*-------------------------------------------------------
 * センサ検出有無のまとめ 
 *-------------------------------------------------------*/
int8_t if_sensor_detected(void)
{
  return if_sn_detected;
}
