// Set digital output-pin and return state, uint8_t writeDigitalOutput(uint8_t id, uint8_t onOff) { uint8_t state; state = onOff; pfio_digital_write(id, state); uint8_t binaryRead; binaryRead = pfio_read_output(); if( (binaryRead & (1 << (id))) == (1 << (id)) ) state = 1; else state = 0; return state; }
void RobotApp(int argc, char *argv[]) { pfio_init(); while (1) { pfio_digital_write(LIGHT1, RED); pfio_digital_write(LIGHT2, RED); _delay_ms(SWITCH_TIME); pfio_digital_write(LIGHT1, RED); pfio_digital_write(LIGHT2, GREEN); _delay_ms(CYCLE_TIME); pfio_digital_write(LIGHT1, RED); pfio_digital_write(LIGHT2, RED); _delay_ms(SWITCH_TIME); pfio_digital_write(LIGHT1, GREEN); pfio_digital_write(LIGHT2, RED); _delay_ms(CYCLE_TIME); } pfio_deinit(); printf ("Ready.\n"); }
/* Set the state of an LED */ void piface_power_state(int led, int state) { pfio_digital_write(pins[led], state); }
char pfio_init(void) { if ((spi = malloc(sizeof(Spi))) == NULL) return -1; // initialise the spi with some values // create the path string char path[MAXPATH]; if (snprintf(path, MAXPATH, "/dev/spidev%d.%d", SPI_BUS, SPI_DEVICE) >= MAXPATH) { fprintf(stderr, "ERROR: Bus and/or device number is invalid."); return -1; } // try to open the device if ((spi->fd = open(path, O_RDWR, 0)) < 0) { fprintf(stderr, "ERROR: Can not open device"); return -1; } // try to control the device char temp; if (ioctl(spi->fd, SPI_IOC_RD_MODE, &temp) < 0) { fprintf(stderr, "ERROR: Can not get spi mode"); return -1; } spi->mode = temp; // try to get the bits per word if (ioctl(spi->fd, SPI_IOC_RD_BITS_PER_WORD, &temp) < 0) { fprintf(stderr, "ERROR: Can not get bits per word"); return -1; } spi->bitsperword = temp; // try to get the max speed int maxspeed; if (ioctl(spi->fd, SPI_IOC_RD_MAX_SPEED_HZ, &maxspeed) < 0) { fprintf(stderr, "ERROR: Can not get max speed hz"); return -1; } spi->maxspeed = maxspeed; // set up some ports spi_write(IOCON, 8); // enable hardware addressing spi_write(IODIRA, 0); // set port A as outputs spi_write(IODIRB, 0xFF); // set port B as inputs spi_write(GPIOA, 0xFF); // set port A on //spi_write(GPIOB, 0xFF); // set port B on spi_write(GPPUA, 0xFF); // set port A pullups on spi_write(GPPUB, 0xFF); // set port B pullups on // initialise all outputs to 0 int i; for (i = 1; i <= 8; i++) pfio_digital_write(i, 0); return 0; }
void open_door_stop() { pfio_digital_write(PIN_GLED,0); pfio_digital_write(PIN_DOOR,0); }
void open_door_start() { pfio_digital_write(PIN_DOOR,1); pfio_digital_write(PIN_GLED,1); }
void beep(int ms) { pfio_digital_write(PIN_BUZZER,1); usleep(ms*1000); pfio_digital_write(PIN_BUZZER,0); }
int main(void) { if (pfio_init() < 0) perror("Cannot start PFIO"); long int code = 0, last = 0; int count = 0; bool last_d0 = 0, last_d1 = 0; read_config(); if(signal(SIGHUP, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGHUP."); if(signal(SIGUSR1, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGUSR1."); if(signal(SIGINT, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGINT."); beep(1000); #if DEBUG printf("Ready!\n"); #endif while (1) { int i = pfio_read_input(); if(i == 0xFF) usleep(1); struct timeval tv; gettimeofday(&tv, NULL); long int now = tv.tv_usec + tv.tv_sec*1000; if((now - last > TIMEOUT)||(now<last)) { #if DEBUG printf(" - "); #endif count = 0; code = 0; } bool d0 = (i & MASK_D0)==0; bool d1 = (i & MASK_D1)==0; bool button = (i & MASK_EXIT)==0; bool lockcmd = (i & MASK_LOCK)==0; if(lockcmd || locked) { #if DEBUG printf("*** LOCK ***\n"); #endif while(((pfio_read_input() & MASK_LOCK)==0)||locked) { pfio_digital_write(PIN_GLED,1); usleep(200000); pfio_digital_write(PIN_GLED,0); usleep(200000); } #if DEBUG printf("*** UNLOCK ***\n"); #endif } if(button) { #if DEBUG printf("Exit button pressed!!!\n"); #endif open_door_start(); beep(100); while((pfio_read_input() & MASK_EXIT)==0) sleep(1); #if DEBUG printf("Exit button released!!!\n"); #endif sleep(DOOR_DELAY); open_door_stop(); } if(d0 && !last_d0) { #if DEBUG printf("0"); #endif code = code << 1; count++; } if(d1 && !last_d1) { #if DEBUG printf("1"); #endif code = (code << 1) + 1; count++; } if(count==BITSCOUNT) { #if DEBUG printf("[%li]\n",code); #endif struct card * c = cards; bool allowed = 0; while(c) { if(c->code == code) { allowed = 1; break; } c = c->next; }; if(allowed) { // Accepte #if DEBUG printf("Access authorized!\n"); #endif open_door(0); #if DEBUG printf("OK\n"); #endif } else { // Refuse #if DEBUG printf("Access denied!\n"); #endif int n; for(n=0; n<3; n++) { usleep(200000); beep(100); } #if DEBUG printf("OK\n"); #endif } code = 0; count = 0; } #if DEBUG fflush(stdout); #endif last_d0 = d0; last_d1 = d1; last = now; } pfio_deinit(); return 0; }