/* Title: inductor.c
 * Author: Jed Marti KI7NNP
 * Description: Find the best inductor measurements for a requested inductance.
 * Revision History: (Created Sat Jan 27 12:23:34 2024)
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>

#include "inductor.h"
#include "equations.h"

// See cmdline.c
extern int32_t W;
extern double maxlength;
extern double minlength;
extern double maxradius;
extern double minradius;
extern int32_t minturns;
extern int32_t maxturns;
extern double uh;
extern uint8_t E;

double WDIA;

int32_t main(int32_t argc, char *argv[])
{
  printf("inductor V%d.%02d.%03d\n", VERSION, RELEASE, PATCH);
  cmdline(argc, argv);

  // Validate the values. If no inductance, just punt.
  if (uh == -1.0)
  {
    fprintf(stderr, "!!!!! Missing inductance in microHenryies\n");
    exit(-1);
  }

  // Length ranges.
  if (maxlength < minlength)
  {
    fprintf(stderr, "maxlength %g\" < minlength %g\n", meters2inches(maxlength),
            meters2inches(minlength));
    exit(-1);
  }

  // Radius ranges.
  if (maxradius <  minradius)
  {
    fprintf(stderr, "maxradius %g\" < minradius %g\n", meters2inches(maxradius),
            meters2inches(minradius));
    exit(-1);
  }

  // Wire diameter failure.
  if ((WDIA = gaugeDiameter(W)) == -1.0)
  {
    fprintf(stderr, "Wire gauge %d unknown to this system\n", W);
    exit(-1);
  }
  WDIA = inches2meters(WDIA);

  // Turns range.
  if (minturns > maxturns)
  {
    fprintf(stderr, "minimum number of turns %d greater than maximum number of turns %d\n",
            minturns, maxturns);
    exit(-1);
  }


  // Go for the run.
  findBest();
}


// EOF
