int main(void)
{	
	init();
	BOOL motors_stopped = FALSE;
	
	send_welcome();
	        
    while (1)
    {	
		if (motors_stopped)
		{
			led_on ( STOP_LED );
			led_off ( GO_LED );
		} else
		{
			led_on( GO_LED );
			led_off( STOP_LED );
		}
		
		read_buttons();
		if (stop_button_pressed)
		{
			motors_stopped = TRUE;
			stop_motors();
		}
		if (prog_button_pressed)
		{
			motors_stopped = FALSE; 
			resume_motors();
		}

		encoder_timeslice();		
		limit_switch_timeslice();
		motor_timeslice_10ms();		
		serial_timeslice();		// handle incoming messages (if avail)

		watchdog_reset();
		//delay(one_second/10);
    }
    return (0);
}
示例#2
0
/*
** This is the process spawned by pthread_create, into which we pass StateInfo.
** Using pthread_self, we relocate the appropriate instance and set up some
** variables for the game, log some stuff to log, send a welcome message, etc.
** We then enter the main game loop, waiting for messages from the client
** which we then handle with game_step. Once the game ends, we remove the
** appropriate instance, close the socket and print to log before finally
** exiting the thread.
*/
void *run_instance(void *param)
{
    /*
    ** Because we can only pass one thing into this function, we pass the
    ** thing with the greatest amount of useful info, the Instances struct. 
    ** We then find  again the target Instance by using the thread_id, 
    ** which we can find out using pthread_self() passed to get_instance().
    */
    StateInfo *state_info = (StateInfo*)param;
    Instance *instance = get_instance(state_info, pthread_self());
    char *correct = instance->code;

    int sock_id = instance->s;
    char *ip4 = instance->ip4;

    char log_buf[LOG_MSG_LEN];

    send_welcome(sock_id);

    // Add our own sentinel for printing purposes.
    char msg[CODE_LENGTH+1];
    
    // Main game loop. We don't have to worry about the len that recv returns
    // because we know we will get 4 chars and these are all that concern us.
    while (recv(sock_id, &msg, CODE_LENGTH, 0))
    {
        msg[CODE_LENGTH] = '\0';
        if (game_step(msg, correct, instance) == 0)
            break;
    }

    remove_instance(state_info, instance->t);
    close(sock_id);
    
    sprintf(log_buf, "(%s)(%d) Client disconnected.\n", ip4, sock_id);
    write_log(log_buf);

    pthread_exit(NULL);
}