Exemplo n.º 1
0
int		main(int ac, char **av)
{
	t_list	*list;
	t_list	*current;
	int		i;

	i = 1;
	list = NULL;
	init_term();
	if (ac > 1)
	{
		while (av[i] != 0)
		{
			ft_new_elem(&list, av[i]);
			i++;
		}
		current = list;
		print_list(list, current);
		while (42)
			read_button(&list, &current);
	}
	else
		ft_putendl_fd("Too few arguments", 2);
	return (0);
}
Exemplo n.º 2
0
/**
 *  @brief Keypad thread.
 *
 *    Continuously outputs to the 7-Segment displays
 *    and reads buttons from the keypad.
 *
 *    Because of the way the 7-Segment HW are wired to the keypad,
 *    each column scan is used to update the LEDs for one 7-Segment at a time.
 *    The only LEDs that are lit at any one time is the one selected by port A.
 *
 *    If the current LEDs are turned off, refreshed with a new value,
 *    and then the next column is selected, there's a ghosting effect.
 *    To resolve this, the command to turn off the LED is given
 *    BEFORE the column is changed.
 *
 *  @param Void.
 *  @return Void.
 */
void * keypad(void){
  int col;
  int timeout = scroll_delay;
  char str[6];
  
  while(alive){
    pthread_mutex_lock(&display_Mutex);
    if(display_flag == CHANGED){
      timeout = 1; //Trigger an update
    }

    if(--timeout == 0){
      update_display(); ///< display.c
      timeout = scroll_delay;
    }
    pthread_mutex_unlock(&display_Mutex);

    for(col=0;col<COLS;col++){
      write_to_port(C, 0);                  // LEDS off
      write_to_port(A, (BYTE) (01 << col)); // select column
      write_to_port(C, digits[col]);        // next LED pattern

      write(fd_RS232,"@00P1?\r",7);         // Read the column
      usleep(SLEEP);
      read(fd_RS232,str,6);
      
      pthread_mutex_lock(&button_Mutex);
      read_button(col,str[4]);
      pthread_mutex_unlock(&button_Mutex);
    }
  }
    pthread_exit(0);
}
Exemplo n.º 3
0
/*------------------------------------------------------------------------------
 * keypad thread - this function continiously outputs to LEDs and reads buttons
 *------------------------------------------------------------------------------
 */
void * keypad(){
  int col;
  int timeout = DELAY;
  char str[6];
  
  while(alive){
    if(display_flag == CHANGED){
      timeout = 1; //Trigger an update
    }
    if(--timeout == 0){
      update_display();
      timeout = DELAY;
    }
    for(col=0;col<COLSX;col++){
      write_to_port(C, 0);        // LEDS off
      write_to_port(A, (BYTE) (01 << col));     // select column
      write_to_port(C, digits[col]);  // next LED pattern  

      write(fd_RS232,"@00P1?\r",7);             // Read the column
      usleep(SLEEP);
      read(fd_RS232,str,6);
      
      read_button(col,str[4]);
    }
  }
  return 0;
}
/*int main()
{
	LED_INIT();
	uart_init(COM3, 115200);
	//uart_interrupt_init(COM3, &listener);
	tft_init(0, WHITE, BLACK, GREEN);
	button_init();
	ticks_init();
	motor_init();
	pneumatic_init();
	int count = 0;
	tft_init(0, WHITE, BLACK, GREEN);
	linear_ccd_init();
	adc_init();
	while(true) {
		if(count != get_ms_ticks()){
			count = get_ms_ticks();
			if(count % 40 == 0){
				linear_ccd_clear();
				linear_ccd_read();
				linear_ccd_prints();
				if(find_white_line() != 0) {
					tft_prints(2, 1, "%3d", find_white_line());
					tft_update();
				}

				if(find_white_line() < 50) {
					motor_control(1,0,100);

				}
				else {
					motor_control(1,0,0);

				}
				//tft_update();
			}
		}
	}
	return 0;
}*/
int main() {
    tft_init(0,WHITE,BLACK,RED);
    linear_ccd_init();
    ticks_init();
    GPIO_switch_init();
    pneumatic_init();
    button_init();
    LED_INIT();
    motor_init();
    uart_init(COM3, 115200);
    uart_interrupt_init(COM3,&listener);

    while(true) {
//listener(const uint8_t byte);
        tft_clear();
        tft_prints(2,4,"okay la") ;
        tft_update();

        while(!read_button(1))
        {   LED_ON(GPIOB, LED_M);
            motor_control(1,1,100);
        }
        /*if(read_GPIO_switch(GPIOA,GPIO_Pin_9)==1&&!read_button(1))
        { tft_clear();
        tft_prints(2,4,"grip");
        tft_update();*/
        /*pneumatic_control(GPIOB,GPIO_Pin_13,1);}
         else
        { tft_clear();
        	tft_prints(2,4,"let it go~");
        	tft_update();
        pneumatic_control(GPIOB,GPIO_Pin_13,0);}*/

        int time = get_ms_ticks();
        if(time%40==0)
        {
            linear_ccd_clear();
            linear_ccd_read();
            linear_ccd_prints();

        }
        //turnn(find_white_line());

    }
}
Exemplo n.º 5
0
/**********MAIN ROUTINE*************/
int main(int argc, char** argv) {
    //char i = '0';
    config_init();
    lcdInit();
    lcdWriteStrC("Counting test: ");
    while (1) {
        /*
        lcdSetPos(0,1);
        lcdWriteChar(i++);
        if(i > '9') i = '0';
        for(int j = 0; j < 10; j++) __delay_ms(50);
         */
        menu = read_button(); // wait for any changes in buttons
        menu_update(menu);
    }

    return (EXIT_SUCCESS);
}
Exemplo n.º 6
0
/* Get LEDs' lighting direction.
 *  1: Clockwise.
 * -1: Counterclockwise.
 */
