public void ConfigureInputPort(byte pin, bool enablePullUp = false, bool enableInterrupt = true) { if (IsValidPin(pin)) { // set the port direction this.SetPortDirection(pin, PortDirectionType.Input); // refresh out pull up state // TODO: do away with this and trust internal state? _gppu = _i2cPeripheral.ReadRegister(_PullupResistorConfigurationRegister); _gppu = BitHelpers.SetBit(_gppu, pin, enablePullUp); _i2cPeripheral.WriteRegister(_PullupResistorConfigurationRegister, _gppu); if (enableInterrupt) { // interrupt on change (whether or not we want to raise an interrupt on the interrupt pin on change) byte gpinten = _i2cPeripheral.ReadRegister(_InterruptOnChangeRegister); gpinten = BitHelpers.SetBit(gpinten, pin, true); // interrupt control register; whether or not the change is based // on default comparison value, or if a change from previous. We // want to raise on change, so we set it to 0, always. byte interruptControl = _i2cPeripheral.ReadRegister(_InterruptControlRegister); interruptControl = BitHelpers.SetBit(interruptControl, pin, false); } } else { throw new Exception("Pin is out of range"); } }
private byte WriteParam(byte param, int value) { Write8(REG_PARAMWR, (byte)value); Write8(REG_COMMAND, (byte)(param | PARAM_SET)); return(si1145.ReadRegister(REG_PARAMRD)); }
/* public MCP23008(bool pinA0, bool pinA1, bool pinA2, ushort speed = 100) * : this(MCPAddressTable.GetAddressFromPins(pinA0, pinA1, pinA2), speed) * { * // nothing goes here * } */ public MCP23008(II2cBus i2cBus, byte address = 0x20) { // tried this, based on a forum post, but seems to have no effect. //H.OutputPort SDA = new H.OutputPort(N.Pins.GPIO_PIN_A4, false); //H.OutputPort SCK = new H.OutputPort(N.Pins.GPIO_PIN_A5, false); //SDA.Dispose(); //SCK.Dispose(); // configure our i2c bus so we can talk to the chip _i2cPeripheral = new I2cPeripheral(i2cBus, address); Console.WriteLine("initialized."); Initialize(); Console.WriteLine("Chip Reset."); // read in the initial state of the chip _iodir = _i2cPeripheral.ReadRegister(_IODirectionRegister); // tried some sleeping, but also has no effect on its reliability //Thread.Sleep(100); //Console.WriteLine("IODIR: " + _iodir.ToString("X")); _gpio = _i2cPeripheral.ReadRegister(_GPIORegister); //Thread.Sleep(100); //Console.WriteLine("GPIO: " + _gpio.ToString("X")); _olat = _i2cPeripheral.ReadRegister(_OutputLatchRegister); //Thread.Sleep(100); //Console.WriteLine("OLAT: " + _olat.ToString("X")); }
/// <summary> /// Put the sensor into manual integration mode. /// </summary> public void ManualStart() { var timing = tsl2561.ReadRegister(Registers.Timing); timing |= 0x03; tsl2561.WriteRegister(Registers.Timing, timing); timing |= 0xf7; // ~0x08; tsl2561.WriteRegister(Registers.Timing, timing); }
/// <summary> /// Turn the heater on or off. /// </summary> /// <param name="onOrOff">Heater status, true = turn heater on, false = turn heater off.</param> public void Heater(bool onOrOff) { var register = si7021.ReadRegister(READ_USER_REGISTER); register &= 0xfd; if (onOrOff) { register |= 0x02; } si7021.WriteRegister(WRITE_USER_REGISTER, register); }
/// <summary> /// Create a new instance of the ADXL345 communicating over the I2C interface. /// </summary> /// <param name="address">Address of the I2C sensor</param> /// <param name="i2cBus">I2C bus</param> /// <param name="updateInterval">How frequently this sensor should be updated.</param> /// <param name="accelerationChangeNotificationThreshold">Notification threshold, changes greater than +/- this value will generate and interrupt.</param> public Adxl345(II2cBus i2cBus, byte address = 0x53, ushort updateInterval = 100, double accelerationChangeNotificationThreshold = 5.0F) { if ((address != 0x1d) && (address != 0x53)) { throw new ArgumentOutOfRangeException(nameof(address), "ADXL345 address can only be 0x1d or 0x53."); } if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod)) { throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update interval should be 0 or greater than " + MinimumPollingPeriod); } _updateInterval = updateInterval; AccelerationChangeNotificationThreshold = accelerationChangeNotificationThreshold; _adxl345 = new I2cPeripheral(i2cBus, address); var deviceID = _adxl345.ReadRegister(Registers.DeviceID); if (deviceID != 0xe5) { throw new Exception("Invalid device ID."); } if (updateInterval > 0) { StartUpdating(); } else { Update(); } }
/// <summary> /// Create a new SI1145 sensor object. /// </summary> /// <param name="address">Address of the chip on the I2C bus (default to 0x60).</param> /// <param name="iscBus">I2cBus (default to 400 KHz).</param> public SI1145(II2cBus i2cBus, byte address = 0x60) { _si1145 = new I2cPeripheral(i2cBus, address); if (_si1145.ReadRegister(Registers.PartID) != 0x45) { throw new Exception("Invalid part ID"); } }
/// <summary> /// Force the sensor to make a reading and update the relevant properties. /// </summary> async Task Update() { int temp = 0; // // Get the humidity first. // groveTH02.WriteRegister(Registers.Config, StartMeasurement); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // await Task.Delay(40); while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } byte[] data = groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 4; Conditions.Humidity = (((float)temp) / 16) - 24; // // Now get the temperature. // groveTH02.WriteRegister(Registers.Config, StartMeasurement | MeasureTemperature); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // await Task.Delay(40); while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } data = groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 2; Conditions.Temperature = (((float)temp) / 32) - 50; }
/// <summary> /// Create a new instance of the ADXL345 communicating over the I2C interface. /// </summary> /// <param name="address">Address of the I2C sensor</param> /// <param name="i2cBus">I2C bus</param> public Adxl345(II2cBus i2cBus, byte address = 0x53) { adxl345 = new I2cPeripheral(i2cBus, address); var deviceID = adxl345.ReadRegister(Registers.DeviceID); if (deviceID != 0xe5) { throw new Exception("Invalid device ID."); } }
/// <summary> /// Create a new SI1145 sensor object. /// </summary> /// <param name="address">Address of the chip on the I2C bus (default to 0x60).</param> /// <param name="i2cBus">I2cBus (default to 400 KHz).</param> public Si1145(II2cBus i2cBus, byte address = 0x60) { si1145 = new I2cPeripheral(i2cBus, address); if (si1145.ReadRegister(REG_PARTID) != 0x45) { throw new Exception("Invalid part ID"); } Initialize(); }
/// <summary> /// Force the sensor to make a reading and update the relevanyt properties. /// </summary> public void Read() { var controlRegister = _mag3110.ReadRegister((byte)Registers.Control1); controlRegister |= 0x02; _mag3110.WriteRegister((byte)Registers.Control1, controlRegister); var data = _mag3110.ReadRegisters((byte)Registers.XMSB, 6); X = (short)((data[0] << 8) | data[1]); Y = (short)((data[2] << 8) | data[3]); Z = (short)((data[4] << 8) | data[5]); }
/// <summary> /// Create a new instance of the ADXL345 communicating over the I2C interface. /// </summary> /// <param name="address">Address of the I2C sensor</param> /// <param name="i2cBus">I2C bus</param> public Adxl345(II2cBus i2cBus, byte address = 0x53) { if ((address != 0x1d) && (address != 0x53)) { throw new ArgumentOutOfRangeException(nameof(address), "ADXL345 address can only be 0x1d or 0x53."); } _adxl345 = new I2cPeripheral(i2cBus, address); var deviceID = _adxl345.ReadRegister(Registers.DeviceID); if (deviceID != 0xe5) { throw new Exception("Invalid device ID."); } }
/// <summary> /// Create a new MPL3115A2 object with the default address and speed settings. /// </summary> /// <param name="address">Address of the sensor (default = 0x60).</param> /// <param name="i2cBus">I2cBus (Maximum is 400 kHz).</param> public Mpl3115A2(II2cBus i2cBus, byte address = 0x60) { var device = new I2cPeripheral(i2cBus, address); _mpl3115a2 = device; if (_mpl3115a2.ReadRegister(Registers.WhoAmI) != 0xc4) { throw new Exception("Unexpected device ID, expected 0xc4"); } _mpl3115a2.WriteRegister(Registers.Control1, (byte)(ControlRegisterBits.Active | ControlRegisterBits.OverSample128)); _mpl3115a2.WriteRegister(Registers.DataConfiguration, (byte)(ConfigurationRegisterBits.DataReadyEvent | ConfigurationRegisterBits.EnablePressureEvent | ConfigurationRegisterBits.EnableTemperatureEvent)); }
/// <summary> /// Create a new MAG3110 object using the default parameters for the component. /// </summary> /// <param name="interruptPort">Interrupt port used to detect end of conversions.</param> /// <param name="address">Address of the MAG3110 (default = 0x0e).</param> /// <param name="i2cBus">I2C bus object - default = 400 KHz).</param> public Mag3110(II2cBus i2cBus, IDigitalInputPort interruptPort = null, byte address = 0x0e) { _mag3110 = new I2cPeripheral(i2cBus, address); var deviceID = _mag3110.ReadRegister((byte)Registers.WhoAmI); if (deviceID != 0xc4) { throw new Exception("Unknown device ID, " + deviceID + " retruend, 0xc4 expected"); } if (interruptPort != null) { _digitalInputPort = interruptPort; _digitalInputPort.Changed += DigitalInputPortChanged; } Reset(); }
protected virtual void SetFrequency(int frequency) { double prescaleval = 25000000.0; // # 25MHz prescaleval = prescaleval / 4096.0; //# 12-bit prescaleval = prescaleval / frequency; prescaleval -= 1.0; double prescale = Math.Floor(prescaleval + 0.5); byte oldmode = i2cPeripheral.ReadRegister(Mode1); byte newmode = (byte)((oldmode & 0x7F) | 0x10); // # sleep Write(Mode1, newmode); // # go to sleep Write(PreScale, (byte)((int)(Math.Floor(prescale)))); Write(Mode1, oldmode); Thread.Sleep(5); Write(Mode1, (byte)(oldmode | 0x80 | mode1AI)); }
private byte ReadI2cRegister(byte address) { return(i2cDevice.ReadRegister(address)); }
/// <summary> /// Read resolution /// </summary> public byte GetResolution() { return i2CPeripheral.ReadRegister(MCP_RESOLUTION); }
protected byte Read(byte address) { var result = i2cPeripheral.ReadRegister(address); return(result); }