Esempio n. 1
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            analogIns.SliceCount  = analogInputCount[0];
            digitalIns.SliceCount = digitalInputCount[0];

            /// Using a Queue and iterate over it (nice to handle and inexpensive)
            foreach (byte b in Encoding.GetEncoding(1252).GetBytes(ansiMessage[0]))
            {
                // we should check for max buffer size and not constantly enque things...
                buffer.Enqueue(b);
            }

            // A cache for sysex data
            Queue <byte> cache = new Queue <byte>();
            // A flag if parsing sysex data
            bool bIsSysex = false;

            // PARSE:
            while (buffer.Count > 0)
            {
                byte current = buffer.Dequeue();
                switch (current)
                {
                case Command.SYSEX_START:
                case Command.SYSEX_END:
                    //            byte cmd = buffer.Dequeue();
                    if (current == Command.SYSEX_START)
                    {
                        bIsSysex = true;
                    }
                    else if (current == Command.SYSEX_END)
                    {
                        // Process the Sysexdata:
                        ProcessSysex(cache);
                        bIsSysex = false;
                    }
                    cache.Clear();
                    break;

                default:
                    if (bIsSysex)
                    {
                        cache.Enqueue(current);
                    }
                    else
                    {
                        // Treat Ananlog & Digital Messages:
                        bool hasDigitalMessage = FirmataUtils.VerifiyCommand(current, Command.DIGITALMESSAGE);
                        bool hasAnalogMessage  = FirmataUtils.VerifiyCommand(current, Command.ANALOGMESSAGE);
                        // We have a data for commands
                        if (buffer.Count >= 2 && (hasDigitalMessage || hasAnalogMessage))
                        {
                            // Reihenfolge matters!
                            byte[] data = { current, buffer.Dequeue(), buffer.Dequeue() };
                            // Check for Analog Command
                            if (hasAnalogMessage)
                            {
                                int pinNum, value;
                                FirmataUtils.DecodeAnalogMessage(data, out pinNum, out value);
                                if (pinNum < analogInputCount[0])
                                {
                                    analogIns[pinNum] = value;                             // assign the found value to the spread
                                }
                            }
                            else if (hasDigitalMessage)
                            {
                                int port; int[] vals;
                                // Decode the values from the bytes:
                                FirmataUtils.DecodePortMessage(data, out port, out vals);
                                // Fill the spread with parsed pinstates
                                for (int i = 0; i < Constants.BitsPerPort; i++)
                                {
                                    int pinNum = i + Constants.BitsPerPort * port;
                                    if (pinNum < digitalIns.SliceCount)
                                    {
                                        digitalIns[pinNum] = vals[i];
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
Esempio n. 2
0
        private void HandleStream(Stream InStream)
        {
            // Check, if the incoming Stream is usable
            if (InStream == null || InStream.Length == 0 || !InStream.CanRead)
            {
                return;
            }

            // Read the incoming bytes to the internal stream buffer
            while (InStream.Position < InStream.Length)
            {
                Buffer.Enqueue((byte)InStream.ReadByte());
            }

            // A cache for sysex data
            Queue <byte> cache = new Queue <byte>();
            // A flag if parsing sysex data
            bool bIsSysex = false;

            // PARSE:
            while (Buffer.Count > 0)
            {
                byte current = Buffer.Dequeue();
                switch (current)
                {
                case Command.SYSEX_START:
                case Command.SYSEX_END:
                    if (current == Command.SYSEX_START)
                    {
                        bIsSysex = true;
                    }
                    else if (current == Command.SYSEX_END)
                    {
                        // Process the Sysexdata:
                        ProcessSysex(cache);
                        bIsSysex = false;
                    }
                    cache.Clear();
                    break;

                default:
                    if (bIsSysex) // Collect bytes for the SysSex message cache
                    {
                        cache.Enqueue(current);
                    }
                    else
                    {
                        // Treat Ananlog & Digital Messages:
                        bool hasDigitalMessage = FirmataUtils.VerifiyCommand(current, Command.DIGITALMESSAGE);
                        bool hasAnalogMessage  = FirmataUtils.VerifiyCommand(current, Command.ANALOGMESSAGE);
                        // We have a data for commands
                        if (Buffer.Count >= 2 && (hasDigitalMessage || hasAnalogMessage))
                        {
                            // Reihenfolge matters!
                            byte[] data = { current, Buffer.Dequeue(), Buffer.Dequeue() };
                            // Check for Analog Command
                            if (hasAnalogMessage)
                            {
                                int pinNum, value;
                                FirmataUtils.DecodeAnalogMessage(data, out pinNum, out value);
                                if (pinNum < FAnalogInputCount[0])
                                {
                                    FAnalogIns[pinNum] = value;                  // assign the found value to the spread
                                }
                            }
                            else if (hasDigitalMessage)
                            {
                                int port; int[] vals;
                                // Decode the values from the bytes:
                                FirmataUtils.DecodePortMessage(data, out port, out vals);
                                // Fill the spread with parsed pinstates
                                for (int i = 0; i < Constants.BitsPerPort; i++)
                                {
                                    int pinNum = i + Constants.BitsPerPort * port;
                                    if (pinNum < FDigitalIns.SliceCount)
                                    {
                                        FDigitalIns[pinNum] = vals[i];
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }