Exemple #1
0
        public static FrameworkElement MakeSingleLineItem(BadgeCaps device, string message, bool halfSize = false, bool fullWidth = true)
        {
            var   size    = halfSize ? 7 : 12;
            var   font    = new FontFamily(halfSize ? "Lucida Console" : "Arial");
            Brush color   = Brushes.White;
            var   element = new TextBlock()
            {
                Text                = message,
                Background          = Brushes.Transparent,
                FontSize            = size,
                Margin              = halfSize ? new Thickness(0, 0, 0, -1) : new Thickness(0, -2, 0, 0),
                FontFamily          = font,
                TextWrapping        = TextWrapping.NoWrap,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                TextAlignment       = System.Windows.TextAlignment.Center,
                Foreground          = color,
                MinWidth            = !fullWidth || halfSize ? 0 : device.Width,
                UseLayoutRounding   = true,
                SnapsToDevicePixels = true
            };

            TextOptions.SetTextFormattingMode(element, TextFormattingMode.Display);
            TextOptions.SetTextRenderingMode(element, TextRenderingMode.Aliased);
            TextOptions.SetTextHintingMode(element, TextHintingMode.Fixed);

            return(element);
        }
Exemple #2
0
 public SlidingTransition(
     BadgeCaps device,
     MessageQueueItem item1,
     MessageQueueItem item2,
     SlidingDirection direction,
     Easing ease,
     float speed,
     float?padding)
 {
     Item1     = item1;
     Item2     = item2;
     Direction = direction;
     Ease      = ease;
     Speed     = speed;
     if (IsVertical)
     {
         Padding       = padding.HasValue ? padding.Value : device.Height;
         m_startOffset = Item1.Element.RenderY;
         m_distance    = AnimHelper.Round(Item1.Element.ClipHeight + m_startOffset + Padding);
     }
     else
     {
         Padding       = padding.HasValue ? padding.Value : device.Width;
         m_startOffset = Item1.Element.RenderX;
         m_distance    = AnimHelper.Round(Item1.Element.ClipWidth + m_startOffset + Padding);
     }
     m_currOffset    = m_startOffset;
     m_totalDuration = m_distance / Speed;
 }
Exemple #3
0
        public WpfVisual(BadgeCaps device, FrameworkElement element, int defaultWidth = -1, int defaultHeight = -1, bool dither = false, bool enableBlend = false)
        {
            if (defaultWidth <= 0)
            {
                defaultWidth = device.Width;
            }
            if (defaultHeight <= 0)
            {
                defaultHeight = device.Height;
            }

            Element = element;
            Element.Measure(new Size(defaultWidth, defaultHeight));
            Element.Arrange(new Rect(0, 0, defaultWidth, defaultHeight));

            ClipWidth  = (int)Math.Ceiling(Element.ActualWidth);
            ClipHeight = (int)Math.Ceiling(Element.ActualHeight);

            Dither      = dither;
            EnableBlend = enableBlend;

            m_cachedIntermediate = new BadgeRenderTarget(ClipWidth, ClipHeight, PixelFormat.TwoBits);
            m_renderTarget       = new RenderTargetBitmap(ClipWidth, ClipHeight, 96, 96, PixelFormats.Pbgra32);

            Update(0); // To avoid remeasuring on a background thread
            UpdateCachedImage();
        }
Exemple #4
0
 public SlidingPosition(
     BadgeCaps device,
     MessageQueueItem item,
     SlidingDirection direction,
     Easing ease,
     float speed,
     float padding)
 {
     Item      = item;
     Direction = direction;
     Ease      = ease;
     Speed     = speed;
     Padding   = padding;
     if (IsVertical)
     {
         m_distance = Item.Element.ClipHeight - device.Height;
     }
     else
     {
         m_distance = Item.Element.ClipWidth - device.Width;
     }
     m_distance     += 2 * Padding;
     m_startOffset   = Padding;
     m_currOffset    = 0;
     m_totalDuration = m_distance / Speed;
 }
Exemple #5
0
 void ResponseHandler(object sender, BadgeResponseEventArgs args)
 {
     if ((args.FromBadge == m_connection) && (args.Code == ResponseCodes.Setting) && ((SettingValue)(args.Response[0] & 0xF) == SettingValue.Caps))
     {
         m_device = args.FromBadge.Device;
     }
 }
