/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset; int nfds = 1; int gpio_in_fd, gpio_out_fd, timeout, rc, value; unsigned int gpio_in, gpio_out; unsigned long cntr = 0; if (argc < 3) { printf("Usage: gpio-int <gpio-input-pin> <gpio-output-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level and copy its value to output pin\n"); exit(-1); } gpio_in = atoi(argv[1]); gpio_out = atoi(argv[2]); /* initialize input pin */ gpio_export(gpio_in); gpio_set_dir(gpio_in, 0); gpio_set_edge(gpio_in, "both"); gpio_in_fd = gpio_fd_open(gpio_in); /* initialize output pin */ gpio_export(gpio_out); gpio_set_dir(gpio_out, 1); gpio_out_fd = gpio_fd_open(gpio_out); timeout = POLL_TIMEOUT; while (1) { memset(&fdset, 0, sizeof(fdset)); fdset.fd = gpio_in_fd; fdset.events = POLLPRI; rc = poll(&fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (fdset.revents & POLLPRI) { printf("\r%lu", ++cntr); gpio_get_value(gpio_in, &value); gpio_set_value(gpio_out, value); } fflush(stdout); } gpio_fd_close(gpio_in_fd); gpio_fd_close(gpio_out_fd); return 0; }
/**************************************************************** * Main ****************************************************************/ int main() { int gpio_fd; unsigned int gpio = 68; //i2c_init(); //system("chmod 777 /sys/class/gpio"); #if 0 system("chmod 777 /sys/class/gpio/export"); system("echo 68 > export"); system("chmod 777 /sys/class/gpio/gpio68/direction"); system("chmod 777 /sys/class/gpio/gpio68/edge"); system("chmod 777 /sys/class/gpio/gpio68/value"); gpio_export(gpio); gpio_set_dir(gpio, 0); gpio_set_edge(gpio, "rising"); gpio_fd = gpio_fd_open(gpio); gpio_fd_close(gpio_fd); #endif printf("main finished!"); return 0; }
JNIEXPORT jint JNICALL Java_com_robot_et_core_hardware_wakeup_WakeUp_wakeUpInit (JNIEnv *env, jclass cls) { int gpio; int ret; /* i2c init */ ret = i2c_init(); if (ret < 0) { return ret; } system("setenforce 0"); system("chmod 777 /sys/class/gpio"); system("chmod 777 /sys/class/gpio/export"); system("echo 68 > /sys/class/gpio/export"); system("chmod 777 /sys/class/gpio/gpio68"); system("chmod 777 /sys/class/gpio/gpio68/direction"); system("chmod 777 /sys/class/gpio/gpio68/edge"); system("chmod 777 /sys/class/gpio/gpio68/value"); gpio = GPIO_C4; gpio_export(gpio); gpio_set_dir(gpio, 0); gpio_set_edge(gpio, (char*)"rising"); gpioC4_fileId = gpio_fd_open(gpio); if (gpioC4_fileId < 0) { return WAKEUP_FAIL; } return gpioC4_fileId; }
JNIEXPORT jint JNICALL Java_com_robot_et_core_hardware_wakeup_WakeUp_faceWakeUpInit (JNIEnv *env, jclass cls) { int gpio; system("setenforce 0"); system("chmod 777 /sys/class/gpio"); system("chmod 777 /sys/class/gpio/export"); system("echo 72 > /sys/class/gpio/export"); system("chmod 777 /sys/class/gpio/gpio72"); system("chmod 777 /sys/class/gpio/gpio72/direction"); system("chmod 777 /sys/class/gpio/gpio72/edge"); system("chmod 777 /sys/class/gpio/gpio72/value"); gpio = GPIO_C8; gpio_export(gpio); gpio_set_dir(gpio, 0); gpio_set_edge(gpio, (char*)"rising"); gpioC8_fileId = gpio_fd_open(gpio); if (gpioC8_fileId < 0) { return WAKEUP_FAIL; } return gpioC8_fileId; }
void init(void) { //Setup LED0 as GPIO OUT export_gpio(LED0); set_gpio_direction(LED0, "out"); set_gpio_value(LED0, led0_value); led0_fd = gpio_fd_open(LED0); //Setup MUX for PWM on LED1 set_mux_value(LED1_MUX, LED1_MUX_VAL); set_pwm(LED1_PWM, LED1_PWM_FREQ, read_ain(POT)/41); //Divide by 41 to put in the range [0, 99] //Setup button as GPIO IN export_gpio(BUTTON); set_gpio_direction(BUTTON, "in"); set_gpio_edge(BUTTON, "rising"); button_fd = gpio_fd_open(BUTTON); }
int main(int argc, char **argv, char **envp){ struct pollfd fdset[2]; int nfds = 2; int gpio_fd, timeout, rc; char *buf[MAX_BUF]; unsigned int gpio; int len; if (argc < 2) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); exit(-1); } gpio = atoi(argv[1]); gpio_fd = gpio_fd_open(gpio); timeout = POLL_TIMEOUT; fdset[0].fd = gpio_fd; fdset[0].events = POLLPRI | POLLERR; printf("timeout: %i\n", timeout); rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset[0].revents & POLLPRI) { len = read(fdset[1].fd, buf, MAX_BUF); printf("\npoll() GPIO %d interrupt occurred\n", gpio); } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("POLLIN"); } printf("%i\n", rc); printf("%i\n", fdset[0].revents); /*struct pollfd pfd; pfd.fd = open("GPIO", O_RDONLY|O_NONBLOCK); pfd.events = POLLIN | POLLPRI; int a = poll(&pfd, 1, -1); printf("%i\n", a); return 0;*/ }
int gpio_input_trap_proc() { int shutdown_fd = -1; int gpio_shutdown; char *buf[MAX_BUF]; fd_set exceptfds; int res; gpio_shutdown = GetGpioNumberByFuncName("shutdownSwitch"); if (-1 != gpio_shutdown) { LOG(LOG_NOTICE, "set shutdownSwitch on GPIO %d", gpio_shutdown); gpio_export(gpio_shutdown); gpio_set_dir(gpio_shutdown, 2); gpio_set_value(gpio_shutdown, 1); gpio_set_edge(gpio_shutdown, "both"); //shutdown_fd = gpio_fd_open(gpio_shutdown); } else { LOG(LOG_NOTICE, "No set shutdownSwitch on GPIO pin"); return 0; } // Trap an output GPIO is restric by GPIO driver in Linux kernel // https://github.com/torvalds/linux/commit/d468bf9ecaabd3bf3a6134e5a369ced82b1d1ca1 // /var/log/kern.log shows: // GPIO chip pinctrl-bcm2835: gpio_lock_as_irq: tried to flag a GPIO set as output for IRQ // gpio-18 (sysfs): failed to flag the GPIO for IRQ // But with dummy read() it still works, however if we use poll() to trap POLLPRI // system could freeze (could be kernel bugs) while (1) { shutdown_fd = gpio_fd_open(gpio_shutdown); FD_ZERO(&exceptfds); FD_SET(shutdown_fd, &exceptfds); read(shutdown_fd, buf, MAX_BUF); //dummy read(); any newly opened file is considered changed. res = select(shutdown_fd+1, NULL, // readfds - not needed NULL, // writefds - not needed &exceptfds, NULL); // timeout (never) if (res > 0 && FD_ISSET(shutdown_fd, &exceptfds)) { if (gpio_get_value(gpio_shutdown) == 0) { LOG(LOG_NOTICE, "get shutdown signal from GPIO %d", gpio_shutdown); Shutdown(); } } gpio_fd_close(shutdown_fd); read(shutdown_fd, buf, MAX_BUF); } return 0; }
int main(int argc, char** argv) { //create a variable to store whether we are sending a '1' or a '0' char set_value[5]; //Integer to keep track of whether we want on or off int toggle = 0; int onOffTime; // Time in micro sec to keep the signal on/off int gpio = 60; int gpio_fd; if (argc < 2) { printf("Usage: %s <on/off time in us>\n\n", argv[0]); printf("Toggle gpio 60 at the period given\n"); exit(-1); } onOffTime = atoi(argv[1]); printf("**********************************\n" "* Welcome to PIN Blink program *\n" "* ....blinking gpio 60 *\n" "* ....period of %d us.........*\n" "**********************************\n", 2*onOffTime); //Using sysfs we need to write the gpio number to /sys/class/gpio/export //This will create the folder /sys/class/gpio/gpio60 gpio_export(gpio); printf("...export file accessed, new pin now accessible\n"); //SET DIRECTION gpio_set_dir(gpio, "out"); printf("...direction set to output\n"); gpio_fd = gpio_fd_open(gpio, O_RDONLY); //Run an infinite loop - will require Ctrl-C to exit this program while(1) { toggle = !toggle; gpio_set_value(gpio, toggle); // printf("...value set to %d...\n", toggle); //Pause for a while usleep(onOffTime); } gpio_fd_close(gpio_fd); return 0; }
void RangeFinder::start() { nfds = 2; // Set the signal callback for Ctrl-C // signal(SIGINT, signal_handler); // gpio = atoi(argv[1]); export_gpio(); set_gpio_direction(0); set_gpio_edge("both"); // Can be rising, falling or both gpio_fd = gpio_fd_open(); threadRunning = 1; pthread_create(&rangeFinderThread_t, 0, &RangeFinder::start_thread, this); }
/****************************************************************** * void* mode_unpressed_handler(void* ptr) * wait on rising edge of mode button *******************************************************************/ void* mode_unpressed_handler(void* ptr){ struct pollfd fdset[1]; char buf[MAX_BUF]; int gpio_fd = gpio_fd_open(MODE_BTN); fdset[0].fd = gpio_fd; fdset[0].events = POLLPRI; // high-priority interrupt // keep running until the program closes while(get_state() != EXITING) { // system hangs here until FIFO interrupt poll(fdset, 1, POLL_TIMEOUT); if (fdset[0].revents & POLLPRI) { lseek(fdset[0].fd, 0, SEEK_SET); read(fdset[0].fd, buf, MAX_BUF); if(get_mode_button_state()==UNPRESSED){ mode_unpressed_func(); } } } gpio_fd_close(gpio_fd); return 0; }
int main(int argc, char *argv[]) { HueLightResponse *hueLightResponder= NULL; HueIPResponse *IPAddr= new HueIPResponse("https://www.meethue.com/api/nupnp"); http.AddRequest(IPAddr); int state = HUE_STATE_NEED_IP; struct pollfd fdset[2]; int nfds = 2; int gpio_fd, timeout, rc; char *buf[MAX_BUF]; unsigned int interrupt_line= 17;/*17;*/ unsigned int reset_line= 4; int len; int counter=0; Stream *theLog = new linuxLog(); afSPI *theSPI = new linuxSPI(); gpio_export(interrupt_line); gpio_set_dir(interrupt_line, 0); gpio_set_edge(interrupt_line, "falling"); gpio_fd = gpio_fd_open(interrupt_line); timeout = POLL_TIMEOUT; fprintf(stdout,"Test\n"); theLib = iafLib::create(0,isr_callback,onAttributeSet_callback,onAttributeSetComplete_callback,theLog,theSPI); theLib->mcuISR(); /* we need to hook up and use the reset line */ gpio_export(reset_line); gpio_set_dir(reset_line, 1); gpio_set_value(reset_line,0); timespec sleep_time; timespec remaining; sleep_time.tv_sec=0; sleep_time.tv_nsec=250000; nanosleep(&sleep_time,&remaining); /* check for E_INTR? and call again? */ gpio_set_value(reset_line,1); while (1) { counter++; memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd; fdset[1].events = POLLPRI; lseek(gpio_fd, 0, SEEK_SET); /* consume any prior interrupt */ read(gpio_fd, buf, sizeof (buf)); rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset[1].revents & POLLPRI) { len = read(fdset[1].fd, buf, MAX_BUF); printf("\npoll() GPIO %d interrupt occurred\n", interrupt_line); lseek(gpio_fd, 0, SEEK_SET); /* consume interrupt */ read(gpio_fd, buf, sizeof (buf)); theLib->mcuISR(); } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); //printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } switch (state) { case HUE_STATE_NEED_IP: // we need to check and see if we have an IP, then we can switch if (IPAddr->completed()) { state = HUE_STATE_HAS_IP; IPAddr->get_ip(hue_ip); fprintf(stderr,"IP: %s\n",hue_ip); sprintf(hue_prefix,"http://%s/api/huelibrary/",hue_ip); state = HUE_STATE_HAS_IP; } break; case HUE_STATE_HAS_IP: // every so many loops check and start the lights query to update the lights.. if (counter % 100 == 0) { //check to see how many HTTP requests are outstanding if (hueLightResponder == NULL) { printf("****initializing new request to get the lights\n"); char hue_request[1024]; sprintf(hue_request,"%slights",hue_prefix); hueLightResponder = new HueLightResponse(hue_request,theLib); http.AddRequest(hueLightResponder); } else if (hueLightResponder->completed()) { delete hueLightResponder; char hue_request[1024]; sprintf(hue_request,"%slights",hue_prefix); hueLightResponder = new HueLightResponse(hue_request,theLib); http.AddRequest(hueLightResponder); } } } http.Update(); http.Status(); theLib->loop(); fflush(stdout); } gpio_fd_close(gpio_fd); return 0; }
/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset[5]; // modified to 3, 2 previously ... int nfds = 5; // how many different gpiointerrupts do you have, thats nfds ... int gpio_fd[4], timeout, rc; char buf[MAX_BUF]; unsigned int gpio; int len; int i; // counter variable ... ledport[0] = 60; // gpios of leds ledport[1] = 48; // gpios of leds ledport[2] = 49; // gpios of leds ledport[3] = 30; // gpios of leds int buttonport[4] = {23,7,26,51}; //gpios of buttons int toggle[4] = {0}; // array used to toggle leds ... unsigned int seq = 0x3; signal(SIGINT, signal_handler); system("echo 'welcome to the led interrupt program, press hw button to toggle led'"); // access led for(i=0;i<4;i++) { gpio_export(ledport[i]); gpio_set_dir(ledport[i],"out"); gpio_set_value(ledport[i],0); } for(i=0;i<4;i++) { gpio_export(buttonport[i]); gpio_set_dir(buttonport[i], "in"); gpio_set_edge(buttonport[i], "both"); // Can be rising, falling or both gpio_fd[i] = gpio_fd_open(buttonport[i], O_RDONLY); } timeout = POLL_TIMEOUT; while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; for(i=1;i<5;i++) { fdset[i].fd = gpio_fd[(i-1)]; fdset[i].events = POLLPRI; } rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { //printf("."); } for(i=1;i<5;i++) { if (fdset[i].revents & POLLPRI) { lseek(fdset[i].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[i].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d\n", // buttonport[i-1], buf[0], len); if(buf[0] == '1') { turnTimes(1, 1,&seq, avgANval); printf("Brightest Light at: %d (%d) \n", avgANval[brightPos], brightPos); turnToLight( &seq); sleep(1); } } } trackLight( &seq); if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } fflush(stdout); } return 0; }
int main(int argc, char **argv, char **envp) { struct pollfd fdset[3]; int nfds = 3; int gpio_b1_fd, gpio_b2_fd, gpio_b3_fd; int timeout, rc; char *buf[MAX_BUF]; int b1_first = 1; int b2_first = 1; int b3_first = 1; gpio_b1_fd = gpio_fd_open(20); gpio_b2_fd = gpio_fd_open(7); gpio_b3_fd = gpio_fd_open(106); timeout = POLL_TIMEOUT; while (1) { memset((void*)fdset, 0, sizeof(fdset)); memset(buf, 0, MAX_BUF); //fdset[0].fd = STDIN_FILENO; //fdset[0].events = POLLIN; fdset[0].fd = gpio_b1_fd; fdset[0].events = POLLPRI; fdset[1].fd = gpio_b2_fd; fdset[1].events = POLLPRI; fdset[2].fd = gpio_b3_fd; fdset[2].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed! %s\n", strerror(errno)); return -1; } if (rc == 0) { // timeout occured //printf("."); } if (fdset[0].revents & POLLPRI) { printf("b1_first = %i\n", b1_first); read(fdset[0].fd, buf, MAX_BUF); if ( b1_first == 1 ) { b1_first = 0; } else { printf("\npoll() GPIO %d interrupt occurred -> Button 1\n", 20); syslog(LOG_ERR, "* BUTTON 1 *"); syslog(LOG_ERR, "* SHUTDOWN 1 *"); //printf("buf[0] = %i\n", (int)buf[0]); // SHUTDOWN gpio_set_value(82, 0); nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); gpio_set_value(82, 1); nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); gpio_set_value(82, 0); nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
int main(int argc, char *argv[]) { struct pollfd fdset[2]; int nfds = 2; int gpio_fd, timeout, rc; char *buf[MAX_BUF]; unsigned int interrupt_line= 17;/*17;*/ unsigned int reset_line= 4; int len; int counter=0; Stream *theLog = new linuxLog(); afSPI *theSPI = new linuxSPI(); gpio_export(interrupt_line); gpio_set_dir(interrupt_line, 0); gpio_set_edge(interrupt_line, "falling"); gpio_fd = gpio_fd_open(interrupt_line); timeout = POLL_TIMEOUT; fprintf(stdout,"Test\n"); theLib = iafLib::create(0,isr_callback,onAttributeSet_callback,onAttributeSetComplete_callback,theLog,theSPI); theLib->mcuISR(); /* we need to hook up and use the reset line */ gpio_export(reset_line); gpio_set_dir(reset_line, 1); gpio_set_value(reset_line,0); timespec sleep_time; timespec remaining; sleep_time.tv_sec=0; sleep_time.tv_nsec=250000; nanosleep(&sleep_time,&remaining); /* check for E_INTR? and call again? */ gpio_set_value(reset_line,1); while (1) { counter++; memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd; fdset[1].events = POLLPRI; lseek(gpio_fd, 0, SEEK_SET); /* consume any prior interrupt */ read(gpio_fd, buf, sizeof (buf)); rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset[1].revents & POLLPRI) { len = read(fdset[1].fd, buf, MAX_BUF); printf("\npoll() GPIO %d interrupt occurred\n", interrupt_line); lseek(gpio_fd, 0, SEEK_SET); /* consume interrupt */ read(gpio_fd, buf, sizeof (buf)); theLib->mcuISR(); } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); //printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } if (blinking) { if (counter % 20 ==0 ) { toggleModuloLED(); } } else { setModuloLED(false); } theLib->loop(); fflush(stdout); } gpio_fd_close(gpio_fd); return 0; }
/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset[2]; int nfds = 2; int gpio_fd, timeout, rc; char *buf[MAX_BUF]; unsigned int gpio; int len; if (argc < 2) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); exit(-1); } gpio = atoi(argv[1]); gpio_export(gpio); gpio_set_dir(gpio, 0); gpio_set_edge(gpio, "rising"); gpio_fd = gpio_fd_open(gpio); timeout = POLL_TIMEOUT; while (1) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd; fdset[1].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset[1].revents & POLLPRI) { len = read(fdset[1].fd, buf, MAX_BUF); printf("\npoll() GPIO %d interrupt occurred\n", gpio); } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } fflush(stdout); } gpio_fd_close(gpio_fd); return 0; }
int wiringOliISR (int pin, char *pinName, int mode, void (*function)(void)) { pthread_t threadId ; pthread_attr_t attr; char *modeS ; char fName [64] ; // char pinS [8] ; // pid_t pid ; int count, i ; char c ; int bcmGpioPin ; // char pinName[10] = {}; bcmGpioPin = pinGpio(pin) ; if (mode != INT_EDGE_SETUP) { /**/ if (mode == INT_EDGE_FALLING) modeS = "falling" ; else if (mode == INT_EDGE_RISING) modeS = "rising" ; else modeS = "both" ; // There's really no need to fork now, // as we're no longer using gpio utility here! // if ((pid = fork ()) < 0) { // printf("Failed to fork!\n"); // return -1; // } // if (pid == 0) // Child, exec // { gpio_export(bcmGpioPin); gpio_set_dir(bcmGpioPin, 0, pinName); gpio_set_edge(bcmGpioPin, modeS, pinName); // sysFds [bcmGpioPin] = gpio_fd_open(bcmGpioPin, pinName); // } // else // Parent, wait // wait (NULL) ; } if (sysFds [bcmGpioPin] == -1) { sprintf (fName, "/sys/class/gpio/gpio%d%s/value", bcmGpioPin, pinName) ; // printf("Opening %s for ISR!\n", fName); // if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0) { if ((sysFds [bcmGpioPin] = gpio_fd_open (bcmGpioPin, pinName)) < 0) { printf("Failed to gpio_fd_open!\n"); return -1; } } // Clear any initial pending interrupt ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ; for (i = 0 ; i < count ; ++i) read (sysFds [bcmGpioPin], &c, 1) ; isrFunctions [bcmGpioPin] = function ; pinPass = bcmGpioPin ; strcpy(pinPassName, pinName); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_mutex_lock (&pinMutex) ; pthread_create (&threadId, &attr, interruptHandler, NULL) ; while (pinPass != -1) delay (1) ; pthread_mutex_unlock (&pinMutex) ; pthread_attr_destroy ( &attr ); return 0 ; }
int main(int argc, char *argv[]){ //The gpio port numbers needed to drive the motor unsigned int gpio1 = 30; unsigned int gpio2 = 31; unsigned int gpio3 = 48; unsigned int gpio4 = 51; unsigned int gpio5 = 15; unsigned int gpios[4] = {gpio1, gpio2, gpio3, gpio4}; int gpio_fd1, gpio_fd2, gpio_fd3, gpio_fd4, gpio_fd5; int behavior = atoi(argv[1]); int rc; int len; int nfds = 2; //Port numbers for the analog in char AnalogIn1[] = "AIN0"; char AnalogIn2[] = "AIN1"; //char analogs[] = {AnalogIn1, AnalogIn2}; //printf("checking init analogs: %s \n", analogs[0]); char buf[64]; //Arrays to hold the values we get back from the sensors struct pollfd fdset[2]; int Sensor1_val; int Sensor2_val; //int Sensor_sum; int mode = atoi(argv[1]); //Initializes the position as zero, but we dont really know where this is int pos = 0; //Set up the gpios so we can use them to drive the motor gpio_export(gpio1); gpio_export(gpio2); gpio_export(gpio3); gpio_export(gpio4); gpio_export(gpio5); gpio_set_dir(gpio1, "out"); gpio_set_dir(gpio2, "out"); gpio_set_dir(gpio3, "out"); gpio_set_dir(gpio4, "out"); gpio_set_dir(gpio5, "in"); gpio_set_edge(gpio5, "rising"); gpio_fd1 = gpio_fd_open(gpio1, O_RDONLY); gpio_fd2 = gpio_fd_open(gpio2, O_RDONLY); gpio_fd3 = gpio_fd_open(gpio3, O_RDONLY); gpio_fd4 = gpio_fd_open(gpio4, O_RDONLY); gpio_fd5 = gpio_fd_open(gpio5, O_RDONLY); int cycle = 0; int minValue = 999999; int minPosition = 0; int Sensor_avg = 0; //Wait for the start button to be pushed before we acutally start doing anything printf("Press the start button to begin the program \n"); len = read(gpio_fd5, buf, 64); while(1){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd5; fdset[1].events = POLLPRI; rc = poll(fdset, nfds, 3000); if(rc < 0){ printf("Poll failed \n"); } if(fdset[1].revents & POLLPRI){ lseek(fdset[1].fd, 0, SEEK_SET); len = read(fdset[1].fd, buf, 64); printf("Triggered \n"); //pos = problemOne(gpios, pos); pos = problemTwo(gpios, AnalogIn1, AnalogIn2, pos, mode); //break; } } printf("Begining our steps to attempt to come up with "); for(cycle = 0; cycle <= MAX_CYCLES; cycle++){ printf("One cycle number: %d", cycle); Sensor1_val = analogIn(AnalogIn1); Sensor2_val = analogIn(AnalogIn2); Sensor_avg = (Sensor1_val + Sensor2_val)/ 2; if(Sensor_avg < minValue){ minValue = Sensor_avg; minPosition = cycle; } //Should move the IR sensors here } printf("%d\n", minValue); if(minPosition <=10){ //Rotate clockwise to position } else{ //Rotate counterclockwise to position } while(1){ Sensor1_val = analogIn(AnalogIn1); Sensor2_val = analogIn(AnalogIn2); if(Sensor1_val > Sensor2_val){ //Rotate clockwise once } else{ //Rotate counterclockwise } } return 0; }
int main(int argc, char** argv){ //variable declarations struct pollfd fdset[1]; int nfds = 1; int timeout = 100; int rc; char* buf[MAX_BUF]; int gpio1, gpio2, gpio_count1, gpio_count2; int gpio1_fd, gpio2_fd; int gpio2_value = 0; int program = 0; int ain = 0; float duty_cycle = 0; int i2c_read; //check that at least two arguments are passed in if(argc < 3){ printf("Usage: %s <input-gpio> <output-gpio>\n", argv[0]); printf("polls input-gpio, and writes value to output-gpio\n"); fflush(stdout); return 1; } //set signal handler if (signal(SIGINT, signal_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); //assign gpio values gpio1 = atoi(argv[1]); gpio2 = atoi(argv[2]); //argument 1 will be input export_gpio(gpio1); set_gpio_direction(gpio1, "in"); set_gpio_edge(gpio1, "falling"); gpio1_fd = gpio_fd_open(gpio1); //argument 2 will be output export_gpio(gpio2); set_gpio_direction(gpio2, "out"); set_gpio_value(gpio2, gpio2_value); gpio2_fd = gpio_fd_open(gpio2); //prepare gpio_count1 and gpio_count2 gpio_count1 = 30; gpio_count2 = 31; export_gpio(gpio_count1); export_gpio(gpio_count2); set_mux_value("gpmc_wait0",7); set_gpio_direction(gpio_count1, "out"); set_gpio_direction(gpio_count2, "out"); //prepare i2c bus int file; while(keepgoing){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = gpio1_fd; fdset[0].events = POLLPRI; //fdset[1].fd = gpio2_fd; //fdset[1].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0){ printf("\npoll() failed!\n"); } if((fdset[0].revents & POLLPRI) == POLLPRI) { read(fdset[0].fd, buf, MAX_BUF); usleep(100); gpio2_value = ~(gpio2_value)&1; set_gpio_value(gpio2, gpio2_value); program++; if(program > 3) program = 0; set_gpio_value(gpio_count1, program & 1); set_gpio_value(gpio_count2, program & 2); } switch(program){ case 0: duty_cycle = read_ain("ain6"); duty_cycle = duty_cycle/4095 * 100; set_mux_value("gpmc_a2", 6); set_pwm("ehrpwm.1:0", 1000, (int) duty_cycle); break; case 1: ain = read_ain("ain6"); printf("ain5 = %d\n", ain); unset_pwm("ehrpwm.1:0"); break; case 2: unset_pwm("ehrpwm.1:0"); break; case 3: if((file = open("/dev/i2c-3", O_RDWR)) < 0){ printf("failed to open i2c-3 bus\n"); return 1; } if (ioctl(file, I2C_SLAVE, 72) < 0){ printf("Could not set address for i2c\n"); return 1; } i2c_read = i2c_smbus_read_byte_data(file, 0); close(file); printf("0x%02x (%d)\n", i2c_read, i2c_read); break; default: break; } } gpio_fd_close(gpio1_fd); gpio_fd_close(gpio2_fd); unexport_gpio(gpio1); unexport_gpio(gpio2); unexport_gpio(gpio_count1); unexport_gpio(gpio_count2); unset_pwm("ehrpwm.1:0"); set_mux_value("gpmc_a2", 7); fflush(stdout); return 0; }
int main(int argc, char** argv){ //Create an array to store GPIO FDs the switches are connected to int switchGPIOFDs[4]; int switchPorts[4] = {50, 51, 3, 2}; int ledPorts[4] = {30,31,48,60}; int nfds = 5; int rc, timeout, value, i; struct pollfd fdset[5]; timeout = POLL_TIMEOUT; for(i = 0; i<4 ; i++){ gpio_export(switchPorts[i]); gpio_export(ledPorts[i]); gpio_set_dir(switchPorts[i], "in"); gpio_set_dir(ledPorts[i], "out"); gpio_set_edge(switchPorts[i], "both"); switchGPIOFDs[i] = gpio_fd_open(switchPorts[i], O_RDONLY); } //Set signal callback for Ctrl-C signal(SIGINT, signal_handler); while(keepgoing){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; //Set the FDs for the switch GPIOs for(i = 0; i<4; i++){ fdset[i+1].fd = switchGPIOFDs[i]; fdset[i+1].events = POLLPRI; } rc = poll(fdset, nfds, timeout); if(rc < 0) { printf("\npoll() failed\n"); return -1; } if(rc == 0){ printf("."); } for(i=0; i<4; i++){ if(fdset[i+1].revents & POLLPRI){ gpio_get_value(switchPorts[i], &value); gpio_set_value(ledPorts[i], value); } } fflush(stdout); } for(i = 0; i<4; i++){ gpio_fd_close(switchGPIOFDs[i]); } return 0; }
/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset[7]; //struct pollfd2 fdset2[2]; int nfds = 7; int gpio_fd,gpio_fd2,gpio_fd3,gpio_fd4,gpio_fd5,gpio_fd6,timeout, rc; char buf[MAX_BUF]; int right, down, left, up, shake, exit1; int len; if (argc < 1) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); exit(-1); } // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); right = 7; down = 49; left = 30; up = 60; shake = 50; exit1 = 31; gpio_export(right); gpio_set_dir(right, "in"); gpio_set_edge(right, "rising"); // Can be rising, falling or both gpio_fd = gpio_fd_open(right, O_RDONLY); gpio_export(down); gpio_set_dir(down, "in"); gpio_set_edge(down, "rising"); // Can be rising, falling or both gpio_fd2 = gpio_fd_open(down, O_RDONLY); gpio_export(left); gpio_set_dir(left, "in"); gpio_set_edge(left, "rising"); // Can be rising, falling or both gpio_fd3 = gpio_fd_open(left, O_RDONLY); gpio_export(up); gpio_set_dir(up, "in"); gpio_set_edge(up, "rising"); // Can be rising, falling or both gpio_fd4 = gpio_fd_open(up, O_RDONLY); gpio_export(shake); gpio_set_dir(shake, "in"); gpio_set_edge(shake, "rising"); // Can be rising, falling or both gpio_fd5 = gpio_fd_open(shake, O_RDONLY); gpio_export(exit1); gpio_set_dir(exit1, "in"); gpio_set_edge(exit1, "rising"); // Can be rising, falling or both gpio_fd6 = gpio_fd_open(exit1, O_RDONLY); timeout = POLL_TIMEOUT; int count=0; int length = 8; int height = 8; int i; int j; //int stop = 0; int LEDarray [length][height]; for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { LEDarray[i][j] = 0; } } LEDarray [0][0] = 1; for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } //initialize the start point as 0,0 int x = 0; int y = 0; //char input[1]; while (keepgoing) { int rightDir = 0; int downDir = 0; int leftDir = 0; int upDir = 0; int setShake = 0; int setExit = 0; memset((void*)fdset, 0, sizeof(fdset)); //memset((void*)fdset2, 0, sizeof(fdset2)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd; fdset[1].events = POLLPRI; fdset[2].fd = gpio_fd2; fdset[2].events = POLLPRI; fdset[3].fd = gpio_fd3; fdset[3].events = POLLPRI; fdset[4].fd = gpio_fd4; fdset[4].events = POLLPRI; fdset[5].fd = gpio_fd5; fdset[5].events = POLLPRI; fdset[6].fd = gpio_fd6; fdset[6].events = POLLPRI; /* fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd2; fdset[1].events = POLLPRI; */ rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf(".\n"); } if (fdset[1].revents & POLLPRI) { lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[1].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); rightDir = 1; count++; } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } if (fdset[2].revents & POLLPRI) { lseek(fdset[2].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[2].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); downDir = 1; count++; } if (fdset[3].revents & POLLPRI) { lseek(fdset[3].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[3].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); leftDir = 1; count++; } if (fdset[4].revents & POLLPRI) { lseek(fdset[4].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[4].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); upDir = 1; count++; } if (fdset[5].revents & POLLPRI) { lseek(fdset[5].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[5].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); setShake = 1; count++; } if (fdset[6].revents & POLLPRI) { lseek(fdset[6].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[6].fd, buf, MAX_BUF); //printf("\npoll() GPIO %d interrupt occurred, value=%c, len=%d, count =%d\n", //gpio, buf[0], len, count); setExit = 1; count++; } if(rightDir == 1){ y++; LEDarray [x][y] = 1; //printArray(length, height, LEDarray); printf("\n"); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } else if(downDir == 1){ x++; LEDarray [x][y] = 1; //printArray(length, height, LEDarray); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } else if(leftDir == 1){ y--; LEDarray [x][y] = 1; //printArray(length, height, LEDarray); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } else if(upDir == 1){ x--; LEDarray [x][y] = 1; //printArray(length, height, LEDarray); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } else if(setShake == 1){ //LEDarray = intialZero(length, height, LEDarray); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { LEDarray[i][j] = 0; } } //printArray(length, height, LEDarray); for (i = 0; i < length; i++) { for (j = 0; j < height; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } else if(setExit == 1){ keepgoing = 0; //printArray(length, height, LEDarray); for (i = 0; i < length-1; i++) { for (j = 0; j < height-1; j++) { printf("%i ", LEDarray [i][j]); } printf("\n"); } } fflush(stdout); } gpio_fd_close(gpio_fd); gpio_fd_close(gpio_fd2); gpio_fd_close(gpio_fd3); gpio_fd_close(gpio_fd4); gpio_fd_close(gpio_fd5); gpio_fd_close(gpio_fd6); return 0; }
int main(int argc, char** argv){ //variable declarations struct pollfd fdset[1]; int nfds = 1; int timeout = 3000; int rc; char* buf[MAX_BUF]; int gpio1, gpio2; int gpio1_fd, gpio2_fd; int gpio2_value = 0; int pattern =0; int value =0; char *end; int res, i2cbus, address, size, file; int daddress; char filename[20]; //Setting the signal handler for Ctrl + C if (signal(SIGINT, signal_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); //Argument checking for at least five if(argc < 6){ printf("Usage: %s <input-gpio> <output-gpio> <i2c-bus> <i2c-address> <register>\n", argv[0]); printf("polls input-gpio, and writes value to output-gpio\n"); fflush(stdout); return 1; } //Assigning gpio values gpio1 = atoi(argv[1]); gpio2 = atoi(argv[2]); //Input for Argument 1 export_gpio(gpio1); set_gpio_direction(gpio1, "in"); set_gpio_edge(gpio1, "falling"); gpio1_fd = gpio_fd_open(gpio1); //Output for Argument 2 export_gpio(gpio2); set_gpio_direction(gpio2, "out"); set_gpio_value(gpio2, gpio2_value); gpio2_fd = gpio_fd_open(gpio2); //Assigning I2C values i2cbus = atoi(argv[3]); address = atoi(argv[4]); daddress = atoi(argv[5]); size = I2C_SMBUS_BYTE; sprintf(filename, "/dev/i2c-%d", i2cbus); file = open(filename, O_RDWR); if (file<0) { if (errno == ENOENT) { fprintf(stderr, "Error: Could not open file " "/dev/i2c-%d: %s\n", i2cbus, strerror(ENOENT)); } else { fprintf(stderr, "Error: Could not open file " "`%s': %s\n", filename, strerror(errno)); if (errno == EACCES) fprintf(stderr, "Run as root?\n"); } exit(1); } if (ioctl(file, I2C_SLAVE, address) < 0) { fprintf(stderr, "Error: Could not set address to 0x%02x: %s\n", address, strerror(errno)); return -errno; } while(loop){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = gpio1_fd; fdset[0].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0){ printf("\npoll() failed!\n"); } if (rc == 0){ printf("."); } if((fdset[0].revents & POLLPRI) == POLLPRI) { read(fdset[0].fd, buf, MAX_BUF); printf("interrupt value=%c\n", buf[0]); pattern++; if(pattern == 4){ pattern = 0; } } printf("Case %d\n",pattern); switch(pattern){ // Blink led case 0: if(gpio2_value){ gpio2_value = 0; } else{ gpio2_value = 1; } set_gpio_value(gpio2, gpio2_value); break; // PWM output for blinking LED by frequency and duty cycle case 1: set_mux_value("gpmc_a2",6); set_pwm("ehrpwm.1:0",10,25); break; // Analog In for reading Voltage from POT case 2: unset_pwm("ehrpwm.1:0"); value = read_ain("ain6"); printf("Voltage: %d\n",value); break; // I2C Device reading temperature from the sensor case 3: printf("Case 3\n"); res = i2c_smbus_write_byte(file, daddress); if (res < 0) { fprintf(stderr, "Warning - write failed, filename=%s, daddress=%d\n", filename, daddress); } res = i2c_smbus_read_byte_data(file, daddress); if (res < 0) { fprintf(stderr, "Error: Read failed, res=%d\n", res); exit(2); } printf("Temperature: %d°C\n", res); break; default: break; } } close(file); gpio_fd_close(gpio1_fd); gpio_fd_close(gpio2_fd); unexport_gpio(gpio1); unexport_gpio(gpio2); fflush(stdout); return 0; }
int main(int argc, char **argv, char **envp) { struct pollfd fdset[7]; int nfds = 7; int gpio_fd1, gpio_fd2, gpio_fd3, gpio_fd4,gpio_fd5,gpio_fd6, timeout, rc; char buf[MAX_BUF]; unsigned int gpio; int len; char board[HEIGHT][WIDTH]; int xpos=WIDTH/2; int ypos=HEIGHT/2; int row; int col; int colorint=0; char color[]={'x','y','z'}; for(row=0;row<HEIGHT;row++){ printf("\n"); for(col=0;col<WIDTH;col++){ board[row][col]=' '; } } int res, i2cbus, address, file; char filename[20]; int force = 0; i2cbus = lookup_i2c_bus("1"); printf("i2cbus = %d\n", i2cbus); if (i2cbus < 0) help(); address = parse_i2c_address("0x70"); printf("address = 0x%2x\n", address); if (address < 0) help(); file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0); // printf("file = %d\n", file); if (file < 0 || check_funcs(file) || set_slave_addr(file, address, force)) exit(1); // Check the return value on these if there is trouble i2c_smbus_write_byte(file, 0x21); // Start oscillator (p10) i2c_smbus_write_byte(file, 0x81); // Disp on, blink off (p11) i2c_smbus_write_byte(file, 0xe7); // Full brightness (page 15) // Display a series of pictures system("/home/root/homework3/i2csetup.sh"); board[xpos][ypos]='x'; printboard(board,file,colorint); //printf("wgat"); /*if (argc < 2) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); FILE* f = fopen("/sys/class/leds/beaglebone\:green\:usr0/brightness", "w"); FILE* trig = fopen("/sys/class/leds/beaglebone\:green\:usr0/trigger", "w"); if (trig == NULL){ printf("oops\n"); exit(EXIT_FAILURE); } fprintf(trig, "none"); fclose(trig); if (fled== NULL){ printf("oops\n"); exit(EXIT_FAILURE); } printf("should light up\n"); fprintf(fled, "1"); fclose(fled); usleep(10000000); exit(-1); } */ // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); gpio_export(GPIOIN1); gpio_set_dir(GPIOIN1, "in"); gpio_set_edge(GPIOIN1, "both"); // Can be rising, falling or both gpio_fd1 = gpio_fd_open(GPIOIN1, O_RDONLY); gpio_export(GPIOIN2); gpio_set_dir(GPIOIN2, "in"); gpio_set_edge(GPIOIN2, "both"); // Can be rising, falling or both gpio_fd2 = gpio_fd_open(GPIOIN2, O_RDONLY); gpio_export(GPIOIN3); gpio_set_dir(GPIOIN3, "in"); gpio_set_edge(GPIOIN3, "both"); // Can be rising, falling or both gpio_fd3 = gpio_fd_open(GPIOIN3, O_RDONLY); gpio_export(GPIOIN4); gpio_set_dir(GPIOIN4, "in"); gpio_set_edge(GPIOIN4, "both"); // Can be rising, falling or both gpio_fd4 = gpio_fd_open(GPIOIN4, O_RDONLY); gpio_export(GPIOIN5); gpio_set_dir(GPIOIN5, "in"); gpio_set_edge(GPIOIN5, "both"); // Can be rising, falling or both gpio_fd5 = gpio_fd_open(GPIOIN5, O_RDONLY); gpio_export(GPIOIN6); gpio_set_dir(GPIOIN6, "in"); gpio_set_edge(GPIOIN6, "both"); // Can be rising, falling or both gpio_fd6 = gpio_fd_open(GPIOIN6, O_RDONLY); timeout = POLL_TIMEOUT; FILE* f0 = fopen("/sys/class/leds/beaglebone:green:usr0/trigger", "w"); FILE* f1 = fopen("/sys/class/leds/beaglebone:green:usr1/trigger", "w"); FILE* f2 = fopen("/sys/class/leds/beaglebone:green:usr2/trigger", "w"); FILE* f3 = fopen("/sys/class/leds/beaglebone:green:usr3/trigger", "w"); if (f0 == NULL){ exit(EXIT_FAILURE); } fprintf(f0, "none"); fclose(f0); if (f1 == NULL){ exit(EXIT_FAILURE); } fprintf(f1, "none"); fclose(f1); if (f2 == NULL){ exit(EXIT_FAILURE); } fprintf(f2, "none"); fclose(f2); if (f3 == NULL){ exit(EXIT_FAILURE); } fprintf(f3, "none"); fclose(f3); while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd1; fdset[1].events = POLLPRI; fdset[2].fd = gpio_fd2; fdset[2].events = POLLPRI; fdset[3].fd = gpio_fd3; fdset[3].events = POLLPRI; fdset[4].fd = gpio_fd4; fdset[4].events = POLLPRI; fdset[5].fd = gpio_fd5; fdset[5].events = POLLPRI; fdset[6].fd = gpio_fd6; fdset[6].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (fdset[1].revents & POLLPRI) { lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[1].fd, buf, MAX_BUF); FILE* f = fopen("/sys/class/leds/beaglebone:green:usr3/brightness", "w"); if (f == NULL){ exit(EXIT_FAILURE); } if (buf[0]=='1'){ fprintf(f, "0"); } if (buf[0]=='0'){ fprintf(f, "1"); if((xpos+1)<WIDTH){ board[xpos+1][ypos]='x'; xpos++; } printboard(board,file,colorint); } fclose(f); } if (fdset[2].revents & POLLPRI) { lseek(fdset[2].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[2].fd, buf, MAX_BUF); FILE* f = fopen("/sys/class/leds/beaglebone:green:usr2/brightness", "w"); if (f == NULL){ exit(EXIT_FAILURE); } if (buf[0]=='1'){ fprintf(f, "0"); } if (buf[0]=='0'){ fprintf(f, "1"); if((xpos-1)>=0){ board[xpos-1][ypos]='x'; xpos--; } printboard(board,file,colorint); } fclose(f); } if (fdset[3].revents & POLLPRI) { lseek(fdset[3].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[3].fd, buf, MAX_BUF); FILE* f = fopen("/sys/class/leds/beaglebone:green:usr1/brightness", "w"); if (f == NULL){ exit(EXIT_FAILURE); } if (buf[0]=='1'){ fprintf(f, "0"); } if (buf[0]=='0'){ if((ypos+1)<HEIGHT){ board[xpos][ypos+1]='x'; ypos++; } printboard(board,file,colorint); fprintf(f, "1"); } fclose(f); } if (fdset[4].revents & POLLPRI) { lseek(fdset[4].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[4].fd, buf, MAX_BUF); FILE* f = fopen("/sys/class/leds/beaglebone:green:usr0/brightness", "w"); if (f == NULL){ exit(EXIT_FAILURE); } if (buf[0]=='1'){ fprintf(f, "0"); } if (buf[0]=='0'){ if((ypos-1)>=0){ board[xpos][ypos-1]='x'; ypos--; } printboard(board,file,colorint); fprintf(f, "1"); } fclose(f); } if (fdset[5].revents & POLLPRI) { lseek(fdset[5].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[5].fd, buf, MAX_BUF); if (buf[0]=='0'){ for(row=0;row<HEIGHT;row++){ printf("\n"); for(col=0;col<WIDTH;col++){ board[row][col]=' '; } printf("\n"); system("i2cget -y 1 0x48 0"); printboard(board,file,colorint); } } } if (fdset[6].revents & POLLPRI) { lseek(fdset[6].fd, 0, SEEK_SET); // Read from the start of the file read(fdset[6].fd, buf, MAX_BUF); if (buf[0]=='0'){ if(colorint<=0){ colorint++; }else{ colorint=0; } printf("\n"); system("i2cget -y 1 0x4a 0"); printboard(board,file,colorint); } } /*if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); }*/ fflush(stdout); } gpio_fd_close(gpio_fd1); gpio_fd_close(gpio_fd2); gpio_fd_close(gpio_fd3); gpio_fd_close(gpio_fd4); return 0; }
int main(int argc, char **argv, char **envp) { int xbound = atoi(argv[1]); int ybound = atoi(argv[2]); struct pollfd fdset[7]; int nfds = 7; int gpio_fd1, gpio_fd2, gpio_fd3, gpio_fd4, gpio_fd5, gpio_fd6, timeout, rc; char buf[MAX_BUF]; unsigned int gpio1, gpio2, gpio3, gpio4, gpio5, gpio6, gpio7, gpio8; int len; if (argc < 2) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); exit(-1); } // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); gpio1 = atoi(argv[5]); gpio2 = atoi(argv[6]); gpio3 = atoi(argv[7]); gpio4 = atoi(argv[8]); gpio5 = atoi(argv[9]); gpio6 = atoi(argv[10]); gpio_export(gpio1); gpio_export(gpio2); gpio_export(gpio3); gpio_export(gpio4); gpio_export(gpio5); gpio_export(gpio6); gpio_set_dir(gpio1, "in"); gpio_set_dir(gpio2, "in"); gpio_set_dir(gpio3, "in"); gpio_set_dir(gpio4, "in"); gpio_set_dir(gpio5, "in"); gpio_set_dir(gpio6, "in"); gpio_set_edge(gpio1, "rising"); gpio_set_edge(gpio2, "rising"); gpio_set_edge(gpio3, "rising"); gpio_set_edge(gpio4, "rising"); gpio_set_edge(gpio5, "rising"); gpio_set_edge(gpio6, "rising"); gpio_fd1 = gpio_fd_open(gpio1, O_RDONLY); gpio_fd2 = gpio_fd_open(gpio2, O_RDONLY); gpio_fd3 = gpio_fd_open(gpio3, O_RDONLY); gpio_fd4 = gpio_fd_open(gpio4, O_RDONLY); gpio_fd5 = gpio_fd_open(gpio5, O_RDONLY); gpio_fd6 = gpio_fd_open(gpio6, O_RDONLY); timeout = POLL_TIMEOUT; int erase = 1; char **a; a = (char **) malloc(xbound*sizeof(char *)); int t = 0; for(t; t <= atoi(argv[1]) ; t++){ a[t] = (char *) malloc(ybound*sizeof(char)); } int r = 0; int s = 0; for(r; r <= xbound; r++) { s = 0; for(s; s <= ybound; s++) { a[r][s] = 'a'; printf("%c", a[r][s]); } printf("\n"); } int posx = atoi(argv[3]); int posy = atoi(argv[4]); a[posx][posy] = 'X'; while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_fd1; fdset[1].events = POLLPRI; fdset[2].fd = gpio_fd2; fdset[2].events = POLLPRI; fdset[3].fd = gpio_fd3; fdset[3].events = POLLPRI; fdset[4].fd = gpio_fd4; fdset[4].events = POLLPRI; fdset[5].fd = gpio_fd5; fdset[5].events = POLLPRI; fdset[6].fd = gpio_fd6; fdset[6].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } int i = 1; for( i; i <= 6; i++){ if (fdset[i].revents & POLLPRI) { lseek(fdset[i].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[i].fd, buf, MAX_BUF); if(i == 5) { printf("Switching draw modes \n"); erase = !erase; } else if(i == 6) { system(" clear "); r = 0; s = 0; for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { a[r][s] = 'a'; printf("%c", a[r][s]); } printf("\n"); } } else if(i == 1) { system(" clear "); if(posx == xbound) { printf("Cant go past bounds \n"); r = 0; s = 0; for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } else { r = 0; s = 0; posx +=1; if(erase == 0) { a[posx][posy] = 'X'; } else{ a[posx][posy] = 'a'; } for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } } else if(i == 2) { system(" clear "); if(posx == 0) { printf("Cant go past bounds \n"); r = 0; s = 0; for(r; r <= atoi(argv[1]); r++) { s =0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } else { r = 0; s = 0; posx -=1; if(erase == 0) { a[posx][posy] = 'X'; } else { a[posx][posy] = 'a'; } for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } } else if(i == 3) { system( "clear"); if(posy == ybound) { printf("Cant go past bounds \n"); r = 0; s = 0; for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } else { r = 0; s = 0; posy +=1; if(erase == 0) { a[posx][posy] = 'X'; } else { a[posx][posy] = 'a'; } for(r; r <= atoi(argv[1]); r++) { s= 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } } else if( i == 4) { system(" clear "); if(posy == 0) { printf("Cant go past bounds \n"); r = 0; s = 0; for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } else { r = 0; s = 0; posy -=1; if(erase == 0) { a[posx][posy] = 'X'; } else { a[posx][posy] = 'a'; } for(r; r <= atoi(argv[1]); r++) { s = 0; for(s; s <= atoi(argv[2]); s++) { printf("%c", a[r][s]); } printf("\n"); } } } else{ continue; } } } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } fflush(stdout); } gpio_fd_close(gpio_fd1); gpio_fd_close(gpio_fd2); gpio_fd_close(gpio_fd3); gpio_fd_close(gpio_fd4); return 0; }
int main(int argc, char **argv, char **envp) { struct pollfd fdset[10]; int nfds = 10; int gpio_fdbut0, gpio_fdbut1, gpio_fdbut2, gpio_fdbut3, gpio_fdbut4, gpio_fdbut5, gpio_fdbut6, gpio_fdbut7, gpio_fdbut8, gpio_fdbut9, timeout, rc; char buf[MAX_BUF]; unsigned int gpio; unsigned int but0 = 50; //0->1 p14 //L unsigned int but1 = 60; //1->0 p12 //R unsigned int but2 = 3; //1->0 p3 //U unsigned int but3 = 2; //0->1 p2 // D use these for directional unsigned int but4 = 30; //1->0 p10 // A unsigned int but5 = 31; //1->0 p12 // B unsigned int but6 = 48; //1->0 p15 // Start unsigned int but7 = 49; //0->1 p23 //Select unsigned int but8 = 115; //0->1 p27 //End unsigned int but9 = 14; //1->0 p26 //Change game int len; int state[4]; state[0] = 1; state[1] = 1; state[2] = 1; state[3] = 1; char ain0[] = "AIN0"; char ain1[] = "AIN1"; int ainR[2]; int deadband = 200; //deadband control //output an analog pin, to enable reading printf("test\n"); system("SLOTS=/sys/devices/bone_capemgr.*/slots"); system("PINS=/sys/kernel/debug/pinctrl/44e10800.pinmux/pins"); system("echo cape-bone-iio > SLOTS"); // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); gpio_export(but0); gpio_export(but1); gpio_export(but2); gpio_export(but3); gpio_export(but4); gpio_export(but5); gpio_export(but6); gpio_export(but7); gpio_export(but8); gpio_export(but9); gpio_set_dir(but0, "in"); gpio_set_dir(but1, "in"); gpio_set_dir(but2, "in"); gpio_set_dir(but3, "in"); gpio_set_dir(but4, "in"); gpio_set_dir(but5, "in"); gpio_set_dir(but6, "in"); gpio_set_dir(but7, "in"); gpio_set_dir(but8, "in"); gpio_set_dir(but9, "in"); gpio_set_edge(but0, "falling");// Can be rising, falling or both gpio_set_edge(but1, "falling"); gpio_set_edge(but2, "falling"); gpio_set_edge(but3, "falling"); gpio_set_edge(but4, "rising"); gpio_set_edge(but5, "rising"); gpio_set_edge(but6, "rising"); gpio_set_edge(but7, "rising"); gpio_set_edge(but8, "rising"); gpio_set_edge(but9, "rising"); gpio_fdbut0 = gpio_fd_open(but0, O_RDONLY); gpio_fdbut1 = gpio_fd_open(but1, O_RDONLY); gpio_fdbut2 = gpio_fd_open(but2, O_RDONLY); gpio_fdbut3 = gpio_fd_open(but3, O_RDONLY); gpio_fdbut4 = gpio_fd_open(but4, O_RDONLY); gpio_fdbut5 = gpio_fd_open(but5, O_RDONLY); gpio_fdbut6 = gpio_fd_open(but6, O_RDONLY); gpio_fdbut7 = gpio_fd_open(but7, O_RDONLY); gpio_fdbut8 = gpio_fd_open(but8, O_RDONLY); gpio_fdbut9 = gpio_fd_open(but9, O_RDONLY); timeout = POLL_TIMEOUT; while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = gpio_fdbut0; fdset[0].events = POLLPRI; fdset[1].fd = gpio_fdbut1; fdset[1].events = POLLPRI; fdset[2].fd = gpio_fdbut2; fdset[2].events = POLLPRI; fdset[3].fd = gpio_fdbut3; fdset[3].events = POLLPRI; fdset[4].fd = gpio_fdbut4; fdset[4].events = POLLPRI; fdset[5].fd = gpio_fdbut5; fdset[5].events = POLLPRI; fdset[6].fd = gpio_fdbut6; fdset[6].events = POLLPRI; fdset[7].fd = gpio_fdbut7; fdset[7].events = POLLPRI; fdset[8].fd = gpio_fdbut8; fdset[8].events = POLLPRI; fdset[9].fd = gpio_fdbut9; fdset[9].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { //printf("."); } if (fdset[0].revents & POLLPRI) { lseek(fdset[0].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[0].fd, buf, MAX_BUF); system("xdotool key a"); system("xdotool key Return"); //state[0] = !state[0]; } if (fdset[1].revents & POLLPRI) { lseek(fdset[1].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[1].fd, buf, MAX_BUF); system("xdotool key d"); system("xdotool key Return"); //state[1] = !state[1]; } if (fdset[2].revents & POLLPRI) { lseek(fdset[2].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[2].fd, buf, MAX_BUF); system("xdotool key w"); system("xdotool key Return"); //state[2] = !state[2]; } if (fdset[3].revents & POLLPRI) { lseek(fdset[3].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[3].fd, buf, MAX_BUF); //system("xdotool key Down"); system("xdotool key s"); system("xdotool key Return"); //state[3] = !state[3]; } if (fdset[4].revents & POLLPRI) { lseek(fdset[4].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[4].fd, buf, MAX_BUF); system("xdotool key q"); system("xdotool key Return"); } if (fdset[5].revents & POLLPRI) { lseek(fdset[5].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[5].fd, buf, MAX_BUF); system("xdotool key Control_L"); } if (fdset[6].revents & POLLPRI) { lseek(fdset[6].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[6].fd, buf, MAX_BUF); system("xdotool key Return"); } if (fdset[7].revents & POLLPRI) { lseek(fdset[7].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[7].fd, buf, MAX_BUF); system("xdotool key d"); } if (fdset[8].revents & POLLPRI) { lseek(fdset[8].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[8].fd, buf, MAX_BUF); system("xdotool key e"); } if (fdset[9].revents & POLLPRI) { lseek(fdset[9].fd, 0, SEEK_SET);// Read from the start of the file len = read(fdset[9].fd, buf, MAX_BUF); system("xdotool key f"); } /* if (state[0] == 1){ system("xdotool key Left"); } if (state[1] == 1){ system("xdotool key Right"); } if (state[2] == 1){ system("xdotool key Up"); } if (state[3] == 1){ system("xdotool key Down"); } */ ainR[0] = analogRead(ain0); ainR[1] = analogRead(ain1); ainR[0] = analogRead(ain0); ainR[1] = analogRead(ain1); ainR[0] = analogRead(ain0); ainR[1] = analogRead(ain1); ainR[0] = analogRead(ain0); ainR[1] = analogRead(ain1); ainR[0] = analogRead(ain0); ainR[1] = analogRead(ain1); //printf("value %d %d\n", ainR[0], ainR[1]); if (ainR[0] - deadband > 880){ system("xdotool key s"); system("xdotool key Return"); } if (ainR[0] + deadband < 880){ system("xdotool key w"); system("xdotool key Return"); } if (ainR[1] - deadband > 880){ system("xdotool key d"); system("xdotool key Return"); } if (ainR[1] + deadband < 880){ system("xdotool key a"); system("xdotool key Return"); //printf("Mission succesfull"); } //fflush(stdout); usleep(100); //0.1ms } gpio_fd_close(gpio_fdbut0); gpio_fd_close(gpio_fdbut1); gpio_fd_close(gpio_fdbut2); gpio_fd_close(gpio_fdbut3); gpio_fd_close(gpio_fdbut4); gpio_fd_close(gpio_fdbut5); gpio_fd_close(gpio_fdbut6); gpio_fd_close(gpio_fdbut7); gpio_fd_close(gpio_fdbut8); gpio_fd_close(gpio_fdbut9); return 0; }
int main(int argc, char *argv[]) { int ret = 0; int fd; int sysfs_fd; int no_tty = !isatty( fileno(stdout) ); fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); /* * spi mode */ ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) pabort("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) pabort("can't get spi mode"); /* * bits per word */ ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't get bits per word"); fprintf(stderr, "spi mode: %d\n", mode); fprintf(stderr, "bits per word: %d\n", bits); // enable master clock for the AD // divisor results in roughly 4.9MHz // this also inits the general purpose IO gz_clock_ena(GZ_CLK_5MHz,5); // enables sysfs entry for the GPIO pin gpio_export(drdy_GPIO); // set to input gpio_set_dir(drdy_GPIO,0); // set interrupt detection to falling edge gpio_set_edge(drdy_GPIO,"falling"); // get a file descriptor for the GPIO pin sysfs_fd = gpio_fd_open(drdy_GPIO); // resets the AD7705 so that it expects a write to the communication register printf("sending reset\n"); writeReset(fd); // tell the AD7705 that the next write will be to the clock register writeReg(fd,0x20); // write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock writeReg(fd,0x0C); //channel1 writeReg(fd,0x11); // intiates a self calibration and then after that starts converting writeReg(fd,0x40); ret = gpio_poll(sysfs_fd,1000); if (ret<1) { fprintf(stderr,"Poll error chennel1 set-up %d\n",ret); } //channel0 // tell the AD7705 that the next write will be the setup register writeReg(fd,0x10); // intiates a self calibration and then after that starts converting writeReg(fd,0x40); ret = gpio_poll(sysfs_fd,1000); if (ret<1) { fprintf(stderr,"Poll error chennel0 set-up %d\n",ret); } // we read data in an endless loop and display it // this needs to run in a thread ideally while (1) { //channel0 // tell the AD7705 to read the data register (16 bits) writeReg(fd,0x38); ret = gpio_poll(sysfs_fd,1000); if (ret<1) { fprintf(stderr,"Poll error read data channel0 %d\n",ret); } // read the data register by performing two 8 bit reads int value0 = readData(fd)-0x8000; //channel1 // tell the AD7705 to read the data register (16 bits) writeReg(fd,0x39); ret = gpio_poll(sysfs_fd,1000); if (ret<1) { fprintf(stderr,"Poll error read data channel0 %d\n",ret); } // read the data register by performing two 8 bit reads int value1 = readData(fd)-0x8000; fprintf(stderr,"data0 = %d data1 = %d \r",value0,value1); } close(fd); gpio_fd_close(sysfs_fd); return ret; }
int main(int argc, char **argv, char **envp) { int file; const char *filename = "/dev/i2c-1"; if((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus.\n"); exit(1); } int device_addr = 0x53; if(ioctl(file, I2C_SLAVE, device_addr) < 0) { printf("Failed to acquire bus and/or talk to slave.\n"); exit(1); } write_i2c(file, 0x2c, SAMPLING_RATE); //BW_RATE -- Last 4 bits = sampling rate write_i2c(file, 0x31, 0x20 | RANGE); //DATA_FORMAT -- Last 2 bits = range write_i2c(file, 0x2e, 0x02); //INT_ENABLE write_i2c(file, 0x38, 0x41); //FIFO_CTL write_i2c(file, 0x2d, 0x08); //POWER_CTL int buttons[] = BUTTON_GPIO_PINS; int button_size = sizeof(buttons)/sizeof(buttons[0]); int button_active_edges[] = BUTTON_ACTIVE_EDGES; struct pollfd fdset[button_size]; int nfds = button_size; int gpio_fd[button_size]; int button; for(button = 0; button < button_size; button++) { gpio_export(buttons[button]); gpio_set_dir(buttons[button], "in"); gpio_set_edge(buttons[button], button_active_edges[button] ? "rising" : "falling"); gpio_fd[button] = gpio_fd_open(buttons[button], O_RDONLY); } int num_samples = 0; long int z_nooffset = 0; long int z_accumulator = 0; long int previous_z_accumulator = 0; long int z_diff = 0; long int previous_z = 0; long int z_jerk_fifo[QUEUE_LENGTH] = {0}; long int z_accel_fifo[QUEUE_LENGTH] = {0}; while(1) { memset((void*)fdset, 0, sizeof(fdset)); for(button = 0; button < button_size; button++) { fdset[button].fd = gpio_fd[button]; fdset[button].events = POLLPRI; } poll(fdset, nfds, 100); for(button = 0; button < button_size; button++) { if(fdset[button].revents & POLLPRI) { char buf[1]; read(fdset[button].fd, buf, 1); int button_state; gpio_get_value(buttons[button], &button_state); while(button_state == button_active_edges[button]) { char write_buf[1]; write_buf[0] = 0x32; if(write(file, write_buf,1) != 1) { printf("Failed to write to the i2c bus.\n"); } int reg_num; signed char read_buf[6]; if(read(file, read_buf, 6) != 6) { printf("Failed to read from the i2c bus.\n"); } else { long int z_data = ((((long int)read_buf[5]) << 8) | (0xff & read_buf[4])); num_samples++; if( num_samples > 1 ) { z_diff = (z_data - previous_z); z_nooffset += (z_diff - z_jerk_fifo[QUEUE_LENGTH - 1]); z_accumulator = (z_nooffset); } /*if( num_samples % 1 == 0 ) { printf(" \r"); printf("Current vel = %d\r", z_nooffset); fflush(stdout); }*/ if( (z_accumulator * previous_z_accumulator) < 0 && abs(z_accumulator - previous_z_accumulator) > 50 ) { // Put stuff to do on hits here! printf("HIT!\n"); printf("Acceleration = %d.\n", z_nooffset); fflush(stdout); } int index; for( index = QUEUE_LENGTH - 1; index >= 0; index-- ){ z_jerk_fifo[index] = z_jerk_fifo[index - 1]; z_accel_fifo[index] = z_accel_fifo[index - 1]; } z_jerk_fifo[0] = z_diff; z_accel_fifo[0] = z_nooffset; previous_z = z_data; previous_z_accumulator = z_accumulator; } gpio_get_value(buttons[button], &button_state); } } } } }
ADCreader::ADCreader() { int ret = 0; // set up ringbuffer samples = new int[MAX_SAMPLES]; // pointer for incoming data pIn = samples; // pointer for outgoing data pOut = samples; // SPI constants static const char *device = "/dev/spidev0.0"; mode = SPI_CPHA | SPI_CPOL; bits = 8; speed = 50000; delay = 10; drdy_GPIO = 22; // open SPI device fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); /* * spi mode */ ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) pabort("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) pabort("can't get spi mode"); /* * bits per word */ ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't get bits per word"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz"); fprintf(stderr, "spi mode: %d\n", mode); fprintf(stderr, "bits per word: %d\n", bits); fprintf(stderr, "max speed: %d Hz (%d KHz)\n", speed, speed/1000); // enable master clock for the AD // divisor results in roughly 4.9MHz // this also inits the general purpose IO gz_clock_ena(GZ_CLK_5MHz,5); // enables sysfs entry for the GPIO pin gpio_export(drdy_GPIO); // set to input gpio_set_dir(drdy_GPIO,0); // set interrupt detection to falling edge gpio_set_edge(drdy_GPIO,"falling"); // get a file descriptor for the GPIO pin sysfs_fd = gpio_fd_open(drdy_GPIO); // resets the AD7705 so that it expects a write to the communication register writeReset(fd); // tell the AD7705 that the next write will be to the clock register writeReg(fd,0x20); // write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock writeReg(fd,0x0C); // tell the AD7705 that the next write will be the setup register writeReg(fd,0x10); // intiates a self calibration and then after that starts converting writeReg(fd,0x40); }
int main(int argc, char **argv, char **envp) { signal(SIGINT, signal_handler); int i; int buttons[] = BUTTON_GPIO_PINS; int button_size = sizeof(buttons)/sizeof(buttons[0]); int button_active_edges[] = BUTTON_ACTIVE_EDGES; int leds[] = LED_GPIO_PINS; int led_size = sizeof(leds)/sizeof(leds[0]); struct pollfd fdset[button_size]; int nfds = 4; int gpio_fd[button_size + led_size]; for(i = 0; i < button_size; i++) { gpio_export(buttons[i]); gpio_set_dir(buttons[i], "in"); gpio_set_edge(buttons[i], "both"); gpio_fd[i] = gpio_fd_open(buttons[i], O_RDONLY); } for(i = 0; i < led_size; i++) { gpio_export(leds[i]); gpio_set_dir(leds[i], "out"); gpio_fd[i+4] = gpio_fd_open(leds[i], O_RDONLY); } while(keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); for(i = 0; i < button_size; i++) { fdset[i].fd = gpio_fd[i]; fdset[i].events = POLLPRI; } poll(fdset, nfds, TIMEOUT); for(i = 0; i < button_size; i++) { if(fdset[i].revents & POLLPRI) { char buf[1]; read(fdset[i].fd, buf, 1); int button_state; gpio_get_value(buttons[i], &button_state); int led_state = (button_active_edges[i] == button_state); gpio_set_value(leds[i], led_state); printf("\nButton (GPIO %d) changed state to %d.\n", buttons[i], button_state); printf("\tLED (GPIO %d) changed state to %d as a result\n", leds[i], led_state); } } fflush(stdout); } for(i = 0; i < button_size + led_size; i++) { gpio_fd_close(gpio_fd[i]); } printf("\nCtrl-C pressed--exiting...\n"); return 0; }
/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset[2]; int nfds = 2; int gpio_in_fd, gpio_out_fd, timeout, rc; char buf[MAX_BUF]; int gpio_in, gpio_out; int len; if (argc < 3) { printf("Usage: %s <gpio-in> <gpio-out>\n\n"); printf("Interrupt driven output\n"); exit(-1); } // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); gpio_in = atoi(argv[1]); gpio_out = atoi(argv[2]); gpio_export(gpio_in); gpio_export(gpio_out); gpio_set_dir(gpio_in, "in"); gpio_set_dir(gpio_out, "out"); gpio_set_edge(gpio_in, "both"); // Can be rising, falling or both gpio_in_fd = gpio_fd_open(gpio_in, O_RDONLY); gpio_out_fd = gpio_fd_open(gpio_out, O_RDONLY); timeout = POLL_TIMEOUT; while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_in_fd; fdset[1].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (fdset[1].revents & POLLPRI) { lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file len = read(fdset[1].fd, buf, MAX_BUF); unsigned int value; if (buf[0] == '0') { value = 0; } else if (buf[0] == '1') { value = 1; } gpio_set_value(gpio_out, value); } fflush(stdout); } gpio_fd_close(gpio_in_fd); gpio_fd_close(gpio_out_fd); return 0; }
/**************************************************************** * Main ****************************************************************/ int main(int argc, char **argv, char **envp) { struct pollfd fdset[5]; int nfds = 5; int gpio_in_1_fd, gpio_out_1_fd, gpio_in_2_fd, gpio_out_2_fd, gpio_in_3_fd, gpio_out_3_fd, gpio_in_4_fd, gpio_out_4_fd; int timeout, rc; char buf[MAX_BUF]; int gpio_in_1, gpio_out_1, gpio_in_2, gpio_out_2, gpio_in_3, gpio_out_3, gpio_in_4, gpio_out_4; int len; // run with ./etch_gpio 50 115 51 49 20 48 7 112 if (argc < 9) { printf("Usage: %s <gpio-in> <gpio-out>, ... , ... , ...\n\n"); printf("Interrupt driven output of four buttons, put in pairs of in/out\n"); exit(-1); } // Set the signal callback for Ctrl-C signal(SIGINT, signal_handler); gpio_in_1 = atoi(argv[1]); gpio_out_1 = atoi(argv[2]); gpio_export(gpio_in_1); gpio_export(gpio_out_1); gpio_set_dir(gpio_in_1, "in"); gpio_set_dir(gpio_out_1, "out"); gpio_set_edge(gpio_in_1, "falling"); gpio_in_1_fd = gpio_fd_open(gpio_in_1, O_RDONLY); gpio_out_1_fd = gpio_fd_open(gpio_out_1, O_RDONLY); gpio_in_2 = atoi(argv[3]); gpio_out_2 = atoi(argv[4]); gpio_export(gpio_in_2); gpio_export(gpio_out_2); gpio_set_dir(gpio_in_2, "in"); gpio_set_dir(gpio_out_2, "out"); gpio_set_edge(gpio_in_2, "falling"); gpio_in_2_fd = gpio_fd_open(gpio_in_2, O_RDONLY); gpio_out_2_fd = gpio_fd_open(gpio_out_2, O_RDONLY); gpio_in_3 = atoi(argv[5]); gpio_out_3 = atoi(argv[6]); gpio_export(gpio_in_3); gpio_export(gpio_out_3); gpio_set_dir(gpio_in_3, "in"); gpio_set_dir(gpio_out_3, "out"); gpio_set_edge(gpio_in_3, "falling"); gpio_in_3_fd = gpio_fd_open(gpio_in_3, O_RDONLY); gpio_out_3_fd = gpio_fd_open(gpio_out_3, O_RDONLY); gpio_in_4 = atoi(argv[7]); gpio_out_4 = atoi(argv[8]); gpio_export(gpio_in_4); gpio_export(gpio_out_4); gpio_set_dir(gpio_in_4, "in"); gpio_set_dir(gpio_out_4, "out"); gpio_set_edge(gpio_in_4, "falling"); gpio_in_4_fd = gpio_fd_open(gpio_in_4, O_RDONLY); gpio_out_4_fd = gpio_fd_open(gpio_out_4, O_RDONLY); timeout = POLL_TIMEOUT; int grid[N][N]; int i, j; for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { grid[i][j] = 0; } } point currentPoint = {.x = 0, .y = 0}; int currentDirection = NONE; int isErasing = 0; while (keepgoing) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpio_in_1_fd; fdset[1].events = POLLPRI; fdset[2].fd = gpio_in_2_fd; fdset[2].events = POLLPRI; fdset[3].fd = gpio_in_3_fd; fdset[3].events = POLLPRI; fdset[4].fd = gpio_in_4_fd; fdset[4].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (fdset[1].revents & POLLPRI) { lseek(fdset[1].fd, 0, SEEK_SET); len = read(fdset[1].fd, buf, MAX_BUF); unsigned int value; if (buf[0] == '0') { value = 0; } else if (buf[0] == '1') { value = 1; } gpio_set_value(gpio_out_1, value); currentDirection = UP; } else if (fdset[2].revents & POLLPRI) { lseek(fdset[2].fd, 0, SEEK_SET); len = read(fdset[2].fd, buf, MAX_BUF); unsigned int value; if (buf[0] == '0') { value = 0; } else if (buf[0] == '1') { value = 1; } gpio_set_value(gpio_out_2, value); currentDirection = DOWN; } else if (fdset[3].revents & POLLPRI) { lseek(fdset[3].fd, 0, SEEK_SET); len = read(fdset[3].fd, buf, MAX_BUF); unsigned int value; if (buf[0] == '0') { value = 0; } else if (buf[0] == '1') { value = 1; } gpio_set_value(gpio_out_3, value); currentDirection = RIGHT; } else if (fdset[4].revents & POLLPRI) { lseek(fdset[4].fd, 0, SEEK_SET); len = read(fdset[4].fd, buf, MAX_BUF); unsigned int value; if (buf[0] == '0') { value = 0; } else if (buf[0] == '1') { value = 1; } gpio_set_value(gpio_out_4, value); currentDirection = LEFT; } else { currentDirection = NONE; } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); //printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); if (buf[0]=='e') { isErasing = !isErasing; } } // Evaluate currentPoint = updatePoint(currentDirection, currentPoint); #ifdef DEBUG printf("After Move: %d, %d\n", currentPoint.x, currentPoint.y); #endif updateBoardBasedOnCurrentPoint(grid, currentPoint, isErasing); // Print the N by N matrix printGrid(grid, isErasing); } gpio_fd_close(gpio_in_1_fd); gpio_fd_close(gpio_out_1_fd); gpio_fd_close(gpio_in_2_fd); gpio_fd_close(gpio_out_2_fd); gpio_fd_close(gpio_in_3_fd); gpio_fd_close(gpio_out_3_fd); gpio_fd_close(gpio_in_4_fd); gpio_fd_close(gpio_out_4_fd); return 0; } point updatePoint(int dir, point pt) { point returnPoint; #ifdef DEBUG printf("Before Move: %d, %d\n", pt.x, pt.y); #endif switch (dir) { case UP: #ifdef DEBUG printf("Moving up\n"); #endif if (pt.x > 0) { pt.x--; } // returnPoint = pt; // break; return pt; case DOWN: #ifdef DEBUG printf("Moving down\n"); #endif if (pt.x < N-1) { pt.x++; } // returnPoint = pt; // break; return pt; case LEFT: #ifdef DEBUG printf("Moving left\n"); #endif if (pt.y > 0) { pt.y--; } // returnPoint = pt; // break; return pt; case RIGHT: #ifdef DEBUG printf("Moving right\n"); #endif if (pt.y < N-1) { pt.y++; } // returnPoint = pt; // break; return pt; case NONE: #ifdef DEBUG printf("Moving not at all\n"); #endif // returnPoint = pt; // break; return pt; default: // Should never get here. Ever. // printf("This message should never appear\n"); break; // return returnPoint; } }