using System; using System.Text; using Microsoft.SPOT.Hardware; using SWModules; namespace HWModules // This is a Singleton instantiation of FIFOQueue class based on // C# in Depth, Third Edition Jon Skeet // http://csharpindepth.com/Articles/General/Singleton.aspx // "a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance" { public sealed class EnergyMonitorPump { //TODO P1 this may not need to be a singleton if it is only used in StateBase private EnergyMonitorPump() { underVoltageError = new ErrorHandler(3, underVoltageErrorTS, underVoltageLabel, ErrorHandler.ErrorType.critical, ErrorHandler.setRecoveryMode); //underVoltageError.criticalErrorHandler += new ErrorHandler.CriticalErrorHandler(ErrorHandler.setRecoveryMode); } public static EnergyMonitorPump Instance { get { return Nested.instance;} } private class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly EnergyMonitorPump instance = new EnergyMonitorPump(); } EnergyMonitorLTC2946 em = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xDE >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Vdd, 0.025, 5.0); private static TimeSpan underVoltageErrorTS = new TimeSpan(10000, 0, 0); private static StringBuilder underVoltageLabel = new StringBuilder("Pump Under Voltage Error"); private static ErrorHandler underVoltageError; private static EnergyMonitorLTC2946.EM_VandI emData; private const double busVoltageEndMissionThreshold = 26.5; public EnergyMonitorLTC2946.EM_VandI getVoltsAndMilliAmps() { emData = em.getVandI(); if (SV.CurrentState != CPFStates.SMNotRunning) //TODO P3 might not need to check state as this might only be called after state machine starts { if (emData.voltage < busVoltageEndMissionThreshold) underVoltageError.logError(); } return emData; } } }