Example #1
1
upm_result_t mma7361_update(const mma7361_context dev)
{
  assert(dev != NULL);

  float sample;

  if (dev->aio_x)
    {
      if ((sample = (float)mraa_aio_read(dev->aio_x)) < 0.0)
        {
          printf("%s: mraa_aio_read(x) failed.\n", __FUNCTION__);
          return UPM_ERROR_OPERATION_FAILED;
        }

      dev->normalized_x = sample / dev->a_res;
      dev->volts_x = dev->normalized_x * dev->a_ref;
      dev->accel_x = MAP(dev->volts_x, 0.0, MMA_OUTPUT_AREF,
                         -dev->g_range, dev->g_range);
    }

  if (dev->aio_y)
    {
      if ((sample = (float)mraa_aio_read(dev->aio_y)) < 0.0)
        {
          printf("%s: mraa_aio_read(y) failed.\n", __FUNCTION__);
          return UPM_ERROR_OPERATION_FAILED;
        }

      dev->normalized_y = sample / dev->a_res;
      dev->volts_y = dev->normalized_y * dev->a_ref;
      dev->accel_y = MAP(dev->volts_y, 0.0, MMA_OUTPUT_AREF,
                         -dev->g_range, dev->g_range);
    }

  if (dev->aio_z)
    {
      if ((sample = (float)mraa_aio_read(dev->aio_z)) < 0.0)
        {
          printf("%s: mraa_aio_read(z) failed.\n", __FUNCTION__);
          return UPM_ERROR_OPERATION_FAILED;
        }

      dev->normalized_z = sample / dev->a_res;
      dev->volts_z = dev->normalized_z * dev->a_ref;
      dev->accel_z = MAP(dev->volts_z, 0.0, MMA_OUTPUT_AREF,
                         -dev->g_range, dev->g_range);
    }

  return UPM_SUCCESS;
}
Example #2
1
int main(){

        /* Delay to sleep for human sight*/
        struct timespec a,b;

        a.tv_sec = 0;
        a.tv_nsec = 500000000L;

        /* Golden Rule */
        mraa_aio_context adc_a0;
        uint16_t adc_value = 0;
        float adc_value_float = 0.0;

        /* Initialize Pin */
        adc_a0 = mraa_aio_init(0);
        if (adc_a0 ==NULL){
                return 1;
        }

        /* Read and print values Loopy-Loop */
        for(;;){
                adc_value = mraa_aio_read(adc_a0);
                adc_value_float = mraa_aio_read_float(adc_a0);
                fprintf(stdout, "ADC A0: %d\n", adc_value, adc_value);
                fprintf(stdout, "ADC A0 float: %.3f\n", adc_value_float);
                if(nanosleep(&a,&b)<0){
                        return 1;
                }
        }

        mraa_aio_close(adc_a0);
        return MRAA_SUCCESS;
}
Example #3
1
upm_result_t rotary_get_value_voltage (const rotary_context dev,
                                       float* volts)
{
    float val = 0.0;
    val = mraa_aio_read(dev->aio);
    *volts = (dev->m_aRef / dev->m_aRes) * (float)val;

    return UPM_SUCCESS;
}
Example #4
1
upm_result_t rotary_get_value_angle (rotary_context dev, float* rotval)
{
    float val = 0.0;
    val = mraa_aio_read(dev->aio);

    // return degrees
    *rotval = val * (float)ROTARY_MAX_ANGLE / dev->m_aRes;

    return UPM_SUCCESS;
}
Example #5
1
File: aio.c Project: KurtE/mraa
float
mraa_aio_read_float(mraa_aio_context dev)
{
    if (dev == NULL) {
        syslog(LOG_ERR, "aio: Device not valid");
        return -1.0;
    }

    unsigned int analog_value_int = mraa_aio_read(dev);

    return analog_value_int / max_analog_value;
}
Example #6
1
int main(int argc, char **argv) {
	
	int k, N = STD_N, adcNumber = STD_ADCPIN, bench = BENCH_FLAG;
	float yMax = STD_YMAX, lines = STD_LINES, DELTA = STD_DELTA;
	mraa_aio_context adc;

	struct timeval inicio, fim;
	double tmili;

	if (argc == 2) {
		yMax = atof(argv[1]);
	}
	else if (argc == 3) {
		yMax = atof(argv[1]);
		lines = atof(argv[2]);
	}

	adc = mraa_aio_init(adcNumber);
	if (adc == NULL) return 1;

	hideCursor();
	clear();

	while (1) {
		cplx samples[N];
		double deltaFreq;

		gettimeofday(&inicio, NULL);
		for (k = 0; k < N; k++) {
			uint16_t sample = mraa_aio_read(adc);
			samples[k] = (cplx) 5*sample/(resolution - 1.0);
		}
		gettimeofday(&fim, NULL);

		tmili = (double) (1000 *(fim.tv_sec - inicio.tv_sec) + (fim.tv_usec - inicio.tv_usec) / 1000);
		deltaFreq = 1000.0 / tmili;

		if (bench == 1) {
			printf("\n>>>> Bench with:\n     N=%d\n     delta=%f\n", N, DELTA);
			printf(">>>> Sample time : %.2fms\n", tmili);
			printf("     deltaFreq   : %.2fHz\n", deltaFreq);
			printf("     max freq.   : %.2fHz\n", 500*N/tmili);
		}
		runFFT(samples, N, DELTA, deltaFreq, bench, yMax, lines);
		printCoord (N/2, 40);
	}

	mraa_aio_close(adc);
	return 0;
}
Example #7
1
File: ad8232.c Project: g-vidal/upm
upm_result_t ad8232_get_value(ad8232_context dev, int* value) {
    int reading = 0;

    if (mraa_gpio_read(dev->gpio_lo_minus) ||
        mraa_gpio_read(dev->gpio_lo_plus)) {
        reading = 0;
    }
    else {
        reading = mraa_aio_read(dev->aio);
    }

    *value = reading;

    return UPM_SUCCESS;
}
Example #8
1
void * writeData() {
	for (;;) {
		if (rep > 0) {
			float newTemp = tempConverter(mraa_aio_read(tempSensor));
			if (scale == 'F')
				newTemp = 32 + (1.8*newTemp);
			char tarr[20];
			convertTime(tarr);
			if (log_flag && logOpt)
				fprintf(myFile,"%s %.1f\n",tarr,newTemp);
			if (log_flag)
				fprintf(stderr,"%s %.1f\n",tarr,newTemp);
			if (iflag == 0)
				iflag = 1;
			sleep(duration);
		}
	}
	return 0;
}
Example #9
1
upm_result_t gp2y0a_get_value(gp2y0a_context dev, float a_ref,
                              uint8_t samples, float* value){
    int val;
    int sum = 0;
    int i = 0;

    if (samples <= 0)
      samples = 1;

    for(i=0; i<samples; i++) {
        val = mraa_aio_read(dev->aio);
        sum += val;
    }
    val = sum/samples;
    float volts = (float)(val * a_ref) / (float)dev->a_res;
    *value = volts;

    return UPM_SUCCESS;
}
Example #10
1
int main(){
	signal(SIGINT, &sig_handler);

	uint16_t rotary_value = 0;
	float value = 0.0f;
	mraa_pwm_context pwm1;
	mraa_aio_context rotary;

	pwm1 = mraa_pwm_init(5);
	rotary = mraa_aio_init(0);

	if (rotary == NULL || pwm1 == NULL) {
		return 1;
		printf("Broken\n");
	 }

	mraa_pwm_period_ms(pwm1, 20);
	mraa_pwm_enable(pwm1, 1);


	while(isrunning){
		rotary_value = mraa_aio_read(rotary);
		//convert to 0.00 to 1.00 scale
		value = ((float) rotary_value)/102;

		//convert to 0.025 to 0.1 scale (avoid rattle)
		value = value/13.33;
		value = value + 0.025;
 if (value >= .25){
	 	mraa_pwm_enable(pwm1, 1);
		printf("%f\n", value);
		mraa_pwm_write(pwm1, value);
 }
 else{
//		mraa_pwm_write(pwm1, 0);
		mraa_pwm_enable(pwm1, 0);

	}
	}
		mraa_pwm_write(pwm1, 0.025f);
		mraa_pwm_enable(pwm1, 0);
	return 0;
}
Example #11
1
upm_result_t light_get_lux(const light_context dev, float *value)
{
    *value = mraa_aio_read(dev->aio);
    if (*value < 0)
        return UPM_ERROR_OPERATION_FAILED;

    /* Get max adc value range 1023, 2047, 4095, etc... */
    float max_adc = (1 << mraa_aio_get_bit(dev->aio)) - 1;

    /* Convert the value to lux */
    *value = 10000.0/pow(((max_adc - *value) * 10.0 / *value)*15.0,4.0/3.0);

     /* Apply scale */
    *value *= dev->m_scale;

    /* Apply the offset in lux */
    *value += dev->m_offset;

    return UPM_SUCCESS;
}
int main() {
	float Rsensor; //Resistance of sensor in K
	// Setup()
	mraa_init();
	mraa_aio_context sensor = mraa_aio_init(analog_Pin_0);
    uint16_t adc_value = 0;
    float adc_value_float = 0.0;

    for (;;) {
        adc_value = mraa_aio_read(sensor);
        adc_value_float = mraa_aio_read_float(sensor);

        Rsensor=(float)(1023-adc_value)*10/adc_value;

        fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
        fprintf(stdout, "ADC A0 read float - %.5f\n", adc_value_float);
        printf("Cant Luz:  %.5f\n", Rsensor);
        usleep(10000);
    }

    mraa_aio_close(sensor);
    return MRAA_SUCCESS;

}
Example #13
0
 /**
  * Read a value from the AIO pin. By default mraa will shift
  * the raw value up or down to a 10 bit value.
  *
  * @returns The current input voltage. By default, a 10bit value
  */
 int
 read()
 {
     return mraa_aio_read(m_aio);
 }
