def test_return_freezing_given_actual_data(self):
     calibrator = Calibrator(self._hardware_calibration_coefficients,
                             253952, 524288)
     self.assertAlmostEqual(calibrator.temperature(209505),
                            0,
                            places=-1,
                            msg="Fails to return 0degC given data that "
                            "matches that.")
 def test_returns_3_atm_pressure_given_actual_data(self):
     calibrator = Calibrator(self._hardware_calibration_coefficients,
                             253952, 524288)
     self.assertAlmostEqual(calibrator.pressure(-3274260, 209505),
                            303000,
                            places=-1,
                            msg="Fails to return 3 atm pressure given "
                            "data that matches that.")
 def test_return_standard_temperature_given_actual_data(self):
     calibrator = Calibrator(self._hardware_calibration_coefficients,
                             253952, 524288)
     self.assertAlmostEqual(calibrator.temperature(167393),
                            20,
                            places=-1,
                            msg="Fails to return 20degC given data that "
                            "matches that.")
 def test_returns_2_atm_pressure_given_actual_data(self):
     calibrator = Calibrator(self._hardware_calibration_coefficients,
                             253952, 524288)
     self.assertAlmostEqual(calibrator.pressure(-3132150, 167393),
                            202000,
                            places=-1,
                            msg="Fails to return 2 atm pressure given "
                            "data that matches that.")
 def test_one_coefficients_and_data(self):
     calibrator = Calibrator((1, 1, 1, 1, 1, 1, 1, 1, 1), 1, 1)
     self.assertAlmostEqual(calibrator.pressure(1, 1),
                            7.0,
                            msg="Fails to return 7.0 pressure when "
                            "all coefficients and data equal 1.0.")
     self.assertAlmostEqual(calibrator.temperature(1),
                            1.5,
                            msg="Fails to return 1.5 temperature "
                            "when all coefficients and data equal 1.0.")
 def test_zero_coefficients_and_non_zero_data(self):
     calibrator = Calibrator((0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 1, 1)
     self.assertAlmostEqual(calibrator.pressure(10, 10),
                            0.0,
                            msg="Fails to return 0 pressure when all "
                            "coefficients equal 0.")
     self.assertAlmostEqual(calibrator.temperature(10),
                            0.0,
                            msg="Fails to return 0 temperature when "
                            "all coefficient equal 0.")
 def test_returns_standard_pressure_given_actual_data(self):
     calibrator = Calibrator(self._hardware_calibration_coefficients,
                             253952, 524288)
     # Raw pressure and temperature found by solving the compensating
     # equation for raw pressure and temperature given the hardware
     # calibration coefficients.
     # This is true for this and the following few tests.
     self.assertAlmostEqual(calibrator.pressure(-2968390, 167393),
                            101000,
                            places=-1,
                            msg="Fails to return sea level pressure given "
                            "data that matches that.")
 def test_one_coefficients_and_zero_data(self):
     calibrator = Calibrator((1, 1, 1, 1, 1, 1, 1, 1, 1), 1, 1)
     self.assertAlmostEqual(calibrator.pressure(0, 0),
                            1.0,
                            msg="Fails to return 1.0 pressure when "
                            "all coefficients equal 1.0 and data "
                            "equals 0.")
     self.assertAlmostEqual(calibrator.temperature(0),
                            0.5,
                            msg="Fails to return 1.5 temperature "
                            "when all coefficients equal 1.0 and "
                            "data equals 0.")
 def test_zero_scaling_factor_raises_exception(self):
     with self.assertRaises(ZeroDivisionError,
                            msg="Fails to raise a ZeroDivisionError "
                            "when initialized with a scaling factor "
                            "equal to 0."):
         calibrator = Calibrator((0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                                 0,
                                 0)
import constants

if "__main__" == __name__:
    print("--Initialization")
    mux = I2CMux(constants.PRESSURE_SENSOR_MUX_ADDRESS)
    mux.select_channel(0)
    comms = Communicator(dump_communication=False)
    print("--Set Op Mode")
    comms.set_op_mode(PressureSensor.OpMode.command)
    print("--Set Pressure Sampling")
    comms.set_pressure_sampling()
    print("--Set Temperature Sampling")
    comms.set_temperature_sampling()

    calibrator = Calibrator(comms.calibration_coefficients,
                            comms.pressure_scale_factor,
                            comms.temperature_scale_factor)

    try:
        while True:
            print("--Get Pressure")
            raw_pressure = comms.raw_pressure()
            print("--Get Temperature")
            raw_temperature = comms.raw_temperature()
            pressure = calibrator.pressure(raw_pressure, raw_temperature)
            temperature = calibrator.temperature(raw_temperature)
            print(f"Time:\t\t{time.time()*1000}\n"
                  f"Pressure:\t{pressure}\n"
                  f"Temperature:\t{temperature}")
            time.sleep(1.0)
    finally: