public byte[] GetLED2(Logger_class log) { byte[] tempReturn; if (!_CheckInit()) { return(null); } log.WriteLog("Calling results for get LED 2..."); using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_LED2 }); tempReturn = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (tempReturn[0] == COMMAND_GET_LED2) { if (log != null) { log.WriteLog("Get LED2 response: " + this._BytesToString(tempReturn)); } return(tempReturn); } else { if (log != null) { log.WriteLog("Got a nonsense reponse from COMMAND_GET_LED2..." + this._BytesToString(tempReturn)); } throw new InvalidProgramException("Nonsense response during COMMAND_GET_LED2."); } } }
/// <summary> /// Sets the battery monitoring limits used for setting the LED color. The colors shown range from full red at minimum (or below), yellow at half, and green at maximum or higher. These values are stored in the EEPROM and reloaded when the board is powered. /// </summary> /// <param name="minimum">Minimum voltage (0 V minimum)</param> /// <param name="maximum">Maximum voltage (36.3 V maximum)</param> /// <param name="log">Optional logging output routine</param> public void SetBatteryMonitoringLimits(decimal minimum, decimal maximum, Logger_class log = null) { // my original values were 6.98 / 35.02; I don't know what the defaults are if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } minimum /= Convert.ToDecimal(VOLTAGE_PIN_MAX); maximum /= Convert.ToDecimal(VOLTAGE_PIN_MAX); byte levelMin = Math.Max(Convert.ToByte(0x00), Math.Min(Convert.ToByte(0xFF), Convert.ToByte(minimum * 0xFF))); byte levelMax = Math.Max(Convert.ToByte(0x00), Math.Min(Convert.ToByte(0xFF), Convert.ToByte(maximum * 0xFF))); if (log != null) { log.WriteLog("Trying to set battery monitoring limits to: 0x" + levelMin.ToString("X2") + " V. min and 0x" + levelMax.ToString("X2") + " V. max..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_BATT_LIMITS, levelMin, levelMax }); System.Threading.Thread.Sleep(200); // note: this was recommended in the Python version for EEPROM writing } }
public byte GetBoardID(Logger_class log = null) { byte tempReturn = 0x00; if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } if (log != null) { log.WriteLog("Checking board ID..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_ID }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (response[0] == COMMAND_GET_ID) { tempReturn = response[1]; } } return(tempReturn); }
/// <summary> /// Use this method to create AMG88xx device if you are using PI 1 /// </summary> /// <param name="devPath"></param> /// <param name="address">address of AMG88xx</param> public AMG8833(string devPath, byte address) { i2cPath = devPath; _i2cBus = I2CBus.Open(i2cPath); _i2cBus.set_slave_address(address); init(); }
public AMG8833() { i2cPath = "/dev/i2c-1"; _i2cBus = I2CBus.Open(i2cPath); _i2cBus.set_slave_address(0x69); init(); }
public void SetFailsafe(bool setting, Logger_class log = null) { if (!_CheckInit()) { return; } bool currentState = this.GetFailsafe(log); if (currentState == setting) { if (log != null) { log.WriteLog("Called setting for failsafe: " + setting.ToString() + " but it already was..."); } return; } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_FAILSAFE, Convert.ToByte(!currentState) }); if (log != null) { log.WriteLog("Set failsafe to " + (!currentState).ToString()); } } }
public bool GetFailsafe(Logger_class log = null) { if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } bool tempReturn = false; using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_FAILSAFE }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (log != null) { log.WriteLog("Got response on failsafe: " + _BytesToString(response)); } if (response[0] == COMMAND_GET_FAILSAFE) { tempReturn = Convert.ToBoolean(response[1]); } } return(tempReturn); }
public byte[] GetDriveFaultB(Logger_class log = null) { if (!_CheckInit(log, true)) { return(null); } byte[] tempReturn; if (log != null) { log.WriteLog("Getting drive fault B status..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_DRIVE_B_FAULT }); tempReturn = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (log != null) { log.WriteLog("Raw Response: " + _BytesToString(tempReturn)); } } return(tempReturn); }
public int GetMotorB(Logger_class log = null) { int tempReturn = 0; if (!_CheckInit(log, true)) { return(0); } if (log != null) { log.WriteLog("Getting power level for B motors..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_B }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (response == null) { if (log != null) { log.WriteLog("*** ERROR: no response from ThunderBorg..."); } throw new NullReferenceException("No parseable response from B motors on GetMotorB request."); } else if (response[0] != COMMAND_GET_B) { if (log != null) { log.WriteLog("Didn't get an expected response from the B motors on GetMotorB request."); } throw new IndexOutOfRangeException("Unexpected response from B motors on GetMotorB request."); } else { byte power_direction = response[1]; byte power_level = response[2]; if (log != null) { log.WriteLog("Raw response: " + _BytesToString(response)); } if (power_direction == COMMAND_VALUE_FWD) { tempReturn = power_level; } else if (power_direction == COMMAND_VALUE_REV) { tempReturn = -power_level; } } } return(tempReturn); }
//Method to initialize the BMP280 sensor public void Initialize() { Debug.WriteLine("BMP280::Initialize"); try { // read from I2C device bus 1 i2cBus = I2CBus.Open(I2CBusPath); } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace); throw; } }
public SI7021Sensor() { try { i2cPath = "/dev/i2c-1"; _i2cBus = I2CBus.Open(i2cPath); _i2cBus.set_slave_address(0x40); init(); _isAvailable = true; } catch { _isAvailable = false; } }
public SI7021Sensor(string i2c_port) { try { i2cPath = i2c_port; _i2cBus = I2CBus.Open(i2cPath); _i2cBus.set_slave_address(0x40); init(); _isAvailable = true; } catch { _isAvailable = false; } }
public void SetLED2(byte red, byte green, byte blue, Logger_class log = null) { if (!_CheckInit()) { return; } if (log != null) { log.WriteLog("Setting LED 2..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LED2, red, green, blue }); } }
public void SetLEDBatteryMonitor(bool setting, Logger_class log = null) { if (!_CheckInit()) { return; } if (log != null) { log.WriteLog("Setting LED battery monitor to: " + setting.ToString()); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LED_BATT_MON, Convert.ToByte(setting) }); } }
public void AllStop(Logger_class log = null) { if (!_CheckInit(log, true)) { return; } if (log != null) { log.WriteLog("Calling all stop."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_ALL_OFF }); } }
public void SetAllMotors(int power, Logger_class log = null) { if (!_CheckInit()) { return; } if ((power > 255) || (power < -255)) { if (log != null) { log.WriteLog("Power level out of range -255 <= power <= 255; rejecting command..."); } throw new IndexOutOfRangeException("Invalid power setting to motors; range outside of -255 <= power <= 255."); } if (log != null) { log.WriteLog("Setting all motors to: " + power.ToString("X2")); } byte parsedPower = 0; if (power > 0) { parsedPower = Convert.ToByte(power); } else { parsedPower = Convert.ToByte(-power); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { if (power > 0) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_ALL_FWD, parsedPower }); } else { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_ALL_REV, parsedPower }); } } }
public decimal[] GetBatteryMonitoringLimits(Logger_class log = null) { decimal tempMin = 0.00M; decimal tempMax = 0.00M; if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_BATT_LIMITS }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (response[0] == COMMAND_GET_BATT_LIMITS) { tempMin = response[1]; tempMax = response[2]; tempMin /= Convert.ToDecimal(0xFF); tempMax /= Convert.ToDecimal(0xFF); tempMin *= VOLTAGE_PIN_MAX; tempMax *= VOLTAGE_PIN_MAX; tempMin = Math.Round(tempMin, 2); tempMax = Math.Round(tempMax, 2); decimal[] tempReturn = { tempMin, tempMax }; if (log != null) { log.WriteLog("Voltages found - min: " + tempMin.ToString() + " max: " + tempMax.ToString()); } return(tempReturn); } } return(null); }
public decimal GetBatteryVoltage(Logger_class log = null) { int tempIntReturn = 0; decimal tempReturn = 0.00M; if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } if (log != null) { log.WriteLog("Getting battery voltage..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_BATT_VOLT }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (log != null) { log.WriteLog("GET_BATT_VOLT response: " + this._BytesToString(response)); } if (response[0] == COMMAND_GET_BATT_VOLT) { tempIntReturn = ((int)response[1] << 8) + (int)response[2]; } } tempReturn = Convert.ToDecimal(tempIntReturn) / Convert.ToDecimal(COMMAND_ANALOG_MAX); tempReturn *= VOLTAGE_PIN_MAX; tempReturn += VOLTAGE_PIN_CORRECTION; tempReturn = Math.Round(tempReturn, 2); return(tempReturn); }
public bool GetLEDBatteryMonitor(Logger_class log = null) { bool tempReturn = false; if (!_CheckInit()) { throw new NullReferenceException("ThunderBorg not initiated."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_LED_BATT_MON }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); if (log != null) { log.WriteLog("Response from COMMAND_GET_LED_BATT_MON: " + this._BytesToString(response)); } if (response[0] == COMMAND_GET_LED_BATT_MON) { if (response[1] == COMMAND_VALUE_ON) { tempReturn = true; return(tempReturn); } else if (response[1] == COMMAND_VALUE_OFF) { tempReturn = false; return(tempReturn); } } else { throw new InvalidProgramException("Nonsense response during COMMAND_GET_LED_BATT_MON."); } } return(tempReturn); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); I2CBus bus = new I2CBus(); bus.Open("/dev/i2c-1"); bus.SetDevice(0x58); SGP30 sensor = new SGP30(bus); string str = sensor.GetSerialNumber(); Console.WriteLine($"SGP30 Serial number: {str}"); sensor.InitAirQuality(); var baseLine = sensor.GetBaseline(); Console.WriteLine($"Baseline: {baseLine.Raw_CO2} {baseLine.Raw_VOC}"); //sensor.SetBaseline(0, 0); values are only available after 12 hours or something so no clue // what a valid baseline is // while (true) // { // var result = sensor.MeasureAirQuality(); // Console.WriteLine($"{DateTime.Now} {result.CO2_PartsPerMillion}"); // Thread.Sleep(1000); // } BME280 sensor2 = new BME280(bus, 0x76); while (true) { var temperature = sensor2.ReadTemperature(); Console.WriteLine($"{DateTime.Now} Temperature: {temperature}"); Thread.Sleep(1000); } }
public void SetLEDs(byte red, byte green, byte blue, Logger_class log = null) { if (!_CheckInit()) { return; } if (log != null) { log.WriteLog("Setting LEDs to " + red.ToString() + ", " + green.ToString() + ", " + blue.ToString() + "..."); } using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString())) { bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LEDS, red, green, blue }); byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN); //if (log != null) //{ // log.WriteLog("Raw response to SET_LEDS: " + this._BytesToString(response)); //} } }