public ThunderBorg_class(ILogger log = null, bool tryOtherBus = false) { if (log != null) { this._log = log; log.WriteLog("THROTTLE_CODE: " + THROTTLE_CODE.ToString()); log.WriteLog(); log.WriteLog("Finding ThunderBorg_class..."); _TBorgAddress = ThunderBorg_class.ScanForThunderBorg(1, log); } else { _TBorgAddress = ThunderBorg_class.ScanForThunderBorg(); } if (log != null) { log.WriteLog("Loding ThunderBorg_class on bus " + _bus.ToString("X2") + ", address " + _TBorgAddress.ToString("X2")); } }
/// <summary> /// Sets the _initialized_ [ThunderBorg_class] board to the settings contained in this object. **NOTE** if the board address differs from this object's board address, this method will not run. /// </summary> /// <param name="borg">The _intiialized_ ThunderBorg board to be enumerated</param> /// <param name="logger">[Optional] Logger class to keep track of actions and other information</param> public void SetCurrentEnvironment(ThunderBorg_class borg, ILogger logger = null) { Stopwatch elapsedTime = new Stopwatch(); elapsedTime.Start(); bool log = false; if (logger != null) { log = true; } if (!this.IsUsed) { if (log) { logger.WriteLog("Not writing these values; the settings haven't been used."); } throw new ArgumentException("ThunderBorgSettings class not used; no values contained therein.", "ThunderBorgSettings"); } if (log) { logger.WriteLog("Setting board values to current enumeration..."); } if (log) { logger.WriteLog("Issuing all stop, first..."); } borg.AllStop(); if (borg.CurrentAddress != this.BoardAddress) { if (log) { logger.WriteLog("Possibly invalid settings; this board's address is 0x" + borg.CurrentAddress.ToString("X2") + " but the setting says 0x" + this.BoardAddress.ToString("X2")); } throw new ArgumentException("ThunderBorg_class address doesn't match settings address.", "ThunderBorgSettings.BoardAddress"); } else { if (log) { logger.WriteLog("Address is confirmed at 0x" + borg.CurrentAddress); } } if (log) { logger.WriteLog("VOLTAGE_PIN_MAX is a constant set by code; cannot change; current value: " + this.VoltagePinMax.ToString()); } borg.SetBatteryMonitoringLimits(this.BatteryMonitorMin, this.BatteryMonitorMax); if (log) { logger.WriteLog("\tSet battery monitoring min: " + this.BatteryMonitorMin.ToString() + " max: " + this.BatteryMonitorMax.ToString()); } borg.SetLED1(this.LED1_Red, this.LED1_Green, this.LED1_Blue); borg.SetLED2(this.LED2_Red, this.LED2_Green, this.LED2_Blue); if (log) { logger.WriteLog("\tSet LED1: R: 0x" + this.LED1_Red.ToString("X2") + " G: 0x" + this.LED1_Green.ToString("X2") + " B: 0x" + this.LED1_Blue.ToString("X2")); } if (log) { logger.WriteLog("\tSet LED2: R: 0x" + this.LED2_Red.ToString("X2") + " G: 0x" + this.LED2_Green.ToString("X2") + " B: 0x" + this.LED2_Blue.ToString("X2")); } borg.SetLEDBatteryMonitor(this.BatteryMonitoringState); if (log) { logger.WriteLog("\tCurrent battery LED monitor state: " + this.BatteryMonitoringState.ToString()); } borg.SetFailsafe(this.FailSafeState); if (log) { logger.WriteLog("\tSet failsafe state: " + this.FailSafeState.ToString()); } if (log) { logger.WriteLog("\tSettings complete..."); } elapsedTime.Stop(); if (log) { logger.WriteLog("\tBoard setting took " + elapsedTime.ElapsedMilliseconds.ToString() + " milliseconds..."); } }
/// <summary> /// Enumerates the [ThunderBorg_class] board passed into it, and records the settings it finds. /// </summary> /// <param name="borg">The _intiialized_ ThunderBorg board to be enumerated</param> /// <param name="logger">[Optional] Logger class to keep track of actions and other information</param> public void GetCurrentEnvironment(ThunderBorg_class borg, ILogger logger = null) { Stopwatch elapsedTime = new Stopwatch(); elapsedTime.Start(); bool log = false; if (logger != null) { log = true; } if (log) { logger.WriteLog("Starting board enumeration..."); } if (log) { logger.WriteLog("\tIssuing all stop, first..."); } borg.AllStop(); this.BoardAddress = borg.CurrentAddress; if (log) { logger.WriteLog("\tCurrent board address: 0x" + this.BoardAddress.ToString("X2")); } this.VoltagePinMax = borg.VoltagePinMax; if (log) { logger.WriteLog("\tCurrent VOLTAGE_PIN_MAX: " + this.VoltagePinMax.ToString()); } decimal[] batteryMonitorResponse = borg.GetBatteryMonitoringLimits(); this.BatteryMonitorMin = batteryMonitorResponse[0]; this.BatteryMonitorMax = batteryMonitorResponse[1]; if (log) { logger.WriteLog("\tBattery monitoring min: " + this.BatteryMonitorMin.ToString() + " max: " + this.BatteryMonitorMax.ToString()); } byte[] LED1Settings = borg.GetLED1(); this.LED1_Red = LED1Settings[1]; this.LED1_Green = LED1Settings[2]; this.LED1_Blue = LED1Settings[3]; byte[] LED2Settings = borg.GetLED2(); this.LED2_Red = LED2Settings[1]; this.LED2_Green = LED2Settings[2]; this.LED2_Blue = LED2Settings[3]; if (log) { logger.WriteLog("\tLED1: R: 0x" + this.LED1_Red.ToString("X2") + " G: 0x" + this.LED1_Green.ToString("X2") + " B: 0x" + this.LED1_Blue.ToString("X2")); } if (log) { logger.WriteLog("\tLED2: R: 0x" + this.LED2_Red.ToString("X2") + " G: 0x" + this.LED2_Green.ToString("X2") + " B: 0x" + this.LED2_Blue.ToString("X2")); } this.BatteryMonitoringState = borg.GetLEDBatteryMonitor(); if (log) { logger.WriteLog("\tCurrent battery LED monitor state: " + this.BatteryMonitoringState.ToString()); } this.FailSafeState = borg.GetFailsafe(); if (log) { logger.WriteLog("\tCurrent failsafe state: " + this.FailSafeState.ToString()); } if (log) { logger.WriteLog("\tEnumeration complete..."); } elapsedTime.Stop(); if (log) { logger.WriteLog("\tEnumeration took " + elapsedTime.ElapsedMilliseconds.ToString() + " milliseconds..."); } }
public static int SetNewAddress(byte newAddress, byte?oldAddress = null, int?busNumber = 0, ILogger logger = null) { int _oldAddress; bool _foundBoard = false; if ((newAddress <= 0x03) || (newAddress > 0x77)) { logger.WriteLog("Error: I2C addresses below 3 (0x03) and above 119 (0x77) are reserved. Please use a different address."); throw new ArgumentOutOfRangeException("newAddress", "New port number must be between 0x03 and 0x77."); } if (oldAddress == null) { _oldAddress = ThunderBorg_class.ScanForThunderBorg(Convert.ToInt32(busNumber), logger); if (_oldAddress < 0) { throw new Exception("ThunderBorg board not found."); } } else { _oldAddress = Convert.ToInt32(oldAddress); } if (logger != null) { logger.WriteLog("Attempting change of ThunderBorg address from " + _oldAddress.ToString("X2") + " to " + newAddress.ToString("X2"), ILogger.Priority.Information); } // This code was intended to do another check, but on reflection it doesn't seem to do anything. // //ThunderBorg_class changeTB = new ThunderBorg_class(); //try //{ // byte boardResponse = changeTB.GetBoardID(logger); // if (boardResponse == _oldAddress) // HACK: are you sure? // { // if (logger != null) // { // logger.WriteLog("ThunderBorg found at " + _oldAddress.ToString("X2"), ILogger.Priority.Information); // } // _foundBoard = true; // } // else // { // if (logger != null) // { // logger.WriteLog("Found _something_ at " + _oldAddress.ToString("X2") + " but it doesn't appear to be a ThunderBorg.", ILogger.Priority.Critical); // } // _foundBoard = false; // throw new ArgumentOutOfRangeException("oldAddress", "Found different kind of board at address " + _oldAddress.ToString("X2")); // } //} //catch(Exception ex) //{ // // do something //} //if (!_foundBoard) //{ // if (logger != null) // { // logger.WriteLog("No board found.", ILogger.Priority.High); // } //} using (var bus = I2CBus.Open("/dev/i2c-" + busNumber.ToString())) { bus.WriteBytes(_oldAddress, new byte[] { COMMAND_SET_I2C_ADD, newAddress }); System.Threading.Thread.Sleep(200); // let the I2C bus catch up int tempCheck = ThunderBorg_class.ScanForThunderBorg(Convert.ToInt32(busNumber), logger); if (tempCheck == newAddress) { logger.WriteLog("CHANGED BOARD ADDRESS FROM " + _oldAddress.ToString("X2") + " TO " + newAddress.ToString("X2"), ILogger.Priority.Critical); logger.WriteLog("This change will be persistent even after a reboot; keep track of it.", ILogger.Priority.Information); } else { logger.WriteLog("**FAILED** to change ThunderBorg address. Current address: " + tempCheck.ToString("X2")); } return(tempCheck); } }