예제 #1
0
        // Use the "most recently set" haptic state for each of light bar/motor.
        private void setHapticState()
        {
            DS4Color lightBarColor = LightBarColor;
            byte     lightBarFlashDurationOn = LightBarOnDuration, lightBarFlashDurationOff = LightBarOffDuration;
            byte     rumbleMotorStrengthLeftHeavySlow = LeftHeavySlowRumble, rumbleMotorStrengthRightLightFast = rightLightFastRumble;
            int      hapticLen = hapticState.Length;

            for (int i = 0; i < hapticLen; i++)
            {
                DS4HapticState haptic = hapticState[i];
                if (i == hapticStackIndex)
                {
                    break; // rest haven't been used this time
                }
                if (haptic.IsLightBarSet())
                {
                    lightBarColor            = haptic.LightBarColor;
                    lightBarFlashDurationOn  = haptic.LightBarFlashDurationOn;
                    lightBarFlashDurationOff = haptic.LightBarFlashDurationOff;
                }

                if (haptic.IsRumbleSet())
                {
                    rumbleMotorStrengthLeftHeavySlow  = haptic.RumbleMotorStrengthLeftHeavySlow;
                    rumbleMotorStrengthRightLightFast = haptic.RumbleMotorStrengthRightLightFast;
                }
            }

            LightBarColor        = lightBarColor;
            LightBarOnDuration   = lightBarFlashDurationOn;
            LightBarOffDuration  = lightBarFlashDurationOff;
            LeftHeavySlowRumble  = rumbleMotorStrengthLeftHeavySlow;
            RightLightFastRumble = rumbleMotorStrengthRightLightFast;
        }
예제 #2
0
 public void pushHapticState(DS4HapticState hs)
 {
     if (hapticStackIndex == hapticState.Length)
     {
         DS4HapticState[] newHaptics = new DS4HapticState[hapticState.Length + 1];
         Array.Copy(hapticState, newHaptics, hapticState.Length);
         hapticState = newHaptics;
     }
     hapticState[hapticStackIndex++] = hs;
 }
