Example #1
0
        /// <summary>
        /// Perform action based on Serial input
        /// </summary>
        /// <param name="msg">The Serial input message</param>
        public static void PerformAction(string msg)
        {
            // Check if the received message is not empty:
            if (msg == "")
            {
                return;
            }
            if (!msg.Contains(":"))
            {
                return;
            }

            string[] data  = msg.Split(':');
            string   id    = data[0];
            string   state = data[1];

            //PushButtonEvents
            if (id.StartsWith(PushButton.PushButtonIdentifier))
            {
                id.Replace(PushButton.PushButtonIdentifier, "");
                if (!InputDataMapping.HasInitializedAsMatrix)
                {
                    int        btnId = int.Parse(id);
                    PushButton btn   = InputDataMapping.PushButtons.Find(e => e.Identifier == btnId);
                    btn.UpdateState(MathHelper.BoolFromInt(int.Parse(state)));
                    Console.WriteLine(btn.Identifier + " " + btn.IsPressed);
                }
                else
                {
                    int i = 0;
                    foreach (char c in state.TrimEnd())
                    {
                        // Update PushButton's state for each button:
                        PushButton btn = InputDataMapping.PushButtons.Find(e => e.Identifier == i);
                        btn.UpdateState(MathHelper.BoolFromChar(c));
                        i++;
                    }
                }
            }
            else if (id.StartsWith(RotaryEncoder.RotaryEncoderIdentifier)) //Encoder events
            {
                int           encId = int.Parse(id.Replace(RotaryEncoder.RotaryEncoderIdentifier, ""));
                RotaryEncoder enc   = InputDataMapping.RotaryEncoders.Find(e => e.ComponentIdentifier == encId);
                enc.UpdateValue(long.Parse(state));
            }
            else if (id.StartsWith(RotaryEncoder.RotaryEncoderButtonIdentifier))
            {
                int           encId = int.Parse(id.Replace(RotaryEncoder.RotaryEncoderButtonIdentifier, ""));
                RotaryEncoder enc   = InputDataMapping.RotaryEncoders.Find(e => e.ComponentIdentifier == encId);
                enc.UpdateButtonState(MathHelper.BoolFromInt(int.Parse(state)));
            }
            //TODO add knobs here
            else
            {
                Console.WriteLine("Unknown action: {0}", msg);
            }
        }