Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO.Ports;
  4. using System;
  5.  
  6. public class CustomInput : MonoBehaviour, IMeventListener<Mevent>
  7. {
  8.     #if UNITY_EDITOR
  9.  
  10.     public string SerialPortPath = "/dev/tty.usbmodem1421";
  11.     public int BaudRate = 9600;
  12.  
  13.  
  14.     [Header ("Controls")]
  15.     public SteeringWheel Wheel;
  16.     public AnimationCurve WheelCurve;
  17.     [Space]
  18.     public JoyStick ForkStick;
  19.     public AnimationCurve ForkCurve;
  20.     [Space]
  21.     public Pedal Throttle;
  22.     public AnimationCurve ThrottleCurve;
  23.     [Space]
  24.     public Switch Reversal;
  25.     public AnimationCurve ReversalCurve;
  26.  
  27.     [Space]
  28.     public ChaseCamera ChaseCam;
  29.  
  30.     [Header ("Info")]
  31.  
  32.     [Range (0f, 1f)]
  33.     public float WheelValue;
  34.  
  35.     [Range (0f, 1f)]
  36.     public float ForkValue;
  37.  
  38.     [Range (0f, 1f)]
  39.     public float ThrottleValue;
  40.  
  41.  
  42.     SerialPort serialPort;
  43.     bool lastDirection;
  44.     float missionCompletedTimer = 0;
  45.  
  46.     Player player;
  47.  
  48.     bool lastCam;
  49.  
  50.     // Use this for initialization
  51.     void Start()
  52.     {
  53.  
  54.         player = Dependencies.Get<Player> ();
  55.  
  56.         serialPort = new SerialPort (SerialPortPath, BaudRate);
  57.         serialPort.ReadTimeout = 100;
  58.  
  59.         try
  60.         {
  61.             if (!serialPort.IsOpen)
  62.                 serialPort.Open ();
  63.  
  64.         }
  65.         catch (Exception e)
  66.         {
  67.             Debug.LogWarning (e);
  68.         }
  69.  
  70.         lastDirection = false;
  71.         Reversal.Status = false;
  72.         Reversal.UpdateAngle ();
  73.  
  74.         MeventDispatcher<Mevent>.Instance.AddListener (this);
  75.  
  76.     }
  77.  
  78.     float Interpet(string input, AnimationCurve curve)
  79.     {
  80.         float val = float.Parse (input) / 1024f;
  81.         return curve.Evaluate (val);
  82.     }
  83.  
  84.     float Filter(float newValue, float oldValue)
  85.     {
  86.         return newValue;
  87.         /*
  88.         if (Mathf.Abs (newValue - oldValue) > 0.05f)
  89.             return newValue;
  90.         return Mathf.Lerp (oldValue, newValue, 0.1f);
  91.         */
  92.     }
  93.  
  94.     void ReadData()
  95.     {
  96.  
  97.         string value = "";
  98.  
  99.         try
  100.         {
  101.             //432.00,462.00,595.00,589.00,1.00,
  102.  
  103.             value = serialPort.ReadLine ();
  104.             string[] data = value.Split (',');
  105.  
  106.             //  Debug.Log (value);
  107.    
  108.             if (data.Length > 0)    //wheel
  109.             {
  110.                 float val = Interpet (data [0], WheelCurve);
  111.                 val = Filter (val, WheelValue);
  112.                 Wheel.OverrideRotation = Mathf.Lerp (-Mathf.PI, Mathf.PI, val);
  113.                 WheelValue = val;
  114.             }
  115.             if (data.Length > 1)    //fork
  116.             {
  117.                 float val = Interpet (data [1], ForkCurve);
  118.                 val = Filter (val, ForkValue);
  119.                 ForkStick.OverrideAngle = Mathf.Lerp (-ForkStick.AngleLimits, ForkStick.AngleLimits, val);
  120.                 ForkValue = val;
  121.             }
  122.             if (data.Length > 2)    //throttle
  123.             {
  124.                 float val = Interpet (data [2], ThrottleCurve);
  125.                 val = Filter (val, ThrottleValue);
  126.                 Throttle.OverrideAngle = Mathf.Lerp (Throttle.UpAngle, Throttle.DownAngle, val);
  127.                 ThrottleValue = val;
  128.            
  129.             }
  130.             if (data.Length > 3)    //direction
  131.             {
  132.                 bool val = float.Parse (data [3]) > 0.5f;
  133.                
  134.                 Reversal.Status = val;
  135.                
  136.                 if (val != lastDirection)
  137.                     Reversal.UpdateAngle ();
  138.                
  139.                 lastDirection = val;
  140.  
  141.             }
  142.             if (data.Length > 4)    //cam
  143.             {
  144.                 bool val = float.Parse (data [4]) > 0.5f;
  145.  
  146.                 if (val && !lastCam)
  147.                     ChaseCam.YRotationOffset += 90f;
  148.  
  149.                 lastCam = val;
  150.  
  151.             }
  152.             serialPort.BaseStream.Flush ();
  153.  
  154.         }
  155.         catch (Exception e)
  156.         {
  157. //          Debug.Log (value);
  158.         }
  159.     }
  160.    
  161.     // Update is called once per frame
  162.     void Update()
  163.     {
  164.  
  165.         if (!serialPort.IsOpen)
  166.             return;
  167.  
  168.        
  169.         if (serialPort.BytesToRead > 0)
  170.         {      
  171.             ReadData ();
  172.         }
  173.  
  174.         /*
  175.         int r = 0;
  176.         int g = (int)(200f * Mathf.Abs (rot.z));
  177.         int b = 0;
  178.  
  179.         serialPort.WriteLine (string.Format ("{0},{1},{2}", r, g, b));
  180.         */
  181.  
  182.         string data = "a";
  183.  
  184.         if (player.CarriedPallet != null)
  185.             data = "b";
  186.  
  187.         if ((missionCompletedTimer -= Time.deltaTime) > 0f)
  188.             data = "c";
  189.  
  190.  
  191.         serialPort.WriteLine (data);
  192.     }
  193.  
  194.     #region IMeventListener implementation
  195.  
  196.     public void OnMevent(Mevent mevent)
  197.     {
  198.         if (mevent.Type == Mevent.MeventType.MissionCompleted)
  199.         {
  200.             missionCompletedTimer = 3f;
  201.         }
  202.     }
  203.  
  204.     #endregion
  205.  
  206.     void OnApplicationQuit()
  207.     {
  208.         if (serialPort != null && serialPort.IsOpen)
  209.             serialPort.Close ();
  210.     }
  211.  
  212.     #endif
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement