public void TemperatureImageTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); Temperature[,] referenceImage = new Temperature[Amg88xx.Width, Amg88xx.Height]; Random rnd = new Random(); for (int y = 0; y < Amg88xx.Height; y++) { for (int x = 0; x < Amg88xx.Width; x++) { referenceImage[x, y] = Temperature.FromDegreesCelsius(rnd.Next(-80, 321) * PixelTemperatureResolution); (byte tl, byte th) = Amg88xxUtils.ConvertFromTemperature(referenceImage[x, y]); i2cDevice.DataToRead.Enqueue(tl); i2cDevice.DataToRead.Enqueue(th); } } // read image from sensor sensor.ReadImage(); // expectation: one write access to register T01L (lower byte of first pixel) to trigger readout Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.T01L, i2cDevice.DataWritten.Dequeue()); // expectation: all pixels have been read, so nothing is remaining Assert.Empty(i2cDevice.DataToRead); Assert.Equal(referenceImage, sensor.TemperatureImage); }
public void RawPixelIndexerTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); // using a simple linear sequence of numbers as reference image: // 0 to 0x1f8 (504d) Int16[] referenceImage = new Int16[Amg88xx.PixelCount]; for (int n = 0; n < Amg88xx.PixelCount; n++) { referenceImage[n] = (short)(n * 8); // enqueue lower byte (TLxx) i2cDevice.DataToRead.Enqueue((byte)(referenceImage[n] & 0xff)); // enqueue higher byte (THxx) i2cDevice.DataToRead.Enqueue((byte)(referenceImage[n] >> 8)); } // read image from sensor sensor.ReadImage(); // expectation: one write access to register T01L (lower byte of first pixel) to trigger readout Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.T01L, i2cDevice.DataWritten.Dequeue()); // expectation: all pixels have been read, so nothing is remaining Assert.Empty(i2cDevice.DataToRead); for (int n = 0; n < Amg88xx.PixelCount; n++) { Int16 rawPixel = sensor[n]; Assert.Equal(referenceImage[n], rawPixel); } }
public void FlagResetTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.ResetAllFlags(); Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.RST, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)ResetType.Flag, i2cDevice.DataWritten.Dequeue()); }
public void OperatingModeSetTest(OperatingMode targetOperatingMode, byte expectedMode) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.OperatingMode = targetOperatingMode; Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.PCLT, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedMode, i2cDevice.DataWritten.Dequeue()); }
public void ClearInterruptTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.ClearInterrupt(); Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.SCLR, i2cDevice.DataWritten.Dequeue()); Assert.Equal(1 << (byte)StatusClearBit.INTCLR, i2cDevice.DataWritten.Dequeue()); }
public void ClearAllFlagsTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.ClearAllFlags(); Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.SCLR, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)(1 << (byte)StatusClearBit.OVFCLR) | (1 << (byte)StatusClearBit.OVFTHCLR) | (1 << (byte)StatusClearBit.INTCLR), i2cDevice.DataWritten.Dequeue()); }
public void ClearThermistorOverflowTest() { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.ClearThermistorOverflow(); Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.SCLR, i2cDevice.DataWritten.Dequeue()); Assert.Equal(1 << (byte)StatusClearBit.OVFTHCLR, i2cDevice.DataWritten.Dequeue()); }
public void OperatingModeGetTest(byte registerContent, OperatingMode expectedOperatingMode) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(registerContent); var actualOperatingMode = sensor.OperatingMode; Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.PCLT, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedOperatingMode, actualOperatingMode); }
public void FrameRateGetTest(byte registerContent, FrameRate expectedFrameRate) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(registerContent); var actualFrameRate = sensor.FrameRate; Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.FPSC, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedFrameRate, actualFrameRate); }
public void UseMovingAverageModeGetTest(byte registerContent, bool expectedState) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(registerContent); bool actualState = sensor.UseMovingAverageMode; Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.AVE, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedState, actualState); }
public void HasInterruptTest(byte statusRegisterContent, bool interrupt) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(statusRegisterContent); bool status = sensor.HasInterrupt(); Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.STAT, i2cDevice.DataWritten.Dequeue()); Assert.Equal(interrupt, status); }
public void HasThermistorOverflowTest(byte statusRegisterContent, bool thermistorOverflow) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(statusRegisterContent); bool status = sensor.HasThermistorOverflow(); Assert.Single(i2cDevice.DataWritten); Assert.Equal((byte)Register.STAT, i2cDevice.DataWritten.Dequeue()); Assert.Equal(thermistorOverflow, status); }
public void InterruptPinEnabledGetTest(bool expectedState, byte registerValue) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(registerValue); bool actualState = sensor.InterruptPinEnabled; Assert.Single(i2cDevice.DataWritten); // register address is expected two times: once for reading the current register value and once for writing the new one Assert.Equal((byte)Register.INTC, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedState, actualState); }
public void SensorTemperatureGetTest(byte th, byte tl, double expectedTemperature) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); // target temperature is 25°C which is equivalent to 0x190 i2cDevice.DataToRead.Enqueue(tl); i2cDevice.DataToRead.Enqueue(th); Assert.Equal(expectedTemperature, sensor.SensorTemperature.DegreesCelsius); Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.TTHL, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)Register.TTHH, i2cDevice.DataWritten.Dequeue()); }
public void InterruptPinEnabledSetTest(byte initialRegisterContent, bool targetState, byte expectedRegisterContent) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(initialRegisterContent); sensor.InterruptPinEnabled = targetState; Assert.Equal(3, i2cDevice.DataWritten.Count); // register address is expected two times: once for reading the current register value and once for writing the new one Assert.Equal((byte)Register.INTC, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)Register.INTC, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedRegisterContent, i2cDevice.DataWritten.Dequeue()); }
public void UseMovingAverageModeSetTest(bool targetState, byte initialRegisterContent, byte expectedRegisterContent) { I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); // bit 5: if set, moving average is on i2cDevice.DataToRead.Enqueue(initialRegisterContent); sensor.UseMovingAverageMode = targetState; Assert.Equal(3, i2cDevice.DataWritten.Count); // register address is expected two times: once for reading the current register value and once for writing the new one Assert.Equal((byte)Register.AVE, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)Register.AVE, i2cDevice.DataWritten.Dequeue()); Assert.Equal(expectedRegisterContent, i2cDevice.DataWritten.Dequeue()); }
public void InterruptHysteresisLevelSetTest() { Temperature expectedTemperature = Temperature.FromDegreesCelsius(125); I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); sensor.InterruptHysteresis = expectedTemperature; Assert.Equal(4, i2cDevice.DataWritten.Count); (byte refTl, byte refTh) = Amg88xxUtils.ConvertFromTemperature(expectedTemperature); Assert.Equal((byte)Register.INTSL, i2cDevice.DataWritten.Dequeue()); Assert.Equal(refTl, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)Register.INTSH, i2cDevice.DataWritten.Dequeue()); Assert.Equal(refTh, i2cDevice.DataWritten.Dequeue()); }
public void InterruptHysteresisLevelGetTest() { // expected temeperature: 72.75°C // two's complement representation: byte expectedTl = 0x23; byte expectedTh = 0x01; I2cTestDevice i2cDevice = new I2cTestDevice(); Amg88xx sensor = new Amg88xx(i2cDevice); i2cDevice.DataToRead.Enqueue(expectedTl); i2cDevice.DataToRead.Enqueue(expectedTh); Temperature actualTemperature = sensor.InterruptHysteresis; Assert.Equal(2, i2cDevice.DataWritten.Count); Assert.Equal((byte)Register.INTSL, i2cDevice.DataWritten.Dequeue()); Assert.Equal((byte)Register.INTSH, i2cDevice.DataWritten.Dequeue()); Assert.Equal(Amg88xxUtils.ConvertToTemperature(expectedTl, expectedTh).DegreesCelsius, actualTemperature.DegreesCelsius); }
public Result <Unit> Init() { _controller = new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver()); _controller.OpenPin(Pin.HallBottom, PinMode.InputPullUp); _controller.OpenPin(Pin.HallTop, PinMode.InputPullUp); _controller.OpenPin(Pin.PhotoelectricBarrier, PinMode.InputPullUp); _controller.OpenPin(Pin.MotorEnable, PinMode.Output); _controller.OpenPin(Pin.MotorLeft, PinMode.Output); _controller.OpenPin(Pin.MotorRight, PinMode.Output); _controller.OpenPin(Pin.EmergencyTop, PinMode.InputPullUp); _controller.OpenPin(Pin.DC12_1, PinMode.Output); _controller.OpenPin(Pin.DC12_2, PinMode.Output); //_pwmMotor = new SoftwarePwmChannel(Pin.MotorEnable, 200, 0.1); //_pwmMotor.Start(); _controller.Write(Pin.MotorEnable, PinValue.High); _controller.Write(Pin.MotorLeft, PinValue.Low); _controller.Write(Pin.MotorRight, PinValue.Low); _controller.Write(Pin.DC12_1, PinValue.Low); _controller.Write(Pin.DC12_2, PinValue.Low); Console.WriteLine($"Init sensor"); _bh1750Fvi = new Bh1750fvi(I2cDevice.Create(new I2cConnectionSettings(1, Bh1750fviExtenstion.DefaultI2cAddress))); // 23 _vl53L0X = new Vl53L0X(I2cDevice.Create(new I2cConnectionSettings(1, Vl53L0X.DefaultI2cAddress))); // 29 _bme280 = new Bme280(I2cDevice.Create(new I2cConnectionSettings(1, Bmx280Base.SecondaryI2cAddress))); // 76 _measurementTime = _bme280.GetMeasurementDuration(); _bme280.SetPowerMode(Bmx280PowerMode.Normal); //Thread.Sleep(_measurementTime); //_bme280.TryReadTemperature(out var tempValue); //_bme280.TryReadPressure(out var preValue); //_bme280.TryReadHumidity(out var humValue); //_bme280.TryReadAltitude(out var altValue); //Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); //Console.WriteLine($"Pressure: {preValue.Hectopascals:#.##} hPa"); //Console.WriteLine($"Relative humidity: {humValue.Percent:#.##}%"); //Console.WriteLine($"Estimated altitude: {altValue.Meters:#} m"); _amg88xx = new Amg88xx(I2cDevice.Create(new I2cConnectionSettings(1, Amg88xx.AlternativeI2cAddress))); // 69 try { _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress))); // 68 } catch (IOException e) { Console.WriteLine("AK8963 is exposed, try to connect directly."); _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress)), i2CDeviceAk8963: I2cDevice.Create(new I2cConnectionSettings(1, Ak8963.DefaultI2cAddress))); } _mpu9250.MagnetometerMeasurementMode = MeasurementMode.ContinuousMeasurement100Hz; Thread.Sleep(100); Console.WriteLine($"Finished Init sensor"); Run(); Thread.Sleep(100); Calibrate(); return(Unit.Instance); }