using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Phidgets.Events; using Phidgets; namespace InterfaceKit_simple { public partial class Form1 : Form { // Variables private double tempin = 0; double tempout = 0; double hum; int lightout; int lightin; double motion; string tempinL; string tempoutL; string humL; string fanstate = "OFF"; string lux; string luxin; public InterfaceKit ifKit = new InterfaceKit(); public TextLCD tLCD; System.Threading.Timer TimerItem; System.Threading.TimerCallback TimerDelegate; StateObjClass StateObj; bool AutomaticFan = false; bool autoLightIn = false; bool autoLightOut = false; int tempThresh = 25; //Ching and Tom Code AdvancedServo advServo = new AdvancedServo(); RFID rfid = new RFID(); int count = 0; int curPic; LSY_201 camera = new LSY_201(1); ImageConverter img = new ImageConverter(); bool takingpicture = false; string path; List photolist = new List(); //end ching and tom code //constructor public Form1() { InitializeComponent(); labelFanThreshold.Text = tempThresh.ToString(); } // method private void Form1_Load(object sender, EventArgs e) { // TEXT LCD INITS/HANDLERS // tLCD = new TextLCD(); tLCD.Attach += new AttachEventHandler(tLCD_Attach); tLCD.Error += new Phidgets.Events.ErrorEventHandler(tLCD_Error); tLCD.open(); if (!tLCD.Attached) { Console.WriteLine("Waiting for TextLCD to be attached...."); tLCD.waitForAttachment(); } Console.WriteLine("ok"); //Hook the basics event handlers ifKit.Attach += new AttachEventHandler(ifKit_Attach); ifKit.Detach += new DetachEventHandler(ifKit_Detach); ifKit.Error += new Phidgets.Events.ErrorEventHandler(ifKit_Error); //Hook the phidget spcific event handlers ifKit.InputChange += new InputChangeEventHandler(ifKit_InputChange); ifKit.OutputChange += new OutputChangeEventHandler(ifKit_OutputChange); ifKit.SensorChange += new SensorChangeEventHandler(ifKit_SensorChange); //Ching and Tom code advServo.Attach += new AttachEventHandler(advServo_Attach); advServo.Detach += new DetachEventHandler(advServo_Detach); advServo.Error += new Phidgets.Events.ErrorEventHandler(advServo_Error); advServo.PositionChange += new PositionChangeEventHandler (advServo_PositionChange); advServo.VelocityChange += new VelocityChangeEventHandler (advServo_VelocityChange); advServo.open(); rfid.Attach += new AttachEventHandler(rfid_Attach); rfid.Detach += new DetachEventHandler(rfid_Detach); rfid.Error += new Phidgets.Events.ErrorEventHandler(rfid_Error); rfid.Tag += new TagEventHandler(rfid_Tag); rfid.TagLost += new TagEventHandler(rfid_TagLost); rfid.open(); advServo.waitForAttachment(); rfid.waitForAttachment(); //end ching and tom code //Open the object for device connections ifKit.open(); //Wait for an InterfaceKit phidget to be attached Console.WriteLine("Waiting for InterfaceKit to be attached..."); ifKit.waitForAttachment(); Console.WriteLine("Press any key to end..."); Console.WriteLine("ok"); comboBoxFan.SelectedIndex = 1; comboBoxLightIn.SelectedIndex = 1; comboBoxLightOut.SelectedIndex = 1; comboBox1.SelectedIndex = 1; StateObj = new StateObjClass(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; TimerDelegate = new System.Threading.TimerCallback(TimerTask); TimerItem = new System.Threading.Timer(TimerDelegate,StateObj, 1000, 1000); //string lux; ifKit.outputs[7] = false; ifKit.outputs[6] = false; ifKit.outputs[4] = false; ifKit.outputs[3] = false; ifKit.outputs[2] = false; ifKit.outputs[1] = false; tLCD.Detach += new DetachEventHandler(tLCD_Detach); //Ching and tom code advServo.servos[0].Engaged = true; advServo.servos[4].Engaged = false; //end ching and tom code path = String.Copy(Application.ExecutablePath); path = path.Remove(path.LastIndexOf('\\')); //Console.WriteLine(path + "\\output " + count.ToString() + ".jpg"); string[] filePaths = Directory.GetFiles(path, "*.jpg", SearchOption.TopDirectoryOnly); count = filePaths.Length; curPic = count-1; for (int c1 = 0; c1 < count; c1++) { FileInfo fi = new FileInfo("output " + c1.ToString() + ".jpg"); photolist.Add(fi.LastWriteTime); } pictureBox1.ImageLocation = path + "\\output " + curPic.ToString() + ".jpg"; if (curPic >= 0) { label11.Text = photolist[curPic].ToString(); } } static void ifKit_Attach(object sender, AttachEventArgs e) { Console.WriteLine("InterfaceKit {0} attached!", e.Device.SerialNumber.ToString()); } static void ifKit_Detach(object sender, DetachEventArgs e) { Console.WriteLine("InterfaceKit {0} detached!", e.Device.SerialNumber.ToString()); } static void ifKit_Error(object sender, Phidgets.Events.ErrorEventArgs e) { Console.WriteLine(e.Description); } //Input Change event handler...Display the input index and the new value to the //console static void ifKit_InputChange(object sender, InputChangeEventArgs e) { Console.WriteLine("Input index {0} value (1)", e.Index, e.Value.ToString()); } //Output change event handler...Display the output index and the new valu to //the console static void ifKit_OutputChange(object sender, OutputChangeEventArgs e) { Console.WriteLine("Output index {0} value {0}", e.Index, e.Value.ToString()); } //Sensor Change event handler...Display the sensor index and it's new value to //the console static void ifKit_SensorChange(object sender, SensorChangeEventArgs e) { //Console.WriteLine("Sensor index {0} value {1}", e.Index, e.Value); } static void tLCD_Attach(object sender, AttachEventArgs e) { TextLCD attached = (TextLCD)sender; string name = attached.Name; string serialNo = attached.SerialNumber.ToString(); Console.WriteLine("TextLCD name:{0} serial No.: {1} Attached!", name, serialNo); } //Detach event handler, we'll output the name and serial of the phidget that is //detached static void tLCD_Detach(object sender, DetachEventArgs e) { TextLCD detached = (TextLCD)sender; string name = detached.Name; string serialNo = detached.SerialNumber.ToString(); Console.WriteLine("TextLCD name:{0} serial No.: {1} Detached!", name, serialNo); } //TextLCD error event handler, we'll just output any error data to the console static void tLCD_Error(object sender, Phidgets.Events.ErrorEventArgs e) { Console.WriteLine("LCD Error: e.Description"); } //Ching and tom code static void rfid_Attach(object sender, AttachEventArgs e) { Console.WriteLine("RFIDReader {0} attached!", e.Device.SerialNumber.ToString()); } //detach event handler...display the serial number of the detached RFID phidget static void rfid_Detach(object sender, DetachEventArgs e) { Console.WriteLine("RFID reader {0} detached!", e.Device.SerialNumber.ToString()); } //Error event handler...display the error description string static void rfid_Error(object sender, Phidgets.Events.ErrorEventArgs e) { Console.WriteLine(e.Description); } //Print the tag code of the scanned tag static void rfid_Tag(object sender, TagEventArgs e) { //Console.WriteLine("Tag {0} scanned", e.Tag); } //print the tag code for the tag that was just lost static void rfid_TagLost(object sender, TagEventArgs e) { //Console.WriteLine("Tag {0} lost", e.Tag); } static void advServo_Attach(object sender, AttachEventArgs e) { Console.WriteLine("AdvancedServo {0} attached!", e.Device.SerialNumber.ToString()); AdvancedServo attached = (AdvancedServo)sender; for (int i = 0; i < attached.servos.Count; i++) { attached.servos[i].Position = 160.00; //range is -23 to 232 attached.servos[i].VelocityLimit = 1500.00; //max 1500, min 0 attached.servos[i].Acceleration = 4590; //max 4590, min 0 } } //Detach event handler...Display the serial number of the detached servo device static void advServo_Detach(object sender, DetachEventArgs e) { Console.WriteLine("AdvancedServo {0} detached!", e.Device.SerialNumber.ToString()); } //Error event handler....Display the error description to the console static void advServo_Error(object sender, Phidgets.Events.ErrorEventArgs e) { Console.WriteLine("AdvancedServo Error: {0}", e.Description); } //Position CHange event handler...display which motor changed position and its //new position value to the console static void advServo_PositionChange(object sender, PositionChangeEventArgs e) { //Console.WriteLine("Motor #{0} - Actual Position: {1}", // e.Index, e.Position.ToString()); } //Velocity CHange event handler....display which motor's velocity is changing and //its new value to the console static void advServo_VelocityChange(object sender, VelocityChangeEventArgs e) { //Console.WriteLine("Motor #{0} - Actual Velocity: {1}", e.Index, // e.Velocity.ToString()); } //end of ching and tom code static void getLux(InterfaceKit myKit) { int mylightout = myKit.sensors[1].Value; if (mylightout < 50) { myKit.outputs[6] = true; myKit.outputs[4] = true; } else { myKit.outputs[6] = false; myKit.outputs[4] = false; } } static void getMotion(InterfaceKit myKit) { int myMotion = myKit.sensors[7].Value; if (myMotion < 400 || myMotion > 600 || myKit.outputs[3] == true) { myKit.outputs[3] = true; myKit.outputs[2] = true; myKit.outputs[1] = true; } else { myKit.outputs[3] = false; myKit.outputs[2] = false; myKit.outputs[1] = false; } } private void TimerTask(object StateObj) { lightout = ifKit.sensors[1].Value; tempout = ifKit.sensors[0].Value; hum = ifKit.sensors[2].Value; tempin = ifKit.sensors[3].Value; tempout = (tempout * 0.2222) - 61.11; tempin = (tempin * 0.2222) - 61.11; hum = (hum * .1906) - 40.2; tempout = Math.Round(tempout, 2); tempin = Math.Round(tempin, 2); hum = Math.Round(hum, 2); tempinL = tempin.ToString(); tempoutL = tempout.ToString(); humL = hum.ToString(); lux = lightout.ToString(); textBoxTempOut.Text = tempout + " °C"; currentTempInBox.Text = tempinL + " °C"; textBoxHum.Text = humL + " %"; //Ching and tom code if (rfid.TagPresent && rfid.LastTag.CompareTo("01023c1f76") == 0) { advServo.servos[0].Position = 107; comboBox1.SelectedIndex = 1; } else { advServo.servos[0].Position = 160; } if ((ifKit.sensors[7].Value > 600 || ifKit.sensors[7].Value < 400) && takingpicture == false && comboBox1.SelectedIndex.Equals(0)) { advServo.servos[4].Engaged = true; takingpicture = true; //speakerThread.Start(); //while (speakerThread.IsAlive) ; //tLCD.rows[0].DisplayString = "Motion detected"; Console.WriteLine("Taking Picture {0} ", count); List jpeg = camera.takePicture(); Image jpegimage = img.byteArrayToImage(jpeg.ToArray()); jpegimage.Save("output " + count.ToString() + ".jpg"); FileInfo fi = new FileInfo("output " + count.ToString() + ".jpg"); photolist.Add(fi.LastWriteTime); //speakerObject.RequestStop(); //speakerThread.Join(); advServo.servos[4].Engaged = false; takingpicture = false; curPic = count; pictureBox1.ImageLocation = path + "\\output " + curPic.ToString() + ".jpg"; label11.Text = photolist[curPic].ToString(); count++; } //end of ching and tom code if (tempin > tempThresh) { textBoxTempOut.Text = tempout + " °C"; currentTempInBox.Text = tempinL + " °C"; textBoxHum.Text = humL + " %"; //Console.WriteLine("flag"); if (autoLightOut) { getLux(ifKit); } if (autoLightIn) { getMotion(ifKit); } if (AutomaticFan) { ifKit.outputs[7] = true; lightout = ifKit.sensors[1].Value; tempout = ifKit.sensors[0].Value; tempout = (tempout * 0.2222) - 61.11; tempout = Math.Round(tempout, 2); tempoutL = tempout.ToString(); tempin = ifKit.sensors[3].Value; tempin = (tempin * 0.2222) - 61.11; tempin = Math.Round(tempin, 2); tempinL = tempin.ToString(); humL = hum.ToString(); lux = lightout.ToString(); tLCD.rows[0].DisplayString = "TIn:" + tempinL + " " + "FAN:" + fanstate; tLCD.rows[1].DisplayString = "TOut:" + tempoutL + " " + "LUX:" + lux; fanstate = "ON"; } } else { if (AutomaticFan) { fanstate = "OFF"; ifKit.outputs[7] = false; } } tLCD.rows[0].DisplayString = "TIn:" + tempinL + " " + "FAN:" + fanstate; tLCD.rows[1].DisplayString = "TOut:" + tempoutL + " " + "LUX:" + lux; if (autoLightOut) { getLux(ifKit); } if (autoLightIn) { getMotion(ifKit); } } private void label1_Click(object sender, EventArgs e) { } private void comboBoxFan_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxFan.SelectedIndex) { case 0: AutomaticFan = false; ifKit.outputs[7] = true; fanstate = "ON"; break; case 1: AutomaticFan = false; ifKit.outputs[7] = false; fanstate = "OFF"; break; case 2: AutomaticFan = true; break; } } private void label2_Click(object sender, EventArgs e) { } private void trackBar1_Scroll(object sender, EventArgs e) { tempThresh = trackBarFanThres.Value; labelFanThreshold.Text = tempThresh.ToString(); } private void label3_Click(object sender, EventArgs e) { } private void textBoxHum_TextChanged(object sender, EventArgs e) { } private void labelHum_Click(object sender, EventArgs e) { } private void panel1_Paint(object sender, PaintEventArgs e) { } private void currentTempInBox_TextChanged(object sender, EventArgs e) { } private void comboBoxLightIn_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxLightIn.SelectedIndex) { case 0: autoLightIn = false; ifKit.outputs[3] = true; ifKit.outputs[2] = true; ifKit.outputs[1] = true; break; case 1: autoLightIn = false; ifKit.outputs[3] = false; ifKit.outputs[2] = false; ifKit.outputs[1] = false; break; case 2: autoLightIn = true; break; } } private void comboBoxLightOut_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxLightOut.SelectedIndex) { case 0: autoLightOut = false; ifKit.outputs[6] = true; ifKit.outputs[4] = true; break; case 1: autoLightOut = false; ifKit.outputs[6] = false; ifKit.outputs[4] = false; break; case 2: autoLightOut = true; break; } } private void leftClicked(object sender, EventArgs e) { curPic--; if (curPic <0) curPic = count-1; pictureBox1.ImageLocation = path + "\\output " + curPic.ToString() + ".jpg"; label11.Text = photolist[curPic].ToString(); } private void rightClicked(object sender, EventArgs e) { curPic++; if (curPic > count-1) curPic = 0; pictureBox1.ImageLocation = path + "\\output " + curPic.ToString() + ".jpg"; label11.Text = photolist[curPic].ToString(); } } public class StateObjClass { // Used to hold parameters for calls to TimerTask. public int SomeValue; public System.Threading.Timer TimerReference; public bool TimerCanceled; } }