Exemple #1
0
void init_game(_TIMER *level_timer, _TIMER *decay_timer, _PIN *coin_op, Display *score_display, Display *high_display) {
    game.hit_flag = 0;
    game.coin_flag = 0;
    game.score = 0;
    game.high = 0;

    pin_digitalIn(coin_op);
    game.coin_op = coin_op;

    game.level_timer = level_timer;
    game.level_ticks = 0;
    game.level_limit = MAX_LEVEL;
    game.level = 0;

    game.lose_flag = 0;
    game.lose_ticks = 0;

    timer_setPeriod(game.level_timer, 0.2);
    timer_start(game.level_timer);

    game.decay_timer = decay_timer;
    game.decay_ticks = 0;
    game.decay_limit = MAX_DECAY;
    game.life = MAX_LIFE;
    timer_setPeriod(game.decay_timer, 0.002);
    timer_start(game.decay_timer);

    game.score_display = score_display;
    game.high_display = high_display;
    write_display(game.high_display, game.high, 0);
    write_display(game.score_display, game.score, 0);

    game.state = over_game;
    game.last_state = (STATE_HANDLER_T)NULL;
}
Exemple #2
0
void over_game(void) {
    if (game.state != game.last_state) {  // if we are entering the state, do intitialization stuff
        game.last_state = game.state;

        game.hit_flag = 0;
        game.level_ticks = 0;

        game.level_limit = MAX_LEVEL;
        game.level = 0;

        game.decay_ticks = 0;
        game.decay_limit = MAX_DECAY;

        timer_lower(game.level_timer);
        timer_lower(game.decay_timer);
        update_bar_pix(0, &start_color, &end_color);

        if (game.score > game.high) {
            game.high = game.score;
        }

        write_display(game.high_display, game.high, 0);
        write_display(game.score_display, game.score, 0);
        blink_display(game.score_display, 1);
    }

    //run state logic
    if (game.lose_flag) {
        if (timer_flag(game.level_timer)) {
            timer_lower(game.level_timer);
            game.lose_ticks++;
            if (game.lose_ticks == MAX_LOSE) {
                trigger_audio(LOSE);
                game.lose_flag = 0;
            }
        }
    }

    if (pin_read(game.coin_op)) {
        game.coin_flag = 1;
        trigger_audio(START);
    }

    if (game.hit_flag && game.coin_flag) {
        game.state = rest_game;
        game.hit_flag = 0;
        game.coin_flag = 0;
    }

    if (game.state != game.last_state) {  // if we are leaving the state, do clean up stuff
        game.level = 1;
        game.life = MAX_LIFE;
        game.score = 0;
        game.lose_flag = 0;
        game.lose_ticks = 0;
        blink_display(game.score_display, 0);
        write_display(game.score_display, game.score, 0);
    }
}
uint8_t alloc_lcd_addr(uint8_t addr, int i, char* str_to_write){
    if (addr == (0x80 + 16)){
        addr = 0x80 + 40;
        write_display(59, addr, (str_to_write[i]) | 0x80);
        addr++;
        return addr;
    }
    else{
        write_display(59, addr, (str_to_write[i]) | 0x80);
        addr++;
        return addr;
    }
}
void clear_display(i2c_port){
    uint8_t addr = 0x80;
    int i;
    for (i=0; i <33; i++){
        if (addr == (0x80 + 16)){
            addr = 0x80 + 40;
            write_display(i2c_port, addr, emptychar);
            addr++;
        }
        else{
            write_display(i2c_port, addr, emptychar);
            addr++;
        }
    }
}
Exemple #5
0
void rest_game(void) {
    if (game.state != game.last_state) {  // if we are entering the state, do intitialization stuff
        game.last_state = game.state;
        game.level_ticks = 0;
        game.hit_flag = 0;
    }

    //run state logic
    life_percent = (float)game.life/MAX_LIFE-((float)game.decay_ticks/game.decay_limit)/100.0;
    update_bar_pix(life_percent, &start_color, &end_color);

    if (timer_flag(game.decay_timer)) {
        timer_lower(game.decay_timer);
        game.decay_ticks++;
        if (game.decay_ticks == game.decay_limit) {
            game.life--;
            game.decay_ticks = 0;
        }
    }

    if (timer_flag(game.level_timer)) {
        timer_lower(game.level_timer);
        game.level_ticks++;
        if (game.level_ticks == game.level_limit) {
            game.state = level_game;
        }
    }

    if (game.hit_flag) {
        game.score += 25;
        game.hit_flag = 0;
        game.life += 10;
        if (game.life > MAX_LIFE) {
            game.life = MAX_LIFE;
        }
        write_display(game.score_display, game.score, 0);
    }

    // Check for state transitions
    if (!game.life) {
        game.state = over_game;
        game.lose_flag = 1;
    }
}
Exemple #6
0
/* slide_display
*	Writes a string to the display while sliding it useful for strings larger than the display 
*
*	string-> String to display
*	slide-> number of times to slide
*/
void slide_display(unsigned char* string, unsigned char slide)
{
	unsigned char len;
	int spaces;
	len = strlength(string);
	spaces = (len + 4);
	for(char j = 0; j < slide; ++j)
	{
		unsigned char display[4] = {' ', ' ', ' ', ' '};
		for(int i = 0;i < spaces ;++i)
		{
			display[0] = display[1];
			display[1] = display[2];
			display[2] = display[3];
			if(i < len)
				display[3] = string[i];
			else
				display[3] = ' ';
			write_display(display, 0);
			_delay_ms(500);
		}
	}
}
Exemple #7
0
int main(int argc, char *argv[])
{
	exit_if_false(argc == 2,"exactly one input argument required");

	timer_start();

	//-------------------------------------------------------------------//
	
	// counters
	int i, n;

	// file paths
	char *input_file_path, *geometry_file_path, *case_file_path, *data_file_path, *data_numbered_file_path, *display_file_path, *display_numbered_file_path;
	exit_if_false(geometry_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating geometry file path");
	exit_if_false(case_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating case file path");
	exit_if_false(data_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data file path");
	exit_if_false(data_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data numbered file path");
	exit_if_false(display_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display file path");
	exit_if_false(display_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display numbered file path");

	// print string
	char *print;
	exit_if_false(print = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating the print string");

	// mesh structures
	int n_nodes, n_faces, n_elements, n_boundaries_old = 0, n_boundaries, n_terms;
	struct NODE *node;
	struct FACE *face;
	struct ELEMENT *element;
	struct BOUNDARY *boundary_old = NULL, *boundary;
	struct TERM *term;
	EXPRESSION *initial = NULL;
	
	// solution vectors
	int n_u_old = 0, n_u;
	double *u_old = NULL, *u;

	// files
	FILE *input_file, *case_file, *data_file, *geometry_file, *display_file;

	//-------------------------------------------------------------------//

	// opening the input file
	print_info("opening the input file %s",argv[1]);
	input_file_path = argv[1];
	input_file = fopen(input_file_path,"r");
	exit_if_false(input_file != NULL,"opening %s",input_file_path);

	// reading the case file path
	print_info("reading the case file path");
	exit_if_false(fetch_value(input_file,"case_file_path",'s',case_file_path) == FETCH_SUCCESS,"reading case_file_path from %s",input_file_path);
	print_continue(case_file_path);

	// reading the number of variables, variable names and orders
	print_info("reading the variables");
	int n_variables_old = 0, n_variables;
	exit_if_false(fetch_value(input_file,"number_of_variables",'i',&n_variables) == FETCH_SUCCESS,"reading number_of_variables from %s",input_file_path);
	int *variable_order_old = NULL, *variable_order = (int *)malloc(n_variables * sizeof(int));
	exit_if_false(variable_order != NULL,"allocating orders");
	exit_if_false(fetch_vector(input_file, "variable_order", 'i', n_variables, variable_order) == FETCH_SUCCESS,"reading variable_order from %s",input_file_path);
	char **variable_name = allocate_character_matrix(NULL,n_variables,MAX_STRING_LENGTH);
	exit_if_false(variable_name != NULL,"allocating variable names");
	warn_if_false(fetch_vector(input_file,"variable_name",'s',n_variables,variable_name) == FETCH_SUCCESS,"reading variable_name from %s",input_file_path);
	for(i = 0; i < n_variables; i ++) print_continue("%s order %i",variable_name[i],variable_order[i]);

	// reading the number of inner and outer iterations to perform
	print_info("reading the numbers of iterations");
	int outer_iteration = 0, inner_iteration;
	int n_outer_iterations, n_inner_iterations, data_n_outer_iterations, display_n_outer_iterations;
	exit_if_false(fetch_value(input_file,"number_of_inner_iterations",'i',&n_inner_iterations) == FETCH_SUCCESS,"reading number_of_inner_iterations from %s",input_file_path);
	exit_if_false(fetch_value(input_file,"number_of_outer_iterations",'i',&n_outer_iterations) == FETCH_SUCCESS,"reading number_of_outer_iterations from %s",input_file_path);
	print_continue("%i outer of %i inner iterations to be done",n_outer_iterations,n_inner_iterations);

	// read existing case and data
	case_file = fopen(case_file_path,"r");
	if(case_file != NULL)
	{
		print_info("reading existing case file %s",case_file_path);
		read_case(case_file, &n_variables_old, &variable_order_old, &n_nodes, &node, &n_faces, &face, &n_elements, &element, &n_boundaries_old, &boundary_old);
		fclose(case_file);
		n = 0; for(i = 0; i < n_variables; i ++) n += n_elements*ORDER_TO_N_BASIS(variable_order[i]);

		if(fetch_value(input_file,"initial_data_file_path",'s',data_file_path) == FETCH_SUCCESS)
		{
			print_info("reading existing data file %s",data_file_path);
			exit_if_false(data_file = fopen(data_file_path,"r"),"opening %s",data_file_path);
			read_data(data_file, &n_u_old, &u_old, &outer_iteration);
			fclose(data_file);
			exit_if_false(n_u_old == n,"case and initial data does not match");
		}
	}

	// construct new case
	else
	{
		print_info("reading the geometry file path");
		exit_if_false(fetch_value(input_file,"geometry_file_path",'s',geometry_file_path) == FETCH_SUCCESS,"reading geometry_file_path from %s",input_file_path);
		print_continue(geometry_file_path);

		print_info("reading the geometry file %s",geometry_file_path);
		exit_if_false(geometry_file = fopen(geometry_file_path,"r"),"opening %s",geometry_file_path);
		read_geometry(geometry_file, &n_nodes, &node, &n_faces, &face, &n_elements, &element);
		fclose(geometry_file);
		print_continue("%i nodes, %i faces and %i elements",n_nodes,n_faces,n_elements);

		print_info("generating additional connectivity and geometry");
		process_geometry(n_nodes, node, n_faces, face, n_elements, element);
	}

	// read the data file path and output frequency
	print_info("reading the data file path and output frequency");
	exit_if_false(fetch_value(input_file,"data_file_path",'s',data_file_path) == FETCH_SUCCESS,"reading data_file_path from %s",input_file_path);
	if(fetch_value(input_file,"data_number_of_outer_iterations",'i',&data_n_outer_iterations) != FETCH_SUCCESS)
		data_n_outer_iterations = n_outer_iterations + outer_iteration;
	print_continue("data to be written to %s every %i outer iterations",data_file_path,data_n_outer_iterations);

	// read boundaries
	print_info("reading boundaries");
	boundaries_input(input_file, n_faces, face, &n_boundaries, &boundary);
	print_continue("%i boundaries",n_boundaries);

	// read terms
	print_info("reading PDE terms");
	terms_input(input_file, &n_terms, &term);
	print_continue("%i terms",n_terms);

	// update unknown indices and values
	print_info("updating the numbering of the degrees of freedom");
	update_element_unknowns(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element, n_u_old, &n_u, u_old, &u);
	print_continue("%i degrees of freedom",n_u);

	// update face boundaries
	print_info("updating the face boundary associations");
	update_face_boundaries(n_variables, n_faces, face, n_boundaries, boundary);

	// update integration
	print_info("updating integration");
	i = update_face_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face);
	if(i) print_continue("updated %i face quadratures",i);
	i = update_element_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element);
	if(i) print_continue("updated %i element quadratures",i);

	// update numerics
	print_info("updating numerics");
	i = update_face_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face, n_boundaries_old, boundary_old);
	if(i) print_continue("updated %i face interpolations",i);
	i = update_element_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element);
	if(i) print_continue("updated %i element interpolations",i);

	// write case file
	print_info("writing case file %s",case_file_path);
	exit_if_false(case_file = fopen(case_file_path,"w"),"opening %s",case_file_path);
	write_case(case_file, n_variables, variable_order, n_nodes, node, n_faces, face, n_elements, element, n_boundaries, boundary);
	fclose(case_file);

	// read the display file path and output frequency
	print_info("reading the display file path and output frequency");
	if(
			fetch_value(input_file,"display_file_path",'s',display_file_path) == FETCH_SUCCESS &&
			fetch_value(input_file,"display_number_of_outer_iterations",'i',&display_n_outer_iterations) == FETCH_SUCCESS
	  )
		print_continue("display to be written to %s every %i outer iterations",display_file_path,display_n_outer_iterations);
	else
	{
		display_n_outer_iterations = 0;
		warn_if_false(0,"display files will not be written");
	}

	// initialise
	if(initial_input(input_file, n_variables, &initial))
	{
		print_info("initialising the degrees of freedom");
		initialise_values(n_variables, variable_order, n_elements, element, initial, u);
	}

	//-------------------------------------------------------------------//
	
	// allocate and initialise the system
	print_info("allocating and initialising the system");
	SPARSE system = NULL;
	initialise_system(n_variables, variable_order, n_elements, element, n_u, &system);
	double *residual, *max_residual, *du, *max_du;
	exit_if_false(residual = (double *)malloc(n_u * sizeof(double)),"allocating the residuals");
	exit_if_false(max_residual = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum residuals");
	exit_if_false(du = (double *)malloc(n_u * sizeof(double)),"allocating du");
	exit_if_false(max_du = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum changes");
	exit_if_false(u_old = (double *)realloc(u_old, n_u * sizeof(double)),"re-allocating u_old");

	timer_reset();

	// iterate
	print_info("iterating");
	n_outer_iterations += outer_iteration;
	for(; outer_iteration < n_outer_iterations; outer_iteration ++)
	{
		print_output("iteration %i", outer_iteration);

		for(i = 0; i < n_u; i ++) u_old[i] = u[i];

		for(inner_iteration = 0; inner_iteration < n_inner_iterations; inner_iteration ++)
		{
			calculate_system(n_variables, variable_order, n_faces, face, n_elements, element, n_terms, term, n_u, u_old, u, system, residual);

			exit_if_false(sparse_solve(system, du, residual) == SPARSE_SUCCESS,"solving system");
			for(i = 0; i < n_u; i ++) u[i] -= du[i];

			calculate_maximum_changes_and_residuals(n_variables, variable_order, n_elements, element, du, max_du, residual, max_residual);
			for(i = 0; i < n_variables; i ++) sprintf(&print[26*i],"%.6e|%.6e ",max_du[i],max_residual[i]);
			print_continue("%s",print);
		}

		slope_limit(n_variables, variable_order, n_nodes, node, n_elements, element, n_boundaries, boundary, u);

		if(data_n_outer_iterations && outer_iteration % data_n_outer_iterations == 0)
		{
			generate_numbered_file_path(data_numbered_file_path, data_file_path, outer_iteration);
			print_info("writing data to %s",data_numbered_file_path);
			exit_if_false(data_file = fopen(data_numbered_file_path,"w"),"opening %s",data_numbered_file_path);
			write_data(data_file, n_u, u, outer_iteration);
			fclose(data_file);
		}

		if(display_n_outer_iterations && outer_iteration % display_n_outer_iterations == 0)
		{
			generate_numbered_file_path(display_numbered_file_path, display_file_path, outer_iteration);
			print_info("writing display to %s",data_numbered_file_path);
			exit_if_false(display_file = fopen(display_numbered_file_path,"w"),"opening %s",display_numbered_file_path);
			write_display(display_file, n_variables, variable_name, variable_order, n_elements, element, n_u, u);
			fclose(display_file);
		}

		timer_print();
	}

	//-------------------------------------------------------------------//
	
	print_info("freeing all memory");

	fclose(input_file);

	free(geometry_file_path);
	free(case_file_path);
	free(data_file_path);
	free(data_numbered_file_path);
	free(display_file_path);
	free(display_numbered_file_path);

	free(print);

	destroy_nodes(n_nodes,node);
	destroy_faces(n_faces,face,n_variables);
	destroy_elements(n_elements,element,n_variables);
	destroy_boundaries(n_boundaries_old,boundary_old);
	destroy_boundaries(n_boundaries,boundary);
	destroy_terms(n_terms,term);
	destroy_initial(n_variables,initial);

	free(variable_order_old);
	free(variable_order);
	destroy_matrix((void *)variable_name);

	free(u_old);
	free(u);

	sparse_destroy(system);
	free(residual);
	free(max_residual);
	free(du);
	free(max_du);

	return 0;
}