Exemple #6
0
 public SlidingPosition2D(
     BadgeCaps device,
     MessageQueueItem item,
     float directionX,
     float directionY,
     Easing ease,
     float speed,
     float paddingX,
     float paddingY)
 {
     Item            = item;
     DirectionX      = directionX == 0.0f ? 0 : directionX > 0.0f ? 1.0f : -1.0f;
     DirectionY      = directionY == 0.0f ? 0 : directionY > 0.0f ? 1.0f : -1.0f;
     Ease            = ease;
     Speed           = speed;
     PaddingX        = paddingX;
     PaddingY        = paddingY;
     m_distanceX     = (Item.Element.ClipWidth - device.Width) + 2 * PaddingX;
     m_distanceY     = (Item.Element.ClipHeight - device.Height) + 2 * PaddingY;
     m_startOffsetX  = PaddingX;
     m_startOffsetY  = PaddingY;
     m_currOffsetX   = 0;
     m_currOffsetY   = 0;
     m_totalDuration = Math.Max(m_distanceX, m_distanceY) / Speed;
 }
Exemple #7
0
        public static FrameworkElement MakeDoubleLineItem(BadgeCaps device, string message1, string message2, bool fullWidth = true)
        {
            var element1 = MakeSingleLineItem(device, message1, halfSize: true, fullWidth: fullWidth);
            var element2 = MakeSingleLineItem(device, message2, halfSize: true, fullWidth: fullWidth);

            var element = new StackPanel();

            element.Children.Add(element1);
            element.Children.Add(element2);

            return(element);
        }
Exemple #8
0
 public void Connect(string port, BadgeCaps device)
 {
     if (!Connected)
     {
         m_device         = device;
         m_prevBrightness = -1;
         m_responseDispatcher.ResponseHandler += ResponseHandler;
         m_connection = new BadgeConnection(port, device.Baud, m_responseDispatcher);
     }
     else
     {
         throw new Exception("Already connected");
     }
 }
Exemple #9
0
        public static FrameworkElement MakeSplitLineItem(BadgeCaps device, string message1, string message2, string message3, bool fullWidth = true)
        {
            var element1 = MakeSingleLineItem(device, message1, fullWidth: false);
            var element2 = MakeDoubleLineItem(device, message2, message3, fullWidth: false);

            var element = new StackPanel()
            {
                Orientation         = Orientation.Horizontal,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                MinWidth            = !fullWidth ? 0 : device.Width
            };

            element.Children.Add(element1);
            element.Children.Add(element2);

            return(element);
        }
Exemple #10
0
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            BadgeCaps other = obj as BadgeCaps;

            if (other == null)
            {
                return(false);
            }

            return
                ((Width == other.Width) &&
                 (Height == other.Height) &&
                 (BitsPerPixel == other.BitsPerPixel) &&
                 (SupportedFeatures == other.SupportedFeatures));
        }
Exemple #11
0
 public MessageQueueVisual(BadgeCaps device, MessageQueue queue = null)
 {
     Queue      = queue ?? new MessageQueue(device);
     ClipWidth  = device.Width;
     ClipHeight = device.Height;
 }
Exemple #12
0
        void DataReceived(object sender, SerialDataReceivedEventArgs data)
        {
            List <byte[]> responses = new List <byte[]>();

            byte[] buffer = new byte[Stream.BytesToRead + m_inputBufferLength];
            Array.Copy(m_inputBuffer, 0, buffer, 0, m_inputBufferLength);
            Stream.Read(buffer, m_inputBufferLength, buffer.Length - m_inputBufferLength);
            m_inputBufferLength = 0;

            for (int i = 0; i < buffer.Length;)
            {
                ResponseCodes code = BadgeResponses.GetCode(buffer[i]);

                int rem      = buffer.Length - i;
                int minSize  = BadgeResponses.GetMinResponseLength(code);
                int fullSize = int.MaxValue;
                if (minSize <= rem)
                {
                    fullSize = BadgeResponses.GetFullResponseLength(code, buffer, i);
                }

                if (fullSize <= rem)
                {
                    byte[] fullResponse = new byte[fullSize];
                    Array.Copy(buffer, i, fullResponse, 0, fullSize);
                    responses.Add(fullResponse);
                    i += fullSize;

                    // handle a couple of special responses before forwarding them along
                    if (code == ResponseCodes.Ack)
                    {
                        bool fromSerialPacket = ((fullResponse[0] & 0x08) == 0);
                        if (fromSerialPacket)
                        {
                            // reliable packet success!
                            RetirePendingPacket(fullResponse[1]);
                        }
                    }
                    else if (code == ResponseCodes.Error)
                    {
                        ErrorCodes error = (ErrorCodes)(fullResponse[0] & 0xF);
                        if (error == ErrorCodes.CorruptPacketData || error == ErrorCodes.ReceiveBufferOverrun)
                        {
                            // reliable packet failure!
                            PendingPacket packet = RetirePendingPacket(fullResponse[1]);
                            if (packet.Packet != null)
                            {
                                if (packet.Attempt >= m_retryMax)
                                {
                                    m_dispatcher.NotifySendFailure(this, packet.Packet);
                                }
                                else
                                {
                                    lock (m_resendPackets)
                                    {
                                        m_resendPackets.Add(packet);
                                    }
                                }
                            }
                        }
                    }
                    else if (code == ResponseCodes.Setting)
                    {
                        SettingValue valueType = (SettingValue)(fullResponse[0] & 0xF);
                        if (valueType == SettingValue.Caps)
                        {
                            byte version, width, height, bitDepth;
                            SupportedFeatures features;
                            BadgeResponses.DecodeCapsSetting(fullResponse, 0, out version, out width, out height, out bitDepth, out features);
                            Device = new BadgeCaps(version, width, height, bitDepth, features, Baud);
                        }
                    }
                }
                else
                {
                    // needs more
                    m_inputBufferLength = buffer.Length - i;
                    Array.Copy(buffer, i, m_inputBuffer, 0, m_inputBufferLength);
                    break;
                }
            }

            if (responses.Count > 0 && m_dispatcher != null)
            {
                m_dispatcher.EnqueueResponse(this, responses.ToArray());
            }
        }
