public CalibrationForm(JSFNComHandler handler) { //set the handler this.handler = handler; // start the ui InitializeComponent(); // variable for holding current direction int direction = 0; // variable for stopping loop again bool going = true; // start another thread Task.Factory.StartNew(async() => { //just keep going while (going) { //turn the catcher to whatever is in the direction variable (this is not positon, but direction) await handler.SendFunction("catcherTurn", new object[] { direction * 10, 120, 50 }); Thread.Sleep(10); } }); //if the button is pressed down, set direction to 1 turnLeftButton.MouseDown += (object sender, MouseEventArgs e) => { if (e.Button == MouseButtons.Left) { direction = 1; } }; //if left button is released again, set it back to 0 turnLeftButton.MouseUp += (object sender, MouseEventArgs e) => { if (e.Button == MouseButtons.Left) { direction = 0; } }; //if right button is pressed down, set it to -1 turnRightButton.MouseDown += (object sender, MouseEventArgs e) => { if (e.Button == MouseButtons.Left) { direction = -1; } }; //if right button is released again, set it back to 0 turnRightButton.MouseUp += (object sender, MouseEventArgs e) => { if (e.Button == MouseButtons.Left) { direction = 0; } }; //when we're done, we set going to false to stop the loop and close the whole thing doneButton.Click += (object sender, EventArgs e) => { going = false; this.Close(); }; }
// constructor public ResorterStateHandler(string port, int steps) { // create the functions that the arduino has available Dictionary <string, Func <List <object>, object> > jsfnFuncs = new Dictionary <string, Func <List <object>, object> >(); // add a print function that the arduino can use jsfnFuncs.Add("print", new Func <List <object>, object>((List <object> l) => { Console.WriteLine(string.Join(",", l.ToArray())); return(null); })); // create the com handler ComHandler = new JSFNComHandler(port, jsfnFuncs); // set the steps this.steps = steps; }
// create the read thread with the com handler as the argument private static Thread CreateReadThread(JSFNComHandler jsfnHandler) { // create a new thread, and return it return(new Thread(() => { // the new thread will loop until jsfnHandler.continueReadThread is changed to false while (jsfnHandler.continueReadThread) { // continue to read data received by the serial port, output the data when hitting an ";" string readData = jsfnHandler.serialPort.ReadTo(";"); // call the OnDataRecived function on jsfnHandler with the data as the argument jsfnHandler.OnDataRecived(null, readData.Trim()); } ; })); }