Esempio n. 1
0
        public int lastReadValue = 0; //for Editor panel

        public Pin(UduinoDevice arduinoParent, int pin, PinMode mode)
        {
            Manager    = UduinoManager.Instance;
            device     = arduinoParent;
            currentPin = pin;
            pinMode    = mode;
        }
Esempio n. 2
0
        /// <summary>
        /// Retreive the Data from the Serial Prot using Unity Coroutines
        /// </summary>
        /// <param name="target"></param>
        /// <returns>null</returns>
        public IEnumerator ReadSerial(string target)
        {
            while (true)
            {
                UduinoDevice uduino = null;
                if (uduinoDevices.TryGetValue(target, out uduino))
                {
                    if (uduino.read != null)
                    {
                        string data = uduino.ReadFromArduino(uduino.read, 50);
                        uduino.read = null;
                        yield return(null);

                        ReadData(data, target);
                    }
                    else
                    {
                        yield return(null);
                    }
                }
                else
                {
                    yield return(null);
                }
            }
        }
Esempio n. 3
0
        bool TryToFind(UduinoDevice uduinoDevice, bool callAsync = false)
        {
            if (uduinoDevice.getStatus() == BoardStatus.Finding)
            {
                string reading = uduinoDevice.ReadFromArduino("identity", instant: true);
                Log.Debug("Trying to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>.", true);
                if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity")
                {
                    string name = reading.Split(new char[0])[1];
                    uduinoDevice.name = name;
                    if (callAsync)
                    {
                        _manager.InvokeAsync(() =>
                        {
                            uduinoDevice.UduinoFound();
                            _manager.AddUduinoBoard(name, uduinoDevice);
                        });
                    }
                    else
                    {
                        uduinoDevice.UduinoFound();
                        _manager.AddUduinoBoard(name, uduinoDevice);
                    }

                    BoardFound(name);
                    return(true);
                }
                else
                {
                    Log.Debug("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Retrying.");
                }
            }
            return(false);
        }
Esempio n. 4
0
        public virtual void DetectUduino(UduinoDevice uduinoDevice)
        {
            if (uduinoDevice.boardStatus == BoardStatus.Closed)
            {
                return;
            }

            if (_manager.ReadOnThread && Application.isPlaying)
            {
                try
                {
                    Thread _detectionThread = null;
                    _detectionThread = new Thread(() => DetectUduinoThread(uduinoDevice));
                    _detectionThread.Start();
                }
                catch (System.Exception e)
                {
                    Log.Error(e);
                }
            }
            else
            {
                _manager.StartCoroutine(DetectUduinoCoroutine(uduinoDevice));
            }
        }
