Ejemplo n.º 1
0
    IEnumerator SendTelemetryData()
    {
        while (true)
        {
            float dt = 0.05f;
            yield return(new WaitForSecondsRealtime(dt * 2));

            var acc = new DieMessageAcc();
            acc.type = DieMessageType.Telemetry;
            acc.data = new AccelFrame[2];

            // Vector3 accVec = Vector3.down;
            // int x = Mathf.RoundToInt(accVec.x / 8.0f * (float)(1 << 11));
            // int y = Mathf.RoundToInt(accVec.y / 8.0f * (float)(1 << 11));
            // int z = Mathf.RoundToInt(accVec.z / 8.0f * (float)(1 << 11));

            acc.data[0].DeltaTime = (short)(dt * 1000);
            //acc.data[0].X = (short)x;
            //acc.data[0].Y = (short)y;
            //acc.data[0].Z = (short)z;
            acc.data[0].X         = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            acc.data[0].Y         = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            acc.data[0].Z         = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            acc.data[1].DeltaTime = (short)(dt * 1000);
            //acc.data[1].X = (short)x;
            //acc.data[1].Y = (short)y;
            //acc.data[1].Z = (short)z;
            acc.data[1].X = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            acc.data[1].Y = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            acc.data[1].Z = (short)(Random.Range(0, 1 << 12) - (1 << 11));
            onData(DieMessages.ToByteArray(acc));
        }
    }
Ejemplo n.º 2
0
    IEnumerator SendMessageWithAckOrTimeoutCr <T>(T message, DieMessageType ackType, float timeOut, System.Action <DieMessage> ackAction, System.Action timeoutAction)
        where T : DieMessage
    {
        DieMessage ackMessage            = null;
        float      startTime             = Time.time;
        MessageReceivedDelegate callback = (ackMsg) =>
        {
            ackMessage = ackMsg;
        };

        AddMessageHandler(ackType, callback);
        byte[] msgBytes = DieMessages.ToByteArray(message);
        DicePool.Instance.WriteDie(this, msgBytes, msgBytes.Length, null);
        while (ackMessage == null && Time.time < startTime + timeOut)
        {
            yield return(null);
        }
        RemoveMessageHandler(ackType, callback);
        if (ackMessage != null)
        {
            ackAction?.Invoke(ackMessage);
        }
        else
        {
            timeoutAction?.Invoke();
        }
    }
Ejemplo n.º 3
0
    IEnumerator SendMessage <T>(T message)
        where T : DieMessage
    {
        bool msgReceived = false;

        byte[] msgBytes = DieMessages.ToByteArray(message);
        _sendBytes.SendBytes(this, msgBytes, msgBytes.Length, () => msgReceived = true);
        yield return(new WaitUntil(() => msgReceived));
    }
Ejemplo n.º 4
0
    public void OnData(byte[] data)
    {
        // Process the message coming from the actual die!
        var message = DieMessages.FromByteArray(data);
        MessageReceivedDelegate del;

        if (messageDelegates.TryGetValue(message.type, out del))
        {
            del.Invoke(message);
        }
    }
Ejemplo n.º 5
0
 void OnMessageReceived(byte[] data)
 {
     if (data.Length >= 1)
     {
         var message = DieMessages.FromByteArray(data);
         MessageReceivedDelegate del;
         if (messageDelegates.TryGetValue(message.type, out del))
         {
             del.Invoke(message);
         }
     }
 }
Ejemplo n.º 6
0
    IEnumerator SendMessageWithAck <T>(T message, DieMessageType ackType)
        where T : DieMessage
    {
        bool msgReceived = false;
        MessageReceivedDelegate callback = (ackMsg) =>
        {
            msgReceived = true;
        };

        AddMessageHandler(ackType, callback);
        byte[] msgBytes = DieMessages.ToByteArray(message);
        _sendBytes.SendBytes(this, msgBytes, msgBytes.Length, null);

        yield return(new WaitUntil(() => msgReceived));

        RemoveMessageHandler(ackType, callback);
    }
Ejemplo n.º 7
0
    public void DataReceived(byte[] data)
    {
        if (!connected)
        {
            Debug.LogError("Die " + name + " received data while disconnected!");
            return;
        }

        // Process the message coming from the actual die!
        var message = DieMessages.FromByteArray(data);
        MessageReceivedDelegate del;

        if (messageDelegates.TryGetValue(message.type, out del))
        {
            del.Invoke(message);
        }
    }
Ejemplo n.º 8
0
    IEnumerator SendMessageWithAckOrTimeout <T>(T message, DieMessageType ackType, float timeOut)
        where T : DieMessage
    {
        bool  msgReceived = false;
        float startTime   = Time.time;
        MessageReceivedDelegate callback = (ackMsg) =>
        {
            msgReceived = true;
        };

        AddMessageHandler(ackType, callback);
        byte[] msgBytes = DieMessages.ToByteArray(message);
        _sendBytes.SendBytes(this, msgBytes, msgBytes.Length, null);
        while (!msgReceived && Time.time < startTime + timeOut)
        {
            yield return(null);
        }
        RemoveMessageHandler(ackType, callback);
    }
Ejemplo n.º 9
0
 void PostMessage <T>(T message)
     where T : DieMessage
 {
     byte[] msgBytes = DieMessages.ToByteArray(message);
     DicePool.Instance.WriteDie(this, msgBytes, msgBytes.Length, null);
 }
Ejemplo n.º 10
0
 void SendMessage <T>(T message)
     where T : DieMessage
 {
     lastDataSent = DieMessages.ToByteArray(message);
     onData?.Invoke(lastDataSent);
 }
Ejemplo n.º 11
0
 void PostMessage <T>(T message)
     where T : DieMessage
 {
     byte[] msgBytes = DieMessages.ToByteArray(message);
     _sendBytes.SendBytes(this, msgBytes, msgBytes.Length, null);
 }