예제 #1
0
파일: pcsensor.c 프로젝트: raff/temper
usb_dev_handle* pcsensor_open(){
	usb_dev_handle *lvr_winusb = NULL;
	char buf[256];
	int i, ret;

	if (!(lvr_winusb = setup_libusb_access())) {
		return NULL;
	} 

	switch(device_type(lvr_winusb)){
	case 0:
		control_transfer(lvr_winusb, uCmd1 );
		control_transfer(lvr_winusb, uCmd3 );
		control_transfer(lvr_winusb, uCmd2 );
		ret = get_data(lvr_winusb, buf, 256);
		if(debug){	
			printf("Other Stuff (%d bytes):\n", ret);
			for(i = 0; i < ret; i++) {
				printf(" %02x", buf[i] & 0xFF);
				if(i % 16 == 15) {
					printf("\n");
				}
			}
			printf("\n");
		}
		break;
	case 1:
		if (ini_control_transfer(lvr_winusb) < 0) {
			fprintf(stderr, "Failed to ini_control_transfer (device_type 1)");
			return NULL;
		}
      
		control_transfer(lvr_winusb, uTemperatura );
		interrupt_read(lvr_winusb);
 
		control_transfer(lvr_winusb, uIni1 );
		interrupt_read(lvr_winusb);
 
		control_transfer(lvr_winusb, uIni2 );
		interrupt_read(lvr_winusb);
		interrupt_read(lvr_winusb);
		break;
	}

	if(debug){
		printf("device_type=%d\n", device_type(lvr_winusb));
	}
	return lvr_winusb;
}
예제 #2
0
int main( int argc, char **argv) {
 
     usb_dev_handle *lvr_winusb = NULL;
     float tempc[2];
     int c, i;
     struct tm *local;
     time_t t;
     int nr_of_sensors = 1;

     while ((c = getopt (argc, argv, "mfcvhl::a:n:")) != -1)
     switch (c)
       {
       case 'v':
         debug = 1;
         break;
       case 'c':
         formato=1; //Celsius
         break;
       case 'f':
         formato=2; //Fahrenheit
         break;
       case 'm':
         mrtg=1;
         break;
       case 'l':
         if (optarg!=NULL){
           if (!sscanf(optarg,"%i",&seconds)==1) {
             fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
             exit(EXIT_FAILURE);
           } else {           
              bsalir = 0;
              break;
           }
         } else {
           bsalir = 0;
           seconds = 5;
           break;
         }
       case 'a':
         if (!sscanf(optarg,"%i",&calibration)==1) {
             fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
             exit(EXIT_FAILURE);
         } else {           
              break;
         }
       case 'n':
         if (!sscanf(optarg,"%i",&nr_of_sensors)==1) {
             fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
             exit(EXIT_FAILURE);
         } 
         else 
         {           
           if ((nr_of_sensors > 2) || (nr_of_sensors < 1))
           {
             fprintf (stderr, "Error: '%s' is not in range [1..2].\n", optarg);
             exit(EXIT_FAILURE);
           }
              break;
         }
       case '?':
       case 'h':
         printf("pcsensor version %s\n",VERSION);
	 printf("      Aviable options:\n");
	 printf("          -h help\n");
	 printf("          -v verbose\n");
	 printf("          -l[n] loop every 'n' seconds, default value is 5s\n");
	 printf("          -c output only in Celsius\n");
	 printf("          -f output only in Fahrenheit\n");
	 printf("          -a[n] increase or decrease temperature in 'n' degrees for device calibration\n");
	 printf("          -m output for mrtg integration\n");
	 printf("	   -n[n] read number of sensors [1..2]\n");
  
	 exit(EXIT_FAILURE);
       default:
         if (isprint (optopt))
           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
         else
           fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
         exit(EXIT_FAILURE);
       }

     if (optind < argc) {
        fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
        exit(EXIT_FAILURE);
     }
 
     if ((lvr_winusb = setup_libusb_access()) == NULL) {
         exit(EXIT_FAILURE);
     } 

     (void) signal(SIGINT, ex_program);

     ini_control_transfer(lvr_winusb);
      
     control_transfer(lvr_winusb, uTemperatura );
     interrupt_read(lvr_winusb);
 
     control_transfer(lvr_winusb, uIni1 );
     interrupt_read(lvr_winusb);
 
     control_transfer(lvr_winusb, uIni2 );
     interrupt_read(lvr_winusb);
     interrupt_read(lvr_winusb);


 
     do {
           control_transfer(lvr_winusb, uTemperatura );
           interrupt_read_temperature(lvr_winusb, tempc);

           t = time(NULL);
           local = localtime(&t);

           if (mrtg) {
              if (formato==2) {
                  for (i=0;i<nr_of_sensors; i++) 
                  {
                    printf("%.2f\n", (9.0 / 5.0 * tempc[i] + 32.0));
                    printf("%.2f\n", (9.0 / 5.0 * tempc[i] + 32.0));
                  }
              } else {
                  for (i=0;i<nr_of_sensors; i++) 
                  {
                    printf("%.2f\n", tempc[i]);
                    printf("%.2f\n", tempc[i]);
                  }
              }
              
              printf("%02d:%02d\n", 
                          local->tm_hour,
                          local->tm_min);

              printf("pcsensor\n");
           } else {
              for (i=0;i<nr_of_sensors; i++) 
              {
                printf("%04d/%02d/%02d %02d:%02d:%02d ", 
                            local->tm_year +1900, 
                            local->tm_mon + 1, 
                            local->tm_mday,
                            local->tm_hour,
                            local->tm_min,
                            local->tm_sec);

                if (formato==2) {
                    printf("Temperature%d %.2fF\n", i, (9.0 / 5.0 * tempc[i] + 32.0));
                } else if (formato==1) {
                    printf("Temperature%d %.2fC\n", i, tempc[i]);
                } else {
                    printf("Temperature%d %.2fF %.2fC\n", i, (9.0 / 5.0 * tempc[i] + 32.0), tempc[i]);
                }
             }
          }       
           if (!bsalir)
              sleep(seconds);
     } while (!bsalir);
                                       
     usb_release_interface(lvr_winusb, INTERFACE1);
     usb_release_interface(lvr_winusb, INTERFACE2);
     
     usb_close(lvr_winusb); 
      
     return 0; 
}
예제 #3
0
int main( int argc, char **argv) {

     usb_dev_handle *lvr_winusb = NULL;
     float tempc;
     float tempc_measure_offset = 0.0;
     int c;
     struct tm *local;
     time_t t;

     while ((c = getopt (argc, argv, "mnfcvhl::s::")) != -1)
     switch (c)
       {
       case 'v':
         debug = 1;
         break;
       case 'c':
         formato=1; //Celsius
         break;
       case 'f':
         formato=2; //Fahrenheit
         break;
       case 's':
         if (!sscanf(optarg,"%f",&tempc_measure_offset)==1) {
           fprintf (stderr, "Error: '%s' is not (float) numeric.\n", optarg);
           exit(EXIT_FAILURE);
         }
         if (tempc_measure_offset > 100000 || tempc_measure_offset < -100000) {
           fprintf (stderr, "Error: please provide a reasonable offset\n");
           exit(EXIT_FAILURE);
         }
       case 'n':
         formato=3;
         break;
       case 'm':
         mrtg=1;
         break;
       case 'l':
         if (optarg!=NULL){
           if (!sscanf(optarg,"%i",&seconds)==1) {
             fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
             exit(EXIT_FAILURE);
           } else {
              bsalir = 0;
              break;
           }
         } else {
           bsalir = 0;
           seconds = 5;
           break;
         }
       case '?':
       case 'h':
         printf("pcsensor version %s\n",VERSION);
	 printf("      Aviable options:\n");
	 printf("          -h help\n");
	 printf("          -v verbose\n");
	 printf("          -l[n] loop every 'n' seconds, default value is 5s\n");
	 printf("          -s<f> substract 'f' °C (float) from measured temperature\n");
	 printf("          -c output only in Celsius\n");
	 printf("          -f output only in Fahrenheit\n");
	 printf("          -m output for mrtg integration\n");
	 printf("          -n only display value in Celsius for Nagios\n");

	 exit(EXIT_FAILURE);
       default:
         if (isprint (optopt))
           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
         else
           fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
         exit(EXIT_FAILURE);
       }

     if (optind < argc) {
        fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
        exit(EXIT_FAILURE);
     }

     if ((lvr_winusb = setup_libusb_access()) == NULL) {
         exit(EXIT_FAILURE);
     }

     (void) signal(SIGINT, ex_program);

     ini_control_transfer(lvr_winusb);

     control_transfer(lvr_winusb, uTemperatura );
     interrupt_read(lvr_winusb);

     control_transfer(lvr_winusb, uIni1 );
     interrupt_read(lvr_winusb);

     control_transfer(lvr_winusb, uIni2 );
     interrupt_read(lvr_winusb);
     interrupt_read(lvr_winusb);



     do {
           control_transfer(lvr_winusb, uTemperatura );
           interrupt_read_temperatura(lvr_winusb, &tempc);
           tempc = tempc - tempc_measure_offset;

           t = time(NULL);
           local = localtime(&t);

           if (mrtg) {
              if (formato==2) {
                  printf("%.2f\n", (9.0 / 5.0 * tempc + 32.0));
                  printf("%.2f\n", (9.0 / 5.0 * tempc + 32.0));
              } else {
                  printf("%.2f\n", tempc);
                  printf("%.2f\n", tempc);
              }

              printf("%02d:%02d\n",
                          local->tm_hour,
                          local->tm_min);

              printf("pcsensor\n");
           } else {

            if (formato != 3) {

              printf("%04d/%02d/%02d %02d:%02d:%02d ",
                          local->tm_year +1900,
                          local->tm_mon + 1,
                          local->tm_mday,
                          local->tm_hour,
                          local->tm_min,
                          local->tm_sec);

            }

              if (formato==3) {
                  // for Nagios
                  printf("%.2f\n", tempc);
              }
              else if (formato==2) {
                  printf("Temperature %.2fF\n", (9.0 / 5.0 * tempc + 32.0));
              } else if (formato==1) {
                  printf("Temperature %.2fC\n", tempc);
              } else {
                  printf("Temperature %.2fF %.2fC\n", (9.0 / 5.0 * tempc + 32.0), tempc);
              }
              fflush(stdout);
           }

           if (!bsalir)
              sleep(seconds);
     } while (!bsalir);

     usb_release_interface(lvr_winusb, INTERFACE1);
     usb_release_interface(lvr_winusb, INTERFACE2);

     usb_close(lvr_winusb);

     return 0;
}
예제 #4
0
int main(int argc, char **argv) {
  int c;

  while ((c = getopt (argc, argv, "mfcvhl::")) != -1) {
    switch (c) {
    case 'v':
      debug = 1;
      break;
    case 'l':
      if (optarg != NULL) {
        if (!sscanf(optarg,"%i",&seconds) == 1) {
          fprintf(stderr, "Error: '%s' is not numeric.\n", optarg);
          exit(EXIT_FAILURE);
        }
        else {
          bsalir = 0;
          break;
        }
      }
      else {
        bsalir = 0;
        seconds = 5;
        break;
      }
    case 'h':
      printf("Available options:\n");
      printf("    -h help\n");
      printf("    -v verbose\n");
      printf("    -l[n] loop every 'n' seconds, default value is 5s\n");
      exit(EXIT_FAILURE);

    default:
      if (isprint (optopt))
        fprintf (stderr, "Unknown option `-%c'.\n", optopt);
      else
        fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
        exit(EXIT_FAILURE);
    }
  }

  if (optind < argc) {
    fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
    exit(EXIT_FAILURE);
  }



  usb_dev_handle *lvr_winusb = NULL;
  if ((lvr_winusb = setup_libusb_access()) == NULL) {
    exit(EXIT_FAILURE);
  }

  signal(SIGINT, ex_program);

  ini_control_transfer(lvr_winusb);

  control_transfer(lvr_winusb, uTemp);
  interrupt_read(lvr_winusb);

  control_transfer(lvr_winusb, uIni1);
  interrupt_read(lvr_winusb);

  control_transfer(lvr_winusb, uIni2);
  interrupt_read(lvr_winusb);
  interrupt_read(lvr_winusb);

  do {
    control_transfer(lvr_winusb, uTemp);
    float temp_c = interrupt_read_temperature(lvr_winusb);

    time_t t = time(NULL);
    struct tm *local = localtime(&t);

    printf("%04d/%02d/%02d %02d:%02d:%02d ",
      local->tm_year + 1900,
      local->tm_mon + 1,
      local->tm_mday,
      local->tm_hour,
      local->tm_min,
      local->tm_sec);

    printf("Temperature %f\n", temp_c);
    fflush(stdout);

    if (!bsalir)
      sleep(seconds);
  } while (!bsalir);

  usb_release_interface(lvr_winusb, INTERFACE1);
  usb_release_interface(lvr_winusb, INTERFACE2);

  usb_close(lvr_winusb);

  return 0;
}