static int8_t get_leds_direction(void) {
    static int8_t dir = 1;
    /* Current & previous state of User Button. */
    static uint8_t nstate = 0, pstate = 0; // 0: Not pressed, 1: pressed.

    nstate = read_button();
    /* Check the User Button is pressed or not. */
    if((nstate == 1) && (pstate == 0)) {
        /* If the User Button is pressed, reverse the direction. */
        dir *= -1;
        /* Avoid button ocsillation. */
        while(pause_short > 0) {
            delay();
        }
        pause_short = PAUSE_SHORT;
    }
    /* Save the current state by previous state. */
    pstate = nstate;

    return dir;
}
Exemplo n.º 7
0
static BYTE read_joystick(joystick_descriptor_t *joy)
{
    /* read buttons */
    BYTE joy_bits = read_button(joy, HID_FIRE, 16) |
                    read_button(joy, HID_ALT_FIRE, 16) |
                    read_button(joy, HID_LEFT, 4) |
                    read_button(joy, HID_RIGHT, 8) |
                    read_button(joy, HID_UP, 1) |
                    read_button(joy, HID_DOWN, 2) |
                    read_auto_button(joy, HID_AUTO_FIRE, 16) |
                    read_auto_button(joy, HID_AUTO_ALT_FIRE, 16);

    /* axis */
    joy_bits |= read_axis(joy, HID_X_AXIS, 4, 8) |
                read_axis(joy, HID_Y_AXIS, 1, 2);

    /* hat switch */
    joy_bits |= read_hat_switch(joy);

    return joy_bits;
}
Exemplo n.º 8
0
void hexbright::update() {
  unsigned long now;

#if (DEBUG==DEBUG_LOOP)
  unsigned long start_time=micros();
#endif


#ifdef STROBE  
  while (true) {
    do {
      now = micros();
    } while (next_strobe > now && // not ready for strobe
	     continue_time > now); // not ready for update

    if (next_strobe <= now) {
      if (now - next_strobe <26) {
	digitalWriteFast(DPIN_DRV_EN, HIGH);
	delayMicroseconds(strobe_duration);
	digitalWriteFast(DPIN_DRV_EN, LOW);
      }
      next_strobe += strobe_delay;
    }
    if(continue_time <= now) {
      if(strobe_delay>update_delay && // we strobe less than once every 8333 microseconds
	 next_strobe-continue_time < 4000) // and the next strobe is within 4000 microseconds (may occur before we return)
	continue;
      else
	break;
    }
  } // do nothing... (will short circuit once every 70 minutes (micros maxint))
#else
    do {
      now = micros();
    } while (continue_time > now); // not ready for update
#endif  

  // if we're in debug mode, let us know if our loops are too large
#if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT)
  static int i=0;
#if (DEBUG==DEBUG_LOOP)
  static unsigned long last_time = 0;
  if(!i) {
    Serial.print("Time used: ");
    Serial.print(start_time-last_time);
    Serial.println("/8333");
  }
  last_time = now;
#endif
  if(now-continue_time>5000 && !i) {
    // This may be caused by too much processing for our update_delay, or by too many print statements)
    //  If you're triggering this, your button and light will react more slowly, and some accelerometer
    //  data is being missed.
    Serial.println("WARNING: code is too slow");
  }
  if (!i)
    i=1000/update_delay; // display loop output every second
  else
    i--;
#endif
  
  
  // power saving modes described here: http://www.atmel.com/Images/2545s.pdf
  //run overheat protection, time display, track battery usage
  
#ifdef LED
  // regardless of desired led state, turn it off so we can read the button
  _led_off(RLED);
  delayMicroseconds(50); // let the light stabilize...
  read_button();
  // turn on (or off) the leds, if appropriate
  adjust_leds();
#ifdef PRINT_NUMBER
  update_number();
#endif
#else
  read_button();
#endif
  
  read_thermal_sensor(); // takes about .2 ms to execute (fairly long, relative to the other steps)
  read_charge_state();
  read_avr_voltage();

#ifdef ACCELEROMETER
  read_accelerometer();
  find_down();
#endif
  detect_overheating();
  detect_low_battery();
  apply_max_light_level();
  
  // change light levels as requested
  adjust_light();


  // advance time at the same rate as values are changed in the accelerometer.
  //  advance continue_time here, so the first run through short-circuits, 
  //  meaning we will read hardware immediately after power on.
  continue_time = continue_time+(1000*update_delay);
}
Exemplo n.º 9
0
void hexbright::update() {
  // advance time at the same rate as values are changed in the accelerometer.
  continue_time = continue_time+(1000*update_delay);
  
  unsigned long now;
  while (true) {
    do {
      now = micros();
    } while (next_strobe > now && // not ready for strobe
	     continue_time > now); // not ready for update
     
    if (next_strobe <= now) {
      if (now - next_strobe <26) {
	digitalWrite(DPIN_DRV_EN, HIGH);
	delayMicroseconds(strobe_duration);
	digitalWrite(DPIN_DRV_EN, LOW);
      }
      next_strobe += strobe_delay;
    }
    if(continue_time <= now) {
      if(strobe_delay>update_delay && // we strobe less than once every 8333 microseconds
	 next_strobe-continue_time < 4000) // and the next strobe is within 4000 microseconds (may occur before we return)
	continue;
      else
	break;
    }
  } // do nothing... (will short circuit once every 70 minutes (micros maxint))

  // if we're in debug mode, let us know if our loops are too large
#if (DEBUG!=DEBUG_OFF)
  static int i=0;
  static float avg_loop_time = 0;
  static float last_time = 0;
  avg_loop_time = (avg_loop_time*29 + continue_time-last_time)/30;
#if (DEBUG==DEBUG_LOOP)
  if(!i) {
    Serial.print("Average loop time: ");
    Serial.println(avg_loop_time/1000);
  }
#endif
  if(avg_loop_time/1000>update_delay+1 && !i) {
    // This may be caused by too much processing for our update_delay, or by too many print statements (each one takes a few ms)
    Serial.print("WARNING: loop time: ");
    Serial.println(avg_loop_time/1000);
  }
  if (!i)
    i=1000/update_delay; // display loop output every second
  else
    i--;
  last_time = continue_time;
#endif


  // power saving modes described here: http://www.atmel.com/Images/2545s.pdf
  //run overheat protection, time display, track battery usage

  #ifdef LED
  // regardless of desired led state, turn it off so we can read the button
  _led_off(RLED);
  delayMicroseconds(50); // let the light stabilize...
  read_button();
  // turn on (or off) the leds, if appropriate
  adjust_leds();
#ifdef PRINT_NUMBER
  update_number();
#endif
#else
  read_button();
#endif

  read_thermal_sensor(); // takes about .2 ms to execute (fairly long, relative to the other steps)
#ifdef ACCELEROMETER
  read_accelerometer();
  find_down();
#endif
  overheat_protection();

  // change light levels as requested
  adjust_light();
}
Exemplo n.º 10
0
int main(int argc, char **argv) {

	unsigned short display_buffer[8];
	int i;
	int blink=0,running;
	int i2c_fd;
	struct button_state buttons[2];

	time_t start_time,current_time,elapsed;
	int minutes,seconds,old_seconds=0;
	int total_time=0;

	/* Init i2c */
	i2c_fd=init_i2c("/dev/i2c-1");
	if (i2c_fd < 0) {
		fprintf(stderr,"Error opening device!\n");
		return -1;
	}

	/* Init display */
        if (init_display(i2c_fd,HT16K33_ADDRESS0,10)) {
		fprintf(stderr,"Error opening display\n");
		return -1;
        }

	/* Init buttons */
	if (init_button(17,&buttons[0])<0) {
		fprintf(stderr,"Error opening buttons\n");
		return -1;
	}

	if (init_button(27,&buttons[1])<0) {
		fprintf(stderr,"Error opening buttons\n");
		return -1;
	}


/*
   --0A-
  |     |
  5F    1B       : = 1
  |     |
   -G6--
  |     |
  4E    2C
  |     |
   -3D--        . = 7DP


  8  8  :  8  8
  0  1  2  3  4  Column

 so to set segment G of the far left element, then 16 bit display_buffer

    display_buffer[0]=1<<6; // 0x40

 To put 0 in the far right display

	display_buffer[4]=0x003f;

*/

	running=0;
	minutes=0;
	seconds=0;

	while(1) {

		/* clear display */
		for(i=0;i<8;i++) display_buffer[i]=0x00;

		if (running) {
			current_time=time(NULL);
			elapsed=total_time+(current_time-start_time);
			minutes=elapsed/60;
			seconds=elapsed%60;
		}

		/* minutes */
		display_buffer[0]=digits[(minutes/ 10)];
		display_buffer[1]=digits[(minutes% 10)];

		/* seconds */
		display_buffer[3]=digits[(seconds / 10)];
		display_buffer[4]=digits[(seconds % 10)];

		if (seconds!=old_seconds) blink=0;
		old_seconds=seconds;

		blink++;
		if (blink>5) {
			display_buffer[2]|=0x02;
		}
		if (blink>10) {
			blink=0;
		}

		update_display(i2c_fd,HT16K33_ADDRESS0,display_buffer);

		read_button(&buttons[0]);
		read_button(&buttons[1]);

		/* start */
		if (buttons[0].pressed) {
			if (running==0) {
				printf("Starting Timer!\n");
				start_time=time(NULL);
				running=1;
			}
			else {
				printf("Stopping Timer!\n");
				total_time=elapsed;
				running=0;
			}
			buttons[0].pressed=0;


		}

		/* reset */
		if (buttons[1].pressed) {
			printf("Resetting timer!\n");
			running=0;
			minutes=0;
			seconds=0;
			total_time=0;
			buttons[1].pressed=0;

		}

		usleep(100000);
	}

	return 0;

}
Exemplo n.º 11
0
int main( int argc, const char** argv ) {

	// Initialisation (prend au moins une seconde à cause du pwm)
	init_all();

	// Variables;
	int isButtonFree = 1;
	int buttonPreviouslyPushed = 0;
	int mode = 0;
	int sliderValue = 0;
	int posInTab = 0;
	int posWhenReadingTab = 0;
	char* tab = malloc(SIZE_EEPROM);
	char* readingTab = malloc(SIZE_EEPROM);
	int isReadingTabInitalized = 0;

	// Affiche le mode 1 (mode = 0)
	setLedMode(mode);

    while (1) {
		// On lit la valeur du bouton
		isButtonFree = read_button();

		// On regarde si le bouton est appuyé, s'il est appuyé on met buttonPreviouslyPushed à 1
		if(!isButtonFree) {
			buttonPreviouslyPushed = 1;
		}

		// Si on a relaché le bouton (buttonPreviouslyPushed mais le bouton n'est plus activé maintenant)
		if(buttonPreviouslyPushed && isButtonFree){
			// On remet le boolean à 0
			buttonPreviouslyPushed = 0;
			// Si on etait en mode numero 3 (mode = 2), on enregistre dans l'eeprom le tableau
			if(mode == 2) {
				write_eeprom(tab, posInTab, 0);
			}
			// On change le mode
			mode++;
			mode %= 4;
			setLedMode(mode);
		}

		// Si on est sur le mode 1
		if(mode == 0) {
			sliderValue = read_slider();
			set_pwm_value(1000000+sliderValue);
		}

		// Si on est sur le mode 3
		if(mode == 2) {
			sliderValue = read_slider();
			set_pwm_value(1000000+sliderValue);
			// notre sliderValue est entre 0 et 1 000 000, on la convertie en angle
			tab[posInTab] = convertToAngle(sliderValue);
			posInTab++;
			tab[posInTab] = 127;
			// Si on arrive au maximum on change de mode
			if(posInTab >= SIZE_EEPROM - 1 ) {
				buttonPreviouslyPushed = 1;
			}
			// Attente de 50ms pour le mode 3 uniquement
	        usleep(50000);
		}

		// Si on est sur le mode 4
		if(mode == 3) {
			// On lit l'eeprom une seule fois
			if(!isReadingTabInitalized) {
				printf("Lecture de l'eeprom ... ");
	 			read_eeprom(readingTab, posInTab);
				printf("end\n");
				isReadingTabInitalized = 1;
			}
			// On dit au servo de se déplacer à l'angle
			cmd_servo_hard(readingTab[posWhenReadingTab]);

			// On parcourt le tableau, si on atteint le max ou que on trouve 127 on repart à 0
			posWhenReadingTab++;
			if(posWhenReadingTab >= posInTab || readingTab[posWhenReadingTab] == 127) {
				posWhenReadingTab = 0;
				printf("On recommence !\n");
				// Le message de ce printf s'accumule et tous les messages ne s'affichent que au changement de mode
			}
			// Attente de 50ms pour le mode 4 uniquement
	        usleep(50000);
		}

		// Temps de pause dans la boucle infinie pour alleger le processeur
		// Evite egalement les rebonds du bouton
        usleep(1000);
    }
}