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; }
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; }
/** * Aio Constructor, takes a pin number which will map directly to the * board number * * @param pin channel number to read ADC inputs */ Aio(unsigned int pin) { m_aio = mraa_aio_init(pin); if (m_aio == NULL) { throw std::invalid_argument("Invalid AIO pin specified - do you have an ADC?"); } }
mqx_context mqx_init(int16_t pin) { // make sure MRAA is initialized int mraa_rv; if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) { printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); return NULL; } mqx_context dev = (mqx_context) malloc(sizeof(struct _mqx_context)); if (dev == NULL) return NULL; /* Init aio pin */ dev->aio = mraa_aio_init(pin); if (dev->aio == NULL) { free(dev); return NULL; } /* Set the ADC ref, scale, and offset defaults */ dev->m_aRef = 5.0; dev->m_scale = 1.0; dev->m_offset = 0.0; return dev; }
ad8232_context ad8232_init(int lo_plus, int lo_minus, int output, float a_ref) { ad8232_context dev = (ad8232_context) malloc(sizeof(struct _ad8232_context)); if(dev == NULL){ printf("Unable to assign memory to the Heart Rate Monitor structure"); return NULL; } dev->aio = mraa_aio_init(output); dev->gpio_lo_plus = mraa_gpio_init(lo_plus); dev->gpio_lo_minus = mraa_gpio_init(lo_minus); if (dev->aio == NULL || dev->gpio_lo_minus == NULL || dev->gpio_lo_plus == NULL){ printf("The pins did not initialize correctly"); return NULL; } if (mraa_gpio_dir(dev->gpio_lo_minus, MRAA_GPIO_IN) != MRAA_SUCCESS || mraa_gpio_dir(dev->gpio_lo_plus, MRAA_GPIO_IN) != MRAA_SUCCESS){ printf("Couldn't set the direction for the GPIO pins"); return NULL; } dev->a_res = (1 << mraa_aio_get_bit(dev->aio)); return dev; }
mraa_aio_context setupAIO( int pinNumber ) { mraa_aio_context aio = mraa_aio_init( pinNumber ); if ( ! aio ) { printf( " Failed initing aio\n" ); mraa_result_print( MRAA_ERROR_UNSPECIFIED ); return 0; // } else { // printf( " Inited aio: %p\n", aio ); } return aio; }
gp2y0a_context gp2y0a_init(uint8_t pin, float a_ref){ gp2y0a_context dev = (gp2y0a_context) malloc(sizeof(struct _gp2y0a_context)); dev->pin = pin; dev->aio = mraa_aio_init(dev->pin); if(dev->aio == NULL){ free(dev); return NULL; } dev->a_res = (1 << mraa_aio_get_bit(dev->aio)); return dev; }
rotary_context rotary_init(int pin, float aRef) { rotary_context dev = (rotary_context)malloc(sizeof(struct _rotary_context)); if(dev == NULL) return NULL; dev->aio = mraa_aio_init(pin); if(dev->aio == NULL){ free(dev); return NULL; } dev->m_aRef = aRef; // get adc bit range dev->m_aRes = (1 << mraa_aio_get_bit(dev->aio)); return dev; }
light_context light_init(int16_t pin) { light_context dev = (light_context) malloc(sizeof(struct _light_context)); if (dev == NULL) return NULL; /* Init aio pin */ dev->aio = mraa_aio_init(pin); if (dev->aio == NULL) { free(dev); return NULL; } /* Set the ADC ref, scale, and offset defaults */ dev->m_aRef = 5.0; dev->m_scale = 1.0; dev->m_offset = 0.0; return dev; }
vdiv_context vdiv_init(int16_t pin, float voltage_ref) { vdiv_context dev = (vdiv_context)malloc(sizeof(struct _vdiv_context)); if (dev == NULL) return NULL; /* Init aio pin */ dev->aio = mraa_aio_init(pin); if (dev->aio == NULL) { free(dev); return NULL; } /* Set defaults */ dev->m_aRef = 5.0; dev->m_scale = 1.0; dev->m_offset = 0.0; dev->m_vdiv_sw = 3; return 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; }
photodiode::photodiode(unsigned int x):capteur(x),analog(){ analog_in = mraa_aio_init(this->pin); }
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); }
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); }
/** * Aio Constructor, takes a pin number which will map directly to the * board number * * @param pin channel number to read ADC inputs */ Aio(unsigned int pin) { m_aio = mraa_aio_init(pin); }
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; }
micro::micro(unsigned int x, float y):capteur(x), analog(){ analog_in = mraa_aio_init(this->pin); seuil_bruit = y; }
void micro::set_pin(unsigned int x){ this->pin = x; analog_in = mraa_aio_init(this->pin); seuil_bruit =0; }
void photodiode::set_pin(unsigned int x){ this->pin = x; analog_in = mraa_aio_init(this->pin); }
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); }
void potentiometer::set_pin(unsigned int x){ this->pin = x; analog_in = mraa_aio_init(this->pin); }
potentiometer::potentiometer(unsigned int x):capteur(x),analog(){ analog_in = mraa_aio_init(this->pin); }
mma7361_context mma7361_init(int x_pin, int y_pin, int z_pin, int selftest_pin, int sleep_pin, int freefall_pin, int range_pin, float a_ref) { // sanity check - at least one axis needs to be enabled, or what's // the point? if (x_pin < 0 && y_pin < 0 && z_pin < 0) { printf("%s: At least one axis must be enabled.\n", __FUNCTION__); return NULL; } mma7361_context dev = (mma7361_context)malloc(sizeof(struct _mma7361_context)); if (!dev) return NULL; // zero out context memset((void *)dev, 0, sizeof(struct _mma7361_context)); dev->aio_x = NULL; dev->aio_y = NULL; dev->aio_z = NULL; dev->gpio_selftest = NULL; dev->gpio_sleep = NULL; dev->gpio_freefall = NULL; dev->gpio_range = NULL; dev->a_res = 0; dev->a_ref = a_ref; dev->g_range = 1.5; dev->offset_x = dev->offset_y = dev->offset_z = 0.0; dev->scale_x = dev->scale_y = dev->scale_z = 1.0; dev->accel_x = 0.0; dev->accel_y = 0.0; dev->accel_z = 0.0; dev->volts_x = 0.0; dev->volts_y = 0.0; dev->volts_z = 0.0; dev->normalized_x = 0; dev->normalized_y = 0; dev->normalized_z = 0; // initialize the MRAA contexts (only what we need) // analogs if (x_pin >= 0) { if (!(dev->aio_x = mraa_aio_init(x_pin))) { printf("%s: mraa_aio_init(x) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } // set our analog resolution dev->a_res = (float)(1 << mraa_aio_get_bit(dev->aio_x)) - 1; } if (y_pin >= 0) { if (!(dev->aio_y = mraa_aio_init(y_pin))) { printf("%s: mraa_aio_init(y) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } // set our analog resolution if not already set if (!dev->a_res) dev->a_res = (float)(1 << mraa_aio_get_bit(dev->aio_y)) - 1; } if (z_pin >= 0) { if (!(dev->aio_z = mraa_aio_init(z_pin))) { printf("%s: mraa_aio_init(z) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } // set our analog resolution if not already set if (!dev->a_res) dev->a_res = (float)(1 << mraa_aio_get_bit(dev->aio_z)) - 1; } // now the gpios if (selftest_pin >= 0) { if (!(dev->gpio_selftest = mraa_gpio_init(selftest_pin))) { printf("%s: mraa_gpio_init(selftest) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } mraa_gpio_dir(dev->gpio_selftest, MRAA_GPIO_OUT); mma7361_selftest(dev, false); } if (sleep_pin >= 0) { if (!(dev->gpio_sleep = mraa_gpio_init(sleep_pin))) { printf("%s: mraa_gpio_init(sleep) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } mraa_gpio_dir(dev->gpio_sleep, MRAA_GPIO_OUT); mma7361_sleep(dev, false); } if (freefall_pin >= 0) { if (!(dev->gpio_freefall = mraa_gpio_init(freefall_pin))) { printf("%s: mraa_gpio_init(freefall) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } mraa_gpio_dir(dev->gpio_freefall, MRAA_GPIO_IN); } if (range_pin >= 0) { if (!(dev->gpio_range = mraa_gpio_init(range_pin))) { printf("%s: mraa_gpio_init(range) failed.\n", __FUNCTION__); mma7361_close(dev); return NULL; } mraa_gpio_dir(dev->gpio_range, MRAA_GPIO_OUT); mma7361_set_range(dev, false); } return dev; }