private bool SaveToFile(string FileName) { StreamWriter StreamWriter; if (FileName.Length == 0) { SaveFileDialog SaveFileDialog = new SaveFileDialog(); SaveFileDialog.Filter = FileFilter; SaveFileDialog.RestoreDirectory = true; if (SaveFileDialog.ShowDialog() != DialogResult.OK) { return(false); } StreamWriter = new StreamWriter(SaveFileDialog.OpenFile()); FileName = SaveFileDialog.FileName; } else { StreamWriter = new StreamWriter(FileName); } foreach (Control Control in InstructionLayoutPanel.Controls) { PBInstructionBox InstructionControl = (PBInstructionBox)Control; StreamWriter.WriteLine(InstructionControl.ToString()); InstructionControl.Edited = false; } StreamWriter.Close(); return(true); }
private void LoadBoardButton_Click(object sender, EventArgs e) { try { // Initialize the board Program.SpinAPI.Init(); // Set the clock Program.SpinAPI.SetClock(ClockFrequency); // Start programming Program.SpinAPI.StartProgramming(ProgramTarget.PULSE_PROGRAM); // Load the instructions foreach (Control Control in this.InstructionLayoutPanel.Controls) { PBInstructionBox Instruction = (PBInstructionBox)Control; Program.SpinAPI.PBInst(Instruction.Flags, Instruction.OpCode, Instruction.Data, Instruction.Duration, Instruction.TimeUnit); } // Stop programming Program.SpinAPI.StopProgramming(); // Report success MessageBox.Show("Loaded board successfully."); } catch (SpinAPIException ex) { // Report any programming errors MessageBox.Show("Failed to program board: " + ex.Message); } }
public PBInstructionMemento(PBInstructionBox Parent) : base(Parent) { _Duration = Parent.Duration; _TimeUnit = Parent.TimeUnit; _Flags = Parent.Flags; _OpCode = Parent.OpCode; _Data = Parent.Data; }
private void OpenFileMenuStripItem_Click(object sender, EventArgs e) { Stream Stream; StreamReader StreamReader; OpenFileDialog OpenFileDialog = new OpenFileDialog(); if (!ContinueWithUnsavedChanges()) { return; } OpenFileDialog.Filter = FileFilter; OpenFileDialog.RestoreDirectory = true; if (OpenFileDialog.ShowDialog() != DialogResult.OK) { return; } Stream = OpenFileDialog.OpenFile(); // An exception should probably be thrown if this happens, but for now, return. if (Stream == null) { return; } StreamReader = new StreamReader(Stream); CurrentFileName = OpenFileDialog.FileName; InstructionLayoutPanel.Controls.Clear(); while (!StreamReader.EndOfStream) { string[] Fields = StreamReader.ReadLine().Split(new char[] { ' ' }); PBInstructionBox InstructionControl = new PBInstructionBox(); InstructionLayoutPanel.Controls.Add(InstructionControl); InstructionControl.Flags = int.Parse(Fields[0], System.Globalization.NumberStyles.AllowHexSpecifier); InstructionControl.OpCode = (OpCode)int.Parse(Fields[1], System.Globalization.NumberStyles.AllowHexSpecifier); InstructionControl.Data = int.Parse(Fields[2], System.Globalization.NumberStyles.AllowHexSpecifier); InstructionControl.Duration = double.Parse(Fields[3]); InstructionControl.TimeUnit = (TimeUnit)Enum.Parse(typeof(TimeUnit), Fields[4]); InstructionControl.Number = InstructionLayoutPanel.Controls.Count - 1; InstructionControl.Edited = false; } InstructionCountUpDown.Value = InstructionLayoutPanel.Controls.Count; StreamReader.Close(); }
public void SetInstructionStyle(PBInstructionBox Instruction, PBInstructionBox.FlagStyle Style) { try { if (Instruction.InvokeRequired) { Invoke(new SetInstructionStyleCallback(SetInstructionStyle), new object[] { Instruction, Style }); } else { Instruction.Style = Style; } } catch (Exception) { } }
/// <summary> /// Check the instructions for unsaved changes. If there are, prompt the user for whether to continue. /// </summary> /// <returns>true if there are no unsaved changes or the user has chosen to disregard unsaved changes, false otherwise.</returns> private bool ContinueWithUnsavedChanges() { foreach (Control Control in InstructionLayoutPanel.Controls) { PBInstructionBox InstructionControl = (PBInstructionBox)Control; if (InstructionControl.Edited) { DialogResult Result = MessageBox.Show("The current program has unsaved changes. Continue?", "Unsaved Changes...", MessageBoxButtons.YesNo); if (Result == DialogResult.Yes) { return(true); } else { return(false); } } } return(true); }
private void NewFileStripMenuItem_Click(object sender, EventArgs e) { if (!ContinueWithUnsavedChanges()) { return; } // This fires a ValueChanged event on InstructionCountUpDown, so it's before setting the new blank instructions. InstructionCountUpDown.Value = 2; InstructionLayoutPanel.Controls.Clear(); for (int i = 0; i < InstructionCountUpDown.Value; i++) { PBInstructionBox InstructionControl = new PBInstructionBox(); InstructionLayoutPanel.Controls.Add(InstructionControl); // Index instructions from zero. InstructionControl.Number = InstructionLayoutPanel.Controls.Count - 1; } CurrentFileName = ""; }
private void InstructionCountUpDown_ValueChanged(object sender, EventArgs e) { Command Command; int OldInstructionCount = this.InstructionLayoutPanel.Controls.Count; int NewInstructionCount = (int)this.InstructionCountUpDown.Value; if (NewInstructionCount > OldInstructionCount) { for (int i = OldInstructionCount; i < NewInstructionCount; i++) { PBInstructionBox Instruction = new PBInstructionBox(); Instruction.ValueChanged += new EventHandler(Instruction_ValueChanged); Command = new AddInstructionCommand(this, Instruction); Command.Execute(); History.Push(Command); Future.Clear(); RedoEditMenuStripItem.Enabled = false; } } else if (NewInstructionCount < OldInstructionCount) { for (int i = OldInstructionCount; i > NewInstructionCount; i--) { PBInstructionBox Instruction = (PBInstructionBox)InstructionLayoutPanel.Controls[InstructionLayoutPanel.Controls.Count - 1]; Command = new RemoveInstructionCommand(this, Instruction); Command.Execute(); History.Push(Command); Future.Clear(); RedoEditMenuStripItem.Enabled = false; } } this.UndoEditMenuStripItem.Enabled = true; }
public RemoveInstructionCommand(MainForm Parent, PBInstructionBox Instruction) { this.Parent = Parent; this.Instruction = Instruction; }
private void MainForm_Load(object sender, EventArgs e) { // Can only change board number if there are boards. this.BoardNumberUpDown.Enabled = (Program.SpinAPI.BoardCount > 0); // BoardNumberUpDown.Maximum is one less than the number of boards. // But, if there are no boards, it must be set to 0 so setting Value // does not throw an OutOfBoundsException. this.BoardNumberUpDown.Maximum = (Program.SpinAPI.BoardCount > 0) ? Program.SpinAPI.BoardCount - 1 : 0; this.BoardNumberUpDown.Value = this.BoardNumber; // Can only use the board control buttons if there are boards. this.ControlGroupBox.Enabled = (Program.SpinAPI.BoardCount > 0); // Display the firmware id of the board if there is one. this.FirmwareIDTextBox.Text = (Program.SpinAPI.BoardCount > 0) ? this.FirmwareID(this.BoardNumber) : ""; this.VersionTextBox.Text = Program.SpinAPI.Version; this.ClockFreqTextbox.Text = ClockFrequency.ToString(); // Set some minimum number of instructions for the program specified // by InitialInstructionCount and prepare some PBInstructionControls. for (int i = 0; i < MainForm.InitialInstructionCount; i++) { PBInstructionBox Instruction = new PBInstructionBox(); Instruction.ValueChanged += new EventHandler(this.Instruction_ValueChanged); Instruction.Number = this.InstructionLayoutPanel.Controls.Count; try { Instruction.Style = LookupStyle(Program.SpinAPI.GetFirmwareID(this.BoardNumber)); } catch (SpinAPIException exc) { MessageBox.Show("Can not detect board. Please check connections.", exc.Source); Close(); } InstructionLayoutPanel.Controls.Add(Instruction); } if (this.InstructionLayoutPanel.Controls.Count > 0) { // We're using the padding setting of InstructionLayoutPanel to center the // PBInstructionBox controls inside. int SidePadding = (this.InstructionLayoutPanel.Width - InstructionLayoutPanel.Controls[0].Width) / 2; this.InstructionLayoutPanel.Padding = new Padding(SidePadding, 5, SidePadding, 5); } // The following menu items are not implemented and thus left invisible. this.UndoEditMenuStripItem.Enabled = false; this.RedoEditMenuStripItem.Enabled = false; PrintFileMenuStripItem.Visible = false; PrintPreviewFileMenuStripMenuItem.Visible = false; FileMenuStripSeparator2.Visible = false; EditMenuStripSeparator0.Visible = false; CutEditMenuStripItem.Visible = false; CopyEditMenuStripItem.Visible = false; PasteEditMenuStripItem.Visible = false; EditMenuStripSeparator1.Visible = false; SelectAllEditMenuStripItem.Visible = false; ToolsMenuStripItem.Visible = false; ContentsHelpMenuStripItem.Visible = false; IndexHelpMenuStripItem.Visible = false; SearchHelpMenuStripItem.Visible = false; HelpMenuStripSeparator0.Visible = false; }