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;
}
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;
}
Beispiel #3
0
void ad8232_close(ad8232_context dev){
    if (dev->gpio_lo_minus != NULL)
        mraa_gpio_close(dev->gpio_lo_minus);
    if (dev->gpio_lo_plus != NULL)
        mraa_gpio_close(dev->gpio_lo_plus);
    if (dev->aio != NULL)
        mraa_aio_close(dev->aio);

    free(dev);
}
Beispiel #4
0
void mma7361_close(mma7361_context dev)
{
  assert(dev != NULL);

  // analogs
  if (dev->aio_x)
    mraa_aio_close(dev->aio_x);
  if (dev->aio_y)
    mraa_aio_close(dev->aio_y);
  if (dev->aio_z)
    mraa_aio_close(dev->aio_z);

  // gpios
  if (dev->gpio_selftest)
    mraa_gpio_close(dev->gpio_selftest);
  if (dev->gpio_sleep)
    mraa_gpio_close(dev->gpio_sleep);
  if (dev->gpio_freefall)
    mraa_gpio_close(dev->gpio_freefall);
  if (dev->gpio_range)
    mraa_gpio_close(dev->gpio_range);

  free(dev);
}
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;

}
Beispiel #6
0
int main(int argc, char **argv) {
	log_flag = 1;
	static struct option long_options[] = {
		{ "period", required_argument, NULL, 'p' },
		{ "scale", required_argument, NULL, 's' },
		{ "log", required_argument, NULL, 'l' },
		{ "id", required_argument, NULL, 'i' },
		{ "host", required_argument, NULL, 'h' },
		{ 0, 0, 0, 0 }
	};
	int opt;
	while ((opt = getopt_long(argc, argv, "p:s:l:i:h:", long_options, NULL)) >= -1) {
		if (opt == 'p')
			duration = atoi(optarg);
		else if (opt == 's') {
			switch (optarg[0]) {
			case 'C':
				scale = 'C';
				break;
			case 'F':
				scale = 'F';
				break;
			default:
				fprintf(stderr, "ERROR: incorrect scale ussage. Correct usage: --scale=[C/F]\n");
				exit(2);
			}
		}
		else if (opt == 'l') {
			FH = optarg;
			logOpt = 1;
			break;
		}
		else if (opt == 'i') {
			ID = optarg;
			IDFlag = 1;
		}
		else if (opt == 'h') {
			hostName = optarg;
			hostFlag = 1;
		}
		else {
			fprintf(stderr, "ERROR: incorrect usage. Correct usage: ./lab4b --id=[num] --host=[num] --period=[duration] --scale=[C/F] --log\n");
			exit(1);
		}
	}

	int kill = 0;

	if (hostFlag < 1 || IDFlag < 1 || logOpt < 1) {
		fprintf(stderr, "ERROR: incorrect usage. Correct usage: ./lab4b --id=[num] --host=[num] --period=[duration] --scale=[C/F] --log\n");
		exit(1);
	}

	int j = optind;
	while (j < argc) {
		if (kill > 0) {
			fprintf(stderr, "ERROR: incorrect usage. Correct usage: ./lab4b --id=[num] --host=[num] --period=[duration] --scale=[C/F] --log\n");
			exit(1);
		}
		port = atoi(argv[j]);
		kill = 1;
		j++;
	}

	if (port < 0) fprintf(stderr, "ERROR: port requires a positive value.\n");

	int skt; struct sockaddr_in addy; struct hostent *server;
	skt = socket(AF_INET, SOCK_STREAM, 0);
	if (skt <= -1) {
		fprintf(stderr, "ERROR: failed to open socket.\n");
		exit(1);
	}

	server = gethostbyname(hostName);
	if (server == 0) {
		fprintf(stderr, "ERROR: failed to reach host.\n");
		exit(1);
	}

	addy.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
		(char *)&addy.sin_addr.s_addr, server->h_length);
	addy.sin_port = htons(port);

	if (connect(skt, (struct sockaddr*)&addy, sizeof(addy)) <= -1) {
		fprintf(stderr, "ERROR: failed to connect.\n");
		exit(1);
	}

	if (logOpt > 0) myFile = fopen(FH, "a");

	dup2(skt, 2);
	dup2(skt, 0);
	char wID[80] = "";
	strcat(wID, "ID=");
	strcat(wID, ID);
	strcat(wID, "\n");
	fprintf(stderr, "%s", wID);
	fprintf(myFile, "%s", wID);

	tempSensor = mraa_aio_init(1);
	if (tempSensor == 0) {
		fprintf(stderr, "ERROR: failed to detect temperature sensor.\n");
		exit(1);
	}

	pthread_t *trds = malloc(sizeof(pthread_t) * 2);
	pthread_create(trds, 0, writeData, 0);
	pthread_create((1 + trds), 0, getData, 0);
	pthread_join(*(1 + trds), 0);
	pthread_join(*(1 + trds), 0);

	if (logOpt > 0) fclose(myFile);
	mraa_aio_close(tempSensor);
	free(trds);
	exit(0);
}
Beispiel #7
0
 /**
  * Aio destructor
  */
 ~Aio()
 {
     mraa_aio_close(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);
}
Beispiel #9
0
micro:: ~micro(){
  if (analog_in != NULL)
    mraa_aio_close(analog_in);
}
Beispiel #10
0
photodiode::~photodiode(){
  if (analog_in!=NULL)
    mraa_aio_close(analog_in);
}
Beispiel #11
0
int main(int argc, char **argv) {
	log_flag = 1;
	static struct option long_opts[] = {
		{"period", required_argument, NULL, 'p'},
		{"scale", required_argument, NULL, 's'},
		{"log", required_argument, NULL, 'l'},
		{0, 0, 0, 0}
	};
	int opt;
	while ((opt = getopt_long(argc, argv, "p:s:l:", long_opts, NULL)) >= 0) {
		if (opt == 'p') {
			duration = atoi(optarg);
		}
		else if (opt == 's') {
			switch (optarg[0]) {
			case 'C':
				scale = 'C';
				break;
			case 'F':
				scale = 'F';
				break;
			default:
				fprintf(stderr, "ERROR: incorrect scale ussage. Correct usage: --scale=(C/F)\n");
				exit(2);
			}
		}
		else if (opt == 'l') {
			myFH = optarg;
			logOpt = 1;
		}
		else {
			fprintf(stderr, "ERROR: incorrect usage. Correct usage: ./lab4b --period=(duration) --scale=(C/F) --log\n");
			exit(1);
		}
	}
	if (logOpt > 0)
		myFile = fopen(myFH, "a");
	tempSensor = mraa_aio_init(1);
	if (tempSensor == NULL) {
		fprintf(stderr, "ERROR: failed to detect temperature sensor\n");
		exit(1);
	}
	buttonSensor = mraa_gpio_init(73);
	if (buttonSensor == NULL) {
		fprintf(stderr, "ERROR: failed to detect button\n");
		exit(1);
	}
	pthread_t *trd = malloc(sizeof(pthread_t)*3);
	pthread_create(trd, 0, logTemperature, 0);
	pthread_create(trd + 1, 0, checkButton, 0);
	pthread_create(trd + 2, 0, readInput, 0);
	pthread_join(*(trd), 0);
	pthread_join(*(trd + 1), 0);
	pthread_join(*(trd + 2), 0);
	free(trd);
	if (logOpt > 0)
		fclose(myFile);
	mraa_gpio_close(buttonSensor);
	mraa_aio_close(tempSensor);
	exit(0);
}
Beispiel #12
0
void closeAIO(  mraa_aio_context aio ) {

	mraa_aio_close( aio );
}
Beispiel #13
0
void vdiv_close(vdiv_context dev)
{
    mraa_aio_close(dev->aio);
    free(dev);
}
Beispiel #14
0
void light_close(light_context dev)
{
    mraa_aio_close(dev->aio);
    free(dev);
}
Beispiel #15
0
void flex_close(flex_context dev)
{
    mraa_aio_close(dev->aio);
    free(dev);
}
Beispiel #16
0
void mqx_close(mqx_context dev)
{
    mraa_aio_close(dev->aio);
    free(dev);
}
Beispiel #17
0
potentiometer::~potentiometer(){
  if (analog_in!=NULL)
    mraa_aio_close(analog_in);
}
Beispiel #18
0
void rotary_close(rotary_context dev)
{
    mraa_aio_close(dev->aio);
    free(dev);
}