示例#1
0
/****************************************************************
 * 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;
}
示例#2
0
文件: main.c 项目: larmorgs/ECE497
//signal handler that breaks program loop and cleans up
void signal_handler(int signo){
	if (signo == SIGINT) {
		printf("\n^C pressed, unexporting gpios and exiting..\n");
		
		gpio_fd_close(led0_fd);
		gpio_fd_close(button_fd);
		unexport_gpio(LED0);
		unexport_gpio(BUTTON);

		fflush(stdout);

		exit(0);
	}
}
示例#3
0
void ADCreader::run()
{
    running = true;

    while (running) {

        //ask for a read from the comms register
        writeReg(fd,0x08);

        int x = readReg(fd);

        //check for the data ready bit to be low - /DRDY is glitchy on the board! use this instead
        if (!(x & 0x80)){

        // tell the AD7705 to read the data register (16 bits)
        writeReg(fd,0x38);
        // read the data register by performing two 8 bit reads
        int value = readData(fd)-0x8000;


        *pIn = value;
        if (pIn == (&samples[MAX_SAMPLES-1]))
          pIn = samples;
        else
          pIn++;
        }
    }

    close(fd);
    gpio_fd_close(sysfs_fd);
}
示例#4
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;
}
示例#5
0
void ADCreader::run()
{
  running = true;


  while (running) {
    
    // let's wait for data for max one second
    int ret = gpio_poll(sysfs_fd,1000);
    if (ret<1) {
      fprintf(stderr,"Poll error %d\n",ret);
    }
	  
    // tell the AD7705 to read the data register (16 bits)
    writeReg(fd,0x38);

    // read the data register by performing two 8 bit reads
    int value = readData(fd)-0x8000;

    *pIn = value;
    if (pIn == (&samples[MAX_SAMPLES-1])) 
      pIn = samples;
    else
      pIn++;
    
  }
  close(fd);
  gpio_fd_close(sysfs_fd);
}
示例#6
0
文件: gpio.c 项目: kulias/practice
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* 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;
}
// starts here
int main(int argc, char** argv) {
	// usage notice
	if (argc != 5){
		printf("Usage: %s <left> <down> <up> <right>\n\n", argv[0]);
		exit(-1);
	}
		
	init();

	struct pollfd fdset[4];
	unsigned int gpio;
	int rc, timeout;
	int gpio_fd[4];
	char buf[MAX_BUF];
	int len;
	int nfds = 5;

	int i;
	for(i = 0; i < 4; i++) {
		//printf("%d", atoi(argv[i+1]));
		gpio_fd[i] = set_gpio(atoi(argv[i + 1]));
	}

	timeout = 3 * 1000;

	while(1) {
		memset((void*)fdset, 0, sizeof(fdset));
		int i;
		for(i = 0; i < 4; i++) {
			fdset[i].fd = gpio_fd[i];
			fdset[i].events = POLLPRI;
		}
		rc = poll(fdset, nfds, timeout);  
		//printf("rc is : %d", rc);
		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(fdset[0].fd, buf, MAX_BUF);
			//printf("left button pushed\n");
			move_left();
		}
		if(fdset[1].revents & POLLPRI) {
			lseek(fdset[1].fd, 0, SEEK_SET);  
			read(fdset[1].fd, buf, MAX_BUF);
			//printf("down button pushed\n");
			move_down();
		}
		if(fdset[2].revents & POLLPRI) {
			lseek(fdset[2].fd, 0, SEEK_SET);  
			read(fdset[2].fd, buf, MAX_BUF);
			//printf("up button pushed\n");
			move_up();
		}
		if(fdset[3].revents & POLLPRI) {
			lseek(fdset[3].fd, 0, SEEK_SET);  
			read(fdset[3].fd, buf, MAX_BUF);
			//printf("right button pushed\n");
			move_right();
		}
		//if (initialized == 0) clear();
		initialized = 1;
		fflush(stdout);
	}
	for(i = 0; i < 4; i++)
		gpio_fd_close(gpio_fd[i]);
	return 0;
}
示例#10
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;
}
示例#11
0
void ADCreader::run()
{
        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);

        // tell the AD7705 that the next write will be the setup register
        writeReg(fd,0x10);

        // 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);

        // 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);

        // we read data in an endless loop and display it
        // this needs to run in a thread ideally





         // let's wait for data for max one second
          ret = gpio_poll(sysfs_fd,1000);
          if (ret<1) {
            fprintf(stderr,"Poll error %d\n",ret);
          }

          // tell the AD7705 to read the data register (16 bits)
          writeReg(fd,0x38);
          // read the data register by performing two 8 bit reads
          
          //acquire and store the value of resistance (presumably at clean air)
          float init  = readData(fd);

          float vdif = ((init/32768)-1)*2.5; //translate the code into voltage to find Ain1(+)-Ain1(-)

          float Ain = vdif + 0.964; //add Ain1(-) to find the actual voltage 

          float Rair = (4300*5/Ain)-4300; // reverse engineer the voltage divider to find the resistance
          fprintf(stderr,"init = %f \t vdif= %f \t Ain=%f \t Rair = %f \n \n    ", init, vdif, Ain, Rair);



        running = 1;

        while (running) {

          // let's wait for data for max one second
          ret = gpio_poll(sysfs_fd,1000);
          if (ret<1) {
            fprintf(stderr,"Poll error %d\n",ret);
          }

          // tell the AD7705 to read the data register (16 bits)
          writeReg(fd,0x38);
          // read the data register by performing two 8 bit reads
          float value = readData(fd);

          float vdifcurrent=((value/32768)-1)*2.5;

          float Aincurrent  = vdifcurrent + 0.964;

          float Rcurrent = (4300*5/Aincurrent)-4300;
          float Rratio = Rcurrent/Rair; //divide resistance found by resistance in fresh air

	  buffer[bindex-1] = Rratio; //store value in ring buffer
	  
	  /* The following code has been used for debugging purgposes and is now commented out
	  
	  float test = buffer[bindex-1];
	  
	  fprintf(stderr,"data = %f \t vdiff=%f  \t Ain=%f  \t res ratio = %f  \r ", value, vdifcurrent, Aincurrent, test);
	  */
	  
	  bindex = bindex++; // update buffer index
	  
	  if(bindex == 20000000){
	  	bindex = 0;
	  }
	  
	  



        }

        close(fd);
        gpio_fd_close(sysfs_fd);


}
示例#12
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;
}
示例#13
0
static void transfer(int fd){
	int ret;

		uint16_t tx1[] = {
				0x0000,0x0000
		};

		uint16_t rx1[ARRAY_SIZE(tx1)] = {0, };

		struct spi_ioc_transfer tr = {
			.tx_buf = (unsigned long)tx1,
			.rx_buf = (unsigned long)rx1,
			.len = 4,//ARRAY_SIZE(tx),
			.delay_usecs = delay,
			.speed_hz = speed,
			.bits_per_word = bits,
		};

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");

		for (ret = 0; ret < ARRAY_SIZE(tx1); ret++) {
			if (!(ret % 14))
				puts("");
			printf("%.4X ", rx1[ret]);
		}


/*
		uint16_t tx2[] = {
						0x0000
				};

				uint16_t rx2[ARRAY_SIZE(tx2)] = {0, };


						tr.tx_buf = (unsigned long)tx2,
						tr.rx_buf = (unsigned long)rx2,
						tr.len = 4,//ARRAY_SIZE(tx),
						tr.delay_usecs = delay,
						tr.speed_hz = speed,
						tr.bits_per_word = bits,


				ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
				if (ret < 1)
					pabort("can't send spi message");

				for (ret = 0; ret < ARRAY_SIZE(tx2); ret++) {
					if (!(ret % 14))
						puts("");
					printf("%.2X ", rx2[ret]);
				}
*/
		puts("");
}


  
/****************************************************************
 * Our main function with a little more than "Hello world!" only
 ****************************************************************/
 int main(void)
{ 

	gpio_export(GPIO_SPI_CS_Ch1);
	gpio_export(GPIO_SPI_CS_Ch2);
	gpio_export(GPIO_SPI_CS_Ch3);
	gpio_export(GPIO_SPI_INT_Ch1);
	gpio_export(GPIO_SPI_INT_Ch2);
	gpio_export(GPIO_SPI_INT_Ch3);
	gpio_export(GPIO_SPI_CS_Disp);
	gpio_export(GPIO_SPI_Reset_Ch1);
	gpio_export(GPIO_SPI_Reset_Ch2);
	gpio_export(GPIO_SPI_Reset_Ch3);
	gpio_export(GPIO_SPI_CS_Col);
	printf("gpio_export SUCCESS!\n");

	gpio_set_direction(GPIO_SPI_CS_Ch1, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_CS_Ch2, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_CS_Ch3, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_INT_Ch1, INPUT_PIN);
	gpio_set_direction(GPIO_SPI_INT_Ch2, INPUT_PIN);
	gpio_set_direction(GPIO_SPI_INT_Ch3, INPUT_PIN);
	gpio_set_direction(GPIO_SPI_CS_Disp, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_Reset_Ch1, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_Reset_Ch2, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_Reset_Ch3, OUTPUT_PIN);
	gpio_set_direction(GPIO_SPI_CS_Col, OUTPUT_PIN);
	printf("gpio_set_direction SUCCESS!\n");

	gpio_set_edge(GPIO_SPI_INT_Ch1, "rising");
	gpio_set_edge(GPIO_SPI_INT_Ch2, "rising");
	gpio_set_edge(GPIO_SPI_INT_Ch3, "rising");
	printf("gpio_set_edge SUCCESS!\n");


	int fd_GPIO_SPI_CS_Ch1;
	int fd_GPIO_SPI_CS_Ch2;
	int fd_GPIO_SPI_CS_Ch3;
	int fd_GPIO_SPI_INT_Ch1;
	int fd_GPIO_SPI_INT_Ch2;
	int fd_GPIO_SPI_INT_Ch3;
	int fd_GPIO_SPI_CS_Disp;
	int fd_GPIO_SPI_Reset_Ch1;
	int fd_GPIO_SPI_Reset_Ch2;
	int fd_GPIO_SPI_Reset_Ch3;
	int fd_GPIO_SPI_CS_Col;

	fd_GPIO_SPI_CS_Ch1 = gpio_fd_open_R_W(GPIO_SPI_CS_Ch1);
	fd_GPIO_SPI_CS_Ch2 = gpio_fd_open_R_W(GPIO_SPI_CS_Ch2);
	fd_GPIO_SPI_CS_Ch3 = gpio_fd_open_R_W(GPIO_SPI_CS_Ch3);

	fd_GPIO_SPI_INT_Ch1 = gpio_fd_open_R_O(GPIO_SPI_INT_Ch1);
	fd_GPIO_SPI_INT_Ch2 = gpio_fd_open_R_O(GPIO_SPI_INT_Ch2);
	fd_GPIO_SPI_INT_Ch3 = gpio_fd_open_R_O(GPIO_SPI_INT_Ch3);

	fd_GPIO_SPI_CS_Disp = gpio_fd_open_R_W(GPIO_SPI_CS_Disp);
	fd_GPIO_SPI_Reset_Ch1 = gpio_fd_open_R_W(GPIO_SPI_Reset_Ch1);
	fd_GPIO_SPI_Reset_Ch2 = gpio_fd_open_R_W(GPIO_SPI_Reset_Ch2);
	fd_GPIO_SPI_Reset_Ch3 = gpio_fd_open_R_W(GPIO_SPI_Reset_Ch3);
	fd_GPIO_SPI_CS_Col = gpio_fd_open_R_W(GPIO_SPI_CS_Col);

	gpio_set_value(GPIO_SPI_CS_Ch1, HIGHT , fd_GPIO_SPI_CS_Ch1 );
	gpio_set_value(GPIO_SPI_CS_Ch2, HIGHT , fd_GPIO_SPI_CS_Ch2 );
	gpio_set_value(GPIO_SPI_CS_Ch3, HIGHT , fd_GPIO_SPI_CS_Ch3 );
	gpio_set_value(GPIO_SPI_CS_Disp, HIGHT , fd_GPIO_SPI_CS_Disp );
	gpio_set_value(GPIO_SPI_Reset_Ch1, HIGHT , fd_GPIO_SPI_Reset_Ch1 );
	gpio_set_value(GPIO_SPI_Reset_Ch2, HIGHT , fd_GPIO_SPI_Reset_Ch2 );
	gpio_set_value(GPIO_SPI_Reset_Ch3, HIGHT , fd_GPIO_SPI_Reset_Ch3 );
	gpio_set_value(GPIO_SPI_CS_Col, HIGHT , fd_GPIO_SPI_CS_Col );

	//gpio_fd_close(fd_GPIO_SPI_CS_Ch1);
	gpio_fd_close(fd_GPIO_SPI_CS_Ch2);
	gpio_fd_close(fd_GPIO_SPI_CS_Ch3);
	//gpio_fd_close(fd_GPIO_SPI_INT_Ch1);
	gpio_fd_close(fd_GPIO_SPI_INT_Ch2);
	gpio_fd_close(fd_GPIO_SPI_INT_Ch3);
	gpio_fd_close(fd_GPIO_SPI_CS_Disp);
	gpio_fd_close(fd_GPIO_SPI_Reset_Ch1);
	gpio_fd_close(fd_GPIO_SPI_Reset_Ch2);
	gpio_fd_close(fd_GPIO_SPI_Reset_Ch3);
	gpio_fd_close(fd_GPIO_SPI_CS_Col);

	printf("gpio_set_value SUCCESS!\n");
	printf("sleep 10 second\n");
	sleep(10);
	int check;
	check= gpio_get_value_interrupt(fd_GPIO_SPI_INT_Ch1, 0);

	if(check==-1){
				printf("interrupt no happen\n");
			}else{

				printf("interrupt happen %d\n", check);
			}
	printf("sleep 10 second\n");
	sleep(10);
	check= gpio_get_value_interrupt(fd_GPIO_SPI_INT_Ch1, 100);

		if(check==-1){
			printf("interrupt no happen \n");
		}else{

			printf("interrupt happen %d\n", check);
		}

	// ret;
	//int fd;

	//fd=spi_device_open("/dev/spidev1.0");
	//set_spi_settings(fd, SPI_MODE_1, 16 , 16000000, 0);
	//static void transfer(int fd)
	//{

	   int ret = 0;
		int fd;

		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");

		printf("spi mode: %d\n", mode);
		printf("bits per word: %d\n", bits);
		printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

		gpio_set_value(GPIO_SPI_CS_Ch1, LOW , fd_GPIO_SPI_CS_Ch1 );



		transfer(fd);

		gpio_set_value(GPIO_SPI_CS_Ch1, HIGHT , fd_GPIO_SPI_CS_Ch1 );

		close(fd);
		gpio_fd_close(fd_GPIO_SPI_CS_Ch1);


	 return EXIT_SUCCESS;
}
示例#14
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[2];
	int nfds = 2;
	int gpio_fdIn, gpio_fdOut, timeout, rc;
	char *buf[MAX_BUF];
	unsigned int gpioIn, gpioOut;
	int len;
	int count = 0;		// Counts the number of interupts

	if (argc < 3) {
		printf("Usage: %s <gpio-input-pin> <gpio-output-pin>\n\n", argv[0]);
		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);

	// Set up input
	gpioIn = atoi(argv[1]);
	gpio_export(gpioIn);
	gpio_set_dir(gpioIn, "in");
	gpio_set_edge(gpioIn, "both");
	gpio_fdIn = gpio_fd_open(gpioIn, O_RDONLY);

	// Set up output
	gpioOut=atoi(argv[2]);
	gpio_export(gpioOut);
	gpio_set_dir(gpioOut, "out");
	gpio_fdOut = gpio_fd_open(gpioOut, O_WRONLY);

	timeout = POLL_TIMEOUT;
 
	while (keepgoing) {
		memset((void*)fdset, 0, sizeof(fdset));

		fdset[0].fd = STDIN_FILENO;
		fdset[0].events = POLLIN;
      
		fdset[1].fd = gpio_fdIn;
		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) {
			lseek(fdset[1].fd, 0, SEEK_SET);  // Read from the start of the file
			len = read(fdset[1].fd, buf, MAX_BUF);
			++count;
#ifdef DEBUG
			printf("\npoll() GPIO %d interrupt occurred %d times, value=%c, len=%d\n",
				 gpioIn, count, buf[0], len);
#endif
			len = write(gpio_fdOut, buf, len);
#ifdef DEBUG
			printf("Writing %d chars to GPIO %d\n", len, gpioOut);
#endif
		}

		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);
	}

	printf("%d interupts processed\n", count);
	gpio_fd_close(gpio_fdIn);
	gpio_fd_close(gpio_fdOut);
	return 0;
}
示例#16
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);
	}

	// Set the signal callback for Ctrl-C
	signal(SIGINT, signal_handler);

	gpio = atoi(argv[1]);

	gpio_export(gpio);
	gpio_set_dir(gpio, "in");
	gpio_set_edge(gpio, "falling");  // Can be rising, falling or both
	gpio_fd = gpio_fd_open(gpio, 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_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) {
			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\n",
				 gpio, buf[0], len);
				toggle = !toggle;
				printf("%d set to %i",atoi(argv[2]), toggle);
				gpio_set_value(atoi(argv[2]), toggle);
		}

		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;
}
示例#17
0
文件: HW2_take2.c 项目: cattnb/ECE497
/****************************************************************
 * 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, char**envp)
{
	// Initialize bitmap to be displayed
	__u16 board[]=
		{0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};

        struct pollfd fdset[5];
        int nfds = 5;
        int gpio_fd1,gpio_fd2,gpio_fd3,gpio_fd4, timeout, rc;
        char buf[MAX_BUF];
        unsigned int gpio_in1,gpio_in2,gpio_in3,gpio_in4;
        unsigned int gpio_out1,gpio_out2,gpio_out3,gpio_out4;
	int x,y,wait,size;

	int res, i2cbus, address, file;
        char filename[20];
        int force = 0;
        int demo = 0;
	size = 8;

	// Find the i2c Bus
	i2cbus = lookup_i2c_bus("1");
        printf("i2cbus = %d\n", i2cbus);
        if (i2cbus < 0)
                help();
	
	// Getthe address of the led array
        address = parse_i2c_address("0x70");
        printf("address = 0x%2x\n", address);
        if (address < 0)
                help();

	// Get the file to operate the i2c bus
        file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
        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)


	wait = 0;
	x = 0;
	y = -1;

        int len;
	
	// Set GPIO pin numbers
        gpio_in1 = 30;
        gpio_in2 = 31;
        gpio_in3 = 48;
        gpio_in4 = 60;

	// Open each of the GPIO ports
        gpio_export(gpio_in1);
        gpio_export(gpio_in2);
        gpio_export(gpio_in3);
        gpio_export(gpio_in4);

	// Make all the GPIO ports inputs
        gpio_set_dir(gpio_in1, "in");
        gpio_set_dir(gpio_in2, "in");
        gpio_set_dir(gpio_in3, "in");
        gpio_set_dir(gpio_in4, "in");

        // Only trigger on the rising edge of the button
	gpio_set_edge(gpio_in1, "rising");
        gpio_set_edge(gpio_in2, "rising");
        gpio_set_edge(gpio_in3, "rising");
        gpio_set_edge(gpio_in4, "rising");

	// Open the button
        gpio_fd1 = gpio_fd_open(gpio_in1, O_RDONLY);
        gpio_fd2 = gpio_fd_open(gpio_in2, O_RDONLY);
        gpio_fd3 = gpio_fd_open(gpio_in3, O_RDONLY);
        gpio_fd4 = gpio_fd_open(gpio_in4, O_RDONLY);

        timeout = POLL_TIMEOUT;
		

	// Running Loop
        while(keepgoing){
                memset((void*)fdset, 0, sizeof(fdset));

		// Setup interrups for each button
                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;
		write_block(file, board);
		// Poll the buttons
                rc = poll(fdset, nfds, timeout);

		// If top button is pressed
                if (fdset[1].revents){
                        lseek(fdset[1].fd, 0, SEEK_SET);
                        len = read(fdset[1].fd, buf, MAX_BUF);

			// Check to see if boundry is hit
			if(y > 0 && wait > 2){	
				y = y - 1;
	            	}
				wait++;
                      
                }
		// If left button is pressed
     	        if (fdset[2].revents){
		        lseek(fdset[2].fd, 0, SEEK_SET);
                        len = read(fdset[2].fd, buf, MAX_BUF);

			// Check to see if boundry is hit
			if(x > 0 && wait > 2){
				x = x - 1;
			}else
				wait++;
			

                }
		// If right button pressed
                if (fdset[3].revents){
                        lseek(fdset[3].fd, 0, SEEK_SET);
                        len = read(fdset[3].fd, buf, MAX_BUF);
			// Check to see if boundry is hit
			if(x < size && wait > 2){
				x = x + 1;
			}else
				wait++;
			

                }
		// If bottom button is pressed
                if (fdset[4].revents){
                        lseek(fdset[4].fd, 0, SEEK_SET);
                        len = read(fdset[4].fd, buf, MAX_BUF);
	
			// Check to see if boundry is hit
			if(y < size && wait > 2){
				y = y + 1;
			}else
				wait++;
                }

		//y Corresponds to row in the bit map. Then x is how far in the bit has to be turned on, so I used a shift
		board[y] = (1 << x) | board[y];
                fflush(stdout);

        }
	
	// Close GPIO ports
        gpio_fd_close(gpio_fd1);
        gpio_fd_close(gpio_fd2);
        gpio_fd_close(gpio_fd3);
        gpio_fd_close(gpio_fd4);

	// Close ncursers

        return 0;
}
示例#19
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;
}
示例#20
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;
}
示例#21
0
文件: afBlink.cpp 项目: alanswx/afLib
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;
}
示例#22
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;
}
示例#23
0
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;
}
示例#24
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;
}
示例#25
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;
	}
}
示例#26
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;
}
示例#27
0
文件: main.c 项目: jault3/se3910_lab3
/****************************************************************
* Main
****************************************************************/
int main(int argc, char **argv)
{
	/* This variable is a file pointer to the file interface for the GPIO device driver
	  which controls the input pin. */
	uint32_t gpio_fd; // This is the file ID for the input file.

	uint8_t gpioSwitchP1 = 48; //Input Switch for Player 1 (GPIO1_16)
	uint8_t gpioSwitchP2 = 49; //Input Switch for Player 2 (GPIO0_26)

	uint8_t gpioLEDP1= 44; //Output LED light for Player 1 (GPIO1_12)
	uint8_t gpioLEDP2= 26; //Output LED light for Player 2 (GPIO0_26)

	char ioPort[56];   // This is the IO port that is to be referenced.

	if (argc < 3) {
		exit(-1);
	}

	// Set the signal callback for Ctrl-C
	signal(SIGINT, signal_handler);

	// Convert the input into the appropriate parameters.
	char player1[32];
	char player2[32];
	strcpy(player1, argv[1]);
	strcpy(player2, argv[2]);

	printf("Welcome to the game of Anticipation, %s and %s!", player1, player2);

	// Setup the input ports
	(void)gpio_export(gpioSwitchP1);
	(void)gpio_set_dir(gpioSwitchP1, 0);
	(void)gpio_set_edge(gpioSwitchP1, GPIO_BOTH_EDGES);  // Both indicates that an interrupt will fire on both a rising and falling edge.
	gpio_fd = gpio_fd_open(gpioSwitchP1);

	(void)gpio_export(gpioSwitchP2);
	(void)gpio_set_dir(gpioSwitchP2, 0);
	(void)gpio_set_edge(gpioSwitchP2, GPIO_BOTH_EDGES);  // Both indicates that an interrupt will fire on both a rising and falling edge.
	gpio_fd = gpio_fd_open(gpioSwitchP2);

	// Setup the output ports
	(void)gpio_export(gpioLEDP1);
	(void)gpio_set_dir(gpioLEDP1, 1);
	(void)gpio_fd_open(gpioLEDP1);

	(void)gpio_export(gpioLEDP2);
	(void)gpio_set_dir(gpioLEDP2, 1);
	(void)gpio_fd_open(gpioLEDP2);


	//GAME STUFF HERE



	// Start the process pin routine which will handle processing the pin.
	processPin(gpio_fd, gpioLEDP1, gpioSwitchP1);

	//***********************************************************************
	// cleanup the executing system
	// Close the pins
	gpio_fd_close(gpio_fd);

	// Unexport the pins.
	gpio_unexport(gpioSwitchP1);
	gpio_unexport(gpioSwitchP2);
	gpio_unexport(gpioLEDP1);
	gpio_unexport(gpioLEDP2);

	printf("Peace out girl scout\n");
	return 0;
}
示例#28
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;
}
示例#29
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;
}