Beispiel #1
0
static enum switch_state get_switch_state(void)
{
	struct stopwatch sw;
	int sampled_value;
	uint8_t rec_sw;
	static enum switch_state saved_state = not_probed;

	if (saved_state != not_probed)
		return saved_state;

	rec_sw = get_rec_sw_gpio_pin();
	sampled_value = read_gpio(rec_sw) ^ !REC_POL;

	if (!sampled_value) {
		saved_state = no_req;
		display_pattern(WWR_NORMAL_BOOT);
		return saved_state;
	}

	display_pattern(WWR_RECOVERY_PUSHED);
	printk(BIOS_INFO, "recovery button pressed\n");

	stopwatch_init_msecs_expire(&sw, WIPEOUT_MODE_DELAY_MS);

	do {
		sampled_value = read_gpio(rec_sw) ^ !REC_POL;
		if (!sampled_value)
			break;
	} while (!stopwatch_expired(&sw));

	if (sampled_value) {
		display_pattern(WWR_WIPEOUT_REQUEST);
		printk(BIOS_INFO, "wipeout requested, checking recovery\n");
		stopwatch_init_msecs_expire(&sw, RECOVERY_MODE_EXTRA_DELAY_MS);
		do {
			sampled_value = read_gpio(rec_sw) ^ !REC_POL;
			if (!sampled_value)
				break;
		} while (!stopwatch_expired(&sw));

		if (sampled_value) {
			saved_state = recovery_req;
			display_pattern(WWR_RECOVERY_REQUEST);
			printk(BIOS_INFO, "recovery requested\n");
		} else {
			saved_state = wipeout_req;
		}
	} else {
		saved_state = no_req;
		display_pattern(WWR_NORMAL_BOOT);
	}

	return saved_state;
}
Beispiel #2
0
/********************************************************************************
 * cleanup(): Turns off LEDs, closes open file descriptors and unexports the
 *    GPIO pins.
 *******************************************************************************/
int cleanup() {
   int fd = 0;
   char b[8] = {0x00, 0xFE, 0x92, 0x92, 0x92, 0x6C, 0x00, 0x00};
   char y[8] = {0x00, 0x01, 0x02, 0x04, 0x7C, 0x04, 0x02, 0x01};
   char e[8] = {0x00, 0xFE, 0x92, 0x92, 0x82, 0x82, 0x00, 0x00};
   char no_lights[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
   
   /* Say bye */
   display_pattern(b);
   sleep(2);
   display_pattern(y);
   sleep(2);
   display_pattern(e);
   sleep(2);
   
   /* Turn the LEDs off before closing and unexporting */
   display_pattern(no_lights);

   /* Close file descriptors */
   close(ultrasonic_fd);
   close(spi_fd);

   /* Unexport GPIOs */
   if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) >= 0) { // Unexport GPIO 43
      write(fd, "43", 2);
      close(fd);
   }
   else {
      perror("ERROR: Could not unexport GPIO 43.\n");
      return -1;
   }

   if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) >= 0) { // Unexport GPIO 42
      write(fd, "42", 2);
      close(fd);
   }
   else {
      perror("ERROR: Could not unexport GPIO 42.\n");
      return -1;
   }

   if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) >= 0) { // Unexport GPIO 55
      write(fd, "55", 2);
      close(fd);
   }
   else {
      perror("ERROR: Could not unexport GPIO 55.\n");
      return -1;
   }

   return 0;
}
Beispiel #3
0
int main(int argc, char **argv)
{
    st_manager holder;

    /*options */
    char c;
    int disp_f = 0;     /* shows patterns in coloured text */
    int num_f = 0;      /* displays the number of occurences */
    int ind_f = 0;      /* lists all the indices[from 0] of occurence of pattern */

    if (argc < 3) {
        print_usage(argv[0]);
        return EXIT_FAILURE;
    }

    FILE *inp_file = fopen(argv[1], "r");
    char *pattern = argv[2];

    /* parse arguments */
    while ((c = getopt(argc, argv, "dni")) != -1)
        switch(c)
        {
            case 'd':
                disp_f = 1;
                break;
            case 'n':
                num_f = 1;
                break;
            case 'i':
                ind_f = 1;
                break;
            default:
                break;

        }

    /*
    FILE *inp_file = fopen(argv[1], "r");
    char *pattern = argv[2];
    */

    holder = pat_search(inp_file, pattern);
    
    if (num_f) { printf("occurences: %d \n", holder.num_occur); }

    if (disp_f) {
        //rewind file as it has already been read
        fseek(inp_file, 0, SEEK_SET);
        display_pattern(inp_file, pattern, holder);
    }

    if (ind_f) {
        list_indices(holder);
    }

    fclose(inp_file);

    return EXIT_SUCCESS;
}
Beispiel #4
0
void select_note(void)
{
    // Light up buttons according to data
    display_pattern();

    // Go through each of the 16 upper buttons
    for (uint8_t i = 0; i < 16; i++) {
        if (button_pressed(i)) {
            enter_note_init(i);
            break;
        }
    }

    leds_7seg_set(4, current_channel + 1);

    if (button_pressed(BTN_BACK)) {
        for (uint8_t i = 0; i < 16; i++)
            button_led_off(i);
        state = STATE_TOPLEVEL;
    }
}
Beispiel #5
0
/********************************************************************************
 * main(): Calls initalizing functions and contains the infinite loop for
 *    distance detection.
 *******************************************************************************/
