Exemple #1
0
int main(int argc, char* argv[])
{
	printf("Press Maru-Button to Exit Process.\n");
	int iRet = 0;

	CJoystickDrv* pJoystick = NULL;

	try
	{
		// ready GPIO
		if( wiringPiSetupGpio() == -1 ){
			printf("failed to wiringPiSetupGpio()\n");
			return 1;
		}

		// ready PWM
		pinMode(GPIO_NO, PWM_OUTPUT);
		pwmSetMode(PWM_MODE_MS);
		pwmSetClock(DEF_PWM_CLOCK);
		pwmSetRange(DEF_PWM_RANGE);
	
		// ready Joystick
		pJoystick = CJoystickDrv::createInstance();
		if(!pJoystick){
			throw 0;
		}
		if(pJoystick->connectJoystick()!=0){
			printf("failed to connectJoystick()\n");
			throw 0;
		}

		const int axis_min = DUALSHOCK_ANALOG_VAL_MIN;
		const int axis_max = DUALSHOCK_ANALOG_VAL_MAX;
		const int axis_mid = DUALSHOCK_ANALOG_VAL_MID;

		const int servo_min = SERVO_MIN;
		const int servo_max = SERVO_MAX;
		const int servo_mid = SERVO_MID;

		pwmWrite(GPIO_NO, servo_mid);
		printf("begin loop \n");
		int pre_val = servo_mid;
		while(1){
			// Joystickの状態を更新
			if( pJoystick->readJoystick()!=0 ){
				printf("faile to readJoystick()\n");
				throw 0;
			}

			int roll = pJoystick->getAxisState(0);
			int val = servo_mid;
			if(roll==axis_mid){ // 中間値
				val = servo_mid;
			}else if(roll > axis_mid){ // 右
				double rate = fabs( (double)roll / (double)(axis_max) );
				int delta = (int)( (double)( servo_mid - servo_min) * rate );
				val = servo_mid - delta;
				if(val < servo_min){
					val = servo_min;
				}
			}else if(roll < axis_mid){ // 左
				double rate = fabs( (double)roll / (double)(axis_min) );
				int delta = (int)( (double)( servo_max - servo_mid ) * rate );
				val = servo_mid + delta;
				if(val > servo_max){
					val = servo_max;
				}
			}
			if( pre_val != val ){
				pwmWrite(GPIO_NO, val);
				usleep(DELAY_USEC);
				pre_val = val;
			}
			
			//Maru
			if(pJoystick->getButtonState(JOY_MARU) == BUTTON_ON){
				printf("pushed Maru-Button\n");
				break;
			}

			sleep(0);
		}
		printf("end loop\n");

		pwmWrite(GPIO_NO, servo_mid);
		if(pJoystick){
			delete pJoystick;
			pJoystick = NULL;
		}
	}
	catch(...)
	{
		printf("catch!! \n");
		iRet = -1;
		if(pJoystick){
			delete pJoystick;
			pJoystick = NULL;
		}
	}

	return iRet;
}
int main(int argc, char* argv[])
{
	int iRet = 0;

	CJoystickDrv* pJoystick = NULL;

	try
	{
		// GPIOを初期化
		if( RPiGpioDrv::init(RPI_VER_TWO) != 0 ){
			printf("failed to RPiGpioDrv::init().\n");
			throw 0;
		}

		// GPIO_Aを出力に設定
		if( RPiGpioDrv::setPinMode(GPIO_A, GPIO_OUTPUT) != 0 ){
			printf("failed to RPiGpioDrv::setPinMode(GPIO_OUTPUT).\n");
			throw 0;
		}

		// GPIO_Bを入力に設定
		if( RPiGpioDrv::setPinMode(GPIO_B, GPIO_INPUT) != 0 ){
			printf("failed to RPiGpioDrv::setPinMode(GPIO_INPUT).\n");
			throw 0;
		}

		// ready DUALSHOCK
		pJoystick = CJoystickDrv::createInstance();
		if(!pJoystick){
			throw 0;
		}
		if(pJoystick->connectJoystick()!=0){
			printf("failed to connectJoystick()\n");
			throw 0;
		}
		printf("begin loop \n");

		// EXEC_CNT分処理を繰り返す
		int i=0;
		while(1){//(i<=EXEC_CNT){
			// DUALSHOCKの状態を更新
			if( pJoystick->readJoystick()!=0 ){
				printf("faile to readJoystick()\n");
				throw 0;
			}

			//Left
			if(pJoystick->getButtonState(JOY_LEFT) == BUTTON_ON)
			{
				printf("pushed Left-Button\n");

				// GPIO_AをON(Highレベル)にする
				if( RPiGpioDrv::setOutLevel(GPIO_A, GPIO_LV_HIGH) != 0 ){
					printf("failed to setOutLevel(GPIO_LV_HIGH).\n");
					throw 0;
				}
				usleep(DELAY_USEC);
			}

			//Right
			if(pJoystick->getButtonState(JOY_RIGHT) == BUTTON_ON)
			{
				printf("pushed Right-Button\n");

				// GPIO_AをOFF(Lowレベル)にする
				if( RPiGpioDrv::setOutLevel(GPIO_A, GPIO_LV_LOW) != 0 ){
					printf("failed to setOutLevel(GPIO_LV_LOW).\n");
					throw 0;
				}
				usleep(DELAY_USEC);
			}

			//Up
			if(pJoystick->getButtonState(JOY_UP) == BUTTON_ON)
			{
				printf("pushed Up-Button\n");
			}

			//Down
			if(pJoystick->getButtonState(JOY_DOWN) == BUTTON_ON)
			{
				printf("pushed Down-Button\n");
			}

			//Maru
			if(pJoystick->getButtonState(JOY_MARU) == BUTTON_ON)
			{
				printf("pushed Maru-Button\n");
				break;
			}

			i++;
			sleep(0);
		}
		printf("end loop\n");

		if(pJoystick){
			delete pJoystick;
			pJoystick = NULL;
		}
	}
	catch(...)
	{
		printf("catch!! \n");
		iRet = -1;
		if(pJoystick){
			delete pJoystick;
			pJoystick = NULL;
		}
	}

	return iRet;
}