Пример #1
0
 private static void Session_MessageReceived(object sender, FirmataMessageEventArgs eventArgs)
 {
     if (eventArgs.Value.Type == MessageType.StringData)
     {
         _waitHandle.Set(((StringData)eventArgs.Value.Value).Text);
     }
 }
Пример #2
0
        void Session_OnMessageReceived(object sender, FirmataMessageEventArgs eventArgs)
        {
            string o;

            switch (eventArgs.Value.Type)
            {
            case MessageType.StringData:
                o = ((StringData)eventArgs.Value.Value).Text;
                break;

            default:
                o = "?";
                break;
            }

            Console.WriteLine("Message {0} received: {1}", eventArgs.Value.Type, o);
        }
Пример #3
0
        /// <summary>
        /// Handle messages that harve arrived from the board and successfully processed by Firmata
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void HandleFirmataMessageReceived(Object sender, FirmataMessageEventArgs eventArgs)
        {
            var        fmessage = eventArgs.Value;
            ADMMessage message  = null;

            try
            {
                switch (fmessage.Type)
                {
                case Solid.Arduino.Firmata.MessageType.FirmwareResponse:
                    break;

                case Solid.Arduino.Firmata.MessageType.StringData:
                    StringData sd = (StringData)fmessage.Value;
                    try
                    {
                        String serialized = sd.Text;
                        message = ADMMessage.Deserialize <ADMMessage>(serialized, MessageEncoding.BYTES_ARRAY);
                    }
                    catch (Exception e)
                    {
                        String sbytes = BitConverter.ToString(Chetch.Utilities.Convert.ToBytes(sd.Text));
                        Tracing?.TraceEvent(TraceEventType.Error, 4000, "Deserializing {0} produced exception {1}: {2}", sbytes, e.GetType(), e.Message);
                        throw e;
                    }
                    break;

                case Solid.Arduino.Firmata.MessageType.PinStateResponse:
                    break;

                case Solid.Arduino.Firmata.MessageType.DigitalPortState:
                    DigitalPortState portState = (DigitalPortState)fmessage.Value;
                    String           s         = BitConverter.ToString(BitConverter.GetBytes(portState.Pins));

                    int pinsChanged;
                    if (_portStates.ContainsKey(portState.Port))
                    {
                        pinsChanged = portState.Pins ^ _portStates[portState.Port].Pins;
                    }
                    else
                    {
                        pinsChanged = 255;
                    }
                    _portStates[portState.Port] = portState;

                    for (int i = 0; i < 8; i++)
                    {
                        int bit2check = (int)System.Math.Pow(2, i);
                        if ((bit2check & pinsChanged) == 0)
                        {
                            continue;
                        }

                        bool state     = portState.IsSet(i);
                        int  pinNumber = GetPinForPort(portState.Port, i);    //TODO: this might need to be board dependent
                        var  devs      = GetDevicesByPin(pinNumber);
                        if (devs != null)
                        {
                            foreach (var dev in devs)
                            {
                                dev.HandleDigitalPinStateChange(pinNumber, state);
                            }
                        }
                    }

                    //String s1 = System.Convert.ToString(portState.Pins, 2);
                    //String s2 = System.Convert.ToString(pinsChanged, 2);
                    //Debug.Print("Pins/2change: " + s1 + "/" + s2);
                    break;

                case Solid.Arduino.Firmata.MessageType.CapabilityResponse:
                    break;

                default:
                    break;
                }
            } catch (Exception e)
            {
                byte tag = message != null ? message.Tag : (byte)0;
                message       = new ADMMessage();
                message.Type  = Messaging.MessageType.ERROR;
                message.Value = e.Message;
                message.Tag   = tag;
            }

            if (message != null)
            {
                Task.Run(() => { HandleReceivedADMMessage(message); });
            }
        }
Пример #4
0
 private void session_OnMessageReceived(object par_Sender, FirmataMessageEventArgs par_EventArgs)
 {
     _messagesReceived.Enqueue(par_EventArgs.Value);
 }
Пример #5
0
 private void ListenFeedback(object sender, FirmataMessageEventArgs eventArgs)
 {
     MonoBehaviour.print(eventArgs.Value);
 }