Esempio n. 1
0
        public static void Main()
        {
            _mpu        = Mpu6050.StartNew(2);
            _mpuArgList = new ArrayList()
            {
                "", "", ""
            };
            _led  = new PinController.LedBlinker(2);
            _vibe = new PinController.LedBlinker(13);

            Debug.WriteLine("Init");
            _led.On();
            Thread.Sleep(100);
            _led.Off();


            var AppID         = "DDRTwistNShout";
            var serverUdpPort = 7348;
            var nanoConfig    = new ArdNetClientConfig(AppID, null, serverUdpPort);

            nanoConfig.TCP.HeartbeatConfig.HeartbeatInterval    = TimeSpan.FromMilliseconds(2000);
            nanoConfig.TCP.HeartbeatConfig.ForceStrictHeartbeat = true;
            nanoConfig.TCP.HeartbeatConfig.RespondToHeartbeats  = false;

            _ardManager = new ArdNetClientManager(SystemConfig.WiFiCredentials, nanoConfig);
            _ardManager.TcpEndpointConnected    += ArdManager_TcpEndpointConnected;
            _ardManager.TcpEndpointDisconnected += NanoClient_ServerDisconnected;
            _ardManager.StartWorkerThread();
            _ardManager.CommandReceived += ArdManager_CommandReceived;

            MpuValue data;
            var      spinCmd       = TcpRequest.CreateOutbound("SPIN", _mpuArgList);
            DateTime spinStartTime = DateTime.MinValue;

            while (true)
            {
                data = _mpu.GetData();
                //gyro data
                //x, y, z
                //_mpuArgList[0] = data.GyroX.ToString();
                //_mpuArgList[1] = data.GyroY.ToString();
                //_mpuArgList[2] = data.GyroZ.ToString();
                //Debug.WriteLine($"[{data.GyroX} {data.GyroY} {data.GyroZ}]");

                if (abs(data.GyroY) > 15000 && spinStartTime.Equals(DateTime.MinValue))
                {
                    spinStartTime = DateTime.UtcNow;
                }
                else if (abs(data.GyroY) < 5000 && !spinStartTime.Equals(DateTime.MinValue))
                {
                    var spinTime = (DateTime.UtcNow - spinStartTime).TotalMilliseconds;
                    Debug.WriteLine($"SPIN: {spinTime}");
                    spinStartTime = DateTime.MinValue;

                    if (spinTime > 800)
                    {
                        _ardManager.EnqueueTask(x => x.SendCommand(spinCmd));
                    }
                }


                Thread.Sleep(10);
            }
        }
Esempio n. 2
0
        public static void Main()
        {
#if platform_test && !platform_beetle
            Configuration.SetPinFunction(23, DeviceFunction.I2C1_DATA);
            Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK);
            _mpu = Mpu6050.StartNew(1);
#elif !platform_test && platform_beetle
            _mpu = Mpu6050.StartNew(2);
#endif
            _mpuArgList = new ArrayList()
            {
                "", "", ""
            };
            _led = new PinController.LedBlinker(2);
            _touchController.Init();
            _palmTouch = _touchController.OpenPin(27, new TouchPadConfig()
            {
                PinSelectMode = TouchPinSelectMode.GpioIndex
            });
            _triggerTouch = _touchController.OpenPin(13, new TouchPadConfig()
            {
                PinSelectMode = TouchPinSelectMode.GpioIndex
            });


            Debug.WriteLine("Init");
            _led.On();
            Thread.Sleep(100);
            _led.Off();


            var AppID         = "Esp32.HairTrigger";
            var serverUdpPort = 48597;
            var nanoConfig    = new ArdNetClientConfig(AppID, null, serverUdpPort);
            nanoConfig.TCP.HeartbeatConfig.HeartbeatInterval    = TimeSpan.FromMilliseconds(2000);
            nanoConfig.TCP.HeartbeatConfig.ForceStrictHeartbeat = true;
            nanoConfig.TCP.HeartbeatConfig.RespondToHeartbeats  = false;

            _ardManager = new ArdNetClientManager(SystemConfig.WiFiCredentials, nanoConfig);
            _ardManager.TcpEndpointConnected    += ArdManager_TcpEndpointConnected;
            _ardManager.TcpEndpointDisconnected += NanoClient_ServerDisconnected;
            _ardManager.StartWorkerThread();

            Debug.WriteLine("Loop");

            MpuValue data;
            DateTime now;
            while (true)
            {
                _   = _serverMsgHandle.WaitOne();
                now = DateTime.UtcNow;

                if (!_palmTouch.IsTouched())
                {
                    Thread.Sleep(10);
                    continue;
                }

                if ((now - _mpuLastSend).TotalMilliseconds > 75)
                {
                    data = _mpu.GetData();
                    //gyro data
                    //x, y, z
                    _mpuArgList[0] = data.GyroX.ToString();
                    _mpuArgList[1] = data.GyroY.ToString();
                    _mpuArgList[2] = data.GyroZ.ToString();

                    var request = TcpRequest.CreateOutbound("MPU", _mpuArgList);
                    _ardManager.EnqueueTask(x =>
                    {
                        _ = x.SendCommand(request);
                    });
                    _mpuLastSend = now;
                }

                now = DateTime.UtcNow;
                if (!_triggerIsDown && (now - _triggerLastSend).TotalMilliseconds > 50)
                {
                    if (_triggerTouch.IsTouched())
                    {
                        _ardManager.EnqueueTask(x => x.SendCommand(_triggerTcpRequest_Down));
                        _triggerIsDown   = true;
                        _triggerLastSend = now;
                    }
                }
                else if (_triggerIsDown && (now - _triggerLastSend).TotalMilliseconds > 10)
                {
                    if (!_triggerTouch.IsTouched())
                    {
                        _ardManager.EnqueueTask(x => x.SendCommand(_triggerTcpRequest_Up));
                        _triggerIsDown   = false;
                        _triggerLastSend = now;
                    }
                }
            }
        }