Esempio n. 5
0
        /// <summary>
        ///  Read the Serial Port data in a new thread.
        /// </summary>
        public void ReadPorts()
        {
            while (IsRunning())
            {
                // Debug.Log(autoRead);
                lock (uduinoDevices)
                {
                    string[] keys = new string[uduinoDevices.Count];
                    for (int i = 0; i < uduinoDevices.Count; i++)
                    {
                        uduinoDevices.Keys.CopyTo(keys, i);
                    }

                    foreach (string key in keys)
                    {
                        UduinoDevice device = uduinoDevices[key];
                        device.WriteToArduinoLoop();
                        device.ReadFromArduinoLoop();
                    }
                }
                if (limitSendRate)
                {
                    Thread.Sleep((int)sendRateSpeed / 2);
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Retreive the Data from the Serial Prot using Unity Coroutines
 /// </summary>
 /// <param name="target"></param>
 /// <returns>null</returns>
 public IEnumerator ReadSerial(string target = null)
 {
     while (true)
     {
         UduinoDevice uduino = null;
         if (target != null && uduinoDevices.TryGetValue(target, out uduino))
         {
             uduino.WriteToArduinoLoop();
             if (uduino.read != null)
             {
                 uduino.ReadFromArduino(uduino.read);
                 uduino.ReadFromArduinoLoop();
                 yield return(null);
             }
             else
             {
                 yield return(null);
             }
         }
         else
         {
             foreach (KeyValuePair <string, UduinoDevice> uduinoDevice in uduinoDevices)
             {
                 uduinoDevice.Value.ReadFromArduinoLoop();
                 uduinoDevice.Value.WriteToArduinoLoop();
             }
             yield return(null);
         }
     }
 }
        /// <summary>
        /// Discover all active serial ports connected.
        /// When a new serial port is connected, send the IDENTITY request, to get the name of the arduino
        /// </summary>
        /// <param name="portNames">All Serial Ports names, dependings of the current OS</param>
        void Discover(string[] portNames)
        {
            if (portNames.Length == 0)
            {
                Log.Error("Found 0 ports open. Are you sure your arduino is connected ?");
            }
            List <string> tmpPortOpen = new List <string>();

            foreach (string portName in portNames)
            {
                if (!_manager.BlackListedPorts.Contains(portName))
                {
                    if (!tmpPortOpen.Contains(portName))
                    {
                        tmpPortOpen.Add(portName);
                        UduinoDevice tmpDevice = OpenUduinoDevice(portName);
                        tmpDevice.Open();
                        DetectUduino(tmpDevice);
                    }
                }
                else
                {
                    Log.Info("Port <color=#2196F3>[" + portName + "]</color> is blacklisted.");
                }
            }
        }
Esempio n. 8
0
 public void AddUduinoBoard(string name, UduinoDevice board)
 {
     lock (uduinoDevices)
     {
         uduinoDevices.Add(name, board);
         Log.Warning("Board <color=#ff3355>" + name + "</color> <color=#2196F3>[" + board.getIdentity() + "]</color> added.");
     }
 }
Esempio n. 9
0
 public virtual void CloseDevices()
 {
     if (connectedDevice != null)
     {
         UduinoManager.Instance.CloseDevice(connectedDevice);
         connectedDevice = null;
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Get the specific pin from the current board, if the board is already set with SetBoardType
        /// </summary>
        /// <param name="pin">Pin to find</param>
        /// <returns>Int of the pin</returns>
        public int GetPinFromBoard(string pin)
        {
            var e = uduinoDevices.GetEnumerator();

            e.MoveNext();
            UduinoDevice anElement = e.Current.Value;

            int currentBoardType = anElement._boardType;

            return(BoardsTypeList.Boards.GetBoardFromId(currentBoardType).GetPin(pin));
        }
Esempio n. 11
0
        public int lastReadValue = 0; //for Editor panel

        public Pin(UduinoDevice arduinoParent, int pin, PinMode mode)
        {
            Manager    = UduinoManager.Instance;
            device     = arduinoParent;
            currentPin = pin;
            pinMode    = mode;

            if (device != null)
            {
                arduinoName = device.name;
            }
        }
Esempio n. 12
0
 public bool PinTargetExists(UduinoDevice parentArduinoTarget, int currentPinTarget)
 {
     if ((device != null || arduinoName == "" || parentArduinoTarget == null || parentArduinoTarget == null || parentArduinoTarget == device) &&
         currentPinTarget == currentPin)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 13
0
        /// <summary>
        /// Find a board connected to a specific port
        /// </summary>
        /// <param name="portName">Port open</param>
        IEnumerator FindBoardPort(string portName)
        {
            UduinoDevice uduinoDevice = new UduinoDevice(portName, baudRate, readTimeout, writeTimeout);
            int          tries        = 0;

            do
            {
                if (uduinoDevice.getStatus() == SerialStatus.OPEN)
                {
                    string reading = uduinoDevice.ReadFromArduino("identity", instant: true);

                    if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity")
                    {
                        string name = reading.Split(new char[0])[1];
                        uduinoDevice.name = name;
                        lock (uduinoDevices)
                            uduinoDevices.Add(name, uduinoDevice); //Add the new device to the devices array
                        if (!ReadOnThread)
                        {
                            StartCoroutine(ReadSerial(name));                // Initiate the Async reading of variables
                        }
                        Log.Warning("Board <color=#ff3355>" + name + "</color> <color=#2196F3>[" + uduinoDevice.getPort() + "]</color> added to dictionnary");
                        uduinoDevice.UduinoFound();

                        if (Application.isPlaying && _thread == null && readOnThread)
                        {
                            StartThread();
                        }

                        Write(name, "connected");
                        //uduinoDevice.WriteToArduino();
                        InitAllArduinos();
                        break;
                    }
                    else
                    {
                        Log.Debug("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Retrying.");
                    }
                }
                //TODO : compare with yield return new WaitForSeconds(0.5f);
                yield return(null);    //Wait one frame with yield return null
            } while (uduinoDevice.getStatus() != SerialStatus.UNDEF && tries++ < discoverTries);

            if (uduinoDevice.getStatus() != SerialStatus.FOUND)
            {
                Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Closing.");
                uduinoDevice.Close();
                uduinoDevice = null;
            }
        }
Esempio n. 14
0
 public virtual bool BoardNotFound(UduinoDevice uduinoDevice)
 {
     if (uduinoDevice.getStatus() != BoardStatus.Found)
     {
         Log.Warning("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Closing.");
         uduinoDevice.Close();
         uduinoDevice = null;
         return(false);
     }
     else
     {
         return(true);
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Verify if the target exists when we want to get a value
        /// </summary>
        /// <param name="target">Target Uduino Name</param>
        /// <returns>Re</returns>
        private bool UduinoTargetExists(string target)
        {
            UduinoDevice uduino = null;

            if (uduinoDevices.TryGetValue(target, out uduino))
            {
                return(true);
            }
            else
            {
                //TODO: Restart a loop to find all objects
                Log.Warning("The object " + target + " cannot be found. Are you sure it's connected and correctly detected ?");
                return(false);
            }
        }
Esempio n. 16
0
 /// <summary>
 /// Parse the received data
 /// </summary>
 /// <param name="data">Received data</param>
 /// <param name="target">TODO : for the moment target is unused</param>
 void ReadData(string data, string target = null)
 {
     if (data != null && data != "" && data != "Null")
     {
         UduinoDevice uduino = uduinoDevices[target];
         uduino.lastRead = data;
         if (uduino.callback != null)
         {
             uduino.callback(data);
         }
         else
         {
             OnValueReceived(data, target);
         }
     }
 }
Esempio n. 17
0
        /// <summary>
        /// Find a board connected to a specific port
        /// </summary>
        /// <param name="portName">Port open</param>
        public virtual IEnumerator DetectUduino(UduinoDevice uduinoDevice)
        {
            int tries = 0;

            do
            {
                if (uduinoDevice.getStatus() == BoardStatus.Open)
                {
                    string reading = uduinoDevice.ReadFromArduino("identity", instant: true);
                    if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity")
                    {
                        string name = reading.Split(new char[0])[1];
                        uduinoDevice.name = name;
                        _manager.AddUduinoBoard(name, uduinoDevice);
                        uduinoDevice.UduinoFound();

                        if (!_manager.ReadOnThread)
                        {
                            _manager.StartCoroutine(_manager.CoroutineRead(name)); // Initiate the Async reading of variables
                        }
                        else
                        {
                            _manager.StartThread();
                        }

                        uduinoDevice.WriteToArduino("connected");
                        _manager.InitAllArduinos();
                        break;
                    }
                    else
                    {
                        Log.Debug("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Retrying.");
                    }
                }
                yield return(new WaitForSeconds(0.05f));    //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
                // yield return null;    //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
            } while (uduinoDevice.getStatus() != BoardStatus.Undef && tries++ < _manager.DiscoverTries);

            if (uduinoDevice.getStatus() != BoardStatus.Found)
            {
                Log.Warning("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Closing.");
                uduinoDevice.Close();
                uduinoDevice = null;
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Find a board connected to a specific port
        /// </summary>
        /// <param name="uduinoDevice">uduinoDevice</param>
        public IEnumerator DetectUduinoCoroutine(UduinoDevice uduinoDevice)
        {
            uduinoDevice.boardStatus = BoardStatus.Finding;
            int tries = 0;

            yield return(new WaitForSeconds(UduinoManager.Instance.delayBeforeDiscover));

            do
            {
                if (TryToFind(uduinoDevice))
                {
                    break;
                }
                yield return(new WaitForSeconds(0.1f));  //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
                // yield return null;    //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
            } while (uduinoDevice.getStatus() != BoardStatus.Found && tries++ < _manager.DiscoverTries - 1);

            BoardNotFound(uduinoDevice);
        }
Esempio n. 19
0
        public int ParseLastResults()
        {
            UduinoDevice targetUduino = null;

            if (device != null)
            {
                targetUduino = device;
            }
            else
            {
                targetUduino = Manager.GetBoard(arduinoName);
            }

            int returnedValue = ParseIntValue(targetUduino.lastRead);

            if (returnedValue != -1)
            {
                lastReadValue = returnedValue;
            }

            return(lastReadValue);
        }
Esempio n. 20
0
        /// <summary>
        /// Find a board connected to a specific port
        /// </summary>
        /// <param name="portName">Port open</param>
        IEnumerator FindBoardPort(string portName)
        {
            UduinoDevice uduinoDevice = new UduinoDevice(portName, baudRate);
            int          tries        = 0;

            do
            {
                if (uduinoDevice.getStatus() == SerialStatus.OPEN)
                {
                    string reading = uduinoDevice.ReadFromArduino("IDENTITY", 200);
                    if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity")
                    {
                        string name = reading.Split(new char[0])[1];
                        uduinoDevices.Add(name, uduinoDevice); //Add the new device to the devices array
                        if (!ReadOnThread)
                        {
                            StartCoroutine(ReadSerial(name));                // Initiate the Async reading of variables
                        }
                        Log.Info("Board <color=#ff3355>" + name + "</color> <color=#2196F3>[" + uduinoDevice.getPort() + "]</color> added to dictionnary");
                        uduinoDevice.UduinoFound();
                        break;
                    }
                    else
                    {
                        Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Retrying.");
                    }
                }
                yield return(new WaitForSeconds(0.1f));    //Wait one frame
            } while (uduinoDevice.getStatus() != SerialStatus.UNDEF && tries++ < discoverTries);

            if (uduinoDevice.getStatus() != SerialStatus.FOUND)
            {
                Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Closing.");
                uduinoDevice.Close();
                uduinoDevice = null;
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Detect Uduino device from a thread
        /// </summary>
        /// <param name="uduinoDevice"></param>
        public void DetectUduinoThread(UduinoDevice uduinoDevice)
        {
#if UNITY_ANDROID
            if (_manager.ReadOnThread)
            {
                AndroidJNI.AttachCurrentThread(); // Sepcific android related code
            }
#endif
            uduinoDevice.boardStatus = BoardStatus.Finding;
            int tries = 0;
            Thread.Sleep(Mathf.FloorToInt(UduinoManager.Instance.delayBeforeDiscover * 1000));
            do
            {
                if (TryToFind(uduinoDevice, true))
                {
                    return;
                }
                Thread.Sleep(100);
                //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
                // yield return null;    //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ?
            } while (uduinoDevice.getStatus() != BoardStatus.Found && tries++ < _manager.DiscoverTries);

            BoardNotFound(uduinoDevice);
        }