AnimatLab  2
Test
FirmataController.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using AnimatGUI.Framework;
6 
7 namespace RoboticsGUI
8 {
9  namespace RobotIOControls
10  {
11  namespace Firmata
12  {
13 
14  public class FirmataController : AnimatGUI.DataObjects.Robotics.RobotIOControl
15  {
16  #region " Attributes "
17 
18  protected string m_strComPort = "COM4";
19  protected int m_iBaudRate = 115200;
20 
21  #endregion
22 
23  #region " Attributes "
24 
25  public override string Description {get {return "Performs IO with a microcontroller like the Arduino that implements the Firmata protocols.";} set { }}
26  public override string ButtonImageName {get {return "RoboticsGUI.Graphics.Firmata.gif";}}
27  public override string WorkspaceImageName {get {return "RoboticsGUI.Graphics.FirmataSmall.gif";}}
28  public override string PartType {get { return "FirmataController"; }}
29  public override string ModuleName { get { return "RoboticsAnimatSim"; } }
30 
31  public virtual string ComPort
32  {
33  get
34  {
35  return m_strComPort;
36  }
37  set
38  {
39  SetSimData("ComPort", m_strComPort, true);
40  m_strComPort = value;
41  }
42  }
43 
44  public virtual int BaudRate
45  {
46  get
47  {
48  return m_iBaudRate;
49  }
50  set
51  {
52  if (value <= 0)
53  throw new System.Exception("Invalid baud rate specified. Rate: " + value.ToString());
54 
55  SetSimData("BaudRate", value.ToString(), true);
56  m_iBaudRate = value;
57  }
58  }
59 
60  #endregion
61 
62  #region " Methods "
63 
64  public FirmataController(AnimatGUI.Framework.DataObject doParent)
65  : base(doParent)
66  {
67  m_strName = "Firmata Controller";
68 
69  //Setup the parts that are available for this type of controller.
70  m_aryAvailablePartTypes.Add(new FirmataAnalogInput(this));
71  // m_aryAvailablePartTypes.Add(new FirmataAnalogOutput(this)); //This is not supported at the moment. Leaving the code in here for future use.
72  m_aryAvailablePartTypes.Add(new FirmataDigitalInput(this));
73  m_aryAvailablePartTypes.Add(new FirmataDigitalOutput(this));
74  m_aryAvailablePartTypes.Add(new FirmataHingeServo(this));
75  m_aryAvailablePartTypes.Add(new FirmataPrismaticServo(this));
76  m_aryAvailablePartTypes.Add(new FirmataPWMOutput(this));
77  m_aryAvailablePartTypes.Add(new FirmataDynamixelHingeServo(this));
78  m_aryAvailablePartTypes.Add(new FirmataDynamixelPrismaticServo(this));
79 
80  m_thDataTypes.DataTypes.Add(new AnimatGUI.DataObjects.DataType("MotorSendTime", "Motor Send Time", "Seconds", "s", 0, 1));
81  }
82 
83  public override AnimatGUI.Framework.DataObject Clone(AnimatGUI.Framework.DataObject doParent, bool bCutData, AnimatGUI.Framework.DataObject doRoot)
84  {
85  FirmataController doInterface = new FirmataController(doParent);
86  return doInterface;
87  }
88 
89  protected override void CloneInternal(DataObject doOriginal, bool bCutData, DataObject doRoot)
90  {
91  base.CloneInternal(doOriginal, bCutData, doRoot);
92 
93  FirmataController doOrig = (FirmataController)doOriginal;
94 
95  if (doOrig != null)
96  {
97  m_strComPort = doOrig.m_strComPort;
98  m_iBaudRate = doOrig.m_iBaudRate;
99  }
100  }
101 
102  public override void BuildProperties(ref AnimatGuiCtrls.Controls.PropertyTable propTable)
103  {
104  base.BuildProperties(ref propTable);
105 
106  propTable.Properties.Add(new AnimatGuiCtrls.Controls.PropertySpec("Com Port", this.ComPort.GetType(), "ComPort", "Properties", "Com port number", this.ComPort));
107  propTable.Properties.Add(new AnimatGuiCtrls.Controls.PropertySpec("Baud Rate", this.BaudRate.GetType(), "BaudRate", "Properties", "Baud rate to use for communications", this.BaudRate));
108  }
109 
110  public override void LoadData(ManagedAnimatInterfaces.IStdXml oXml)
111  {
112  base.LoadData(oXml);
113 
114  oXml.IntoElem();
115 
116  m_strComPort = oXml.GetChildString("ComPort", m_strComPort);
117  m_iBaudRate = oXml.GetChildInt("BaudRate", m_iBaudRate);
118 
119  oXml.OutOfElem();
120  }
121 
122  public override void SaveData(ManagedAnimatInterfaces.IStdXml oXml)
123  {
124  base.SaveData(oXml);
125 
126  oXml.IntoElem();
127 
128  oXml.AddChildElement("ComPort", m_strComPort);
129  oXml.AddChildElement("BaudRate", m_iBaudRate);
130 
131  oXml.OutOfElem();
132  }
133 
134  public override void SaveSimulationXml(ManagedAnimatInterfaces.IStdXml oXml, ref DataObject nmParentControl, string strName = "")
135  {
136  base.SaveSimulationXml(oXml, ref nmParentControl, strName);
137 
138  oXml.IntoElem();
139 
140  oXml.AddChildElement("ComPort", m_strComPort);
141  oXml.AddChildElement("BaudRate", m_iBaudRate);
142 
143  oXml.OutOfElem();
144  }
145 
146  #endregion
147 
148  }
149  }
150  }
151 }