use of org.openbot.env.AudioPlayer in project OpenBot by isl-org.
the class ControlsFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
phoneController = PhoneController.getInstance(requireContext());
preferencesManager = new SharedPreferencesManager(requireContext());
audioPlayer = new AudioPlayer(requireContext());
masterList = FileUtils.loadConfigJSONFromAsset(requireActivity());
serverCommunication = new ServerCommunication(requireContext(), this);
requireActivity().getSupportFragmentManager().setFragmentResultListener(Constants.GENERIC_MOTION_EVENT, this, (requestKey, result) -> {
MotionEvent motionEvent = result.getParcelable(Constants.DATA);
vehicle.setControl(vehicle.getGameController().processJoystickInput(motionEvent, -1));
processControllerKeyData(Constants.CMD_DRIVE);
});
requireActivity().getSupportFragmentManager().setFragmentResultListener(Constants.KEY_EVENT, this, (requestKey, result) -> processKeyEvent(result.getParcelable(Constants.DATA)));
mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
vehicle = mViewModel.getVehicle().getValue();
startAnimation = AnimationUtils.loadAnimation(requireContext(), R.anim.blink);
mViewModel.getUsbData().observe(getViewLifecycleOwner(), data -> {
char header = data.charAt(0);
String body = data.substring(1);
switch(header) {
case 'f':
vehicle.processVehicleConfig(body);
break;
case 'v':
if (FormatUtils.isNumeric(body)) {
vehicle.setBatteryVoltage(Float.parseFloat(body));
} else {
String[] msgParts = body.split(":");
switch(msgParts[0]) {
case "min":
vehicle.setMinMotorVoltage(Float.parseFloat(msgParts[1]));
case "low":
vehicle.setLowBatteryVoltage(Float.parseFloat(msgParts[1]));
break;
case "max":
vehicle.setMaxBatteryVoltage(Float.parseFloat(msgParts[1]));
break;
default:
Toast.makeText(requireContext().getApplicationContext(), "Invalid voltage message received!", Toast.LENGTH_SHORT).show();
break;
}
}
break;
case 's':
if (FormatUtils.isNumeric(body)) {
vehicle.setSonarReading(Float.parseFloat(body));
}
break;
case 'w':
String[] itemList = body.split(",");
if (itemList.length == 2 && FormatUtils.isNumeric(itemList[0]) && FormatUtils.isNumeric(itemList[1])) {
vehicle.setLeftWheelRpm(Float.parseFloat(itemList[0]));
vehicle.setRightWheelRpm(Float.parseFloat(itemList[1]));
}
break;
case 'b':
// do nothing
break;
}
processUSBData(data);
});
handlePhoneControllerEvents();
vehicle.requestVehicleConfig();
}
Aggregations