Ejemplo n.º 1
0
        public void Evaluate(int SpreadMax)
        {
            if (In.IsChanged)
            {
                Values.SliceCount = PortNumber.SliceCount * 8;                 // 8 bits per port
                Port.SliceCount   = PortNumber.SliceCount;

                foreach (string msg in In)
                {
                    byte[] data = Encoding.GetEncoding(1252).GetBytes(msg);
                    if (!FirmataUtils.ContainsCommand(data, Command.DIGITALMESSAGE))
                    {
                        continue;                         //
                    }
                    int   port;
                    int[] values;
                    if (FirmataUtils.DecodePortMessage(data, out port, out values))
                    {
                        Port[0] = port;
                        Values.AssignFrom(values);
                        CachedValues = Values.Clone();
                    }
                    else
                    {
                        Values = CachedValues.Clone();
                    }
                }
            }
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
0
        private void Decode(byte data)
        {
            if ((data & 0x80) > 0)
            {
                byte cmd = FirmataUtils.GetCommandFromByte(data);

                lastCommand = cmd;
                switch (cmd)
                {
                case Command.RESET:
                    buffer.Clear();
                    break;

                case Command.DIGITALMESSAGE:
                case Command.ANALOGMESSAGE:
                case Command.REPORT_VERSION:
                case Command.SETPINMODE:
                    remaining = 2;
                    buffer.Clear();
                    buffer.Enqueue(data);
                    break;

                case Command.TOGGLEDIGITALREPORT:
                case Command.TOGGLEANALOGREPORT:
                    remaining = 1;
                    buffer.Clear();
                    buffer.Enqueue(data);
                    break;

                case Command.SYSEX_START:
                    buffer.Clear();
                    break;

                case Command.SYSEX_END:
                    // Fire Sysex event
                    ProcessSysex(buffer);
                    break;

                default:
                    // unknown command
                    break;
                }
            }
            else
            {
                buffer.Enqueue(data);
                if (--remaining == 0)
                {
                    // process the message
                    switch (lastCommand)
                    {
                    case Command.ANALOGMESSAGE:
                        int pin, value;
                        FirmataUtils.DecodeAnalogMessage(buffer.ToArray(), out pin, out value);
                        if (pin < FAnalogInputCount[0])
                        {
                            FAnalogIns[pin] = value;                   // assign the found value to the spread
                        }
                        break;

                    case Command.DIGITALMESSAGE:
                        int port; int[] vals;
                        // Decode the values from the bytes:
                        FirmataUtils.DecodePortMessage(buffer.ToArray(), out port, out vals);
                        // Fill the spread with parsed pinstates
                        int pinNum;
                        for (int i = 0; i < Constants.BitsPerPort; i++)
                        {
                            pinNum = i + Constants.BitsPerPort * port;
                            if (pinNum < FDigitalIns.SliceCount)
                            {
                                FDigitalIns[pinNum] = vals[i];
                            }
                        }
                        break;

                    case Command.REPORT_VERSION:
                        int major = (int)buffer.Dequeue();
                        int minor = (int)buffer.Dequeue();
                        FFirmwareMajorVersion[0] = major;
                        FFirmwareMinorVersion[0] = minor;
                        break;

                    default:
                        // unkown byte...
                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
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;
                }
            }
        }