using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SSCEx1
{
	public partial class Form1 : Form
	{
		SerialPort sp;
		int[] PWMs = new int[] {1500, 1500};

		public Form1()
		{
			InitializeComponent();
			sp = new SerialPort("COM1", 115200);
			sp.Open();
			SendCommand();
		}

		// forward
		private void btnForward_Click(object sender, EventArgs e)
		{
			PWMs[0] -= 50;
			PWMs[1] += 50;
			SendCommand();
		}

		// stop
		private void btnStop_Click(object sender, EventArgs e)
		{
			Reset();
		}

		// left
		private void btnLeft_Click(object sender, EventArgs e)
		{
			Reset();
			PWMs[0] -= 50;
			PWMs[1] -= 50;
			SendCommand();
		}

		// right
		private void btnRight_Click(object sender, EventArgs e)
		{
			PWMs[0] += 50;
			PWMs[1] += 50;
			SendCommand();
		}

		// back
		private void btnBack_Click(object sender, EventArgs e)
		{
			PWMs[0] += 50;
			PWMs[1] -= 50;
			SendCommand();
		}

		private void Reset()
		{
			PWMs[0] = 1500;
			PWMs[1] = 1500;
			SendCommand();
		}

		private void SendCommand()
		{
			sp.Write("#0 P" + PWMs[0] + " #1 P" + PWMs[1] + " \r");
		}
	}
}