Пример #1
0
 // Update is called once per frame
 void Update()
 {
     if (PacketsAvailable() > 0)
     {
         ArduinoBuffer packet = GetReceivedPacket();
         OnMessageReceived.Invoke(packet);
     }
 }
Пример #2
0
 /// <summary>
 /// Sends a packet to the arduino.
 /// </summary>
 /// <param name="buffer">The ArduinoBuffer containing the command number as well as the bytearray.</param>
 public void Send(ArduinoBuffer buffer)
 {
     Send(buffer.cmd, buffer.dataType, buffer.buffer);
 }
Пример #3
0
    void OnDataReceive(ArduinoBuffer data)
    {
        if (_command != data.cmd)
        {
            return;
        }
        if (_dataType != data.dataType)
        {
            return;
        }

        if (_dataType == ArduinoDataType.Bytearray)
        {
            byte[] ba = data.buffer;
            _bytesReceived.Invoke(ba);
        }
        else if (_dataType == ArduinoDataType.Bool)
        {
            bool b = BitConverter.ToBoolean(data.buffer, 0);
            _boolReceived.Invoke(b);
        }
        else if (_dataType == ArduinoDataType.Int)
        {
            int i = BitConverter.ToInt32(data.buffer, 0);
            _intReceived.Invoke(i);
        }
        else if (_dataType == ArduinoDataType.Float)
        {
            float f = BitConverter.ToSingle(data.buffer, 0);
            _floatReceived.Invoke(f);
        }
        else if (_dataType == ArduinoDataType.String)
        {
            string s = System.Text.Encoding.ASCII.GetString(data.buffer);
            _stringReceived.Invoke(s);
        }
        else if (_dataType == ArduinoDataType.Int2)
        {
            Vector2Int v = new Vector2Int(
                BitConverter.ToInt32(data.buffer, 0),
                BitConverter.ToInt32(data.buffer, 4));
            _int2Received.Invoke(v);
        }
        else if (_dataType == ArduinoDataType.Int3)
        {
            Vector3Int v = new Vector3Int(
                BitConverter.ToInt32(data.buffer, 0),
                BitConverter.ToInt32(data.buffer, 4),
                BitConverter.ToInt32(data.buffer, 8));
            _int3Received.Invoke(v);
        }
        else if (_dataType == ArduinoDataType.Float2)
        {
            Vector2 v = new Vector2(
                BitConverter.ToSingle(data.buffer, 0),
                BitConverter.ToSingle(data.buffer, 4));
            _float2Received.Invoke(v);
        }
        else if (_dataType == ArduinoDataType.Float3)
        {
            Vector3 v = new Vector3(
                BitConverter.ToSingle(data.buffer, 0),
                BitConverter.ToSingle(data.buffer, 4),
                BitConverter.ToSingle(data.buffer, 8));
            _float3Received.Invoke(v);
        }
        else if (_dataType == ArduinoDataType.Float4)
        {
            Vector4 v = new Vector4(
                BitConverter.ToSingle(data.buffer, 0),
                BitConverter.ToSingle(data.buffer, 4),
                BitConverter.ToSingle(data.buffer, 8),
                BitConverter.ToSingle(data.buffer, 12));
            _float4Received.Invoke(v);
        }
    }