Exemple #1
0
        //Implement Override Functions

        protected override void DidNotifyByReceiveDataCharacteristic(byte[] data)
        {
            switch (data[0])
            {
            case (byte)MipRobotConstants.COMMAND_CODE.GET_GAME_MDOE:
                if (data.Length == 2)
                {
                    GameMode = data[1];

                    MipGameModeHandler?.Invoke(this, GameMode);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_STATUS:
                if (data.Length == 3)
                {
                    BatteryLevel = data[1];
                    Position     = BaseService.ConvertEnumFromInt <MipRobotConstants.POSITION_VALUE>(data[2]);

                    MipBatteryLevelHandler?.Invoke(this, BatteryLevel);
                    MipPositionHandler?.Invoke(this, Position);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_HARDWARE_VERSION:
                if (data.Length == 3)
                {
                    for (byte i = 0; i < MipRobotConstants.VOICE_FIRWARE_MAPPING.Length; i++)
                    {
                        if (data[1] > MipRobotConstants.VOICE_FIRWARE_MAPPING[i])
                        {
                            VoiceFirmwareVersion = i;
                        }
                    }
                    HardwareVersion = data[2];

                    MipVoiceFirmwareVersionHandler?.Invoke(this, VoiceFirmwareVersion);
                    MipHardwareVersionHandler?.Invoke(this, HardwareVersion);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_SOFTWARE_VERSION:
                if (data.Length == 5)
                {
                    FirmwareVersionDate = new DateTime(2000 + data[1], data[2], data[3]);
                    FirmwareVersionId   = data[4];

                    MipFirmwareVersionDateHandler?.Invoke(this, FirmwareVersionDate);
                    MipFirmwareVersionIdHandler?.Invoke(this, FirmwareVersionId);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_VOLUME_LEVEL:
                if (data.Length == 2)
                {
                    VolumeLevel = data[1];

                    MipVolumeLevelHandler?.Invoke(this, VolumeLevel);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_TOY_ACTIVATED_STATUS:
                if (data.Length == 2)
                {
                    ToyActivationStatus = BaseService.ConvertEnumFromInt <BluetoothRobotConstants.ACTIVIATION_STATUS>(data[1]);

                    if (ToyActivationStatus.HasFlag(BluetoothRobotConstants.ACTIVIATION_STATUS.ACTIVATE))
                    {
                        Task.Run(async() =>
                        {
                            await ActivateMipProduct();
                        });
                    }

                    MipToyActivationStatusHandler?.Invoke(this, ToyActivationStatus);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.RECEIVE_IR_COMMAND:
                //TODO callback
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_WEIGHT_LEVEL:
                if (data.Length == 2)
                {
                    WeightLevel    = data[1];
                    LeaningForward = WeightLevel < 0xd3;

                    MipWeightLevelHandler?.Invoke(this, WeightLevel);
                    MipLeaningForwardHandler?.Invoke(this, LeaningForward);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.CHECK_BOOT_MODE:
                if (data.Length == 2)
                {
                    BootMode = BaseService.ConvertEnumFromInt <MipRobotConstants.PING_RESPONSE>(data[1]);

                    MipBootModeHandler?.Invoke(this, BootMode);
                }
                else if (data.Length == 1)
                {
                    BootMode = MipRobotConstants.PING_RESPONSE.NORMAL_ROM_NO_BOOT_LOADER;

                    MipBootModeHandler?.Invoke(this, BootMode);
                }
                break;
            }
        }
Exemple #2
0
        //Implement Override Functions

        protected override void DidNotifyByReceiveDataCharacteristic(byte[] data)
        {
            switch (data[0])
            {
            case (byte)MipRobotConstants.COMMAND_CODE.GET_GAME_MODE:
                if (data.Length == 2)
                {
                    GameMode = BaseService.ConvertEnumFromInt <MipRobotConstants.GAME_MODE>(data[1]);

                    MipGameModeHandler?.Invoke(this, GameMode);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_STATUS:
                if (data.Length == 3)
                {
                    // 0x4D(4.0V) - 0x7C(6.4V)
                    BatteryLevel = ((data[1] - 0x4D) / (0x7C - 0x4D)) * 100;
                    Position     = BaseService.ConvertEnumFromInt <MipRobotConstants.POSITION_VALUE>(data[2]);

                    MipBatteryLevelHandler?.Invoke(this, BatteryLevel);
                    MipPositionHandler?.Invoke(this, Position);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_HARDWARE_VERSION:
                if (data.Length == 3)
                {
                    for (byte i = 0; i < MipRobotConstants.VOICE_FIRMWARE_MAPPING.Length; i++)
                    {
                        if (data[1] > MipRobotConstants.VOICE_FIRMWARE_MAPPING[i])
                        {
                            VoiceFirmwareVersion = i;
                        }
                    }
                    HardwareVersion = data[2];

                    MipVoiceFirmwareVersionHandler?.Invoke(this, VoiceFirmwareVersion);
                    MipHardwareVersionHandler?.Invoke(this, HardwareVersion);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_SOFTWARE_VERSION:
                if (data.Length == 5)
                {
                    FirmwareVersionDate = new DateTime(2000 + data[1], data[2], data[3]);
                    FirmwareVersionId   = data[4];

                    MipFirmwareVersionDateHandler?.Invoke(this, FirmwareVersionDate);
                    MipFirmwareVersionIdHandler?.Invoke(this, FirmwareVersionId);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_VOLUME_LEVEL:
                if (data.Length == 2)
                {
                    VolumeLevel = data[1];

                    MipVolumeLevelHandler?.Invoke(this, VolumeLevel);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_TOY_ACTIVATED_STATUS:
                if (data.Length == 2)
                {
                    ToyActivationStatus = BaseService.ConvertEnumFromInt <BluetoothRobotConstants.ACTIVATION_STATUS>(data[1]);

                    if (ToyActivationStatus.HasFlag(BluetoothRobotConstants.ACTIVATION_STATUS.ACTIVATE))
                    {
                        Task.Run(async() =>
                        {
                            await ActivateMipProduct();
                        });
                    }

                    MipToyActivationStatusHandler?.Invoke(this, ToyActivationStatus);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.RECEIVE_IR_COMMAND:
                Debug.WriteLine("TODO IR Commands");
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GET_WEIGHT_LEVEL:
                if (data.Length == 2)
                {
                    WeightLevel    = data[1];
                    LeaningForward = WeightLevel < 0xD3;

                    MipWeightLevelHandler?.Invoke(this, WeightLevel);
                    MipLeaningForwardHandler?.Invoke(this, LeaningForward);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.CHECK_BOOT_MODE:
                if (data.Length == 2)
                {
                    BootMode = BaseService.ConvertEnumFromInt <MipRobotConstants.PING_RESPONSE>(data[1]);

                    MipBootModeHandler?.Invoke(this, BootMode);
                }
                else if (data.Length == 1)
                {
                    BootMode = MipRobotConstants.PING_RESPONSE.NORMAL_ROM_NO_BOOT_LOADER;

                    MipBootModeHandler?.Invoke(this, BootMode);
                }
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.SET_HEAD_LED:
            {
                HeadLight1 = BaseService.ConvertEnumFromInt <MipRobotConstants.HEAD_LED>(data[1]);
                HeadLight2 = BaseService.ConvertEnumFromInt <MipRobotConstants.HEAD_LED>(data[2]);
                HeadLight3 = BaseService.ConvertEnumFromInt <MipRobotConstants.HEAD_LED>(data[3]);
                HeadLight4 = BaseService.ConvertEnumFromInt <MipRobotConstants.HEAD_LED>(data[4]);

                MipHeadLedHandler?.Invoke(this, HeadLight1, HeadLight2, HeadLight3, HeadLight4);
            }
            break;

            case (byte)MipRobotConstants.COMMAND_CODE.CLAPS_DETECTED:
                Debug.WriteLine("TODO claps");
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.GESTURE_DETECTED:
                MipGestureDetectedHandler?.Invoke(this, BaseService.ConvertEnumFromInt <MipRobotConstants.GESTURE>(data[1]));
                break;

            case (byte)MipRobotConstants.COMMAND_CODE.RADAR_RESPONSE:
                MipRadarResponseHandler?.Invoke(this, BaseService.ConvertEnumFromInt <MipRobotConstants.RADAR_RESPONSE>(data[1]));
                break;

            default:
                Debug.WriteLine("Unhandled receive data {0}", data[0]);
                break;
            }
        }