int main(int argc, char **argv) {
   uint64_t total_time = 0;
   double distance = 0;
   double threshold = 10.0; // This is in cm
   /* Ways to display LEDs */
   char eight_rows[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
   char six_rows[8] = {0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E};
   char four_rows[8] = {0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C};
   char two_rows[8] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
   char x[8] = {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81};
   char no_lights[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

   /* Handle a signal */
   signal(SIGINT, sigint_handler);
   printf("Signal handler set up.\n");

   /* Check for command arguments */
   check_args(argc, argv, &threshold);
   printf("Arguments have been checked.\n");

   /* Setup LED Matrix pins */
   if(init_gpio() != 0) {
      exit(1);
   }
   printf("GPIO initialized.\n");

   /* Setup SPI */
   if(init_spi() != 0) {
      exit(1);
   }
   printf("SPI initialized.\n");
   
   /* Setup ultrasonic sensor */
   if(init_ultrasonic() != 0) {
      exit(1);
   }
   printf("Ultrasonic initialized.\n");

   /* Begin a never ending loop that will read from the ulstrasonic sensor
    * and display lights on the LED matrix until CTRL-C is hit */
   while(1){
      if (read(ultrasonic_fd, &total_time, sizeof(total_time)) < 0) {
         perror("ERROR: Could not read from ultrasonic device.\n");
      }
      
      /* Pretty sure the ultrasonic data sheet lied because using the formulas didn't
       * get me anywhere. So, after some trial and error, I determined using a denominator
       * in th range of 22750 will give me a decent distance number. */
      distance = (double)total_time / 22750;

      printf("Distance is %f\n", distance);
      
      /* Distance scenarios */
      if (distance > (4 * threshold)) { // Object is too far away to register
         display_pattern(eight_rows);
      }
      else if (distance <= (4 * threshold) && distance > (3 * threshold)) { // Object is far but still registered
         display_pattern(six_rows);
      }
      else if (distance <= (3 * threshold) && distance > (2 * threshold)) { // Object is halfway
         display_pattern(four_rows);
      }
      else if (distance <= (2 * threshold) && distance > threshold) { // Object is getting too close 
         display_pattern(two_rows);
      }
      else { // Object has crashed =(
         display_pattern(x);
         usleep(500000);
         display_pattern(no_lights);
         usleep(500000);
      }
   }
}