int main(int argc, char** argv)
{
    mraa_platform_t platform;                /* Type of platform we are on */
    mraa_gpio_context gpio_led = NULL;       /* LED GPIO descriptor */
    mraa_gpio_context gpio_button = NULL;    /* BUTTON GPIO descriptor */
    mraa_aio_context adc_a0 = NULL;          /* Analog-Digital descriptor */
    uint32_t adc_val;                       /* ADC value */
    float delay;                             /* Delay between blinks */
    int ledstate = 0;                        /* LED state, 0=off, 1=on */


    /*
     * Print banner.
     */
     platform = mraa_get_platform_type();
     fprintf(stdout, "MRAA C Demonstration\n");
     fprintf(stdout, "LIBMRAA Version: %s\n", mraa_get_version());
     fprintf(stdout, "Board: %s\n", mraa_get_platform_name());

     /*
      * Check that we are running on the correct board
      */
     platform = mraa_get_platform_type();
     if (platform != MRAA_INTEL_GALILEO_GEN2)
        {
        fprintf(stderr, "ERROR: This program is for a Intel Galileo Gen 2 Board\n");
        exit (-1);
        }

    /*
     * Initialize the LED gpio pin and set it as an output
     */
    gpio_led = mraa_gpio_init(LED);
    if (gpio_led == NULL)
        {
        fprintf(stderr, "Could not initialize LED gpio\n");
        exit (-1);
        }
    mraa_gpio_dir(gpio_led, MRAA_GPIO_OUT);

    /*
     * Initialize the Button gpio pin and set it as an input
     */
     gpio_button = mraa_gpio_init(BUTTON);
     if (gpio_button == NULL)
         {
         fprintf(stderr, "Could not initialize BUTTON gpio\n");
         }
    mraa_gpio_dir(gpio_button, MRAA_GPIO_IN);

    /*
     * Initialize the Analog-Digital converter channel 0
     */

    adc_a0 = mraa_aio_init(0);
    if (adc_a0 == NULL)
        {
        fprintf(stderr, "Failed to initalize ADC channel A0\n");
        exit (-1);
        }

    printf("Vary the rate of blink by turning the poteniometer on A0\n");
    printf("Exit the program by pressing the button on D%d\n\n", BUTTON);

    /*
     * Run the loop until the BUTTON is held down.
     * Each time through the loop, read the potentiometer on A0 and
     * adjust the blink rate.
     */
    while (1)
        {
        if (mraa_gpio_read(gpio_button) == 1)
            break;

        if (adc_a0 != NULL)
            {
            adc_val = mraa_aio_read(adc_a0);
            /* fprintf(stdout, "ADC A0 = %d\n", adc_val); */
            if (adc_val != 0)
                delay = 1000000 * ((float)adc_val/1024);
            else
                delay = 1000000 * (1/1024);
            }

        ledstate = !ledstate;

        mraa_gpio_write(gpio_led, ledstate);

        usleep((long)delay);
        }

    /*
     * Clean up and exit
     */
    (void)mraa_gpio_close(gpio_led);
    (void)mraa_gpio_close(gpio_button);
    (void)mraa_aio_close(adc_a0);

    exit(0);
}
Example #15
0
float micro::get_val(){
    this->valeur = mraa_aio_read(analog_in);
    return this->valeur;
}
Example #16
0
 /**
  * Read a value from the AIO pin. Note this value can never be outside
  * of the bounds of an unsigned short
  *
  * @returns The current input voltage, normalised to a 16-bit value
  */
 int read() {
     // Use basic types to make swig code generation simpler
     return (int) mraa_aio_read(m_aio);
 }
