Пример #1
0
        private void CharacteristicPort_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
        {
            var reader = DataReader.FromBuffer(args.CharacteristicValue);

            byte[] data = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(data);

            Port port = this.GetPortFromPortNumber(data[0]);

            if (port == null)
            {
                //MainBoard.WriteLine("Could not find port");
                return;
            }

            port.Connected = (data[1] == 1);
            Port.Devices device = (data.Length > 3) ? (Port.Devices)data[3] : Port.Devices.UNKNOWN;

            RegisterDeviceAttachement(port, device);
        }
Пример #2
0
        //////////////////////////////////////////
        ////
        ////
        ////
        //// Helper functions
        ////
        ////
        ////
        ////
        //////////////////////////////////////////


        protected void RegisterDeviceAttachement(Port port, Port.Devices type)
        {
            if (port.Connected)
            {
                port.Device = type;
                MainBoard.WriteLine("Port Connected: " + port.Id + " of type " + Enum.GetName(typeof(Port.Devices), type), Color.Green);

                if (port.Device == Port.Devices.TRAIN_MOTOR || port.Device == Port.Devices.BOOST_MOTOR || port.Device == Port.Devices.BOOST_EXT_MOTOR ||
                    port.Device == Port.Devices.BASIC_MOTOR || port.Device == Devices.EV3_MOTOR ||
                    port.Device == Devices.CONTROL_PLUS_L_MOTOR || port.Device == Devices.CONTROL_PLUS_XL_MOTOR)
                {
                    if (port.Device == Port.Devices.TRAIN_MOTOR)
                    {
                        port.Function = Port.Functions.TRAIN_MOTOR;
                    }
                    else if (port.Function == Port.Functions.NOT_USED)
                    {
                        port.Function = Port.Functions.MOTOR;
                    }

                    if (port.Function == Functions.SWITCH_DOUBLECROSS ||
                        port.Function == Functions.SWITCH_STANDARD ||
                        port.Function == Functions.SWITCH_TRIXBRIX)
                    {
                        ActivateSwitchToLeft(port.Id);
                    }
                }

                if (port.Device == Port.Devices.BOOST_DISTANCE)
                {
                    if (port.Function == Port.Functions.NOT_USED)
                    {
                        port.Function = Port.Functions.SENSOR;
                    }
                    ActivatePortDevice((byte)port.Value, (byte)(this.Type == Types.WEDO_2_SMART_HUB ? 0x00 : 0x08), 0, 0);
                }

                if (port.Device == Port.Devices.WEDO2_DISTANCE)
                {
                    if (port.Function == Port.Functions.NOT_USED)
                    {
                        port.Function = Port.Functions.SENSOR;
                    }
                    ActivatePortDevice((byte)port.Value, 0x00, 0, 0);
                }

                if (port.Device == Port.Devices.POWERED_UP_REMOTE_BUTTON)
                {
                    port.Function = Port.Functions.BUTTON;
                    ActivatePortDevice((byte)port.Value, 0x00, 0, 0);
                }

                if (port.Device == Devices.EV3_SENSOR || port.Device == Devices.NXT_SENSOR || port.Device == Devices.EV3_COLOR_SENSOR)
                {
                    port.Function = Port.Functions.SENSOR;
                }

                if (port.Device == Port.Devices.LED_LIGHTS)
                {
                    if (port.Function == Port.Functions.NOT_USED)
                    {
                        port.Function = Port.Functions.LIGHT;
                    }
                    SetLightBrightness(port.Id, 100);
                }

                // Let them know the type has changed
                OnPortTypeUpdate();

                // And data has changed!
                OnDataUpdated();
            }
            else
            {
                port.Device   = Port.Devices.UNKNOWN;
                port.Function = Port.Functions.NOT_USED;

                MainBoard.WriteLine("Port Disconnected: " + port.Id, Color.Purple);
                OnDataUpdated();
            }
        }
Пример #3
0
        private void Brick_BrickChanged(object sender, BrickChangedEventArgs e)
        {
            IsConnected = true;
            bool somethingChanged = false;

            foreach (InputPort type in (InputPort[])Enum.GetValues(typeof(InputPort)))
            {
                // Get the port
                Port p = GetPortFromPortId(type.ToString());

                // Check device type
                Port.Devices device = ConvertTypeToDevice(brick.Ports[type].Type);

                // Are we set? If not register the device type
                if (p.Device != device)
                {
                    p.Connected = (device != Port.Devices.UNKNOWN);
                    RegisterDeviceAttachement(p, device);
                }

                // We need to force Color mode if we have a color detector
                if (brick.Ports[type].Type == DeviceType.Color && brick.Ports[type].Mode != (byte)ColorMode.Color)
                {
                    brick.Ports[type].SetMode(ColorMode.Color);
                }

                // If we do have a color mode then let's update it!
                if (brick.Ports[type].Mode == (byte)ColorMode.Color)
                {
                    p.LatestColor = ConvertEV3ColorToPortColor((EV3Color)brick.Ports[type].RawValue);

                    if (MainBoard.showColorDebug)
                    {
                        MainBoard.WriteLine($"{Name} - Color Received: " + p.LatestColor + " last triggers was " + ((Environment.TickCount - p.LastColorTick) / 1000) + "s ago.");
                    }

                    if (Environment.TickCount - p.LastColorTick > p.DistanceColorCooldownMs)
                    {
                        OnColorTriggered(this, p, p.LatestColor);
                        OnDataUpdated();
                    }
                }

                // If we have an active sensor on, check for event trigger
                else if (p.Value != brick.Ports[type].RawValue && device != Port.Devices.UNKNOWN)
                {
                    p.LatestDistance = brick.Ports[type].RawValue;
                    somethingChanged = true;

                    if (MainBoard.showColorDebug)
                    {
                        MainBoard.WriteLine($"{Name} - Raw Value Received: " + brick.Ports[type].RawValue + " last triggers was " + ((Environment.TickCount - p.LastDistanceTick) / 1000) + "s ago.");
                    }

                    if (Environment.TickCount - p.LastDistanceTick > p.DistanceColorCooldownMs)
                    {
                        OnDistanceTriggered(this, p, p.LatestDistance);
                        OnDataUpdated();
                    }
                }
            }

            if (somethingChanged)
            {
                OnDataUpdated();
            }
        }