Esempio n. 1
0
        private void BbusbPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            buffer += bbusbPort.ReadExisting();

            if (buffer.StartsWith("."))
            {
                BeginInvoke(new UpdateUIDelegate(UpdateUI), BBStatus.CALIBRATING, buffer);
            }

            if (buffer.Contains("\r\n"))
            {
                string[] splitBuffer = buffer.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                string   value       = splitBuffer[0];

                BBStatus status = BBStatus.UNKNOWN;
                if (value.StartsWith("bbusb"))
                {
                    status = BBStatus.CONNECTED;
                }
                else if (value.StartsWith("."))
                {
                    status = BBStatus.CALIBRATING;
                }
                else if (value.StartsWith("c"))
                {
                    status = BBStatus.CALIBRATING;
                }
                else if (value.Contains(","))
                {
                    status = BBStatus.VALUES;
                }
                else
                {
                    status = BBStatus.UNKNOWN;
                }

                BeginInvoke(new UpdateUIDelegate(UpdateUI), status, value);

                if (splitBuffer.Length > 1)
                {
                    buffer = splitBuffer[1];
                }
                else
                {
                    buffer = "";
                }
            }
        }
Esempio n. 2
0
        void UpdateUI(BBStatus status, string data)
        {
            pbrCalibration.Visible = false;

            switch (status)
            {
            case BBStatus.UNKNOWN:
                lblDeviceStatus.Text      = "Unknown status";
                lblDeviceStatus.BackColor = Color.LightGray;
                break;

            case BBStatus.CONNECTED:
                lblDeviceStatus.Text      = "Connected";
                lblDeviceStatus.BackColor = Color.Green;
                break;

            case BBStatus.CALIBRATING:
                lblDeviceStatus.Text      = "Calibrating";
                lblDeviceStatus.BackColor = Color.Yellow;
                pbrCalibration.Value      = Math.Min(100, data.Length * 2);
                pbrCalibration.Visible    = true;
                break;

            case BBStatus.VALUES:
                lblDeviceStatus.Text      = "Receiving";
                lblDeviceStatus.BackColor = Color.White;

                int      right, left, center;
                string[] values = data.Split(',');

                if (values.Length >= 3)
                {
                    int.TryParse(values[0], out right);
                    int.TryParse(values[1], out left);
                    int.TryParse(values[2], out center);

                    if (left > pbrLeft.Maximum)
                    {
                        pbrLeft.Maximum = left;
                    }
                    if (right > pbrRight.Maximum)
                    {
                        pbrRight.Maximum = right;
                    }
                    pbrLeft.Value  = left;
                    pbrRight.Value = right;

                    lblLeft.Text   = left.ToString();
                    lblRight.Text  = right.ToString();
                    lblCenter.Text = center.ToString();

                    int.TryParse(txtTreshold.Text, out threshold);

                    if (center > threshold)
                    {
                        if (!isForwardDown)
                        {
                            InputManager.Keyboard.KeyDown(Keys.S);
                        }
                        isForwardDown = true;

                        if (isBackwardDown)
                        {
                            InputManager.Keyboard.KeyUp(Keys.Z);
                        }
                        isBackwardDown = false;
                    }
                    else if (center < -threshold)
                    {
                        if (isForwardDown)
                        {
                            InputManager.Keyboard.KeyUp(Keys.S);
                        }
                        isForwardDown = false;

                        if (!isBackwardDown)
                        {
                            InputManager.Keyboard.KeyDown(Keys.Z);
                        }
                        isBackwardDown = true;
                    }
                    else
                    {
                        if (isForwardDown)
                        {
                            InputManager.Keyboard.KeyUp(Keys.S);
                        }
                        isForwardDown = false;

                        if (isBackwardDown)
                        {
                            InputManager.Keyboard.KeyUp(Keys.Z);
                        }
                        isBackwardDown = false;
                    }
                }
                break;
            }
        }