#include "H5_Game.h"
#include <string.h>

//last_jump and now_jump need wait 200ms

#define WAIT_TIME 10

H5::H5()
{

	last_left_zupt = 1;
	last_right_zupt = 1;

	CANCLE_DOWN = 0;
	CANCLE_JUMP = 0;

	valid_down_wait_time = 0;
	valid_jump_wait_time = 0;

	last_jump = 0;

	last_down = 0;
}

void H5::Process(int time_stamp, int* right_pos, int* right_att, int* right_acc, int right_zupt, int right_front_press, int right_back_press,
	int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_front_press, int left_back_press,
	int jump, int down, int rssi)
{

	//获取触地
	if (last_left_zupt == 0 && left_zupt == 1)
	{
		//统计步数
		addMotionCount(STEP_COUNT);

		std::cout << "H5GAME:: MOTION_LEFT" << endl;
		result[0] = MOTION_LEFT;
	}
	else
	{
		result[0] = -1;
	}

	if (last_right_zupt == 0 && right_zupt == 1)
	{
		//统计步数
		addMotionCount(STEP_COUNT);

		std::cout << "H5GAME:: MOTION_RIGHT" << endl;
		result[1] = MOTION_RIGHT;
	}
	else
	{
		result[1] = -1;
	}

	//filter down jump

	if (last_jump == 0 && jump == 1)
	{
		//统计步数
		addMotionCount(JUMP_COUNT);

		std::cout << "H5GAME:: MOTION_JUMP" << endl;

		result[2] = MOTION_JUMP;

		valid_jump_wait_time = WAIT_TIME;
	}
	else
	{
		result[2] = -1;
	}

	//reset jump wait time when both foot isn't on the floor
	if ((valid_jump_wait_time > 0 && left_zupt == 0 && right_zupt == 0) || jump == 1)
	{
		valid_jump_wait_time = WAIT_TIME;
	}

	if (valid_jump_wait_time > 0)
	{
		CANCLE_JUMP = 1;

		valid_jump_wait_time--;
	}
	else
	{
		CANCLE_JUMP = 0;
	}


	//same process with jump
	if (last_down == 0 && down == 1 && CANCLE_DOWN == 0)
	{
		//统计步数
		addMotionCount(DOWN_COUNT);

		std::cout << "H5GAME:: MOTION_DOWN" << endl;

		result[3] = MOTION_DOWN;

		valid_down_wait_time = WAIT_TIME;
	}
	else
	{
		result[3] = -1;
	}

	//if ((valid_jump_wait_time > 0) || down == 1)
	//{
	//	valid_down_wait_time = WAIT_TIME;
	//}

	if (valid_down_wait_time > 0)
	{
		CANCLE_DOWN = 1;

		valid_down_wait_time--;
	}
	else
	{
		CANCLE_DOWN = 0;
	}


	last_left_zupt = left_zupt;
	last_right_zupt = right_zupt;

	last_jump = jump;
	last_down = down;
}
