Exemple #1
0
void super_dead_mode(){
    uint16_t counter = 0;
    while(!config.health){
        handle_music();
        // Manage base station comms
        uint8_t b;
        if(CHECK_CHAR()){
            b=AVAIL_CHAR();
            if(b == 0x10) {
                control_transfer();
            }
        }
        counter++;
        delay_1_ms();
        if(counter > config.death_period){
            counter = 0;
            led_off();
            Send_Byte(config.id);
            play_song((uint16_t*)dead_song,sizeof(dead_song)/sizeof(uint16_t),10000,0);
        }
        if(counter == config.death_period-50){
            red_led_on();
        }
    }
}
Exemple #2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    int action, device, value;
    unsigned char buffer[64], i, bmRequestType, bRequest;
    unsigned int vendorID, productID, wValue, wIndex, wLength;
    double *ret, *out;

    action = (int)(mxGetScalar(prhs[0]));
    switch (action) {
        case INITIALIZE:
            initialize();
            break;
        case DEINITIALIZE:
            deinitialize();
            break;
        case SET_DEBUG:
            value = (int)(mxGetScalar(prhs[1]));
            set_debug(value);
            break;
        case OPEN_DEVICE:
            vendorID = (unsigned int)(mxGetScalar(prhs[1]));
            productID = (unsigned int)(mxGetScalar(prhs[2]));
            value = (int)(mxGetScalar(prhs[3]));
            value = open_device(vendorID, productID, value);
            plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
            ret = mxGetPr(plhs[0]);
            ret[0] = (double)value;
            break;
        case CLOSE_DEVICE:
            device = (int)(mxGetScalar(prhs[1]));
            value = close_device(device);
            plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
            ret = mxGetPr(plhs[0]);
            ret[0] = (double)value;
            break;
        case NUM_DEVICES_OPEN:
            value = num_devices_open();
            plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
            ret = mxGetPr(plhs[0]);
            ret[0] = (double)value;
            break;
        case CONTROL_TRANSFER:
            device = (int)(mxGetScalar(prhs[1]));
            bmRequestType = (unsigned char)(mxGetScalar(prhs[2]));
            bRequest = (unsigned char)(mxGetScalar(prhs[3]));
            wValue = (unsigned int)(mxGetScalar(prhs[4]));
            wIndex = (unsigned int)(mxGetScalar(prhs[5]));
            wLength = (unsigned int)(mxGetScalar(prhs[6]));
            value = control_transfer(device, bmRequestType, bRequest, wValue, wIndex, wLength, buffer);
            plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
            ret = mxGetPr(plhs[0]);
            ret[0] = (double)value;
            plhs[1] = mxCreateDoubleMatrix(1, 64, mxREAL);
            out = mxGetPr(plhs[1]);
            for (i = 0; i<64; i++)
                out[i] = (double)buffer[i];
            break;
        default:
            mexErrMsgTxt("Illegal action specified.");
    }
}
Exemple #3
0
static int get_temperature(usb_dev_handle *dev, float *tempC){
	char buf[256];
	int ret, temperature, i;

	control_transfer(dev, uCmd1 );
	control_transfer(dev, uCmd4 );
	for(i = 0; i < 7; i++) {
		control_transfer(dev, uCmd0 );
	}
	control_transfer(dev, uCmd2 );
	ret = get_data(dev, buf, 256);
	if(ret < 2) {
		return -1;
	}

	temperature = (buf[1] & 0xFF) + (buf[0] << 8);	
	*tempC = temperature * (125.0 / 32000.0);
	return 0;
}
Exemple #4
0
static float pcsensor_get_temperature(usb_dev_handle* lvr_winusb, float *tempc) {
    int ret;
    switch (device_type(lvr_winusb)) {
        case 0:
            ret = get_temperature(lvr_winusb, tempc);
            break;
        case 1:
            control_transfer(lvr_winusb, uTemperatura);
            ret = interrupt_read_temperatura(lvr_winusb, tempc);
            break;
    }
    if (ret < 0) {
        return ret;
    }
    return 0;
}
Exemple #5
0
float pcsensor_get_temperature(usb_dev_handle* lvr_winusb){
	float tempc;
	int ret;
	switch(device_type(lvr_winusb)){
	case 0:
		ret = get_temperature(lvr_winusb, &tempc);
		break;
	case 1:
		control_transfer(lvr_winusb, uTemperatura );
		ret = interrupt_read_temperatura(lvr_winusb, &tempc);
		break;
	}
	if(ret < 0){
		return FLT_MIN;
	}
	return tempc;
}
Exemple #6
0
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;
}
Exemple #7
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; 
}
Exemple #8
0
int cyusb_download_fx3(cyusb_handle *h, const char *filename)
{
	int fd;
	unsigned char buf[1000000];
	int nbr;
	int dlen;
	int count;
	unsigned int *pdbuf = NULL;
	unsigned int address;
	unsigned int *pint;
	unsigned int program_entry;
	int r;

	fd = open(filename, O_RDONLY);
	if ( fd < 0 ) { 
	   printf("File not found\n");
	   return -1;
	}
	else printf("File successfully opened\n");
	count = 0;
	checksum = 0;
	nbr = read(fd, buf, 2);		/* Read first 2 bytes, must be equal to 'CY'	*/
	if ( strncmp((char *)buf,"CY",2) ) {
	   printf("Image does not have 'CY' at start. aborting\n");
	   return -2;
	}
	nbr = read(fd, buf, 1);		/* Read 1 byte. bImageCTL	*/
	if ( buf[0] & 0x01 ) {
	   printf("Image does not contain executable code\n");
	   return -3;
	}
	nbr = read(fd, buf, 1);		/* Read 1 byte. bImageType	*/
	if ( !(buf[0] == 0xB0) ) {
	   printf("Not a normal FW binary with checksum\n");
	   return -4;
	}
	while (1) {
	   nbr = read(fd, buf, 4);	/* Read Length of section 1,2,3, ...	*/
	   pdbuf = (unsigned int *)buf;
	   dlen = *pdbuf;
	   nbr = read(fd,buf,4);	/* Read Address of section 1,2,3,...	*/
	   pint = (unsigned int *)buf;   
	   address = *pint;
	   if ( dlen != 0 ) {
	      nbr = read(fd, buf, dlen*4);	/* Read data bytes	*/
	      control_transfer(h, address, buf, dlen*4);
	   }
	   else {
	      program_entry = address;
	      break;
	   }
	}
	nbr = read(fd, buf, 4);			/* Read checksum	*/
	pdbuf = (unsigned int *)buf;
	if ( *pdbuf != checksum ) {
	   printf("Error in checksum\n");
	   return -5;
	}
	sleep(1);
	r = cyusb_control_transfer(h, 0x40, 0xA0, (program_entry & 0x0000ffff ) , program_entry >> 16, NULL, 0, 1000);
	if ( r ) {
	   printf("Ignored error in control_transfer: %d\n", r);
	}
	close(fd);
	return 0;
}
Exemple #9
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;
}
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;
}
Exemple #11
0
static int readtemp(float *temp) {
	int r;
	uint8_t question1[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
	uint8_t question2[] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 };
	uint8_t question3[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
	uint8_t response[8];

	if (debug)
		printf("sending init control transfer\n");
	r = init_control_transfer();
	if (r < 0)
		return r;

	if (debug)
		printf("sending control transfer question1\n");
	r = control_transfer(question1, sizeof(question1));
	if (r < 0)
		return r;
	if (debug)
		printf("reading interrupt response\n");
	r = interrupt_read(response, sizeof(response));
	if (r < 0)
		return r;

	if (debug)
		printf("sending control transfer question2\n");
	r = control_transfer(question2, sizeof(question2));
	if (r < 0)
		return r;
	if (debug)
		printf("reading interrupt response\n");
	r = interrupt_read(response, sizeof(response));
	if (r < 0)
		return r;

	if (debug)
		printf("sending control transfer question3\n");
	r = control_transfer(question3, sizeof(question3));
	if (r < 0)
		return r;
	if (debug)
		printf("reading interrupt response\n");
	r = interrupt_read(response, sizeof(response));
	if (r < 0)
		return r;
	if (debug)
		printf("reading interrupt response\n");
	r = interrupt_read(response, sizeof(response));
	if (r < 0)
		return r;

	if (debug)
		printf("sending control transfer question1\n");
	r = control_transfer(question1, sizeof(question1));
	if (r < 0)
		return r;
	if (debug)
		printf("reading temperature interrupt response\n");
	r = interrupt_read(response, sizeof(response));
	if (r < 0)
		return r;

	*temp = (response[3] & 0xFF) + (response[2] << 8);
    *temp = *temp * (125.0 / 32000.0);

    return 0;
}