예제 #3
0
        public static void updateLightBar(DS4Device device, int deviceNum, DS4State cState, DS4StateExposed eState, Mouse tp)
        {
            DS4Color color;

            if (!defualtLight && !forcelight[deviceNum])
            {
                if (UseCustomLed[deviceNum])
                {
                    if (LedAsBatteryIndicator[deviceNum])
                    {
                        DS4Color fullColor = CustomColor[deviceNum];
                        DS4Color lowColor  = LowColor[deviceNum];

                        color = getTransitionedColor(lowColor, fullColor, device.Battery);
                    }
                    else
                    {
                        color = CustomColor[deviceNum];
                    }
                }
                else
                {
                    if (Rainbow[deviceNum] > 0)
                    {                                                      // Display rainbow
                        DateTime now = DateTime.UtcNow;
                        if (now >= oldnow + TimeSpan.FromMilliseconds(10)) //update by the millisecond that way it's a smooth transtion
                        {
                            oldnow = now;
                            if (device.Charging)
                            {
                                counters[deviceNum] -= 1.5 * 3 / Rainbow[deviceNum];
                            }
                            else
                            {
                                counters[deviceNum] += 1.5 * 3 / Rainbow[deviceNum];
                            }
                        }
                        if (counters[deviceNum] < 0)
                        {
                            counters[deviceNum] = 180000;
                        }
                        if (counters[deviceNum] > 180000)
                        {
                            counters[deviceNum] = 0;
                        }
                        if (LedAsBatteryIndicator[deviceNum])
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, (byte)(2.55 * device.Battery));
                        }
                        else
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        }
                    }
                    else if (LedAsBatteryIndicator[deviceNum])
                    {
                        //if (device.Charging == false || device.Battery >= 100) // when charged, don't show the charging animation
                        {
                            DS4Color fullColor = MainColor[deviceNum];
                            DS4Color lowColor  = LowColor[deviceNum];

                            color = getTransitionedColor(lowColor, fullColor, (uint)device.Battery);
                        }
                    }
                    else
                    {
                        color = MainColor[deviceNum];
                    }
                }

                if (device.Battery <= FlashAt[deviceNum] && !defualtLight && !device.Charging)
                {
                    if (!(FlashColor[deviceNum].red == 0 &&
                          FlashColor[deviceNum].green == 0 &&
                          FlashColor[deviceNum].blue == 0))
                    {
                        color = FlashColor[deviceNum];
                    }
                    if (FlashType[deviceNum] == 1)
                    {
                        if (fadetimer[deviceNum] <= 0)
                        {
                            fadedirection[deviceNum] = true;
                        }
                        else if (fadetimer[deviceNum] >= 100)
                        {
                            fadedirection[deviceNum] = false;
                        }
                        if (fadedirection[deviceNum])
                        {
                            fadetimer[deviceNum] += 1;
                        }
                        else
                        {
                            fadetimer[deviceNum] -= 1;
                        }
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), fadetimer[deviceNum]);
                    }
                }

                if (IdleDisconnectTimeout[deviceNum] > 0 && LedAsBatteryIndicator[deviceNum] && (!device.Charging || device.Battery >= 100))
                {//Fade lightbar by idle time
                    TimeSpan timeratio = new TimeSpan(DateTime.UtcNow.Ticks - device.lastActive.Ticks);
                    double   botratio  = timeratio.TotalMilliseconds;
                    double   topratio  = TimeSpan.FromSeconds(IdleDisconnectTimeout[deviceNum]).TotalMilliseconds;
                    double   ratio     = ((botratio / topratio) * 100);
                    if (ratio >= 50 && ratio <= 100)
                    {
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), (uint)((ratio - 50) * 2));
                    }
                    else if (ratio >= 100)
                    {
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), 100);
                    }
                }
                if (device.Charging && device.Battery < 100)
                {
                    switch (ChargingType[deviceNum])
                    {
                    case 1:
                        if (fadetimer[deviceNum] <= 0)
                        {
                            fadedirection[deviceNum] = true;
                        }
                        else if (fadetimer[deviceNum] >= 105)
                        {
                            fadedirection[deviceNum] = false;
                        }
                        if (fadedirection[deviceNum])
                        {
                            fadetimer[deviceNum] += .1;
                        }
                        else
                        {
                            fadetimer[deviceNum] -= .1;
                        }
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), fadetimer[deviceNum]);
                        break;

                    case 2:
                        counters[deviceNum] += .167;
                        color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        break;

                    case 3:
                        color = ChargingColor[deviceNum];
                        break;

                    default:
                        break;
                    }
                }
            }
            else if (forcelight[deviceNum])
            {
                color = forcedColor[deviceNum];
            }
            else if (shuttingdown)
            {
                color = new DS4Color(0, 0, 0);
            }
            else
            {
                if (device.ConnectionType == ConnectionType.BT)
                {
                    color = new DS4Color(32, 64, 64);
                }
                else
                {
                    color = new DS4Color(0, 0, 0);
                }
            }
            bool distanceprofile = (ProfilePath[deviceNum].ToLower().Contains("distance") || tempprofilename[deviceNum].ToLower().Contains("distance"));

            if (distanceprofile && !defualtLight)
            { //Thing I did for Distance
                float rumble = device.LeftHeavySlowRumble / 2.55f;
                byte  max    = Max(color.red, Max(color.green, color.blue));
                if (device.LeftHeavySlowRumble > 100)
                {
                    color = getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), rumble);
                }
                else
                {
                    color = getTransitionedColor(color, getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), 39.6078f), device.LeftHeavySlowRumble);
                }
            }
            DS4HapticState haptics = new DS4HapticState
            {
                LightBarColor = color
            };

            if (haptics.IsLightBarSet())
            {
                if (forcelight[deviceNum] && forcedFlash[deviceNum] > 0)
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)(25 - forcedFlash[deviceNum]);
                    haptics.LightBarExplicitlyOff    = true;
                }
                else if (device.Battery <= FlashAt[deviceNum] && FlashType[deviceNum] == 0 && !defualtLight && !device.Charging)
                {
                    int level = device.Battery / 10;
                    //if (level >= 10)
                    //level = 0; // all values of ~0% or >~100% are rendered the same
                    haptics.LightBarFlashDurationOn  = BatteryIndicatorDurations[level, 0];
                    haptics.LightBarFlashDurationOff = BatteryIndicatorDurations[level, 1];
                }
                else if (distanceprofile && device.LeftHeavySlowRumble > 155) //also part of Distance
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)((-device.LeftHeavySlowRumble + 265));
                    haptics.LightBarExplicitlyOff    = true;
                }
                else
                {
                    //haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 0;
                    haptics.LightBarExplicitlyOff    = true;
                }
            }
            else
            {
                haptics.LightBarExplicitlyOff = true;
            }
            if (device.LightBarOnDuration != haptics.LightBarFlashDurationOn && device.LightBarOnDuration != 1 && haptics.LightBarFlashDurationOn == 0)
            {
                haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
            }
            if (device.LightBarOnDuration == 1) //helps better reset the color
            {
                System.Threading.Thread.Sleep(5);
            }
            device.pushHapticState(haptics);
        }
