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

/* Includes ------------------------------------------------------------------*/
#include "diode.h"
#include "analog.h"
#include "analyser.h"
#include "analyser_helper.h"
#include "cli.h"
#include "screen.h"
#include <stdio.h>
#include "beep.h"

/* Defines -------------------------------------------------------------------*/

/* Typedefs & Enumerations ---------------------------------------------------*/
typedef struct {
  int32_t vf_mv;
  int32_t vr_mv;
  int32_t i_vf_ua;
  int32_t i_vr_ua;
} ax_diode_vf_t;

/* Global Variables ----------------------------------------------------------*/

/* Private Functions ---------------------------------------------------------*/
void ax_get_vf_vr(lead_t anode, lead_t cathode, ax_diode_vf_t * data)
{
  // Set up
  an_set_load_mv(AX_DIODE_VF_TEST_V);
  an_set_power(false);
  an_set_drive_none();
  // Forward
  an_set_matrix(NODE_LOAD, anode);
  an_set_matrix(NODE_POWER, cathode);
  data->vf_mv = ax_get_lead_mv(anode) - ax_get_lead_mv(cathode);
  data->i_vf_ua = an_get_load_ua();
  // Reverse
  an_set_matrix(NODE_LOAD, cathode);
  an_set_matrix(NODE_POWER, anode);
  data->vr_mv = ax_get_lead_mv(cathode) - ax_get_lead_mv(anode);
  data->i_vr_ua = an_get_load_ua();
  // Clean up
  an_set_matrix_all_open();
}

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

