Пример #1
0
int do_http_post(int sd, char *req, int rlen)
{
	int BUFSIZE = 1024;
	char buf[BUFSIZE];
	int len = 0, n;
        char *p;

	if (is_cmd_led(req)) {
                n = toggle_leds();
		len = generate_http_header(buf, "txt", 1);
                p = buf + len;
                *p++ = n?'1':'0';
                *p = 0;
		len++;
		xil_printf("http POST: ledstatus: %x\r\n", n);
	} else if (is_cmd_switch(req)) {
                unsigned s = get_switch_state();
                int n_switches = 8;

                xil_printf("http POST: switch state: %x\r\n", s);
		len = generate_http_header(buf, "txt", n_switches);
                p = buf + len;
                for (n = 0; n < n_switches; n++) {
                        *p++ = '0' + (s & 0x1);
                        s >>= 1;
                }
                *p = 0;

                len += n_switches;
	} else {
int do_http_post(struct tcp_pcb *pcb, char *req, int rlen)
{
	int BUFSIZE = 1024;
	char buf[BUFSIZE];
	int len, n;
        char *p;

	if (is_cmd_led(req)) {
                n = toggle_leds();
		len = generate_http_header(buf, "js", 1);
                p = buf + len;
                *p++ = n?'1':'0';
                *p = 0;
		len++;
		xil_printf("http POST: ledstatus: %x\r\n", n);
	} else if (is_cmd_switch(req)) {
                unsigned s = get_switch_state();
                int n_switches = 8;
		//char *json_response = "{\"status\":\"10101011\"}";

                xil_printf("http POST: switch state: %x\r\n", s);
		len = generate_http_header(buf, "js", n_switches);
		//len = generate_http_header(buf, "js", strlen(json_response));
                p = buf + len;
#if 1
                for (n = 0; n < n_switches; n++, p++) {
                        *p = '0' + (s & 0x1);
                        s >>= 1;
                }
                *p = 0;

                len += n_switches;
#else
		strcpy(p, json_response);
		len += strlen(json_response);
#endif
	}

	if (tcp_write(pcb, buf, len, 1) != ERR_OK) {
		xil_printf("error writing http POST response to socket\n\r");
		xil_printf("http header = %s\r\n", buf);
		return -1;
	}

	return 0;
}
Пример #3
0
int do_http_post(struct tcp_pcb* pcb, char* req, int rlen)
{
	int BUFSIZE = 1024;
	unsigned char buf[BUFSIZE];
	int len, n;
	char* p;

	if (is_cmd_led(req)) {
		n = toggle_leds();
		len = generate_http_header((char*)buf, "js", 1);
		p = (char*)buf + len;
		*p++ = n ? '1':'0';
		*p = 0;
		len++;
		xil_printf("http POST: ledstatus: %x\r\n", n);
	} else if (is_cmd_switch(req)) {
		unsigned s = get_switch_state();
		int n_switches = 4;

		xil_printf("http POST: switch state: %x\r\n", s);
		len = generate_http_header((char*)buf, "js", n_switches);
		p = (char*)buf + len;
		for (n=0; n<n_switches; n++, p++) {
			*p = '0' + (s & 0x1);
			s >>= 1;
		}
		*p = 0;

		len += n_switches;
	}

	if (tcp_write(pcb, buf, len, 1) != ERR_OK) {
		xil_printf("error writing http POST response to socket\r\n");
		xil_printf("http header = %s\r\n", buf);
		return -1;
	}

	return 0;
}
Пример #4
0
int get_wipeout_mode_switch(void)
{
	return get_switch_state() == wipeout_req;
}
Пример #5
0
int get_recovery_mode_switch(void)
{
	return get_switch_state() == recovery_req;
}
Пример #6
0
/*
 * Main function to run one tick of the game
 */
int run_game()
{
	cli();

	/* Movement phase */
	if(game_state == 1)
	{
		/* Move the reticule based on readings from the rotary encoder */
		int16_t newRX = (playersX[current_player] + (RETICULE_DISTANCE * ml_cos(position))/100);
		int16_t newRY = (playersY[current_player] + (RETICULE_DISTANCE * ml_sin(position))/100);
		
		rectangle reticuleOld = {reticuleX - reticule_SPR->width / 2, reticuleX + reticule_SPR->width / 2 - 1, reticuleY - reticule_SPR->height / 2, reticuleY + reticule_SPR->height / 2 - 1};
		draw_background(level_map, SILVER, reticuleOld, HEIGHT_NO_UI, WIDTH);

		//fill_rectangle(reticuleOld, BLACK);
		if(newRX >= 0 && newRX < WIDTH && newRY >= 0 && newRY < HEIGHT)
			fill_sprite(reticule_SPR, newRX, newRY, HEIGHT_NO_UI, WIDTH);
	
		reticuleX = newRX;
		reticuleY = newRY;

		/* Read firing input */
		if (get_switch_long(_BV(SWC)))
		{
			game_state = 3;
			return 0;
		}		

		/* Read directional input.*/
		if (get_switch_rpt(_BV(SWE))) 
		{
			direction = 4;
			free_sprite(player_SPR);
			player_SPR = botright(current_player);
		}
		else if (get_switch_rpt(_BV(SWW)))
		{
			direction = -4;
			free_sprite(player_SPR);
			player_SPR = botleft(current_player);
		}
		else
		{
			direction = 0;
			return 0;
		}

		/* Move the player */
		int16_t newX = playersX[current_player] + direction;
		int16_t newY = ml_min(level_map[newX - PLAYER_WIDTH], level_map[newX + PLAYER_WIDTH - 1]) - PLAYER_HEIGHT;

		int8_t i;
		int8_t player_collision = 0;
		for(i = 0; i < players; i++)
		{
			if(players_HP[i] != 0 && i != current_player && newX >= playersX[i] - PLAYER_WIDTH && newX <= playersX[i] + PLAYER_WIDTH - 1)
			{
				player_collision = 1;
				break;
			}				
		}

		/* Cancel movement if trying to climb to high or off the sides of the screen */
		if(player_collision || playersY[current_player] - newY > MAX_CLIMB_HEIGHT || newX <= PLAYER_WIDTH || newX + PLAYER_WIDTH > WIDTH)
		{
			/* DEBUGGING prints: */
			//display_string_xy("Error\n", 10, 10);
			//ml_printf("Cannot move there... (%u,%u) to (%u,%u)", playersX[0], playersY[0], newX, newY);
			return 0;
		}
		
		rectangle playerOld = {playersX[current_player] - PLAYER_WIDTH, playersX[current_player] + PLAYER_WIDTH - 1, playersY[current_player] - PLAYER_HEIGHT, playersY[current_player] + PLAYER_HEIGHT - 1};

		fill_rectangle(playerOld, BLACK);
		fill_sprite(player_SPR, newX, newY, HEIGHT_NO_UI, WIDTH);
		
		playersX[current_player] = newX;
		playersY[current_player] = newY;
	}
	/* Projectile phase (missile in the air) */
	else if(game_state == 2)
	{
		/* Find the new position of the projectile */
		int16_t newX = projectileX + proVelX/1000;
		int16_t newY = projectileY + proVelY/1000;
		
		rectangle projectileOld = {projectileX, projectileX, projectileY, projectileY};
		fill_rectangle(projectileOld, BLACK);

		/* End turn if projectile goes off the sides of the level */
		if(newX < 0 || newX > WIDTH)
		{
			start_turn();
			return 0;
		}
		
		/* If the new position is in the ground, EXPLODE!
		 * (Also, if off the bottom of the level) */
		if(newY >= level_map[newX] || newY > HEIGHT_NO_UI)
		{
			int i;
			for(i = -EXPLOSION_RADIUS; i <= EXPLOSION_RADIUS; i++)
			{
				/* Make sure that terrain is on screen */
				if(newX + i >= 0 && newX + i < WIDTH)
				{
					uint16_t new_ground_level = newY + ml_sqrt(EXPLOSION_RADIUS * EXPLOSION_RADIUS - i * i);
					if(new_ground_level >= HEIGHT_NO_UI)
						level_map[newX + i] = HEIGHT_NO_UI - 1; //Clamp to bottom of level
					else if(new_ground_level > level_map[newX + i]) //Don't raise the ground level!
						level_map[newX + i] = new_ground_level; 
				}
			}

			draw_level(level_map, SILVER, newX - EXPLOSION_RADIUS, newX + EXPLOSION_RADIUS);

			/* Check players for damage + redraw them */
			for(i = 0; i < players; i++)
			{
				int16_t deltaX = playersX[i] - newX;
				int16_t deltaY = playersY[i] - newY;
				int16_t distance = ml_sqrt(deltaX * deltaX + deltaY * deltaY);
				if(distance < BLAST_RADIUS)
				{
					int16_t damage = ml_clamp(0, players_HP[i], MAX_EXPLOSION_DAMAGE - (distance * MAX_EXPLOSION_DAMAGE / BLAST_RADIUS));
					players_HP[i] -= damage;
				}

				rectangle playerOld = {playersX[i] - PLAYER_WIDTH, playersX[i] + PLAYER_WIDTH - 1, playersY[i] - PLAYER_HEIGHT, playersY[i] + PLAYER_HEIGHT - 1};
				fill_rectangle(playerOld, BLACK);

				/* If the player is still alive, redraw them */
				if(players_HP[i] != 0)
				{
					free_sprite(player_SPR);
					player_SPR = botleft(i);
					playersY[i] = ml_min(level_map[playersX[i] - PLAYER_WIDTH], level_map[playersX[i] + PLAYER_WIDTH - 1]) - PLAYER_HEIGHT;
					fill_sprite(player_SPR, playersX[i], playersY[i], HEIGHT_NO_UI, WIDTH);
				}
			}

			start_turn();
			return 0;
		}	

		rectangle projectileNew = {newX, newX, newY, newY};
		
		/* Continue turn if off top of level, but only draw if projectile onscreen */
		if(newY >= 0)
		{
			fill_rectangle(projectileNew, RED);
		}

		projectileX = newX;
		projectileY = newY;		

		/* Apply gravity and wind */
		proVelY += 500;
		proVelX += (wind_velocity - 10) * 50;
	}
	/* Charging phase */
	else if(game_state == 3)
	{
		/* Increase the launch speed while the fire button is held */
		if (get_switch_state(_BV(SWC)))
		{
			launch_speed++;
			rectangle bar = {POWER_BAR_START, POWER_BAR_START + launch_speed, 231, 236};
			fill_rectangle(bar, WHITE);

			/* Fire once max power is reached */
			if(launch_speed == 100)
				fire_projectile();
		}
		else
		{
			fire_projectile();
		}
	}	
	/* Menu phase */
	else if(game_state == 4)
	{
		if (get_switch_press(_BV(SWE)))
		{
			if(players == 4)
				players = 2;
			else
				players++;
		}
		else if (get_switch_press(_BV(SWW)))
		{
			if(players == 2)
				players = 4;
			else
				players--;
		}

		ml_printf_at("< %u Players >", 5, 115, players);
		
		if (get_switch_long(_BV(SWC)))
		{
			start_game();
		}
	}
	/* End game phase */
	else if(game_state == 5)
	{
		if (get_switch_long(_BV(SWC)))
		{
			start_menu();
		}
	}

	sei();
	
	return 0;
}
Пример #7
0
uint8_t execute_pdu(pdu_type *pdu) {
	switch (pdu->opcode) {
		case OPCODE_GET_TYPE: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = MODULE_TYPE;
			break;
		}

		case OPCODE_GET_ID: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = get_module_id();
			break;
		}

		case OPCODE_GET_FW_VERSION: {
			pdu->number_of_arguments = 3;
			pdu->arguments[0] = FIRMWARE_MAJOR;
			pdu->arguments[1] = FIRMWARE_MINOR;
			pdu->arguments[2] = FIRMWARE_PATCHLEVEL;

			break;
		}

		case OPCODE_GET_NR_CHANNELS: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = NUM_CHANNELS;

			break;
		}

		case OPCODE_GET_FEATURES: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = 0;

			break;
		}

		case OPCODE_GET_SW_THRESHOLD: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = get_switch_threshold();

			break;
		}

		case OPCODE_GET_DIMMER_THRESHOLD: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = get_long_press_threshold();

			break;
		}


		case OPCODE_GET_DIMMER_DELAY: {
			pdu->number_of_arguments = 1;
			pdu->arguments[0] = get_dimmer_delay();

			break;
		}

		case OPCODE_GET_SW_TIMER: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint16_t channel_timer = get_switch_timer(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = channel_timer;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_GET_CHANNEL_MAPPING: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t channel_mapping = get_switch_mapping(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = channel_mapping;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_GET_DEFAULT_STATE: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t channel_state = get_default_channel_state(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = channel_state;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_GET_DEFAULT_PERCENTAGE: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t percentage = get_default_dimmer_percentage(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = percentage;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_GET_DEFAULT_DIMMER_DIRECTION: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t direction = get_default_dimmer_direction(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = direction;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_SW_THRESHOLD: {
			if (pdu->number_of_arguments == 1) {
				uint16_t threshold = pdu->arguments[0];
				set_switch_threshold(threshold);
				pdu->number_of_arguments = 0;
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_DIMMER_DELAY: {
			if (pdu->number_of_arguments == 1) {
				uint8_t delay = pdu->arguments[0];
				set_dimmer_delay(delay);
				pdu->number_of_arguments = 0;
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_DIMMER_THRESHOLD: {
			if (pdu->number_of_arguments == 1) {
				uint16_t threshold = pdu->arguments[0];
				set_long_press_threshold(threshold);
				pdu->number_of_arguments = 0;
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_SW_TIMER: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];
				uint16_t timer = pdu->arguments[1];

				if (is_valid_channel(channel_number)) {
					set_switch_timer(channel_number, timer);
					pdu->number_of_arguments = 0;
				} else {
					return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_CHANNEL_MAPPING: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];
				uint8_t mapping = pdu->arguments[1];

				if (is_valid_channel(channel_number) && is_valid_channel(mapping)) {
					set_switch_mapping(channel_number, mapping);
					pdu->number_of_arguments = 0;
				} else {
					return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_DEFAULT_STATE: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];
				uint8_t default_state = pdu->arguments[1];

				if (is_valid_channel(channel_number)) {
					if (default_state == 0 || default_state == 1) {
						set_default_channel_state(channel_number, default_state);
						pdu->number_of_arguments = 0;
					} else {
						return ERROR_INVALID_STATE;
					}
				} else {
					return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_DEFAULT_PERCENTAGE: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];
				uint8_t default_percentage = pdu->arguments[1];

				if (is_valid_channel(channel_number)) {
					if (default_percentage >= 0 || default_percentage <= 100) {
						set_default_dimmer_percentage(channel_number, default_percentage);
						pdu->number_of_arguments = 0;
					} else {
						return ERROR_INVALID_PERCENTAGE;
					}
				} else {
					return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SET_DEFAULT_DIMMER_DIRECTION: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];
				uint8_t default_direction = pdu->arguments[1];

				if (is_valid_channel(channel_number)) {
					if (default_direction == 0 || default_direction == 1) {
						set_default_dimmer_direction(channel_number, default_direction);
						pdu->number_of_arguments = 0;
					} else {
						return ERROR_INVALID_STATE;
					}
				} else {
					return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_RELOAD_CONFIGURATION: {
			configuration_load();
			pdu->number_of_arguments = 0;

			break;
		}

		case OPCODE_SAVE_CONFIGURATION: {
			configuration_save();
			pdu->number_of_arguments = 0;

			break;
		}

		case OPCODE_GET_OUTPUT_STATE: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t output_state = get_output_state(channel_number);

					pdu->number_of_arguments = 2;
					pdu->arguments[0] = output_state;
					pdu->arguments[1] = get_percentage(channel_number);
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_GET_INPUT_STATE: {
			if (pdu->number_of_arguments == 1) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t switch_state = get_switch_state(channel_number);

					pdu->number_of_arguments = 1;
					pdu->arguments[0] = switch_state;
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_SWITCH_OUTPUT: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t new_state = pdu->arguments[1];

					if (new_state == STATE_OFF || new_state == STATE_ON) {
						switch_output(channel_number, new_state);

						pdu->number_of_arguments = 0;
					} else {
						return ERROR_INVALID_STATE;
					}
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		case OPCODE_DIM: {
			if (pdu->number_of_arguments == 2) {
				uint8_t channel_number = pdu->arguments[0];

				if (is_valid_channel(channel_number)) {
					uint8_t percentage = pdu->arguments[1];

					if (percentage >= 0 || percentage <= 100) {
						dim(channel_number, percentage);

						pdu->number_of_arguments = 0;
					} else {
						return ERROR_INVALID_PERCENTAGE;
					}
				} else {
					return ERROR_INVALID_CHANNEL_NUMBER;
				}
			} else {
				return ERROR_WRONG_NUMBER_OF_ARGUMENTS;
			}

			break;
		}

		default: {
			return ERROR_INVALID_OPCODE;
		}
	}

	return 0;
}