Exemple #13
0
        void RunFrame()
        {
            var commands = new MemoryStream();

            if (m_prevBrightness != Brightness)
            {
                m_prevBrightness = Brightness;
                BadgeCommands.CreateUpdateBrightnessSetting(commands, Brightness);
            }

            if (UseFrameBuffer)
            {
                BadgeCaps device = Device;
                if (device != null && (m_renderTarget == null || !m_renderTarget.SameDimentions(device.Width, device.Height, device.BitsPerPixel == 1 ? PixelFormat.OneBit : PixelFormat.TwoBits)))
                {
                    m_renderTarget = new BadgeRenderTarget(device.Width, device.Height, device.BitsPerPixel == 1 ? PixelFormat.OneBit : PixelFormat.TwoBits);
                }

                if (m_renderTarget != null)
                {
                    var render = RenderFrame;
                    if (render != null)
                    {
                        render(this, new BadgeFrameEventArgs(Device, m_renderTarget));
                    }

                    if (Dither)
                    {
                        m_renderTarget.DitherImage();
                    }
                    m_renderTarget.PackBuffer(RotateFrame);

                    var ready = FrameReady;
                    if (ready != null)
                    {
                        ready(this, new BadgeFrameEventArgs(Device, m_renderTarget));
                    }

                    int writeBufferLength;
                    BadgeCommands.CreateWriteRect(commands, Target.BackBuffer, m_renderTarget.PackedFormat,
                                                  0, 0, (byte)m_renderTarget.WidthInBlocks, (byte)m_renderTarget.Height, out writeBufferLength);
                    commands.Write(m_renderTarget.PackedBuffer, 0, m_renderTarget.PackedBuffer.Length);
                    BadgeCommands.CreateSwap(commands, false, 0);
                }
            }
            else
            {
                var getCommands = GenerateCommands;
                if (getCommands != null)
                {
                    getCommands(this, new BadgeCommandEventArgs(Device, commands));
                }
            }

            var readyToSend = ReadyToSend;

            if (readyToSend != null)
            {
                readyToSend(this, new BadgeCommandEventArgs(Device, commands));
            }

            SendFrame(commands);
        }
Exemple #14
0
 public BadgeCommandEventArgs(BadgeCaps device, MemoryStream commands)
 {
     Device        = device;
     CommandStream = commands;
 }
Exemple #15
0
 static Badges()
 {
     B1236 = new BadgeCaps(BadgeConnection.Version, 36, 12, 2, 0, 38400);
     B1248 = new BadgeCaps(BadgeConnection.Version, 48, 12, 2, SupportedFeatures.HardwareBrightness, 57600);
 }
Exemple #16
0
 public MessageQueue(BadgeCaps device)
 {
     Device   = device;
     SyncRoot = new object();
 }
Exemple #17
0
 public static MessageQueueItem MakeQueuedItem(BadgeCaps device, FrameworkElement element)
 {
     return(new MessageQueueItem(new WpfVisual(device, element)));
 }
Exemple #18
0
 public BadgeFrameEventArgs(BadgeCaps device, BadgeRenderTarget buffer)
 {
     Device = device;
     Frame  = buffer;
 }