예제 #4
0
        public static void updateLightBar(DS4Device device, int deviceNum)
        {
            DS4Color color;

            if (!defaultLight && !forcelight[deviceNum])
            {
                if (getUseCustomLed(deviceNum))
                {
                    if (getLedAsBatteryIndicator(deviceNum))
                    {
                        DS4Color fullColor = getCustomColor(deviceNum);
                        DS4Color lowColor  = getLowColor(deviceNum);
                        color = getTransitionedColor(lowColor, fullColor, device.getBattery());
                    }
                    else
                    {
                        color = getCustomColor(deviceNum);
                    }
                }
                else
                {
                    double rainbow = getRainbow(deviceNum);
                    if (rainbow > 0)
                    {
                        // Display rainbow
                        DateTime now = DateTime.UtcNow;
                        if (now >= oldnow[deviceNum] + TimeSpan.FromMilliseconds(10)) //update by the millisecond that way it's a smooth transtion
                        {
                            oldnow[deviceNum] = now;
                            if (device.isCharging())
                            {
                                counters[deviceNum] -= 1.5 * 3 / rainbow;
                            }
                            else
                            {
                                counters[deviceNum] += 1.5 * 3 / rainbow;
                            }
                        }

                        if (counters[deviceNum] < 0)
                        {
                            counters[deviceNum] = 180000;
                        }
                        else if (counters[deviceNum] > 180000)
                        {
                            counters[deviceNum] = 0;
                        }

                        if (getLedAsBatteryIndicator(deviceNum))
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, (byte)(device.getBattery() * 2.55));
                        }
                        else
                        {
                            color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        }
                    }
                    else if (getLedAsBatteryIndicator(deviceNum))
                    {
                        DS4Color fullColor = getMainColor(deviceNum);
                        DS4Color lowColor  = getLowColor(deviceNum);
                        color = getTransitionedColor(lowColor, fullColor, device.getBattery());
                    }
                    else
                    {
                        color = getMainColor(deviceNum);
                    }
                }

                if (device.getBattery() <= getFlashAt(deviceNum) && !defaultLight && !device.isCharging())
                {
                    DS4Color flashColor = getFlashColor(deviceNum);
                    if (!(flashColor.red == 0 &&
                          flashColor.green == 0 &&
                          flashColor.blue == 0))
                    {
                        color = flashColor;
                    }

                    if (getFlashType(deviceNum) == 1)
                    {
                        double ratio = 0.0;

                        if (!fadewatches[deviceNum].IsRunning)
                        {
                            bool temp = fadedirection[deviceNum];
                            fadedirection[deviceNum] = !temp;
                            fadewatches[deviceNum].Restart();
                            ratio = temp ? 100.0 : 0.0;
                        }
                        else
                        {
                            long elapsed = fadewatches[deviceNum].ElapsedMilliseconds;

                            if (fadedirection[deviceNum])
                            {
                                if (elapsed < PULSE_FLASH_DURATION)
                                {
                                    elapsed = elapsed / 40;
                                    ratio   = 100.0 * (elapsed / PULSE_FLASH_SEGMENTS);
                                }
                                else
                                {
                                    ratio = 100.0;
                                    fadewatches[deviceNum].Stop();
                                }
                            }
                            else
                            {
                                if (elapsed < PULSE_FLASH_DURATION)
                                {
                                    elapsed = elapsed / 40;
                                    ratio   = (0 - 100.0) * (elapsed / PULSE_FLASH_SEGMENTS) + 100.0;
                                }
                                else
                                {
                                    ratio = 0.0;
                                    fadewatches[deviceNum].Stop();
                                }
                            }
                        }

                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), ratio);
                    }
                }

                int idleDisconnectTimeout = getIdleDisconnectTimeout(deviceNum);
                if (idleDisconnectTimeout > 0 && getLedAsBatteryIndicator(deviceNum) &&
                    (!device.isCharging() || device.getBattery() >= 100))
                {
                    //Fade lightbar by idle time
                    TimeSpan timeratio = new TimeSpan(DateTime.UtcNow.Ticks - device.lastActive.Ticks);
                    double   botratio = timeratio.TotalMilliseconds;
                    double   topratio = TimeSpan.FromSeconds(idleDisconnectTimeout).TotalMilliseconds;
                    double   ratio = 100.0 * (botratio / topratio), elapsed = ratio;
                    if (ratio >= 50.0 && ratio < 100.0)
                    {
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0),
                                                     (uint)(-100.0 * (elapsed = 0.02 * (ratio - 50.0)) * (elapsed - 2.0)));
                    }
                    else if (ratio >= 100.0)
                    {
                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), 100.0);
                    }
                }

                if (device.isCharging() && device.getBattery() < 100)
                {
                    switch (getChargingType(deviceNum))
                    {
                    case 1:
                    {
                        double ratio = 0.0;

                        if (!fadewatches[deviceNum].IsRunning)
                        {
                            bool temp = fadedirection[deviceNum];
                            fadedirection[deviceNum] = !temp;
                            fadewatches[deviceNum].Restart();
                            ratio = temp ? 100.0 : 0.0;
                        }
                        else
                        {
                            long elapsed = fadewatches[deviceNum].ElapsedMilliseconds;

                            if (fadedirection[deviceNum])
                            {
                                if (elapsed < PULSE_CHARGING_DURATION)
                                {
                                    elapsed = elapsed / 40;
                                    ratio   = 100.0 * (elapsed / PULSE_CHARGING_SEGMENTS);
                                }
                                else
                                {
                                    ratio = 100.0;
                                    fadewatches[deviceNum].Stop();
                                }
                            }
                            else
                            {
                                if (elapsed < PULSE_CHARGING_DURATION)
                                {
                                    elapsed = elapsed / 40;
                                    ratio   = (0 - 100.0) * (elapsed / PULSE_CHARGING_SEGMENTS) + 100.0;
                                }
                                else
                                {
                                    ratio = 0.0;
                                    fadewatches[deviceNum].Stop();
                                }
                            }
                        }

                        color = getTransitionedColor(color, new DS4Color(0, 0, 0), ratio);
                        break;
                    }

                    case 2:
                    {
                        counters[deviceNum] += 0.167;
                        color = HuetoRGB((float)counters[deviceNum] % 360, 255);
                        break;
                    }

                    case 3:
                    {
                        color = getChargingColor(deviceNum);
                        break;
                    }

                    default: break;
                    }
                }
            }
            else if (forcelight[deviceNum])
            {
                color = forcedColor[deviceNum];
            }
            else if (shuttingdown)
            {
                color = new DS4Color(0, 0, 0);
            }
            else
            {
                if (device.getConnectionType() == ConnectionType.BT)
                {
                    color = new DS4Color(32, 64, 64);
                }
                else
                {
                    color = new DS4Color(0, 0, 0);
                }
            }

            bool distanceprofile = DistanceProfiles[deviceNum] || tempprofileDistance[deviceNum];

            //distanceprofile = (ProfilePath[deviceNum].ToLower().Contains("distance") || tempprofilename[deviceNum].ToLower().Contains("distance"));
            if (distanceprofile && !defaultLight)
            {
                // Thing I did for Distance
                float rumble = device.getLeftHeavySlowRumble() / 2.55f;
                byte  max    = Max(color.red, Max(color.green, color.blue));
                if (device.getLeftHeavySlowRumble() > 100)
                {
                    color = getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), rumble);
                }
                else
                {
                    color = getTransitionedColor(color, getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), 39.6078f), device.getLeftHeavySlowRumble());
                }
            }

            DS4HapticState haptics = new DS4HapticState
            {
                LightBarColor = color
            };

            if (haptics.IsLightBarSet())
            {
                if (forcelight[deviceNum] && forcedFlash[deviceNum] > 0)
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)(25 - forcedFlash[deviceNum]);
                    haptics.LightBarExplicitlyOff    = true;
                }
                else if (device.getBattery() <= getFlashAt(deviceNum) && getFlashType(deviceNum) == 0 && !defaultLight && !device.isCharging())
                {
                    int level = device.getBattery() / 10;
                    if (level >= 10)
                    {
                        level = 0; // all values of ~0% or >~100% are rendered the same
                    }
                    haptics.LightBarFlashDurationOn  = BatteryIndicatorDurations[level, 0];
                    haptics.LightBarFlashDurationOff = BatteryIndicatorDurations[level, 1];
                }
                else if (distanceprofile && device.getLeftHeavySlowRumble() > 155) //also part of Distance
                {
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)((-device.getLeftHeavySlowRumble() + 265));
                    haptics.LightBarExplicitlyOff    = true;
                }
                else
                {
                    //haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
                    haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 0;
                    haptics.LightBarExplicitlyOff    = true;
                }
            }
            else
            {
                haptics.LightBarExplicitlyOff = true;
            }

            byte tempLightBarOnDuration = device.getLightBarOnDuration();

            if (tempLightBarOnDuration != haptics.LightBarFlashDurationOn && tempLightBarOnDuration != 1 && haptics.LightBarFlashDurationOn == 0)
            {
                haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1;
            }

            device.pushHapticState(ref haptics);
        }
예제 #5
0
 public void LightBarOff()
 {
     if (Controller != null)
     {
         DS4HapticState hapState = new DS4HapticState()
         {
             LightBarExplicitlyOff = true
         };
         Controller.pushHapticState(hapState);
     }
 }
예제 #6
0
 public void pushHapticState(DS4HapticState hs)
 {
     if (hapticStackIndex == hapticState.Length)
     {
         DS4HapticState[] newHaptics = new DS4HapticState[hapticState.Length + 1];
         Array.Copy(hapticState, newHaptics, hapticState.Length);
         hapticState = newHaptics;
     }
     hapticState[hapticStackIndex++] = hs;
 }