int main ()
{
	mraa_aio_context aio0,aio1,aio2,aio3;
	aio0=mraa_aio_init(0);
	aio1=mraa_aio_init(1);
	aio2=mraa_aio_init(2);
	aio3=mraa_aio_init(3);

//	while(1)
//	{
	
	
	   fp =fopen("/media/sdcard/smart_cabinet.txt","w");
		weights[0]=mraa_aio_read(aio0);
		weights[1]=mraa_aio_read(aio1);
		weights[2]=mraa_aio_read(aio2);
		weights[3]=mraa_aio_read(aio3);
		//printf("%d",adc_value[0]);
		printf("\n CHECK \t");
		sum=0;
		for(i=0;i<4;i++)
		{
			powers[i]=0;
			if(weights[i]>200 && weights[i]<2000)
				powers[i]=pow(2,i);
			sum=sum+powers[i];
		}
		//adc_value[1]=mraa_aio_read(aio1);
		//printf("%d",adc_value[1]);
		//adc_value[2]=mraa_aio_read(aio2);
		//printf("%d",adc_value[2]);
		//adc_value[3]=mraa_aio_read(aio3);
		//printf("%d",adc_value[3]);
/*for(i=0;i<3;i++)
		{
			if(adc_value[i] < threshold && sensor_stat[i] ==0)
			{

			   sensor_stat[i]=1;
			}
			else if(adc_value[i] > threshold && sensor_stat[i] ==1)
			{
			   sensor_stat[i]=0;

			}
		}*/
	total_weight=sum*120;
		printf("%d %d  %d %d \n",weights[0],weights[1],weights[2],weights[3]);
	printf("total weight is %d \n",total_weight);
		fprintf(fp,"%s$%d","container1",total_weight);
		//fprintf(fp,"%d$%d$%d$%d\n",mother_id,1,sensor_stat[1],adc_value[1]);
		//fprintf(fp,"%d$%d$%d$%d\n",mother_id,2,sensor_stat[2],adc_value[2]);
		//fprintf(fp,"%d$%d$%d$%d\n",mother_id,3,sensor_stat[3],adc_value[3]);
		printf("finished writing to file\n");

		 fclose(fp);
		
//	}

  return 0;
}
Example #18
0
int readAIO(  mraa_aio_context aio ) {

	return mraa_aio_read( aio );
}
Example #19
0
float potentiometer::get_val(){
  valeur = mraa_aio_read(analog_in); 
  return this->valeur;
}
Example #20
0
float photodiode::get_val(){
  valeur = mraa_aio_read(analog_in); 
  return this->valeur;
}