Esempio n. 1
0
        /// <summary>
        /// Convert parsedV2Sequence into a V3 sequence
        /// </summary>
        private void importSequenceData()
        {
            // instantiate the state machine to process incoming data
            Vixen2xSequenceImportSM import = new Vixen2xSequenceImportSM(Sequence, parsedV2Sequence.EventPeriod);

            // the current color is based on the intensity of a three channel group
            int red   = 0;
            int green = 0;
            int blue  = 0;

            // for each channel in the V2 sequence
            for (var currentElementNum = 0; currentElementNum < parsedV2Sequence.ElementCount; currentElementNum++)
            {
                // Check to see if we are processing more elements than we have mappings. This showed up as an error for a user
                if (currentElementNum >= mappings.Count)
                {
                    Logging.Error("importSequenceData: Trying to process more elements (" + parsedV2Sequence.ElementCount + ") than we have mappings. (" + mappings.Count + ")");
                    break;
                }
                ChannelMapping v2ChannelMapping = mappings[currentElementNum];

                // set the channel number and the time for each v2 event.
                import.OpenChannel(v2ChannelMapping.ElementNodeId, Convert.ToDouble(parsedV2Sequence.EventPeriod));

                // Logging.Debug("importSequenceData:currentElementNum: " + currentElementNum);

                string elementName      = v2ChannelMapping.ChannelName;
                Color  currentColor     = Color.FromArgb(255, 255, 255);
                byte   currentIntensity = 0;

                conversionProgressBar.UpdateProgressBar(currentElementNum);
                Application.DoEvents();

                // is this an unmapped output channel?
                if (Guid.Empty == v2ChannelMapping.ElementNodeId)
                {
                    // no output channel. Move on to the next input channel
                    continue;
                }                 // end no output channel defined

                // do we have a valid guid conversion?
                if (false == m_GuidToV2ChanList.ContainsKey(v2ChannelMapping.ElementNodeId))
                {
                    Logging.Error("importSequenceData: Configuration error. GUID: '" + v2ChannelMapping.ElementNodeId + "' not found in m_GuidToV2ChanList.");
                    continue;
                }

                // is this a valid pixel configuration
                if ((true == v2ChannelMapping.RgbPixel) && (3 != m_GuidToV2ChanList[v2ChannelMapping.ElementNodeId].Count))
                {
                    Logging.Error("importSequenceData: Configuration error. Found '" + m_GuidToV2ChanList[v2ChannelMapping.ElementNodeId].Count + "' V2 channels attached to element '" + elementName + "'. Expected 3(RGB). Converting element to non color mixing mode.");
                    v2ChannelMapping.RgbPixel = false;
                }

                // process each event for this V2 channel
                for (uint currentEventNum = 0; currentEventNum < parsedV2Sequence.EventsPerElement; currentEventNum++)
                {
                    // get the intensity for this V2 channel
                    currentIntensity = parsedV2Sequence.EventData[currentElementNum * parsedV2Sequence.EventsPerElement + currentEventNum];

                    // is this an RGB Pixel?
                    if (true == v2ChannelMapping.RgbPixel)
                    {
                        // Only process the RED channel of a three channel pixel
                        if (Color.Red != v2ChannelMapping.DestinationColor)
                        {
                            // this is not the red channel of a pixel
                            continue;
                        }                         // end not red pixel channel

                        red   = 0;
                        green = 0;
                        blue  = 0;

                        // process the input colors bound to this output channel
                        foreach (ChannelMapping v2Channel in m_GuidToV2ChanList[v2ChannelMapping.ElementNodeId])
                        {
                            // Logging.Info("convertMapping: Processing V2 Channel '" + v2Channel.ChannelName + "' color intensity.");

                            switch (v2Channel.DestinationColor.Name)
                            {
                            case "Red":
                            {
                                red = Math.Max(red, parsedV2Sequence.EventData[(Convert.ToInt64(v2Channel.ChannelNumber) - 1) * parsedV2Sequence.EventsPerElement + currentEventNum]);
                                break;
                            }                                             // end Red

                            case "Green":
                            {
                                green = Math.Max(green, parsedV2Sequence.EventData[(Convert.ToInt64(v2Channel.ChannelNumber) - 1) * parsedV2Sequence.EventsPerElement + currentEventNum]);
                                break;
                            }                                             // end Green

                            case "Blue":
                            {
                                blue = Math.Max(blue, parsedV2Sequence.EventData[(Convert.ToInt64(v2Channel.ChannelNumber) - 1) * parsedV2Sequence.EventsPerElement + currentEventNum]);
                                break;
                            }                                             // end Red

                            default:
                            {
                                Logging.Error("importSequenceData pixel conversion processing error. Skipping processing unexpected color '" + v2Channel.DestinationColor.Name + "' for V2 Channel '" + v2Channel.ChannelName + "(" + v2Channel.ChannelNumber + ")'. Color must be one of 'RED', 'GREEN' or 'BLUE'");
                                break;
                            }                     // end default
                            }                     // end switch on color
                        }                         // end process each V2 channel assigned to the v3 channel

                        // get the max intensity for this v2 channel set
                        currentIntensity = Convert.ToByte(Math.Min((int)255, Math.Max(red, Math.Max(green, blue))));

                        // Scale the color to full intensity and let the intensity value attenuate it.
                        if (0 != currentIntensity)
                        {
                            double multplier = Convert.ToDouble(byte.MaxValue) / Convert.ToDouble(currentIntensity);

                            red   = Math.Min(((int)255), Convert.ToInt32(Convert.ToDouble(red) * multplier));
                            green = Math.Min(((int)255), Convert.ToInt32(Convert.ToDouble(green) * multplier));
                            blue  = Math.Min(((int)255), Convert.ToInt32(Convert.ToDouble(blue) * multplier));
                        }

                        // set the final color
                        currentColor = Color.FromArgb(red, green, blue);
                    }                     // end pixel processing
                    else
                    {
                        // set the non pixel color value
                        currentColor = mappings[currentElementNum].DestinationColor;
                    }                     // end non pixel processing

                    // process the event through the state machine.
                    import.processEvent(currentEventNum, currentColor, currentIntensity);
                }                 // end for each event in the element / channel

                // close this channel
                import.closeChannel();
            }     // end for each input channel
        }         // end importSequenceData
        private void importSequenceData()
        {
            int startEventPosition = resetEventPosition;
            var endEventPosition   = resetEventPosition;
            var priorEventNum      = resetEventPosition;

            var startEventValue   = zeroEventValue;
            var endEventValue     = zeroEventValue;
            var priorEventValue   = zeroEventValue;
            var currentEventValue = zeroEventValue;

            var pbImportValue = 0;

            // These flags are here just to make the code below easier to read, at least for me.
            var patternFound           = false;
            var processingSingleEvents = false;
            var processingGroupEvents  = false;
            var processingRamps        = false;
            var processingFades        = false;
            var currentEventIsZero     = true;
            var currentEventIsNotZero  = false;
            var priorEventisNotZero    = false;

            foreach (patternType pattern in Enum.GetValues(typeof(patternType)))
            {
                processingSingleEvents = pattern == patternType.SingleSetLevel;
                processingGroupEvents  = pattern == patternType.SetLevelTrend;
                processingRamps        = pattern == patternType.PulseRampTrend;
                processingFades        = pattern == patternType.PulseFadeTrend;

                var patternText = ((DescriptionAttribute)((pattern.GetType().GetMember(pattern.ToString()))[0]
                                                          .GetCustomAttributes(typeof(DescriptionAttribute), false)[0])).
                                  Description;

                currentEventValue = zeroEventValue;
                for (var currentElementNum = 0; currentElementNum < parsedV2Sequence.ElementCount; currentElementNum++)
                {
                    conversionProgressBar.StatusLineLabel = string.Format("Finding {0} on Element {1}", patternText,
                                                                          currentElementNum + 1);
                    conversionProgressBar.UpdateProgressBar(++pbImportValue);

                    patternFound    = false;
                    priorEventValue = zeroEventValue;
                    priorEventNum   = resetEventPosition;

                    for (var currentEventNum = 0; currentEventNum < parsedV2Sequence.EventsPerElement; currentEventNum++)
                    {
                        // To keep the progress bar looking snappy
                        if ((currentEventNum % 10) == 0)
                        {
                            Application.DoEvents();
                        }
                        currentEventValue =
                            parsedV2Sequence.EventData[currentElementNum * parsedV2Sequence.EventsPerElement + currentEventNum];

                        currentEventIsZero    = currentEventValue == zeroEventValue;
                        currentEventIsNotZero = !currentEventIsZero;
                        priorEventisNotZero   = priorEventValue != zeroEventValue;

                        // Add a non zero single set level event.
                        if (processingSingleEvents && currentEventIsNotZero)
                        {
                            addEvent(pattern, currentElementNum, currentEventNum, currentEventValue, currentEventNum);

                            startEventPosition = resetEventPosition;
                            endEventPosition   = resetEventPosition;
                        }
                        // Add a ramp, fade or multi set level event since it just ended (a zero event was found)
                        else if (patternFound && !processingSingleEvents && currentEventIsZero && endEventPosition != resetEventPosition)
                        {
                            addEvent(pattern, currentElementNum, startEventPosition, startEventValue, endEventPosition, endEventValue);

                            patternFound       = false;
                            startEventPosition = resetEventPosition;
                            endEventPosition   = resetEventPosition;
                        }
                        // Beggining of a pattern found, set flag and start event postion and value
                        else if (!patternFound && currentEventNum > resetEventPosition &&
                                 ((processingGroupEvents && currentEventIsNotZero && currentEventValue == priorEventValue) ||
                                  (processingFades && currentEventIsNotZero && currentEventValue < priorEventValue) ||
                                  (processingRamps && priorEventisNotZero && currentEventValue > priorEventValue)))
                        {
                            patternFound       = true;
                            startEventPosition = currentEventNum - 1;
                            startEventValue    = priorEventValue;
                            endEventPosition   = currentEventNum;
                            endEventValue      = currentEventValue;
                        }
                        // Pattern continuing, update the end event postion and value.
                        else if (patternFound &&
                                 ((processingGroupEvents && currentEventValue == priorEventValue) ||
                                  (processingFades && currentEventValue < priorEventValue) ||
                                  (processingRamps && priorEventisNotZero && currentEventValue > priorEventValue)))
                        {
                            endEventPosition = currentEventNum;
                            endEventValue    = currentEventValue;
                        }
                        // End of a pattern because none of the other conditions were met.
                        else if (patternFound)
                        {
                            addEvent(pattern, currentElementNum, startEventPosition, startEventValue, priorEventNum, priorEventValue);

                            patternFound       = false;
                            startEventPosition = resetEventPosition;
                            endEventPosition   = resetEventPosition;
                        }
                        priorEventValue = currentEventValue;
                        priorEventNum   = currentEventNum;
                    }                     // for currentEvent

                    // End of the Element, so process any existing patterns.
                    if (patternFound)
                    {
                        addEvent(pattern, currentElementNum, startEventPosition, priorEventValue, priorEventNum);
                    }
                }         // for currentElementNum
            }             // foreach patternType
        }