/* *****************************************************************************
 * File:      analyser_helper.c
 * Date:      18 Apr 2026
 * Author:    Andrew Levido
 *
 * Copyright 2026 Imbrius Pty Ltd - All Rights Reserved
 *
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "analyser_helper.h"
#include "stm32l4xx_hal.h"

#include <stdlib.h>
/* Defines -------------------------------------------------------------------*/

/* Typedefs & Enumerations ---------------------------------------------------*/

/* Global Variables ----------------------------------------------------------*/
const char ax_lookup[4][6] = {
  "None", "Red", "Green", "Blue"
};

char ax_line_1_buffer[AX_MAX_TEXT_SIZE];
char ax_line_2_buffer[AX_MAX_TEXT_SIZE];
/* Private Functions ---------------------------------------------------------*/

/* Public Functions ----------------------------------------------------------*/

int32_t ax_get_i_open_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
    int32_t i_dut;

  an_set_load_mv(low_v ? AX_TEST_V_LO : AX_TEST_V_HI);
  an_set_power(false);
  an_set_drive_none();
  an_set_matrix(NODE_POWER, lead_out);
  an_set_matrix(NODE_LOAD, lead_in);
  an_set_matrix(NODE_DRIVE, LEAD_NONE);
  i_dut = an_get_load_ua();
  an_set_matrix_all_open();
  return(i_dut);
}

int32_t ax_get_i_posi_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t i_dut;

  an_set_load_mv(low_v ? AX_TEST_V_LO : AX_TEST_V_HI);
  an_set_power(false);
  an_set_drive_ua(AX_TEST_DRIVE_I);
  an_set_matrix(NODE_POWER, lead_out);
  an_set_matrix(NODE_LOAD, lead_in);
  an_set_matrix(NODE_DRIVE, lead_drive);
  i_dut = an_get_load_ua();
  an_set_matrix_all_open();
  return(i_dut);
}


int32_t ax_get_i_negi_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t i_dut;

  an_set_load_mv(low_v ? AX_TEST_V_HI - AX_TEST_V_LO : 0);
  an_set_power(true);
  an_set_drive_ua(-AX_TEST_DRIVE_I);
  an_set_matrix(NODE_POWER, lead_in);
  an_set_matrix(NODE_LOAD, lead_out);
  an_set_matrix(NODE_DRIVE, lead_drive);
  i_dut = -an_get_load_ua();
  an_set_matrix_all_open();
  return(i_dut);
}


int32_t ax_get_i_gnd_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t i_dut;

  an_set_load_mv(low_v ? AX_TEST_V_LO : AX_TEST_V_HI);
  an_set_power(false);
  an_set_drive_none();
  an_set_matrix(NODE_POWER, lead_out);
  an_set_matrix(NODE_LOAD, lead_in);
  an_set_matrix_drive_short(lead_drive,  lead_out);
  i_dut = an_get_load_ua();
  an_set_matrix_all_open();
  return(i_dut);
}


int32_t ax_get_i_vplus_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t i_dut;

  an_set_load_mv(low_v ? AX_TEST_V_HI - AX_TEST_V_LO : 0);
  an_set_power(true);
  an_set_drive_none();
  an_set_matrix(NODE_POWER, lead_in);
  an_set_matrix(NODE_LOAD, lead_out);
  an_set_matrix_drive_short(lead_drive, lead_in);
  i_dut = -an_get_load_ua();
  an_set_matrix_all_open();
  return(i_dut);
}


int32_t ax_get_v_posv_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t v_dut;

  an_set_load_mv(low_v ? AX_TEST_V_LO : AX_TEST_V_HI);
  an_set_power(false);
  an_set_drive_mv(AX_TEST_DRIVE_V);
  an_set_matrix(NODE_POWER, lead_out);
  an_set_matrix(NODE_LOAD, lead_in);
  an_set_matrix(NODE_DRIVE, lead_drive);
  v_dut = ax_get_lead_mv(lead_in) - ax_get_lead_mv(lead_out);
  an_set_matrix_all_open();
  return(v_dut);
}
                
int32_t ax_get_v_open_drive(lead_t lead_in, lead_t lead_out, lead_t lead_drive, bool low_v)
{
  int32_t v_dut;

  an_set_load_mv(low_v ? AX_TEST_V_LO : AX_TEST_V_HI);
  an_set_power(false);
  an_set_drive_none();
  an_set_matrix(NODE_POWER, lead_out);
  an_set_matrix(NODE_LOAD, lead_in);
  an_set_matrix(NODE_DRIVE, LEAD_NONE);
  v_dut = ax_get_lead_mv(lead_in) - ax_get_lead_mv(lead_out);
  an_set_matrix_all_open();
  return(v_dut);
}


int32_t ax_get_lead_mv(lead_t lead)
{ 
  int32_t value;

  switch(lead) {
    case LEAD_RED:
      value = an_get_red_mv();
      break;
    case LEAD_GREEN:
      value = an_get_green_mv();
      break;
    case LEAD_BLUE:
      value = an_get_blue_mv();
      break;
    case LEAD_NONE:
    default:
      value = 0;
      break;
  }
  return(value);
}

void ax_print_flt(char * buff, int32_t val)
{
  if(val > 99990) { val = 99990; }
  if(val < -99990) { val = -99990; }

  *buff++ = val < 0 ? '-' : '+';
  val = abs(val);
  
  val = val % 10 >= 5 ? val + 5 : val;
  val = val / 10;

  *buff++ = val / 1000 == 0 ? ' ' : '0' + val / 1000;
  val = val % 1000;
  *buff++ = '0' + val / 100;
  val = val % 100;
  *buff++ = '.';
  *buff++= '0' + val / 10;
  val = val % 10;
  *buff++ = '0' + val;
  *buff++ = 0;

}

char* ax_lead_2_text(lead_t lead)
{
  return ((char*)ax_lookup[lead]);
}

/* Interrupt Service Routines ------------------------------------------------*/

/* End analyser_helper */