void ax_test_diode_aa(lead_t k1, lead_t a1a2, lead_t k2)
{
  ax_diode_vf_t diode_1;
  ax_diode_vf_t diode_2;
  screen_desc_t screen;
  char value_1 [10];
  char value_2 [10];

  // get the forward and reverse voltages 
  cli_printf("\tDiode or Schottky Pair AA?");
  ax_get_vf_vr(a1a2, k1, &diode_1);
  ax_get_vf_vr(a1a2, k2, &diode_2);

  // work out waht we have 

    if(((diode_1.vf_mv <= AX_DIODE_SCHOTTKY_V) &&    // Vf1 <= 400mV and Vr1 > 900mV
        (diode_1.vr_mv > AX_DIODE_LED_V )) &&
        ((diode_2.vf_mv <= AX_DIODE_SCHOTTKY_V) &&        // Vf2 <= 400mV and Vr2 > 900mV
        (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Schottky pauir KK -----------------------------------
      cli_printf("\t-> Schottky Diode Pair AA");
      cli_printf("\t   Cathode 1 = %s, Cathode 2 = %s, Common Anode = %s", ax_lead_2_text(k1), ax_lead_2_text(k2), ax_lead_2_text(a1a2));
      screen.screen = &scrn_schottky_pair_aa;
      screen.top_node_id = (scrn_node_id_t)k1;
      screen.left_node_id = (scrn_node_id_t)a1a2;
      screen.bottom_node_id = (scrn_node_id_t)k2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
      beep(A6, BEEP_SHORT);
    }
    else if(((diode_1.vf_mv < AX_DIODE_LED_V) &&          // Vf1 < 900mV and Vr1 > 900mV
      (diode_1.vr_mv > AX_DIODE_LED_V )) && 
      ((diode_2.vf_mv < AX_DIODE_LED_V) &&         // Vf2 < 900mV and Vr2 > 900mV
      (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Diode Pair AA ------------------------------------
    cli_printf("\t-> PN Diode Pair AA");
    cli_printf("\t   Cathode 1 = %s, Cathode 2 = %s, Common Anode = %s", ax_lead_2_text(k1), ax_lead_2_text(k2), ax_lead_2_text(a1a2));
    screen.screen = &scrn_diode_pair_aa;
    screen.top_node_id = (scrn_node_id_t)k1;
    screen.left_node_id = (scrn_node_id_t)a1a2;
    screen.bottom_node_id = (scrn_node_id_t)k2;
    ax_print_flt(value_1, diode_1.vf_mv);
    ax_print_flt(value_2, diode_1.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode_2.vf_mv);
    ax_print_flt(value_2, diode_2.i_vf_ua);
    sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
    }      
    else if(((diode_1.vf_mv >= AX_DIODE_LED_V) &&          // Vf1 >= 900mV and Vr1 > 900mV
      (diode_1.vr_mv > AX_DIODE_LED_V )) && 
      ((diode_2.vf_mv >= AX_DIODE_LED_V) &&         // Vf2 >= 900mV and Vr2 > 900mV
      (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // LED Pair AA ------------------------------------
      cli_printf("\t-> LED Pair AA");
      cli_printf("\t   Cathode 1 = %s, Cathode 2 = %s, Common Anode = %s", ax_lead_2_text(k1), ax_lead_2_text(k2), ax_lead_2_text(a1a2));
      screen.screen = &scrn_led_pair_aa;
      screen.top_node_id = (scrn_node_id_t)k1;
      screen.left_node_id = (scrn_node_id_t)a1a2;
      screen.bottom_node_id = (scrn_node_id_t)k2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
      beep(A6, BEEP_SHORT);
      }
  else {
    // bad part ----------------------------------------
    cli_printf("\t-> Unknown or Bad Part");
    screen.screen = &scrn_error;
    screen.string1_override = "Unknown or Bad Part";
    screen.string2_override = "";
    beep(C5, BEEP_LONG);
  }
  cli_printf("\t* (1) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_1.vf_mv, diode_1.i_vf_ua, diode_1.vr_mv, diode_1.i_vr_ua);
  cli_printf("\t* (2) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_2.vf_mv, diode_2.i_vf_ua, diode_2.vr_mv, diode_2.i_vr_ua);

  scrn_draw(&screen);             
}

void ax_test_diode_kk(lead_t a1, lead_t k1k2, lead_t a2)
{
  ax_diode_vf_t diode_1;
  ax_diode_vf_t diode_2;
  screen_desc_t screen;
  char value_1 [10];
  char value_2 [10];

  // get the forward and reverse voltages 
  cli_printf("\tDiode LED or Schottky Pair KK?");
  ax_get_vf_vr(a1, k1k2, &diode_1);
  ax_get_vf_vr(a2, k1k2, &diode_2); 


  // work out waht we have 

    if(((diode_1.vf_mv <= AX_DIODE_SCHOTTKY_V) &&    // Vf1 <= 400mV and Vr1 > 900mV
        (diode_1.vr_mv > AX_DIODE_LED_V )) &&
        ((diode_2.vf_mv <= AX_DIODE_SCHOTTKY_V) &&        // Vf2 <= 400mV and Vr2 > 900mV
        (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Schottky pauir KK -----------------------------------
      cli_printf("\t-> Schottky Diode Pair KK");
      cli_printf("\t   Anode 1 = %s, Anode 2 = %s, Common Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(a2), ax_lead_2_text(k1k2));
      screen.screen = &scrn_schottky_pair_kk;
      screen.top_node_id = (scrn_node_id_t)a1;
      screen.left_node_id = (scrn_node_id_t)k1k2;
      screen.bottom_node_id = (scrn_node_id_t)a2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
      beep(A6, BEEP_SHORT);
    }
    else if(((diode_1.vf_mv < AX_DIODE_LED_V) &&          // Vf1 < 900mV and Vr1 > 900mV
      (diode_1.vr_mv > AX_DIODE_LED_V )) && 
      ((diode_2.vf_mv < AX_DIODE_LED_V) &&         // Vf2 < 900mV and Vr2 > 900mV
      (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Diode Pair KK ------------------------------------
     cli_printf("\t-> PN Diode Pair KK");
     cli_printf("\t   Anode 1 = %s, Anode 2 = %s, Common Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(a2), ax_lead_2_text(k1k2));
      screen.screen = &scrn_diode_pair_kk;
      screen.top_node_id = (scrn_node_id_t)a1;
      screen.left_node_id = (scrn_node_id_t)k1k2;
      screen.bottom_node_id = (scrn_node_id_t)a2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
      beep(A6, BEEP_SHORT);
    }
        else if(((diode_1.vf_mv >= AX_DIODE_LED_V) &&          // Vf1 >= 900mV and Vr1 > 900mV
      (diode_1.vr_mv > AX_DIODE_LED_V )) && 
      ((diode_2.vf_mv >= AX_DIODE_LED_V) &&         // Vf2 >= 900mV and Vr2 > 900mV
      (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // LED Pair KK ------------------------------------
     cli_printf("\t-> LED Pair KK");
     cli_printf("\t   Anode 1 = %s, Anode 2 = %s, Common Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(a2), ax_lead_2_text(k1k2));
      screen.screen = &scrn_led_pair_kk;
      screen.top_node_id = (scrn_node_id_t)a1;
      screen.left_node_id = (scrn_node_id_t)k1k2;
      screen.bottom_node_id = (scrn_node_id_t)a2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
      beep(A6, BEEP_SHORT);
    }
  else {
    // bad part ----------------------------------------
    cli_printf("\t-> Unknown or Bad Part");
    screen.screen = &scrn_error;
    screen.string1_override = "Unknown or Bad Part";
    screen.string2_override = "";
    beep(C5, BEEP_LONG);
  }
  cli_printf("\t* (1) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_1.vf_mv, diode_1.i_vf_ua, diode_1.vr_mv, diode_1.i_vr_ua);
  cli_printf("\t* (2) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_2.vf_mv, diode_2.i_vf_ua, diode_2.vr_mv, diode_2.i_vr_ua);
  scrn_draw(&screen);
}

void ax_test_diode_ak(lead_t a1, lead_t k1a2, lead_t k2)
{
  ax_diode_vf_t diode_1;
  ax_diode_vf_t diode_2;
  screen_desc_t screen;
  char value_1 [10];
  char value_2 [10];

  // get the forward and reverse voltages 
  cli_printf("\tDiode or Schottky Pair AK?");
  ax_get_vf_vr(a1, k1a2, &diode_1);
  ax_get_vf_vr(k1a2, k2, &diode_2); 

  // work out waht we have 
  if(((diode_1.vf_mv > AX_DIODE_SCHOTTKY_V) &&          // Vf1 > 400mV and Vr1 > 900mV
      (diode_1.vr_mv > AX_DIODE_LED_V )) && 
      ((diode_2.vf_mv > AX_DIODE_SCHOTTKY_V) &&         // Vf2 > 400mV and Vr2 > 900mV
      (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Diode Pair AK ------------------------------------
      cli_printf("\t-> PN Diode Pair AK");
      cli_printf("\t   Anode 1 = %s, Cathode 1, Anode 2 = %s, Cathode 2 = %s", ax_lead_2_text(a1), ax_lead_2_text(k1a2), ax_lead_2_text(k2));
      screen.screen = &scrn_diode_pair_ak;
      screen.top_node_id = (scrn_node_id_t)a1;
      screen.left_node_id = (scrn_node_id_t)k1a2;
      screen.bottom_node_id = (scrn_node_id_t)k2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
       beep(A6, BEEP_SHORT);
    }
  else if(((diode_1.vf_mv <= AX_DIODE_SCHOTTKY_V) &&    // Vf1 <= 400mV and Vr1 > 900mV
        (diode_1.vr_mv > AX_DIODE_LED_V )) &&
        ((diode_2.vf_mv <= AX_DIODE_SCHOTTKY_V) &&      // Vf2 <= 400mV and Vr2 > 900mV
        (diode_2.vr_mv > AX_DIODE_LED_V ))) {
    // Schottky -----------------------------------------
      cli_printf("\t-> Schottky Diode Pair AK");
      cli_printf("\t   Anode 1 = %s, Cathode 1, Anode 2 = %s, Cathode 2 = %s", ax_lead_2_text(a1), ax_lead_2_text(k1a2), ax_lead_2_text(k2));
      screen.screen = &scrn_diode_pair_ak;
      screen.top_node_id = (scrn_node_id_t)a1;
      screen.left_node_id = (scrn_node_id_t)k1a2;
      screen.bottom_node_id = (scrn_node_id_t)k2;
      ax_print_flt(value_1, diode_1.vf_mv);
      ax_print_flt(value_2, diode_1.i_vf_ua);
      sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
      ax_print_flt(value_1, diode_2.vf_mv);
      ax_print_flt(value_2, diode_2.i_vf_ua);
      sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
      screen.string1_override = ax_line_1_buffer;
      screen.string2_override = ax_line_2_buffer;
       beep(A6, BEEP_SHORT);
    }
  else {
    // bad part ----------------------------------------
    cli_printf("\t-> Unknown or Bad Part");
    screen.screen = &scrn_error;
    screen.string1_override = "Unknown or Bad Part";
    screen.string2_override = "";
    beep(C5, BEEP_LONG);
  }
  cli_printf("\t* (1) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_1.vf_mv, diode_1.i_vf_ua, diode_1.vr_mv, diode_1.i_vr_ua);
  cli_printf("\t* (2) Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
                diode_2.vf_mv, diode_2.i_vf_ua, diode_2.vr_mv, diode_2.i_vr_ua);

  scrn_draw(&screen);
}

void ax_test_diode(lead_t a1, lead_t k1)
{
  ax_diode_vf_t diode;
  screen_desc_t screen;
  char value_1 [10];
  char value_2 [10];

  // get the forward and reverse voltages 
  cli_printf("\tDiode, Zener, Schottky or LED?");
  ax_get_vf_vr(a1, k1, &diode);


  // work out waht we have 
  if((diode.vr_mv < AX_DIODE_ZENER_V) &&                // Vr < 11V 11V & Vf < 900mV
    (diode.vf_mv < AX_DIODE_LED_V )) {
    // Zener ------------------------------------------
    cli_printf("\t-> Zener Diode");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(k1));
    screen.screen = &scrn_zener;
    screen.top_node_id = (scrn_node_id_t)a1;
    screen.bottom_node_id = (scrn_node_id_t)k1;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode.vr_mv);
    ax_print_flt(value_2, diode.i_vr_ua);
    sprintf(ax_line_2_buffer, "Vz = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
  }
  else if((diode.vf_mv > AX_DIODE_LED_V) &&           // Vf > 900mV and Vr > 900mV
        (diode.vr_mv > AX_DIODE_LED_V )){
  // LED ----------------------------------------------
    cli_printf("\t-> LED");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(k1));
    screen.screen = &scrn_led;
    screen.top_node_id = (scrn_node_id_t)a1;
    screen.bottom_node_id = (scrn_node_id_t)k1;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = "";
    beep(A6, BEEP_SHORT);
  }
  else if((diode.vf_mv > AX_DIODE_SCHOTTKY_V) &&      // Vf > 400mV and Vr > 900mV
        (diode.vr_mv > AX_DIODE_LED_V )){
  // Diode --------------------------------------------
    cli_printf("\t-> PN Diode");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(k1));
    screen.screen = &scrn_diode;
    screen.top_node_id = (scrn_node_id_t)a1;
    screen.bottom_node_id = (scrn_node_id_t)k1;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = "";
    beep(A6, BEEP_SHORT);
  }
  else if((diode.vf_mv <= AX_DIODE_SCHOTTKY_V) &&      // Vf <= 400mV and Vr > 900mV
        (diode.vr_mv > AX_DIODE_LED_V )){
  // Schottky -----------------------------------------
    cli_printf("\t-> Schottky Diode");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1), ax_lead_2_text(k1));
    screen.screen = &scrn_schottky;
    screen.top_node_id = (scrn_node_id_t)a1;
    screen.bottom_node_id = (scrn_node_id_t)k1;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = "";
    beep(A6, BEEP_SHORT);
  }
  else {
    // bad part ----------------------------------------
    cli_printf("\t-> Unknown or Bad Part");
    screen.screen = &scrn_error;
    screen.string1_override = "Unknown or Bad Part";
    screen.string2_override = "";
    beep(C5, BEEP_LONG);
  }
  cli_printf("\t* Vf = %+7i mV, If %+7i µA, Vr = %+7i mV, Ir %+7i µA", 
              diode.vf_mv, diode.i_vf_ua, diode.vr_mv, diode.i_vr_ua);
  scrn_draw(&screen);
}

void ax_test_diode_b2b(lead_t a1k2, lead_t k1a2, lead_t nc)
{
  ax_diode_vf_t diode;
  screen_desc_t screen;
  char value_1 [10];
  char value_2 [10];

  // get the forward and reverse voltages 
  cli_printf("\tB2B Diodes, B2B LEDs or Zener?");
  ax_get_vf_vr(a1k2, k1a2, &diode);

  // work out waht we have 
  if((diode.vf_mv > AX_DIODE_LED_V) &&           // Vf1 > 900mV and Vf2 > 900mV
          (diode.vr_mv > AX_DIODE_LED_V )){
    // b2b Led -----------------------------------------
    cli_printf("\t-> LED Pair B2B");
    cli_printf("\t   Anode 1, Cathode 2 = %s, Cathode 2, Anode 1 = %s", ax_lead_2_text(a1k2), ax_lead_2_text(k1a2));
    screen.screen = &scrn_led_b2b;
    screen.top_node_id = (scrn_node_id_t)a1k2;
    screen.bottom_node_id = (scrn_node_id_t)k1a2;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode.vr_mv);
    ax_print_flt(value_2, diode.i_vr_ua);
    sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
  }
  else if((diode.vf_mv > AX_DIODE_LED_V) &&           // Vf1 > 900mV & Vf2 < 900mV
          (diode.vr_mv < AX_DIODE_LED_V )) {
    // Zener ------------------------------------------
    cli_printf("\t-> Zener Diode");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(k1a2), ax_lead_2_text(a1k2));
    screen.screen = &scrn_zener;
    screen.top_node_id = (scrn_node_id_t)k1a2;
    screen.bottom_node_id = (scrn_node_id_t)a1k2;
    ax_print_flt(value_1, diode.vr_mv);
    ax_print_flt(value_2, diode.i_vr_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_2_buffer, "Vz = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
  }
  else if((diode.vf_mv < AX_DIODE_LED_V) &&           // Vf1 < 900mV &  Vf2 > 900mV
          (diode.vr_mv > AX_DIODE_LED_V )){
    // Zener -----------------------------------------
    cli_printf("\t-> Zener Diode");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1k2), ax_lead_2_text(k1a2));
    screen.screen = &scrn_zener;
    screen.top_node_id = (scrn_node_id_t)a1k2;
    screen.bottom_node_id = (scrn_node_id_t)k1a2;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode.vr_mv);
    ax_print_flt(value_2, diode.i_vr_ua);
    sprintf(ax_line_2_buffer, "Vz = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
  }
  else if(((diode.vf_mv > AX_ID_SHORT_V) &&              // 100mV < Vf1 < 900mV
          (diode.vf_mv < AX_DIODE_LED_V)) &&         // and 
          ((diode.vr_mv > AX_ID_SHORT_V) &&              // 100mV < Vf2 < 900mV 
          (diode.vr_mv < AX_DIODE_LED_V))){
    // b2B diode -------------------------------------
    cli_printf("\t-> Diode Pair B2B");
    cli_printf("\t   Anode = %s, Cathode = %s", ax_lead_2_text(a1k2), ax_lead_2_text(k1a2));
    screen.screen = &scrn_diode_b2b;
    screen.top_node_id = (scrn_node_id_t)a1k2;
    screen.bottom_node_id = (scrn_node_id_t)k1a2;
    ax_print_flt(value_1, diode.vf_mv);
    ax_print_flt(value_2, diode.i_vf_ua);
    sprintf(ax_line_1_buffer, "Vf1 = %sV @ %smA", value_1, value_2);
    ax_print_flt(value_1, diode.vr_mv);
    ax_print_flt(value_2, diode.i_vr_ua);
    sprintf(ax_line_2_buffer, "Vf2 = %sV @ %smA", value_1, value_2);
    screen.string1_override = ax_line_1_buffer;
    screen.string2_override = ax_line_2_buffer;
    beep(A6, BEEP_SHORT);
  }
  else {
    // bad part --------------------------------------
    cli_printf("\t-> Unknown or Bad Part");
    screen.screen = &scrn_error;
    screen.string1_override = "Unknown or Bad Part";
    screen.string2_override = "";
    beep(C5, BEEP_LONG);
  }
  cli_printf("\t* (1) Vf = %+7i mV, If %+7i µA", diode.vf_mv, diode.i_vf_ua);
  cli_printf("\t* (2) Vf = %+7i mV, If %+7i µA", diode.vr_mv, diode.i_vr_ua);
  scrn_draw(&screen);
}