/* * ParallelPortPanel.cs * Authors: Taha Zayed (downloaded from http://www.codeproject.com/csharp/Control_e_appliances.asp) * Modifications: August Zinsser * * Copyright ? TODO: Figure out exactly what license/copyright this code falls under */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Xna.Framework; namespace tAC_Engine { /// /// Primarily, this class serves as an easy way for the Logger to also send data out through the parallel /// port. This funcitonality is wrapped with a simple GUI that allows the user to manually toggle bits /// sent out, which may be helpful for debugging. Most of the time, however, this class's form should /// remain hidden. Since this code is intimately tied to Windows architecture, it is probably best /// to remove this class entirely from the released builds that are posted online. /// public partial class ParallelPortPanel : Form { // Variables used for parallel port communication private AxHWINTERFACELib.AxHwinterface mAxHwinterface; // Establishes a portal to the parallel port private const short NIL = -1; // Represents that there is currently no code on the parallel port private const float DURATION = 5.0f; // Milliseconds to latch data on the parallel port private float mLatchTimer = 0.0f; // Counts the time left to latch data private short mCurrentCode = NIL; // The current code on the parallel port private Queue mCodeQueue = new Queue(); // A list of codes to output ASAP private bool mAllowPP; // On/Off switch for parallel port shananagins // Windows Form Variables private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.RadioButton rBtnD0Off; private System.Windows.Forms.RadioButton rBtnD0On; private System.Windows.Forms.RadioButton rBtnD1On; private System.Windows.Forms.RadioButton rBtnD1Off; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.RadioButton rBtnD3Off; private System.Windows.Forms.RadioButton rBtnD3On; private System.Windows.Forms.RadioButton rBtnD2Off; private System.Windows.Forms.RadioButton rBtnD2On; private System.Windows.Forms.GroupBox groupBox5; private System.Windows.Forms.GroupBox groupBox6; private System.Windows.Forms.GroupBox groupBox7; private System.Windows.Forms.GroupBox groupBox8; private System.Windows.Forms.RadioButton rBtnD7On; private System.Windows.Forms.RadioButton rBtnD6On; private System.Windows.Forms.RadioButton rBtnD5On; private System.Windows.Forms.RadioButton rBtnD4On; private System.Windows.Forms.RadioButton rBtnD7Off; private System.Windows.Forms.RadioButton rBtnD6Off; private System.Windows.Forms.RadioButton rBtnD5Off; private System.Windows.Forms.RadioButton rBtnD4Off; private System.Windows.Forms.Label label1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public bool ParallellPortPresent { get { return mAllowPP; } } public ParallelPortPanel() { // // Required for Windows Form Designer support // try { InitializeComponent(); mAxHwinterface.OutPort(888, 0); mAllowPP = true; } catch { mAllowPP = false; //System.Diagnostics.Debug.Assert(false, "There were problems initializing the Parallel Port."); } } /// /// Add a code to the queue to be outputted through the parallel port as soon as all previous codes /// have been outputted. /// /// public void Out(short data) { // Add the data to the queue mCodeQueue.Enqueue(data); } /// /// This should be called as fast as possible to ensure that data is outputted through the parallel /// port fast enough. /// public void Update(GameTime gameTime) { // Return if there is nothing to process if (mCurrentCode == NIL && mCodeQueue.Count == 0) return; // Count down latch time // Do NOT use the mainGame's ElapsedMilliseconds wrapper as that counts since the last LOGIC update mLatchTimer -= (float)gameTime.ElapsedGameTime.TotalMilliseconds; // See if the last data has been outputted long enough if (mLatchTimer <= 0) { // Case 1: Nothing on the port, things in the queue if (mCurrentCode == NIL && mCodeQueue.Count > 0) { // Output the next code mCurrentCode = mCodeQueue.Dequeue(); IO(mCurrentCode); // Reset the timer mLatchTimer = DURATION; } // Case 2: Something on the port else if (mCurrentCode != NIL && mCurrentCode != 0) { // Clear the port mCurrentCode = 0; IO(mCurrentCode); // Reset the timer mLatchTimer = DURATION; } // Case 3: Port has just finished clearing, things on the queue else if (mCurrentCode == 0 && mCodeQueue.Count > 0) { // Output the next code mCurrentCode = mCodeQueue.Dequeue(); IO(mCurrentCode); // Reset the timer mLatchTimer = DURATION; } // Case 4: Port has just finished clearing, nothing on the queue else if (mCurrentCode == 0 && mCodeQueue.Count == 0) { // Empty the current code mCurrentCode = NIL; } else { // Missed a case, this is bad! System.Diagnostics.Debug.Assert(false, "A case has occurred in ParallelPortPanel::Update() that must be defined. CurrentCode=" + mCurrentCode + ", CodeQueueCount=" + mCodeQueue.Count); } } } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ParallelPortPanel)); this.mAxHwinterface = new AxHWINTERFACELib.AxHwinterface(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.rBtnD0Off = new System.Windows.Forms.RadioButton(); this.rBtnD0On = new System.Windows.Forms.RadioButton(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.rBtnD1Off = new System.Windows.Forms.RadioButton(); this.rBtnD1On = new System.Windows.Forms.RadioButton(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.rBtnD3Off = new System.Windows.Forms.RadioButton(); this.rBtnD3On = new System.Windows.Forms.RadioButton(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.rBtnD2Off = new System.Windows.Forms.RadioButton(); this.rBtnD2On = new System.Windows.Forms.RadioButton(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.rBtnD7Off = new System.Windows.Forms.RadioButton(); this.rBtnD7On = new System.Windows.Forms.RadioButton(); this.groupBox6 = new System.Windows.Forms.GroupBox(); this.rBtnD6Off = new System.Windows.Forms.RadioButton(); this.rBtnD6On = new System.Windows.Forms.RadioButton(); this.groupBox7 = new System.Windows.Forms.GroupBox(); this.rBtnD5Off = new System.Windows.Forms.RadioButton(); this.rBtnD5On = new System.Windows.Forms.RadioButton(); this.groupBox8 = new System.Windows.Forms.GroupBox(); this.rBtnD4Off = new System.Windows.Forms.RadioButton(); this.rBtnD4On = new System.Windows.Forms.RadioButton(); this.label1 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.mAxHwinterface)).BeginInit(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.groupBox5.SuspendLayout(); this.groupBox6.SuspendLayout(); this.groupBox7.SuspendLayout(); this.groupBox8.SuspendLayout(); this.SuspendLayout(); // // axHwinterface1 // this.mAxHwinterface.Enabled = true; this.mAxHwinterface.Location = new System.Drawing.Point(464, 256); this.mAxHwinterface.Name = "axHwinterface1"; this.mAxHwinterface.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axHwinterface1.OcxState"))); this.mAxHwinterface.Size = new System.Drawing.Size(100, 50); this.mAxHwinterface.TabIndex = 0; // // groupBox1 // this.groupBox1.Controls.Add(this.rBtnD0Off); this.groupBox1.Controls.Add(this.rBtnD0On); this.groupBox1.Location = new System.Drawing.Point(16, 24); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(128, 96); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Device 0"; // // rBtnD0Off // this.rBtnD0Off.Checked = true; this.rBtnD0Off.Location = new System.Drawing.Point(16, 56); this.rBtnD0Off.Name = "rBtnD0Off"; this.rBtnD0Off.Size = new System.Drawing.Size(104, 24); this.rBtnD0Off.TabIndex = 1; this.rBtnD0Off.TabStop = true; this.rBtnD0Off.Text = "off"; this.rBtnD0Off.CheckedChanged += new System.EventHandler(this.rBtnD0Off_CheckedChanged); // // rBtnD0On // this.rBtnD0On.Location = new System.Drawing.Point(16, 24); this.rBtnD0On.Name = "rBtnD0On"; this.rBtnD0On.Size = new System.Drawing.Size(104, 24); this.rBtnD0On.TabIndex = 0; this.rBtnD0On.Text = "on"; this.rBtnD0On.CheckedChanged += new System.EventHandler(this.rBtnD0On_CheckedChanged); // // groupBox2 // this.groupBox2.Controls.Add(this.rBtnD1Off); this.groupBox2.Controls.Add(this.rBtnD1On); this.groupBox2.Location = new System.Drawing.Point(160, 24); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(128, 96); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "Device 1"; // // rBtnD1Off // this.rBtnD1Off.Checked = true; this.rBtnD1Off.Location = new System.Drawing.Point(16, 56); this.rBtnD1Off.Name = "rBtnD1Off"; this.rBtnD1Off.Size = new System.Drawing.Size(104, 24); this.rBtnD1Off.TabIndex = 1; this.rBtnD1Off.TabStop = true; this.rBtnD1Off.Text = "off"; this.rBtnD1Off.CheckedChanged += new System.EventHandler(this.rBtnD1Off_CheckedChanged); // // rBtnD1On // this.rBtnD1On.Location = new System.Drawing.Point(16, 24); this.rBtnD1On.Name = "rBtnD1On"; this.rBtnD1On.Size = new System.Drawing.Size(104, 24); this.rBtnD1On.TabIndex = 0; this.rBtnD1On.Text = "on"; this.rBtnD1On.CheckedChanged += new System.EventHandler(this.rBtnD1On_CheckedChanged); // // groupBox3 // this.groupBox3.Controls.Add(this.rBtnD3Off); this.groupBox3.Controls.Add(this.rBtnD3On); this.groupBox3.Location = new System.Drawing.Point(432, 24); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(128, 96); this.groupBox3.TabIndex = 4; this.groupBox3.TabStop = false; this.groupBox3.Text = "Device 3"; // // rBtnD3Off // this.rBtnD3Off.Checked = true; this.rBtnD3Off.Location = new System.Drawing.Point(16, 56); this.rBtnD3Off.Name = "rBtnD3Off"; this.rBtnD3Off.Size = new System.Drawing.Size(104, 24); this.rBtnD3Off.TabIndex = 1; this.rBtnD3Off.TabStop = true; this.rBtnD3Off.Text = "off"; this.rBtnD3Off.CheckedChanged += new System.EventHandler(this.rBtnD3Off_CheckedChanged); // // rBtnD3On // this.rBtnD3On.Location = new System.Drawing.Point(16, 24); this.rBtnD3On.Name = "rBtnD3On"; this.rBtnD3On.Size = new System.Drawing.Size(104, 24); this.rBtnD3On.TabIndex = 0; this.rBtnD3On.Text = "on"; this.rBtnD3On.CheckedChanged += new System.EventHandler(this.rBtnD3On_CheckedChanged); // // groupBox4 // this.groupBox4.Controls.Add(this.rBtnD2Off); this.groupBox4.Controls.Add(this.rBtnD2On); this.groupBox4.Location = new System.Drawing.Point(288, 24); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(128, 96); this.groupBox4.TabIndex = 3; this.groupBox4.TabStop = false; this.groupBox4.Text = "Device 2"; // // rBtnD2Off // this.rBtnD2Off.Checked = true; this.rBtnD2Off.Location = new System.Drawing.Point(16, 56); this.rBtnD2Off.Name = "rBtnD2Off"; this.rBtnD2Off.Size = new System.Drawing.Size(104, 24); this.rBtnD2Off.TabIndex = 1; this.rBtnD2Off.TabStop = true; this.rBtnD2Off.Text = "off"; this.rBtnD2Off.CheckedChanged += new System.EventHandler(this.rBtnD2Off_CheckedChanged); // // rBtnD2On // this.rBtnD2On.Location = new System.Drawing.Point(16, 24); this.rBtnD2On.Name = "rBtnD2On"; this.rBtnD2On.Size = new System.Drawing.Size(104, 24); this.rBtnD2On.TabIndex = 0; this.rBtnD2On.Text = "on"; this.rBtnD2On.CheckedChanged += new System.EventHandler(this.rBtnD2On_CheckedChanged); // // groupBox5 // this.groupBox5.Controls.Add(this.rBtnD7Off); this.groupBox5.Controls.Add(this.rBtnD7On); this.groupBox5.Location = new System.Drawing.Point(432, 136); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(128, 96); this.groupBox5.TabIndex = 8; this.groupBox5.TabStop = false; this.groupBox5.Text = "Device 7"; // // rBtnD7Off // this.rBtnD7Off.Checked = true; this.rBtnD7Off.Location = new System.Drawing.Point(16, 56); this.rBtnD7Off.Name = "rBtnD7Off"; this.rBtnD7Off.Size = new System.Drawing.Size(104, 24); this.rBtnD7Off.TabIndex = 1; this.rBtnD7Off.TabStop = true; this.rBtnD7Off.Text = "off"; this.rBtnD7Off.CheckedChanged += new System.EventHandler(this.rBtnD7Off_CheckedChanged); // // rBtnD7On // this.rBtnD7On.Location = new System.Drawing.Point(16, 24); this.rBtnD7On.Name = "rBtnD7On"; this.rBtnD7On.Size = new System.Drawing.Size(104, 24); this.rBtnD7On.TabIndex = 0; this.rBtnD7On.Text = "on"; this.rBtnD7On.CheckedChanged += new System.EventHandler(this.rBtnD7On_CheckedChanged); // // groupBox6 // this.groupBox6.Controls.Add(this.rBtnD6Off); this.groupBox6.Controls.Add(this.rBtnD6On); this.groupBox6.Location = new System.Drawing.Point(288, 136); this.groupBox6.Name = "groupBox6"; this.groupBox6.Size = new System.Drawing.Size(128, 96); this.groupBox6.TabIndex = 7; this.groupBox6.TabStop = false; this.groupBox6.Text = "Device 6"; // // rBtnD6Off // this.rBtnD6Off.Checked = true; this.rBtnD6Off.Location = new System.Drawing.Point(16, 56); this.rBtnD6Off.Name = "rBtnD6Off"; this.rBtnD6Off.Size = new System.Drawing.Size(104, 24); this.rBtnD6Off.TabIndex = 1; this.rBtnD6Off.TabStop = true; this.rBtnD6Off.Text = "off"; this.rBtnD6Off.CheckedChanged += new System.EventHandler(this.rBtnD6Off_CheckedChanged); // // rBtnD6On // this.rBtnD6On.Location = new System.Drawing.Point(16, 24); this.rBtnD6On.Name = "rBtnD6On"; this.rBtnD6On.Size = new System.Drawing.Size(104, 24); this.rBtnD6On.TabIndex = 0; this.rBtnD6On.Text = "on"; this.rBtnD6On.CheckedChanged += new System.EventHandler(this.rBtnD6On_CheckedChanged); // // groupBox7 // this.groupBox7.Controls.Add(this.rBtnD5Off); this.groupBox7.Controls.Add(this.rBtnD5On); this.groupBox7.Location = new System.Drawing.Point(160, 136); this.groupBox7.Name = "groupBox7"; this.groupBox7.Size = new System.Drawing.Size(128, 96); this.groupBox7.TabIndex = 6; this.groupBox7.TabStop = false; this.groupBox7.Text = "Device 5"; // // rBtnD5Off // this.rBtnD5Off.Checked = true; this.rBtnD5Off.Location = new System.Drawing.Point(16, 56); this.rBtnD5Off.Name = "rBtnD5Off"; this.rBtnD5Off.Size = new System.Drawing.Size(104, 24); this.rBtnD5Off.TabIndex = 1; this.rBtnD5Off.TabStop = true; this.rBtnD5Off.Text = "off"; this.rBtnD5Off.CheckedChanged += new System.EventHandler(this.rBtnD5Off_CheckedChanged); // // rBtnD5On // this.rBtnD5On.Location = new System.Drawing.Point(16, 24); this.rBtnD5On.Name = "rBtnD5On"; this.rBtnD5On.Size = new System.Drawing.Size(104, 24); this.rBtnD5On.TabIndex = 0; this.rBtnD5On.Text = "on"; this.rBtnD5On.CheckedChanged += new System.EventHandler(this.rBtnD5On_CheckedChanged); // // groupBox8 // this.groupBox8.Controls.Add(this.rBtnD4Off); this.groupBox8.Controls.Add(this.rBtnD4On); this.groupBox8.Location = new System.Drawing.Point(16, 136); this.groupBox8.Name = "groupBox8"; this.groupBox8.Size = new System.Drawing.Size(128, 96); this.groupBox8.TabIndex = 5; this.groupBox8.TabStop = false; this.groupBox8.Text = "Device 4"; // // rBtnD4Off // this.rBtnD4Off.Checked = true; this.rBtnD4Off.Location = new System.Drawing.Point(16, 56); this.rBtnD4Off.Name = "rBtnD4Off"; this.rBtnD4Off.Size = new System.Drawing.Size(104, 24); this.rBtnD4Off.TabIndex = 1; this.rBtnD4Off.TabStop = true; this.rBtnD4Off.Text = "off"; this.rBtnD4Off.CheckedChanged += new System.EventHandler(this.rBtnD4Off_CheckedChanged); // // rBtnD4On // this.rBtnD4On.Location = new System.Drawing.Point(16, 24); this.rBtnD4On.Name = "rBtnD4On"; this.rBtnD4On.Size = new System.Drawing.Size(104, 24); this.rBtnD4On.TabIndex = 0; this.rBtnD4On.Text = "on"; this.rBtnD4On.CheckedChanged += new System.EventHandler(this.rBtnD4On_CheckedChanged); // // label1 // this.label1.Location = new System.Drawing.Point(40, 256); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(72, 24); this.label1.TabIndex = 9; // // ParallelPortPanel // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(618, 287); this.Controls.Add(this.label1); this.Controls.Add(this.groupBox5); this.Controls.Add(this.groupBox6); this.Controls.Add(this.groupBox7); this.Controls.Add(this.groupBox8); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.mAxHwinterface); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "ParallelPortPanel"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Control electical appliances"; this.Load += new System.EventHandler(this.ParallelPortPanel_Load); ((System.ComponentModel.ISupportInitialize)(this.mAxHwinterface)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox4.ResumeLayout(false); this.groupBox5.ResumeLayout(false); this.groupBox6.ResumeLayout(false); this.groupBox7.ResumeLayout(false); this.groupBox8.ResumeLayout(false); this.ResumeLayout(false); } #endregion //device 0 private void rBtnD0On_CheckedChanged(object sender, System.EventArgs e) { IO(1); } private void rBtnD0Off_CheckedChanged(object sender, System.EventArgs e) { IO(1); } //device 1 private void rBtnD1On_CheckedChanged(object sender, System.EventArgs e) { IO(2); } private void rBtnD1Off_CheckedChanged(object sender, System.EventArgs e) { IO(2); } //device 2 private void rBtnD2On_CheckedChanged(object sender, System.EventArgs e) { IO(4); } private void rBtnD2Off_CheckedChanged(object sender, System.EventArgs e) { IO(4); } //device 3 private void rBtnD3On_CheckedChanged(object sender, System.EventArgs e) { IO(8); } private void rBtnD3Off_CheckedChanged(object sender, System.EventArgs e) { IO(8); } //device 4 private void rBtnD4On_CheckedChanged(object sender, System.EventArgs e) { IO(16); } private void rBtnD4Off_CheckedChanged(object sender, System.EventArgs e) { IO(16); } //device 5 private void rBtnD5On_CheckedChanged(object sender, System.EventArgs e) { IO(32); } private void rBtnD5Off_CheckedChanged(object sender, System.EventArgs e) { IO(32); } //device 6 private void rBtnD6On_CheckedChanged(object sender, System.EventArgs e) { IO(64); } private void rBtnD6Off_CheckedChanged(object sender, System.EventArgs e) { IO(64); } //device 7 private void rBtnD7On_CheckedChanged(object sender, System.EventArgs e) { IO(128); } private void rBtnD7Off_CheckedChanged(object sender, System.EventArgs e) { IO(128); } private void IO(int data) { //int val; //val=mAxHwinterface.InPort(888); mAxHwinterface.OutPort(888,(short)data); label1.Text=mAxHwinterface.InPort(888).ToString(); } private void ParallelPortPanel_Load(object sender, EventArgs e) { } } }