Exemplo n.º 1
0
int main(void) 
{
    int fahr;
    for (fahr = 0; fahr <= 300; fahr += 20)
        printf("%3d %6.1f\n", fahr, fahr2celsius(fahr));
    return 0;
}
int printFahrList(int start, int end, int step, int withHead) {
  if (start <= end && step < 0 || start >= end && step > 0 || step == 0) {
    printf("params error!\n");
    return 1;
  }

  if (withHead) {
    printf("fahr\tcelsius\n");
  }

  int i = start;
  while (start <= end ? i <= end : i >= end) {
    printf("%d\t%d\n", i, fahr2celsius(i));
    i += step;
  }
}
Exemplo n.º 3
0
/* 当 fahr = 0, 20, ……, 300 时,分别打印华氏温度与摄氏温度对照表;
 * 浮点数版本 */
main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;   /* 温度表的下限 */
    upper = 300; /* 温度表的上限 */
    step = 20;   /* 步长 */

    fahr = lower;
    while (fahr <= upper) {
        celsius = fahr2celsius(fahr);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
Exemplo n.º 4
0
void write_recorded_data_USB502(configurationBlock *cblock, usb502_data *data_502 )
{
  /* Create CVS file to be read into OpenOffice.  When reading file, choose file type as TEXT cvs */
  char filename[80];
  FILE *fp;
  int i;
  float t, logew, dewPoint;
  struct tm ltime;
  char dates[40];
 
  printf("Enter filename: ");
  scanf("%s", filename);
  fp = fopen(filename, "w");
 
  fprintf(fp, "\"Name\",\"%s\"\n", cblock->name);
  fprintf(fp, "\"Serial Number\",\"%ld\"\n", cblock->serialNumber);
  fprintf(fp, "\"Model\",\"USB-502\"\n");
  if (cblock->inputType == 0) {
    fprintf(fp, "\"Sample\",\"Date/Time\",\"Temperature (C)\",\"Humidity\",\"Dew point (C)\"\n");
  } else {
    fprintf(fp, "\"Sample\",\"Date/Time\",\"Temperature (F)\",\"Humidity\",\"Dew point (F)\"\n");
  }
  for (i = 0; i < cblock->sampleCount; i++) {
    localtime_r(&data_502[i].time, &ltime);
    strftime(dates, 79, "%D %r", &ltime);
    if (cblock->inputType == 0) {
      t = data_502[i].temperature;
    } else {
      t = fahr2celsius(data_502[i].temperature);
    }
    logew = (0.66077 +(7.5*t/(237.3 + t))) + (log10(data_502[i].humidity) - 2.0);
    dewPoint = ((0.66077 - logew)*237.3) / (logew - 8.16077);
    if (cblock->inputType == 1) dewPoint = celsius2fahr(dewPoint);
    strftime(dates, 79, "%D %r", &ltime);
    fprintf(fp,"%5d,%s,%.2f,%.1f,%.1f\n", i+1, dates, data_502[i].temperature, data_502[i].humidity, dewPoint);
  }
  fclose(fp);
}
Exemplo n.º 5
0
int main( void )
{
  usb_dev_handle *udev = NULL;
  configurationBlock cb;
  time_t currentTime;
  struct tm ltime;
  float t, logew, dewPoint;
  int i, j;
  usb501_data data_501[USB500_MAX_VALUES];
  usb502_data data_502[USB500_MAX_VALUES];
  usb503_data data_503[USB500_MAX_VALUES];
  usb504_data data_504[USB500_MAX_VALUES];  
  char dates[40];
  char name[16];
  char ans[80];
  int type;

  udev = usb_device_find_USB500(USB500_VID, USB500_PID);
  if (udev) {
    printf("Success, found a USB 500!\n");
    printf("Size of configuration block: %d\n", (int) sizeof(cb));
  } else {
    printf("Failure, did not find a USB 500!\n");
    return 0;
  }
  printf("\n\n");

  while (1) {
    printf("1. Status\n");
    printf("2. Configure USB 500 device for logging\n");
    printf("3. Download data\n");
    printf("4. Repair SRAM\n");
    printf("5. Exit\n");
    printf("Please select from 1-5:  \n");

    scanf("%s", ans);
    switch(ans[0]) {
      case '1':
        type = read_configuration_block_USB500(udev, &cb);
	printf("\n\n");
	switch(type) {
	  case 1:  printf("Device:\t\t\t USB-501 Temperature Logger (version 1.6 and earlier)\n"); break;
	  case 2:  printf("Device:\t\t\t USB-501 Temperature Logger (version 1.7 and later)\n"); break;
	  case 3:  printf("Device:\t\t\t USB-502 Humidity and Temperature Logger\n"); break;
	  case 4:  printf("Device:\t\t\t USB-503 Voltage Logger\n"); break;
    	  case 5:  printf("Device:\t\t\t USB-504 Current Logger\n"); break;
	  case 6:  printf("Device:\t\t\t USB-503B Voltage Logger\n"); break;
    	  case 7:  printf("Device:\t\t\t USB-504B Current Logger\n"); break;
          default: printf("Device:\t\t\t Unkown\n"); break;
	}
	printf("Device Name:\t\t %s\n", cb.name);
	if (cb.flagBits & LOGGING_STATE) {
          if (cb.startTimeOffset == 0) {
	    printf("Status:\t\t\t Logging\n");
	  } else {
	    printf("Status:\t\t\t Delayed Start\n");
            printf("Delay:\t\t\t %ld\n", cb.startTimeOffset);
	  }
	} else {
  	  printf("Status:\t\t\t Off\n");
	}

        ltime.tm_sec = cb.startTimeSeconds;
        ltime.tm_min = cb.startTimeMinutes;
        ltime.tm_hour = cb.startTimeHours;
        ltime.tm_mday = cb.startTimeDay;
        ltime.tm_mon = cb.startTimeMonth - 1;
        ltime.tm_year = cb.startTimeYear + 100;
        strftime(dates, 79, "%m/%d/%Y", &ltime);
	printf("Start Date:\t\t %s\n", dates);
        strftime(dates, 79, "%r", &ltime);
	printf("Start Time:\t\t %s\n", dates);

	printf("Number of Readings:\t %-d\n", cb.sampleCount);
	printf("Sample Interval:\t %d seconds\n", cb.sampleRate);
	if (type < 4) {            // USB-501 or USB-502
  	  if (cb.inputType == 0) { // Celsius
	    printf("Scale:\t\t\t Celsius");
	  } else {
	    printf("Scale:\t\t\t Fahrenheit");
	  }
	  if (cb.type == 3) {
	    printf(", %%rh\n");
	  } else {
	    printf("\n");
	  }
	} else {                   // USB-503 or USB-504
	  if (cb.inputType == 0) {
	    printf("Calibration:\t\t Standard Calibration.\n");
	  } else {
	    printf("Calibration:\t\t Custom Calibration.\n");
	  }
	  if (cb.flagBits2 & 0x1) {
	    printf("LEDs:\t\t\t Disabled to save battery power.\n");
	  } else {
	    printf("LEDs:\t\t\t Enabled.\n");
	  }
	  if (cb.flagBits2 & 0x2) {
	    printf("Scale:\t\t\t Negative gradient scaling.\n");
	  } else {
	    printf("Scale:\t\t\t Normal positive gradient scaling.\n");
	  }
	}
	if (cb.flagBits & HIGH_ALARM_STATE) {
	  printf("High Alarm:\t\t Enabled\n");
	} else {
	  printf("High Alarm:\t\t Disabled\n");	  
	}
	if (cb.flagBits & LOW_ALARM_STATE) {
	  printf("Low Alarm:\t\t Enabled\n");
	} else {
	  printf("Low Alarm:\t\t Disabled\n");	  
	}
	printf("Serial Number:\t\t %ld\n", cb.serialNumber);
	strncpy(name, cb.version, 4);
	printf("Firmware Version:\t %s\n", name);
	if (type >= 4) {
	  printf("Measurement Unit:\t %s.\n", cb.displayUnitText);
	  printf("User Calibration Input 1: %s.\n", cb.calibrationInput1Text);
  	  printf("User Calibration Output 1: %s.\n", cb.calibrationInput1Text);
  	  printf("User Calibration Input 2: %s.\n", cb.calibrationOutput2Text);
    	  printf("User Calibration Output 2: %s.\n", cb.calibrationOutput2Text);
	  printf("Scaling Factor:\t\t %f\n", cb.capScalingFactor);
	  printf("High Alarm Level Text: %s.\n", cb.highAlarmLevelText);
	  printf("Low Alarm Level Text: %s.\n", cb.lowAlarmLevelText);
	  printf("Default Range Description Text: %s.\n", cb.defaultRangeDescriptionText);
	  printf("Default Input Unit Text: %s.\n", cb.defaultInputUnitText);
	  printf("Default Display Unit: %s.\n", cb.defaultDisplayUnit);
	  printf("Default Calibration Input 1 Text: %s.\n", cb.defaultCalibrationInput1Text);
	  printf("Default Calibration Output 1 Text: %s.\n", cb.defaultCalibrationOutput1Text);
	  printf("Default Calibration Input 2 Text: %s.\n", cb.defaultCalibrationInput2Text);
	  printf("Default Calibration Output 2 Text: %s.\n", cb.defaultCalibrationOutput2Text);
	  printf("Default High Alarm Level Text: %s.\n", cb.defaultHighAlarmLevelText);
	  printf("Default Low Alarm Level Text: %s.\n", cb.defaultLowAlarmLevelText);
	}
	printf("\n\n");
	break;
      case '2':
        stop_logging_USB500(udev, &cb);
        /* initialize the parameter block */

        // sanity check
        printf("cb.type = %d\n", cb.type);
        printf("serial number = %ld\n", cb.serialNumber);
	cb.command = 0x0;
	printf("Device name = \"%s\": ",cb.name);
	tcflush(0, TCIOFLUSH);
	getchar();
	fgets(name, 15, stdin);
	if (name[0] != '\n') {
  	  cb.name[15] = '\0';  // null terminate
	  j = 0;
	  for (i = 0; i < strlen(cb.name); i++) {
	    switch (name[i]) {
	    case '\r':
	    case '\n':
	    case '%':
	    case '&':
	    case '*':
	    case ',':
	    case '.':
	    case '/':
	    case ':':
	    case '<':
	    case '>':
	    case '?':
	    case '\\':
	    case '(':
	    case ')':
	      break;
	    default:
	      cb.name[j++] = name[i];
	    }
	  }
	}
        if (type < 4) {
  	  printf("0 = Celsius, 1 = Fahrenheit: ");
	  scanf("%hhd", &cb.inputType);
	  if (cb.inputType == 0) { // Celsius
	    cb.calibrationMValue = 0.5;
	    cb.calibrationCValue = -40;
	  } else {                // Fahrenheit
	    cb.inputType = 1;
	    cb.calibrationMValue = 1.0;
	    cb.calibrationCValue = -40;
	  }
	}

	printf("Enter sample rate [seconds/sample].  Must be > 10:  ");
	scanf("%hd", &cb.sampleRate);
	if (cb.sampleRate < 10) cb.sampleRate = 10;
        write_configuration_block_USB500(udev, &cb);

        /* uncomment to set alarms */
        set_alarm(udev, &cb);
	
        time(&currentTime);
	printf("Setting Device time to: %s and start logging\n", ctime(&currentTime));
        start_logging_USB500(udev, &cb); 
	break;

      case '3':
        type = read_configuration_block_USB500(udev, &cb);
        switch(type) {
          case 1:
          case 2:
            read_recorded_data_USB501(udev, &cb, data_501);
   	    printf("\n\nModel: USB-501\n");
	    if (cb.inputType == 0) {
	      printf("Sample         Date/Time            Temperature (C) \n");
	    } else {
	      printf("Sample         Date/Time            Temperature (F) \n");
	    }
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_501[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_501[i].temperature);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB501(&cb, data_501);
	    break;
          case 3:
            read_recorded_data_USB502(udev, &cb, data_502);
	    printf("\n\nModel: USB-502\n");
	    if (cb.inputType == 0) {
	      printf("Sample         Date/Time            Temperature (C)       Humidity    Dew point (C)\n");
  	    } else {
	      printf("Sample         Date/Time            Temperature (F)       Humidity    Dew point (F)\n");
	    }
	    for (i = 0; i < cb.sampleCount; i++) {
	      localtime_r(&data_502[i].time, &ltime);
	      if (cb.inputType == 0) {
	        t = data_502[i].temperature;
	      } else {
	        t = fahr2celsius(data_502[i].temperature);
	      }
	      logew = (0.66077 +(7.5*t/(237.3 + t))) + (log10(data_502[i].humidity) - 2.0);
	      dewPoint = ((0.66077 - logew)*237.3) / (logew - 8.16077);
	      if (cb.inputType == 1) dewPoint = celsius2fahr(dewPoint);
	      strftime(dates, 79, "%D %r", &ltime);
	      printf("%5d    %s           %.2f               %.1f        %.1f\n",
		     i+1, dates, data_502[i].temperature, data_502[i].humidity, dewPoint);
	    }	
	    printf("\n\n");
	    printf("Save to file? [y/n] ");
	    scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB502(&cb, data_502);
	    break;
          case 4:
          case 6:
            read_recorded_data_USB503(udev, &cb, data_503);
	    printf("\n\nModel: USB-503\n");
	    printf("Sample         Date/Time            Voltage (V) \n");
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_503[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_503[i].voltage);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB503(&cb, data_503);
	    break;
          case 5:
          case 7:
	    read_recorded_data_USB504(udev, &cb, data_504);
	    printf("Sample         Date/Time            Current (mA)\n");
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_504[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_504[i].current);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB504(&cb, data_504);
	    break;
	}
        break;

      case '4':
        printf("******* WARNING*******\n");
        printf("Only proceed if your device is not working.\n");
        printf("Do you want to proceed? [y/n]");
        scanf("%s", ans);
        if (ans[0] == 'n') break;
        /* get the serial number and software version */
        read_configuration_block_USB500(udev, &cb);
        printf("    1. USB-501 version 1.6 and earlier.\n");
        printf("    2. USB-501 version 1.7 and later.\n");
        printf("    3. USB-502\n");
        printf("Enter your Device type [1-3]: \n");
        scanf("%hhd", &cb.type);
        strncpy(cb.name, "EasyLog USB", 15);
	// strncpy(cb.version, "v2.0", 4);
        cb.inputType = 1;
        cb.command = 0x0;
        cb.calibrationMValue = 1.0;
	cb.calibrationCValue = -40;
        cb.sampleRate = 60;
        cb.flagBits = 0x0;
        time(&currentTime);
        localtime_r(&currentTime, &ltime);	
        cb.startTimeHours = ltime.tm_hour;
        cb.startTimeMinutes = ltime.tm_min;
	cb.startTimeSeconds = ltime.tm_sec;
	cb.startTimeDay = ltime.tm_mday;
	cb.startTimeMonth = ltime.tm_mon + 1;    // months from 1-12
	cb.startTimeYear = ltime.tm_year - 100;  // years start from 2000 not 1900.
        cb.alarm1.channel1.highAlarm = 0x0;
        cb.alarm1.channel1.lowAlarm = 0x0;
        write_configuration_block_USB500(udev, &cb);
	break;
      case '5':
	printf("Remove unit from USB port.\n");
	cleanup_USB500(udev);
        return 0;
        break;
    }
  }
}