Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO.Ports;
- using System;
- public class CustomInput : MonoBehaviour, IMeventListener<Mevent>
- {
- #if UNITY_EDITOR
- public string SerialPortPath = "/dev/tty.usbmodem1421";
- public int BaudRate = 9600;
- [Header ("Controls")]
- public SteeringWheel Wheel;
- public AnimationCurve WheelCurve;
- [Space]
- public JoyStick ForkStick;
- public AnimationCurve ForkCurve;
- [Space]
- public Pedal Throttle;
- public AnimationCurve ThrottleCurve;
- [Space]
- public Switch Reversal;
- public AnimationCurve ReversalCurve;
- [Space]
- public ChaseCamera ChaseCam;
- [Header ("Info")]
- [Range (0f, 1f)]
- public float WheelValue;
- [Range (0f, 1f)]
- public float ForkValue;
- [Range (0f, 1f)]
- public float ThrottleValue;
- SerialPort serialPort;
- bool lastDirection;
- float missionCompletedTimer = 0;
- Player player;
- bool lastCam;
- // Use this for initialization
- void Start()
- {
- player = Dependencies.Get<Player> ();
- serialPort = new SerialPort (SerialPortPath, BaudRate);
- serialPort.ReadTimeout = 100;
- try
- {
- if (!serialPort.IsOpen)
- serialPort.Open ();
- }
- catch (Exception e)
- {
- Debug.LogWarning (e);
- }
- lastDirection = false;
- Reversal.Status = false;
- Reversal.UpdateAngle ();
- MeventDispatcher<Mevent>.Instance.AddListener (this);
- }
- float Interpet(string input, AnimationCurve curve)
- {
- float val = float.Parse (input) / 1024f;
- return curve.Evaluate (val);
- }
- float Filter(float newValue, float oldValue)
- {
- return newValue;
- /*
- if (Mathf.Abs (newValue - oldValue) > 0.05f)
- return newValue;
- return Mathf.Lerp (oldValue, newValue, 0.1f);
- */
- }
- void ReadData()
- {
- string value = "";
- try
- {
- //432.00,462.00,595.00,589.00,1.00,
- value = serialPort.ReadLine ();
- string[] data = value.Split (',');
- // Debug.Log (value);
- if (data.Length > 0) //wheel
- {
- float val = Interpet (data [0], WheelCurve);
- val = Filter (val, WheelValue);
- Wheel.OverrideRotation = Mathf.Lerp (-Mathf.PI, Mathf.PI, val);
- WheelValue = val;
- }
- if (data.Length > 1) //fork
- {
- float val = Interpet (data [1], ForkCurve);
- val = Filter (val, ForkValue);
- ForkStick.OverrideAngle = Mathf.Lerp (-ForkStick.AngleLimits, ForkStick.AngleLimits, val);
- ForkValue = val;
- }
- if (data.Length > 2) //throttle
- {
- float val = Interpet (data [2], ThrottleCurve);
- val = Filter (val, ThrottleValue);
- Throttle.OverrideAngle = Mathf.Lerp (Throttle.UpAngle, Throttle.DownAngle, val);
- ThrottleValue = val;
- }
- if (data.Length > 3) //direction
- {
- bool val = float.Parse (data [3]) > 0.5f;
- Reversal.Status = val;
- if (val != lastDirection)
- Reversal.UpdateAngle ();
- lastDirection = val;
- }
- if (data.Length > 4) //cam
- {
- bool val = float.Parse (data [4]) > 0.5f;
- if (val && !lastCam)
- ChaseCam.YRotationOffset += 90f;
- lastCam = val;
- }
- serialPort.BaseStream.Flush ();
- }
- catch (Exception e)
- {
- // Debug.Log (value);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (!serialPort.IsOpen)
- return;
- if (serialPort.BytesToRead > 0)
- {
- ReadData ();
- }
- /*
- int r = 0;
- int g = (int)(200f * Mathf.Abs (rot.z));
- int b = 0;
- serialPort.WriteLine (string.Format ("{0},{1},{2}", r, g, b));
- */
- string data = "a";
- if (player.CarriedPallet != null)
- data = "b";
- if ((missionCompletedTimer -= Time.deltaTime) > 0f)
- data = "c";
- serialPort.WriteLine (data);
- }
- #region IMeventListener implementation
- public void OnMevent(Mevent mevent)
- {
- if (mevent.Type == Mevent.MeventType.MissionCompleted)
- {
- missionCompletedTimer = 3f;
- }
- }
- #endregion
- void OnApplicationQuit()
- {
- if (serialPort != null && serialPort.IsOpen)
- serialPort.Close ();
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement