mraa_result_t mraa_intel_edison_uart_init_pre(int index) { if (miniboard == 0) { mraa_gpio_write(tristate, 0); mraa_gpio_context io0_output = mraa_gpio_init_raw(248); mraa_gpio_context io0_pullup = mraa_gpio_init_raw(216); mraa_gpio_context io1_output = mraa_gpio_init_raw(249); mraa_gpio_context io1_pullup = mraa_gpio_init_raw(217); mraa_gpio_dir(io0_output, MRAA_GPIO_OUT); mraa_gpio_dir(io0_pullup, MRAA_GPIO_OUT); mraa_gpio_dir(io1_output, MRAA_GPIO_OUT); mraa_gpio_dir(io1_pullup, MRAA_GPIO_IN); mraa_gpio_write(io0_output, 0); mraa_gpio_write(io0_pullup, 0); mraa_gpio_write(io1_output, 1); mraa_gpio_close(io0_output); mraa_gpio_close(io0_pullup); mraa_gpio_close(io1_output); mraa_gpio_close(io1_pullup); } mraa_result_t ret; ret = mraa_intel_edison_pinmode_change(130,1); //IO0 RX ret = mraa_intel_edison_pinmode_change(131,1); //IO1 TX return ret; }
void get_data(mraa_gpio_context gpio, int* readData) { mraa_gpio_dir(gpio, MRAA_GPIO_OUT); mraa_gpio_write(gpio, 1); mraa_gpio_write(gpio, 0); usleep(18000); mraa_gpio_write(gpio, 1); usleep(30); mraa_gpio_dir(gpio, MRAA_GPIO_IN); *readData = mraa_gpio_read(gpio); usleep(80); printf("first readData >>> %d\n", *readData); *readData = mraa_gpio_read(gpio); usleep(80); printf("second readData >>> %d\n", *readData); int i; for (i = 0; i < 40; i++) { *readData = mraa_gpio_read(gpio); printf("[%d] >>> %d\n", i, *readData); } }
mraa_result_t mraa_intel_edison_aio_init_pre(unsigned int aio) { if (aio > plat->aio_count) { syslog(LOG_ERR, "edison: Invalid analog input channel"); return MRAA_ERROR_INVALID_RESOURCE; } int pin = 14 + aio; mraa_gpio_context output_e; output_e = mraa_gpio_init_raw(outputen[pin]); if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; if (mraa_gpio_write(output_e, 0) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; mraa_gpio_close(output_e); mraa_gpio_context pullup_pin; pullup_pin = mraa_gpio_init_raw(pullup_map[pin]); if (mraa_gpio_dir(pullup_pin, MRAA_GPIO_IN) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; mraa_gpio_close(pullup_pin); return MRAA_SUCCESS; }
mraa_result_t mraa_intel_edison_uart_init_pre(int index) { if (index != 0) { syslog(LOG_ERR, "edison: Failed to write to drive mode"); return MRAA_ERROR_INVALID_RESOURCE; } if (miniboard == 0) { mraa_gpio_write(tristate, 0); mraa_gpio_context io0_output = mraa_gpio_init_raw(248); mraa_gpio_context io0_pullup = mraa_gpio_init_raw(216); mraa_gpio_context io1_output = mraa_gpio_init_raw(249); mraa_gpio_context io1_pullup = mraa_gpio_init_raw(217); mraa_gpio_dir(io0_output, MRAA_GPIO_OUT); mraa_gpio_dir(io0_pullup, MRAA_GPIO_OUT); mraa_gpio_dir(io1_output, MRAA_GPIO_OUT); mraa_gpio_dir(io1_pullup, MRAA_GPIO_IN); mraa_gpio_write(io0_output, 0); mraa_gpio_write(io0_pullup, 0); mraa_gpio_write(io1_output, 1); mraa_gpio_close(io0_output); mraa_gpio_close(io0_pullup); mraa_gpio_close(io1_output); mraa_gpio_close(io1_pullup); } mraa_result_t ret; ret = mraa_intel_edison_pinmode_change(130, 1); // IO0 RX ret = mraa_intel_edison_pinmode_change(131, 1); // IO1 TX return ret; }
mraa_result_t mraa_intel_galileo_gen2_uart_init_pre(int index) { mraa_gpio_context io0_output = mraa_gpio_init_raw(32); if (io0_output == NULL) { return MRAA_ERROR_INVALID_RESOURCE; } mraa_gpio_context io1_output = mraa_gpio_init_raw(28); if (io1_output == NULL) { mraa_gpio_close(io0_output); return MRAA_ERROR_INVALID_RESOURCE; } int status = 0; status += mraa_gpio_dir(io0_output, MRAA_GPIO_OUT); status += mraa_gpio_dir(io1_output, MRAA_GPIO_OUT); status += mraa_gpio_write(io0_output, 1); status += mraa_gpio_write(io1_output, 0); mraa_gpio_close(io0_output); mraa_gpio_close(io1_output); if (status > 0) { return MRAA_ERROR_UNSPECIFIED; } return MRAA_SUCCESS; }
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_result_t mraa_intel_edison_pwm_init_pre(int pin) { if (miniboard == 1) { return mraa_intel_edison_pinmode_change(plat->pins[pin].gpio.pinmap, 1); } if (pin < 0 || pin > 19) return MRAA_ERROR_INVALID_RESOURCE; if (!plat->pins[pin].capabilites.pwm) return MRAA_ERROR_INVALID_RESOURCE; mraa_gpio_context output_e; output_e = mraa_gpio_init_raw(outputen[pin]); if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; if (mraa_gpio_write(output_e, 1) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; mraa_gpio_close(output_e); mraa_gpio_context pullup_pin; pullup_pin = mraa_gpio_init_raw(pullup_map[pin]); if (mraa_gpio_dir(pullup_pin, MRAA_GPIO_IN) != MRAA_SUCCESS) return MRAA_ERROR_INVALID_RESOURCE; mraa_gpio_close(pullup_pin); mraa_intel_edison_pinmode_change(plat->pins[pin].gpio.pinmap, 1); return MRAA_SUCCESS; }
mraa_result_t mraa_intel_galileo_gen2_i2c_init_pre(unsigned int bus) { mraa_gpio_context io18 = mraa_gpio_init_raw(57); int status = 0; if (io18 == NULL) { return MRAA_ERROR_UNSPECIFIED; } status += mraa_gpio_dir(io18, MRAA_GPIO_IN); status += mraa_gpio_mode(io18, MRAA_GPIO_HIZ); mraa_gpio_close(io18); mraa_gpio_context io19 = mraa_gpio_init_raw(59); if (io19 == NULL) { return MRAA_ERROR_UNSPECIFIED; } status += mraa_gpio_dir(io19, MRAA_GPIO_IN); status += mraa_gpio_mode(io19, MRAA_GPIO_HIZ); mraa_gpio_close(io19); if (status > 0) { return MRAA_ERROR_UNSPECIFIED; } return MRAA_SUCCESS; }
int main(int argc, char** argv) { mraa_platform_t platform = mraa_get_platform_type(); mraa_gpio_context gpio, gpio_in = NULL; const char* board_name = mraa_get_platform_name(); int ledstate = 0; switch (platform) { case MRAA_INTEL_GALILEO_GEN1: gpio = mraa_gpio_init_raw(3); break; case MRAA_INTEL_MINNOWBOARD_MAX: // there is no onboard LED that we can flash on the minnowboard max // but on the calamari lure pin 21 is an LED. If you don't have the // lure put an LED on pin 21 gpio = mraa_gpio_init(21); break; case MRAA_INTEL_JOULE_EXPANSION: gpio = mraa_gpio_init(101); break; default: gpio = mraa_gpio_init(13); } fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name); if (gpio == NULL) { fprintf(stdout, "Could not initilaize gpio\n"); return 1; } // on platforms with physical button use gpio_in if (platform == MRAA_INTEL_MINNOWBOARD_MAX) { gpio_in = mraa_gpio_init(14); if (gpio_in != NULL) { mraa_gpio_dir(gpio_in, MRAA_GPIO_IN); // S1 on minnowboardmax's calamari lure maps to pin 14, SW1 != S1 fprintf(stdout, "Press and hold S1 to stop, Press SW1 to shutdown!\n"); } } mraa_gpio_dir(gpio, MRAA_GPIO_OUT); for (;;) { if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) { return 0; } ledstate = !ledstate; mraa_gpio_write(gpio, !ledstate); sleep(1); } return 0; }
mraa_result_t mraa_intel_edison_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode) { if (dev->value_fp != -1) { if (close(dev->value_fp) != 0) { return MRAA_ERROR_INVALID_RESOURCE; } dev->value_fp = -1; } mraa_gpio_context pullup_e; pullup_e = mraa_gpio_init_raw(pullup_map[dev->phy_pin]); if (pullup_e == NULL) { return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_dir(pullup_e, MRAA_GPIO_IN) != MRAA_SUCCESS) { syslog(LOG_ERR, "edison: Failed to set gpio mode-pullup"); mraa_gpio_close(pullup_e); return MRAA_ERROR_INVALID_RESOURCE; } int value = -1; switch (mode) { case MRAA_GPIO_STRONG: break; case MRAA_GPIO_PULLUP: value = 1; break; case MRAA_GPIO_PULLDOWN: value = 0; break; case MRAA_GPIO_HIZ: return MRAA_SUCCESS; break; default: return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED; } if (value != -1) { if (mraa_gpio_dir(pullup_e, MRAA_GPIO_OUT) != MRAA_SUCCESS) { syslog(LOG_ERR, "edison: Error setting pullup"); mraa_gpio_close(pullup_e); return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_write(pullup_e, value) != MRAA_SUCCESS) { syslog(LOG_ERR, "edison: Error setting pullup"); mraa_gpio_close(pullup_e); return MRAA_ERROR_INVALID_RESOURCE; } } return mraa_gpio_close(pullup_e); }
void MPU9250_GPIO_Init() { S0 = mraa_gpio_init(32); S1 = mraa_gpio_init(33); S2 = mraa_gpio_init(46); S3 = mraa_gpio_init(47); mraa_gpio_dir(S0, MRAA_GPIO_OUT); mraa_gpio_dir(S1, MRAA_GPIO_OUT); mraa_gpio_dir(S2, MRAA_GPIO_OUT); mraa_gpio_dir(S3, MRAA_GPIO_OUT); }
int main(int argc, char **argv) { int i, j, output, outputArray[n_reads], i_reads, tmp; mraa_gpio_context clk = mraa_gpio_init(4); mraa_gpio_context dat = mraa_gpio_init(2); mraa_gpio_dir(clk, MRAA_GPIO_OUT); mraa_gpio_dir(dat, MRAA_GPIO_IN); mraa_gpio_use_mmaped(clk, 1); i_reads = n_reads; while (i_reads > 0) { while(mraa_gpio_read(dat)) {} output = 0; for(i = 0; i < 24; i++) { mraa_gpio_write(clk, 1); fuckDelay(2000); output |= (mraa_gpio_read(dat)<< (23-i)); mraa_gpio_write(clk, 0); fuckDelay(2000); } mraa_gpio_write(clk, 1); fuckDelay(2000); mraa_gpio_write(clk, 0); if(output < 16777215) { //fprintf(stdout, "%d\n", output); outputArray[i_reads-1] = output; i_reads--; // decrement only if the valid read } } //sorting the array for(i = 1; i < n_reads; i++){ j = i; while ((j > 0) && (outputArray[j-1]>outputArray[j])) { tmp = outputArray[j]; outputArray[j] = outputArray[j-1]; outputArray[j-1] = tmp; j--; } } fprintf(stdout, "%d\n", outputArray[5]); }
FskErr mraaDigitalSetDirection(FskPinDigital pin, FskPinsDigitalDirection direction) { mraaDigital md = (mraaDigital)pin; mraa_result_t result = MRAA_SUCCESS; md->direction = direction; if (kFskPinDigitalDirectionOut == direction) result = mraa_gpio_dir(md->context, MRAA_GPIO_OUT); else if (kFskPinDigitalDirectionIn == direction) result = mraa_gpio_dir(md->context, MRAA_GPIO_IN); return (MRAA_SUCCESS == result) ? kFskErrNone : kFskErrOperationFailed; }
int main(int argc, char **argv) { mraa_platform_t platform = mraa_get_platform_type(); mraa_gpio_context gpio, gpio_in = NULL; char* board_name = mraa_get_platform_name(); int ledstate = 0; switch (platform) { case MRAA_INTEL_GALILEO_GEN1: gpio = mraa_gpio_init_raw(3); break; case MRAA_INTEL_MINNOWBOARD_MAX: gpio = mraa_gpio_init(21); break; default: gpio = mraa_gpio_init(13); } fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name); if (gpio == NULL) { fprintf(stdout, "Could not initilaize gpio\n"); return 1; } // on platforms with physical button use gpio_in if (platform == MRAA_INTEL_MINNOWBOARD_MAX) { gpio_in = mraa_gpio_init(14); if (gpio_in != NULL) { mraa_gpio_dir(gpio_in, MRAA_GPIO_IN); fprintf(stdout, "Press and hold S1 to stop\n"); } } mraa_gpio_dir(gpio, MRAA_GPIO_OUT); for (;;) { if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) { return 0; } ledstate = !ledstate; mraa_gpio_write(gpio, !ledstate); sleep(1); } return 0; }
mraa_result_t mraa_intel_galileo_gen2_uart_init_pre(int index) { mraa_gpio_context io0_output = mraa_gpio_init_raw(32); mraa_gpio_context io1_output = mraa_gpio_init_raw(28); mraa_gpio_dir(io0_output, MRAA_GPIO_OUT); mraa_gpio_dir(io1_output, MRAA_GPIO_OUT); mraa_gpio_write(io0_output, 1); mraa_gpio_write(io1_output, 0); mraa_gpio_close(io0_output); mraa_gpio_close(io1_output); return MRAA_SUCCESS; }
mraa_result_t mraa_setup_mux_mapped(mraa_pin_t meta) { int mi; for (mi = 0; mi < meta.mux_total; mi++) { mraa_gpio_context mux_i; mux_i = mraa_gpio_init_raw(meta.mux[mi].pin); if (mux_i == NULL) { return MRAA_ERROR_INVALID_HANDLE; } // this function will sometimes fail, however this is not critical as // long as the write succeeds - Test case galileo gen2 pin2 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT); mraa_gpio_owner(mux_i, 0); if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) { mraa_gpio_close(mux_i); return MRAA_ERROR_INVALID_RESOURCE; } mraa_gpio_close(mux_i); } return MRAA_SUCCESS; }
mraa_result_t mraa_intel_galileo_g1_pwm_init_pre(int pin) { // Gen1 galileo has no kernel muxing for GPIOs/PWM so ends up with leakage // on the PWM pins from the GPIO pins if not set to output high if (plat->pins[pin].capabilities.gpio == 1) { mraa_gpio_context mux_i; mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap); if (mux_i == NULL) { syslog(LOG_ERR, "pwm_init: error in gpio->pwm%i transition. gpio_init", pin); return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS) { syslog(LOG_ERR, "pwm_init: error in gpio->pwm%i transition. gpio_dir", pin); return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS) { syslog(LOG_ERR, "pwm_init: error in gpio->pwm%i transition. gpio_write", pin); return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_close(mux_i) != MRAA_SUCCESS) { syslog(LOG_ERR, "pwm_init: error in gpio->pwm%i transition. gpio_close", pin); return MRAA_ERROR_INVALID_RESOURCE; } } return MRAA_SUCCESS; }
upm_result_t kx122_install_isr(const kx122_context dev, mraa_gpio_edge_t edge,KX122_INTERRUPT_PIN_T intp, int pin, void (*isr)(void *), void *arg) { assert(dev != NULL); mraa_gpio_context isr_gpio = NULL; if (!(isr_gpio = mraa_gpio_init(pin))){ printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } mraa_gpio_dir(isr_gpio, MRAA_GPIO_IN); if (mraa_gpio_isr(isr_gpio, edge, isr, arg)){ mraa_gpio_close(isr_gpio); printf("%s: mraa_gpio_isr() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } if(intp == INT1){ dev->gpio1 = isr_gpio; } else{ dev->gpio2 = isr_gpio; } return UPM_SUCCESS; }
mraa_result_t mraa_intel_galileo_gen2_dir_pre(mraa_gpio_context dev, mraa_gpio_dir_t dir) { if (dev->phy_pin >= 0) { int pin = dev->phy_pin; if (plat->pins[pin].gpio.complex_cap.complex_pin != 1) return MRAA_SUCCESS; if (plat->pins[pin].gpio.complex_cap.output_en == 1) { if (!agpioOutputen[pin]) { agpioOutputen[pin] = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable); if (agpioOutputen[pin] == NULL) { return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_dir(agpioOutputen[pin], MRAA_GPIO_OUT) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } int output_val = 1; if (dir == MRAA_GPIO_OUT) { output_val = 0; } if (mraa_gpio_write(agpioOutputen[pin], output_val) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } } return MRAA_SUCCESS; }
mraa_result_t mraa_intel_edison_gpio_dir_pre(mraa_gpio_context dev, mraa_gpio_dir_t dir) { if (dev->phy_pin >= 0) { if (mraa_gpio_write(tristate, 0) != MRAA_SUCCESS) { // call can sometimes fail, this does not actually mean much except // that the kernel drivers don't always behave very well syslog(LOG_NOTICE, "edison: Failed to write to tristate"); } int pin = dev->phy_pin; if (!agpioOutputen[pin]) { agpioOutputen[pin] = mraa_gpio_init_raw(outputen[pin]); if (agpioOutputen[pin] == NULL) { return MRAA_ERROR_INVALID_RESOURCE; } if (mraa_gpio_dir(agpioOutputen[pin], MRAA_GPIO_OUT) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } int output_val = 0; if (dir == MRAA_GPIO_OUT) { output_val = 1; } if (mraa_gpio_write(agpioOutputen[pin], output_val) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } return MRAA_SUCCESS; }
mraa_gpio_context setupISRO( int pinNumber, void (*isrHandler)(void *) ) { mraa_gpio_context isro = mraa_gpio_init( pinNumber ); if ( ! isro ) { printf( " Failed initing isro\n" ); mraa_result_print( MRAA_ERROR_UNSPECIFIED ); return 0; // } else { // printf( " Inited isro on pin: %d\n", pinNumber ); } response = mraa_gpio_dir( isro, MRAA_GPIO_IN ); if (response != MRAA_SUCCESS) { // printf( " Failed setting isro pin direction\n" ); mraa_result_print((mraa_result_t) response); return 0; } // printf( " Setup isro pin direction to IN\n" ); mraa_result_t result = mraa_gpio_isr( isro, MRAA_GPIO_EDGE_BOTH, isrHandler, isro); if ( result != MRAA_SUCCESS ) { // printf( " Setup isro interrupt service routine failed with result: %d\n", result ); return 0; } // printf( " Setup isro interrupt service routine\n" ); // Init to test pin level // lastTime = getTimeCheck(); // isr1( NULL ); // Check level return isro; }
int main(int argc, char **argv) { mraa_platform_t platform = mraa_get_platform_type(); mraa_gpio_context gpio; char board_name[] = "Some weird devboard that isn't recognised..."; int ledstate = 0; switch (platform) { case MRAA_INTEL_GALILEO_GEN1: strcpy(board_name, "Intel Galileo Gen1"); gpio = mraa_gpio_init_raw(3); break; case MRAA_INTEL_GALILEO_GEN2: strcpy(board_name, "Intel Galileo Gen2"); default: gpio = mraa_gpio_init(13); } fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name); mraa_gpio_dir(gpio, MRAA_GPIO_OUT); for (;;) { ledstate = !ledstate; mraa_gpio_write(gpio, !ledstate); sleep(1); } return 0; }
mraa_pin_t* mraa_setup_pwm(int pin) { if (plat == NULL) return NULL; if (plat->pins[pin].capabilites.pwm != 1) return NULL; if (plat->pins[pin].capabilites.gpio == 1) { mraa_gpio_context mux_i; mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap); if (mux_i == NULL) return NULL; if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS) return NULL; // Current REV D quirk. //TODO GEN 2 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS) return NULL; if (mraa_gpio_close(mux_i) != MRAA_SUCCESS) return NULL; } if (plat->pins[pin].pwm.mux_total > 0) if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS) return NULL; mraa_pin_t *ret; ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t)); ret->pinmap = plat->pins[pin].pwm.pinmap; ret->parent_id = plat->pins[pin].pwm.parent_id; return ret; }
int main(int argc, char** argv) { mraa_gpio_context ir_gpio = NULL; //set ir io as input ir_gpio = mraa_gpio_init(7); if (ir_gpio == NULL) { fprintf(stdout, "Could not initilaize ir_gpio\n"); return 1; } mraa_gpio_dir(ir_gpio, MRAA_GPIO_IN); //create take photo thread pthread_t t; pthread_create(&t,NULL,take_photo_thread,NULL); //pthread_join(t,NULL); //main loop for (;;) { if( ir_state != mraa_gpio_read(ir_gpio)) { ir_state = !ir_state; printf("%d\n",ir_state); sleep(1); } } return 0; }
led_context led_init(int 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; } led_context dev = (led_context)malloc(sizeof(struct _led_context)); if (!dev) return NULL; dev->name = NULL; dev->gpioled = NULL; dev->led_pin = pin; dev->gpio = mraa_gpio_init(dev->led_pin); if (mraa_gpio_dir(dev->gpio, MRAA_GPIO_OUT) != MRAA_SUCCESS) { printf("%s: Unable to set pin %d as output.\n", __FUNCTION__, pin); free(dev); return NULL; } return dev; }
ppd42ns_context ppd42ns_init(int pin) { ppd42ns_context dev = (ppd42ns_context)malloc(sizeof(struct _ppd42ns_context)); if (!dev) return NULL; dev->gpio = NULL; // 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); ppd42ns_close(dev); return NULL; } // MRAA contexts... if ( !(dev->gpio = mraa_gpio_init(pin)) ) { printf("%s: mraa_gpio_init() failed\n", __FUNCTION__); ppd42ns_close(dev); return NULL; } mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); return dev; }
upm_result_t bno055_install_isr(const bno055_context dev, int gpio, mraa_gpio_edge_t level, void (*isr)(void *), void *arg) { assert(dev != NULL); // delete any existing ISR and GPIO context bno055_uninstall_isr(dev); // create gpio context if (!(dev->gpio = mraa_gpio_init(gpio))) { printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); if (mraa_gpio_isr(dev->gpio, level, isr, arg)) { mraa_gpio_close(dev->gpio); dev->gpio = NULL; printf("%s: mraa_gpio_isr() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } return UPM_SUCCESS; }
int main(int argc, char* argv[]) { int i, pin; if (argc != 2) { printf("Input GPIO number..."); exit(1); } pin = atoi(argv[1]); printf("GPIO >>> %d\n", pin); mraa_gpio_context gpio; gpio = mraa_gpio_init(pin); mraa_gpio_dir(gpio, MRAA_GPIO_OUT); while (1) { mraa_gpio_write(gpio, 0); printf("Off\n"); sleep(1); mraa_gpio_write(gpio, 1); printf("On\n"); sleep(1); } mraa_gpio_close(gpio); return 0; }
tb6612::tb6612(): _dcA(0.0), _dcB(0.0) { // _pwmA is pwm channel 0, on pin 20 in mraa _pwmA = mraa_pwm_init(20); mraa_pwm_period_us(_pwmA, 1000); mraa_pwm_enable(_pwmA, 1); // _pwmB is pwm channel 1, on pin 14 in mraa _pwmB = mraa_pwm_init(14); mraa_pwm_period_us(_pwmB, 1000); mraa_pwm_enable(_pwmB, 1); mraa_pwm_write(_pwmA, 0.01); mraa_pwm_write(_pwmB, 0.01); mraa_pwm_write(_pwmA, 0.0); mraa_pwm_write(_pwmB, 0.0); // _A1 and _A2 are on GPIO48 and GPIO47, respectively, which are pins 33 and // 46 in mraa, respectively. _A1 = mraa_gpio_init(33); _A2 = mraa_gpio_init(46); mraa_gpio_dir(_A1, MRAA_GPIO_OUT); mraa_gpio_mode(_A1, MRAA_GPIO_STRONG); mraa_gpio_write(_A1, 1); mraa_gpio_dir(_A2, MRAA_GPIO_OUT); mraa_gpio_mode(_A2, MRAA_GPIO_STRONG); mraa_gpio_write(_A2, 1); // _B1 and _B2 are on GPIO15 and GPIO14, respectively, which are pins 48 and // 36, respectively _B1 = mraa_gpio_init(48); _B2 = mraa_gpio_init(36); mraa_gpio_dir(_B1, MRAA_GPIO_OUT); mraa_gpio_mode(_B1, MRAA_GPIO_STRONG); mraa_gpio_write(_B1, 1); mraa_gpio_dir(_B2, MRAA_GPIO_OUT); mraa_gpio_mode(_B2, MRAA_GPIO_STRONG); mraa_gpio_write(_B2, 1); // _standbyPin is on GPIO49, which is pin 47 in mraa _standbyPin = mraa_gpio_init(47); mraa_gpio_dir(_standbyPin, MRAA_GPIO_OUT); mraa_gpio_mode(_standbyPin, MRAA_GPIO_STRONG); mraa_gpio_write(_standbyPin, 1); }
int main() { mraa_gpio_context trig[5], echo[5]; int i; for (i=0;i<5;i++) { trig[i]=NULL; echo[i]=NULL; } trig[0] = mraa_gpio_init(32); echo[0] = mraa_gpio_init(46); trig[1] = mraa_gpio_init(31); echo[1] = mraa_gpio_init(45); trig[2] = mraa_gpio_init(33); echo[2] = mraa_gpio_init(47); trig[3] = mraa_gpio_init(25); echo[3] = mraa_gpio_init(13); trig[4] = mraa_gpio_init(21); echo[4] = mraa_gpio_init(00); for (i=0;i<5;i++) { if (trig[i] == NULL || echo[i] == NULL) { fprintf(stderr, "Initialize failed"); return 1; } mraa_gpio_dir(trig[i], MRAA_GPIO_OUT); mraa_gpio_dir(echo[i], MRAA_GPIO_IN); } printf("\n"); while (1) { for (i=0;i<5;i++) { printf(":"); fflush(stdout); double result = get_distance(trig[i], echo[i]); printf("%.2f\t", result); } printf("\n"); usleep(1000); } }