public async Task InitializeHardware(IBotStateSettings settings, CancellationToken token)
        {
            Log("Detaching on startup.");
            await DetachController(token).ConfigureAwait(false);

            if (settings.ScreenOff)
            {
                Log("Turning off screen.");
                await SetScreen(ScreenState.Off, token).ConfigureAwait(false);
            }

            Log("Setting BDSP-specific hid waits.");
            await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.keySleepTime, 50), token).ConfigureAwait(false);

            await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.pollRate, 50), token).ConfigureAwait(false);
        }
Exemple #2
0
        public async Task <bool> SpinUntilChangedSurprise(int waitms, CancellationToken token)
        {
            const int fastSleep    = 10;     // between commands
            const int defaultSleep = 50;     // original
            const int delay        = 100;    // per stick command
            const int m            = 25_000; // magnitude of stick movement
            var       sw           = new Stopwatch();

            bool changed   = false;
            var  searching = await Connection.ReadBytesAsync(SurpriseTradeSearchOffset, 4, token).ConfigureAwait(false);

            await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, fastSleep), token).ConfigureAwait(false);

            sw.Start();
            do
            {
                // Spin the Left Stick in a circle counter-clockwise, starting from 0deg (polar) in increments of 90deg.
                if (!await SpinCircle().ConfigureAwait(false))
                {
                    continue;
                }
                changed = true;
                break;
            } while (sw.ElapsedMilliseconds < waitms);

            async Task <bool> SpinCircle()
            {
                return(await Step(m, 0).ConfigureAwait(false) || // →
                       await Step(0, m).ConfigureAwait(false) || // ↑
                       await Step(-m, 0).ConfigureAwait(false) || // ←
                       await Step(0, -m).ConfigureAwait(false));  // ↓

                async Task <bool> Step(short x, short y)
                {
                    var now = sw.ElapsedMilliseconds;

                    await SetStick(SwitchStick.LEFT, x, y, 2 *fastSleep, token).ConfigureAwait(false);

                    if (await ReadIsChanged(SurpriseTradeSearchOffset, searching, token).ConfigureAwait(false))
                    {
                        return(true);
                    }

                    // wait the rest of this step's delay
                    var wait = delay - (sw.ElapsedMilliseconds - now);

                    if (wait > 0)
                    {
                        await Task.Delay((int)wait, token).ConfigureAwait(false);
                    }
                    return(false);
                }
            }

            // Gracefully clean up
            await Connection.SendAsync(SwitchCommand.ResetStick(SwitchStick.LEFT), token).ConfigureAwait(false);

            await Task.Delay(fastSleep, token).ConfigureAwait(false);

            await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, defaultSleep), token).ConfigureAwait(false);

            await Task.Delay(defaultSleep, token).ConfigureAwait(false);

            return(changed);
        }