Example #1
0
        /// <summary>
        /// Writes a SmcSettings object as text to the specified streamwriter.
        /// </summary>
        /// <param name="settings">The settings to read from.</param>
        /// <param name="sw">The file to write to.</param>
        /// <param name="device">The device that these settings came from (optional!).  Adds extra comments to the file.</param>
        public static void save(SmcSettings settings, StreamWriter sw, Smc device)
        {
            XmlTextWriter writer = new XmlTextWriter(sw);

            writer.Formatting = Formatting.Indented;
            writer.WriteComment("Pololu Simple Motor Controller settings file. http://www.pololu.com/docs/0J44");
            writer.WriteComment("Created on: " + DateTime.Now.ToString("u").TrimEnd('Z')); // save local time in a sortable format
            if (device != null)
            {
                writer.WriteComment("Device model: " + Smc.productIdToLongModelString(device.productId));
                writer.WriteComment("Device serial number: " + device.getSerialNumber());
                writer.WriteComment("Device firmware version: " + device.getFirmwareVersionString());
            }
            writer.WriteStartElement("SmcSettings");

            // XML file version, so that we can parse old XML types in the future and old versions of the
            // configuration utility don't try to read newer settings files that have a different format.
            writer.WriteAttributeString("version", "1");

            // [Add-new-settings-here]

            writer.WriteElementString("InputMode", settings.inputMode.ToString());
            writer.WriteElementString("MixingMode", settings.mixingMode.ToString());

            writer.WriteElementBool("DisableSafeStart", settings.disableSafeStart);
            writer.WriteElementBool("IgnorePotDisconnect", settings.ignorePotDisconnect);
            writer.WriteElementBool("IgnoreErrLineHigh", settings.ignoreErrLineHigh);
            writer.WriteElementBool("NeverSuspend", settings.neverSuspend);

            writer.WriteElementBool("TempLimitGradual", settings.tempLimitGradual);
            writer.WriteElementRange("OverTemp", settings.overTempMin, settings.overTempMax);
            writer.WriteElementU32("LowVinShutoffTimeout", settings.lowVinShutoffTimeout);
            writer.WriteElementU32("LowVinShutoffMv", settings.lowVinShutoffMv);
            writer.WriteElementU32("LowVinStartupMv", settings.lowVinStartupMv);
            writer.WriteElementU32("HighVinShutoffMv", settings.highVinShutoffMv);

            writer.WriteElementString("SerialMode", settings.serialMode.ToString());
            writer.WriteElementU32("SerialDeviceNumber", settings.serialDeviceNumber);
            writer.WriteElementU32("CommandTimeout", settings.commandTimeout);
            writer.WriteElementString("CrcMode", settings.crcMode.ToString());
            writer.WriteElementBool("UartResponseDelay", settings.uartResponseDelay);
            writer.WriteElementBool("UseFixedBaudRate", settings.useFixedBaudRate);
            writer.WriteElementU32("FixedBaudRate", settings.fixedBaudRateBps);

            writer.WriteComment("Input Settings");
            writer.WriteElementChannelSettings(SmcChannel.Rc1, settings.rc1);
            writer.WriteElementChannelSettings(SmcChannel.Rc2, settings.rc2);
            writer.WriteElementChannelSettings(SmcChannel.Analog1, settings.analog1);
            writer.WriteElementChannelSettings(SmcChannel.Analog2, settings.analog2);

            writer.WriteComment("Motor Settings");
            writer.WriteElementU32("PwmPeriodFactor", settings.pwmPeriodFactor);
            writer.WriteElementBool("MotorInvert", settings.motorInvert);
            writer.WriteElementU32("SpeedZeroBrakeAmount", settings.speedZeroBrakeAmount);
            writer.WriteElementU32("SpeedUpdatePeriod", settings.speedUpdatePeriod);
            writer.WriteElementMotorLimits("ForwardLimits", settings.forwardLimits);
            writer.WriteElementMotorLimits("ReverseLimits", settings.reverseLimits);

            writer.WriteComment("Advanced Settings");
            writer.WriteElementRange("PulsePeriod", settings.minPulsePeriod, settings.maxPulsePeriod);
            writer.WriteElementU32("RcTimeout", settings.rcTimeout);
            writer.WriteElementU32("ConsecGoodPulses", settings.consecGoodPulses);
            writer.WriteElementS32("VinMultiplierOffset", settings.vinMultiplierOffset);

            writer.WriteEndElement(); // End SmcSettings tag.
        }