private async Task SetSpyderConsoleSegments(IEnumerable <SegmentConfig> segments)
        {
            if (segments == null || !segments.Any())
            {
                return;
            }

            var layoutButton = new KeyConfig()
            {
                SegmentID = 18, SpyderButtonIndex = 6
            };
            var editButton = new KeyConfig()
            {
                SegmentID = 18, SpyderButtonIndex = 14
            };

            //Press and release buttons to put keyboard into edit mode
            await PressAndRelease(layoutButton).ConfigureAwait(false);
            await PressAndRelease(editButton).ConfigureAwait(false);

            //Press segment buttons to select mode
            foreach (SegmentConfig segment in segments)
            {
                //Segment Type enum maps to the button index on our segment we are setting
                await PressAndRelease(segment.SegmentID, (int)segment.SegmentType).ConfigureAwait(false);
                await PressAndRelease(segment.SegmentID, 8 + segment.Index).ConfigureAwait(false);
            }

            //Press and release button to exit edit mode
            await PressAndRelease(editButton).ConfigureAwait(false);
        }
        private void SpyderConsole_Refresh(IPRefreshPacket pkt)
        {
            var newSegments = pkt.GetPortRefresh();

            List <int>            keysOn    = new List <int>();
            List <int>            keysOff   = new List <int>();
            List <QuickKeyButton> quickKeys = new List <QuickKeyButton>();

            foreach (var newSegment in newSegments)
            {
                int segmentID = newSegment.Key;
                var segment   = newSegment.Value;

                var spyderKeys = segment.ColorButtons;
                var sm3Keys    = buttonMap.GetKeysFromSegmentID(segmentID);
                var mappings   = from spyderKey in spyderKeys
                                 join sm3Key in sm3Keys on spyderKey.ControlID equals sm3Key.Value.SpyderButtonIndex
                                 select new
                {
                    Sm3Index  = sm3Key.Key,
                    Mapping   = sm3Key.Value,
                    SpyderKey = spyderKey,
                    spyderKey.Text,
                    QuickKeyIndex = KeyConfig.GetQuickKeyIndex(sm3Key.Key)
                };

                var lampMappings = from mapping in mappings
                                   where mapping.QuickKeyIndex == -1
                                   select new
                {
                    Index  = mapping.Sm3Index,
                    LampOn = ShouldLampBeOn(mapping.SpyderKey)
                };

                var quickKeyMappings = from mapping in mappings
                                       where mapping.QuickKeyIndex != -1
                                       select new QuickKeyButton()
                {
                    Index = mapping.QuickKeyIndex,
                    Text  = GetQuickKeyText(segment, mapping.SpyderKey),
                    Color = GetQuickKeyColor(mapping.SpyderKey)
                };

                keysOn.AddRange(lampMappings.Where(key => key.LampOn).Select(key => key.Index));
                keysOff.AddRange(lampMappings.Where(key => !key.LampOn).Select(key => key.Index));
                quickKeys.AddRange(quickKeyMappings);
            }

            //Send lamp updates to SM3
            screenMasterConsole.SetLamps(true, keysOn.ToArray());
            screenMasterConsole.SetLamps(false, keysOff.ToArray());
            screenMasterConsole.SetQuickKeys(quickKeys.ToArray());
        }
 private async Task PressAndRelease(KeyConfig spyderKey)
 {
     await PressAndRelease(spyderKey.SegmentID, spyderKey.SpyderButtonIndex).ConfigureAwait(false);
 }