int main(int argc, char *argv[]) {
  if (argc != 2) {
    puts("Path to COM port required.\n");
    return 1;
  }
  if (ow_init(argv[1])) {
    puts("Bus INIT failed. Check COM port.\n");
    return 1;
  }

  uint8_t c = 0, diff = OW_SEARCH_FIRST;
  int16_t temp_dc;

  while (diff != OW_LAST_DEVICE) {
    DS18X20_find_sensor(&diff, id);
    if (diff == OW_ERR_PRESENCE) {
      puts("All sensors are offline now.\n");
      ow_finit();
      return 1;
    }
    if (diff == OW_ERR_DATA) {
      puts("Bus error.\n");
      ow_finit();
      return 1;
    }
    fprintf(stdout, "Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ", \
           argv[1], c, id[0], get_type_by_id(id[0]), id[6], id[5], id[4], id[3], id[2], id[1], id[7]);
    fflush(stdout);
    c ++;

    if (DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL) == DS18X20_OK) {
      while (DS18X20_conversion_in_progress() == DS18X20_CONVERTING) {
        delay_ms(100); /* It will take a while */
      }
      if (DS18X20_read_decicelsius(id, &temp_dc) == DS18X20_OK) {
        /* Copied from my MCU code, so no float point */
        fprintf(stdout, "TEMP %3d.%01d C\n", temp_dc / 10, temp_dc > 0 ? temp_dc % 10 : -temp_dc % 10);
        continue;
      }
    }

    puts("MEASURE FAILED!\n");

  }
  puts("Sensors listed.\n");

  ow_finit();
  return 0;
}
示例#2
0
int main(int argc, char **argv)
{
  int not, bnot, error;
  int xdim, ydim;
  int topol, neigh;
  float alpha1, radius1;
  int fixed, weights;
  float alpha2, radius2;
  float qerror, qerrorb;
  char *in_data_file, *in_test_file, *out_code_file, *alpha_s;
  struct entries *data = NULL;
  struct entries *testdata = NULL;
  struct entries *codes = NULL;
  struct entries *codess = NULL;
  struct entries *tmp;
  struct teach_params params;
  long buffer, length1, length2, noc, nod;
  int qmode;
  struct typelist *type_tmp;

  error = 0;
  global_options(argc, argv);
  print_description();

  not = get_int("Give the number of trials", 0);
  in_data_file = get_str("Give the input data file name");
  in_test_file = get_str("Give the input test file name");
  out_code_file = get_str("Give the output map file name");

  topol = topol_type(get_str("Give the topology type"));
  if (topol == TOPOL_UNKNOWN) {
    ifverbose(2)
      fprintf(stderr, "Unknown topology type, using hexagonal\n");
    topol = TOPOL_HEXA;
  }
  neigh = neigh_type(get_str("Give the neighborhood type"));
  if (neigh == NEIGH_UNKNOWN) {
    ifverbose(2)
      fprintf(stderr, "Unknown neighborhood type, using bubble\n");
    neigh = NEIGH_BUBBLE;
  }

  xdim = get_int("Give the x-dimension", 0);
  ydim = get_int("Give the y-dimension", 0);

  length1 = get_int("Give the training length of first part", 0);
  alpha1 = get_float("Give the training rate of first part", 0.0);
  radius1 = get_float("Give the radius in first part", 0.0);
  length2 = get_int("Give the training length of second part", 0);
  alpha2 = get_float("Give the training rate of second part", 0.0);
  radius2 = get_float("Give the radius in second part", 0.0);

  printf("\n");

  fixed = (int) oatoi(extract_parameter(argc, argv, FIXPOINTS, OPTION), 0);
  weights = (int) oatoi(extract_parameter(argc, argv, WEIGHTS, OPTION), 0);
  buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0);
  alpha_s = extract_parameter(argc, argv, "-alpha_type", OPTION);
  qmode = oatoi(extract_parameter(argc, argv, "-qetype", OPTION), 0);

  use_fixed(fixed);
  use_weights(weights);

  label_not_needed(1);

  ifverbose(2)
    fprintf(stderr, "Input entries are read from file %s\n", in_data_file);
  data = open_entries(in_data_file);
  if (data == NULL)
    {
      fprintf(stderr, "Can't open data file '%s'\n", in_data_file);
      error = 1;
      goto end;
    }
  set_buffer(data, buffer);

  ifverbose(2)
    fprintf(stderr, "Test entries are read from file %s\n", in_test_file);
  testdata = open_entries(in_test_file);
  if (testdata == NULL)
    {
      fprintf(stderr, "Can't open test data file '%s'\n", in_test_file);
      error = 1;
      goto end;
    }
  set_buffer(testdata, buffer);

  noc = xdim * ydim;
  if (noc <= 0) 
    {
      fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim);
      error = 1;
      goto end;
    }
  if (xdim < 0) 
    {
      fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim);
      error = 1;
      goto end;
    }

  if (alpha_s)
    {
      type_tmp = get_type_by_str(alpha_list, alpha_s);
      if (type_tmp->data == NULL)
	{
	  fprintf(stderr, "Unknown alpha type %s\n", alpha_s);
	  error = 1;
	  goto end;
	}
    }
  else
    type_tmp = get_type_by_id(alpha_list, ALPHA_LINEAR);

  params.alpha_type = type_tmp->id;
  params.alpha_func = type_tmp->data;


  codess = NULL;
  qerrorb = FLT_MAX;
  bnot = 0;
  while (not) {
    init_random(not);

    ifverbose(2)
      fprintf(stderr, "Initializing codebook\n");
    
    codes = randinit_codes(data, topol, neigh, xdim, ydim);
    if (codes == NULL)
      {
	fprintf(stderr, "Error initializing random codebook, aborting\n");
	error = 1;
	goto end;
      }

    set_teach_params(&params, codes, NULL, 0);
    set_som_params(&params);
    params.data = data;
    
    params.length = length1;
    params.alpha = alpha1;
    params.radius = radius1;

    ifverbose(2)
      fprintf(stderr, "Training map, first part, rlen: %ld alpha: %f\n", 
	      params.length, params.alpha);
    codes = som_training(&params);

    params.length = length2;
    params.alpha = alpha2;
    params.radius = radius2;
    ifverbose(2)
      fprintf(stderr, "Training map, second part, rlen: %ld alpha: %f\n", 
	      params.length, params.alpha);
    codes = som_training(&params);

    params.data = testdata;
    ifverbose(2)
      fprintf(stderr, "Calculating quantization error\n");

    if (qmode > 0)
      qerror = find_qerror2(&params);
    else
      qerror = find_qerror(&params);
    nod = testdata->num_entries;

    if (qerror < qerrorb) {
      qerrorb = qerror;
      bnot = not;

      tmp = codess;
      codess = codes;
      codes = tmp;
    }

    close_entries(codes);
    codes = NULL;
    ifverbose(1)
      fprintf(stderr, "%3d: %f\n", not, qerror/(float) nod);
    not--;
  }

  if (codess != NULL) 
    {
      ifverbose(2)
	fprintf(stdout, "Codebook entries are saved to file %s\n", out_code_file);
      save_entries(codess, out_code_file);
      ifverbose(1)
	fprintf(stdout, "Smallest error with random seed %3d: %f\n",
                bnot, qerrorb/(float) nod);
    }

 end:
  if (codess)
    close_entries(codess);
  if (data)
    close_entries(data);
  if (testdata)
    close_entries(testdata);
  
  return(error);
}