fossasia / pslab-python

Python Library for PSLab Desktop: https://pslab.io
GNU General Public License v3.0
1.63k stars 225 forks source link

BMP180: migrate to new I2C API #192

Closed orangecms closed 1 year ago

orangecms commented 3 years ago

I am a bit unsure regarding the "extra" API in the I2C sensors. It looks like it was quite tightly coupled to the old Python desktop app. Let's discuss that in the next meeting.

orangecms commented 3 years ago

WIth this

from pslab import ScienceLab
from pslab.external.BMP180 import BMP180
from time import sleep

psl = ScienceLab()  # This initializes the I2C bus.

sensor = BMP180()

data = sensor.readTemperature()
print(data)
sleep(2)
data = sensor.readPressure()
print(data)

I get

calib: 1.2451171875 0.001999969482421875 0.006079673767089844 0.012499809265136719 65535.0 20.400000000000002 1.59375 255 4.98046875 0.194549560546875 65.535 0.0024901963770389557 2.364375 0.9929838180541992 4.42087184637785e-06
12.8
996.8078400249741

As of now. 12.8 looks quite odd for the temperature, and I always get that same value across multiple reads, using a BME280. It's definitely not 12.8 Celsius here. Almost 1000 (hPa) for the pressure is reasonable, but I also get the same value across multiple reads. Something is :fish:y.

bessman commented 3 years ago

As of now. 12.8 looks quite odd for the temperature, and I always get that same value across multiple reads, using a BME280. It's definitely not 12.8 Celsius here. Almost 1000 (hPa) for the pressure is reasonable, but I also get the same value across multiple reads. Something is 🐟y.

From a brief look at the code, it looks like in order to tell the sensor to actually measure anything you need call initTemperature/initPressure. readTemperature/readPressure just fetches a register value which is static until a measurement command is sent.

However, both initTemperature and initPressure are broken because the signature of I2CSlave.write changed.

orangecms commented 3 years ago

From a brief look at the code, it looks like in order to tell the sensor to actually measure anything you need call initTemperature/initPressure. readTemperature/readPressure just fetches a register value which is static until a measurement command is sent.

Isn't that happening in the init?

However, both initTemperature and initPressure are broken because the signature of I2CSlave.write changed.

Oh then I did something wrong with that part; what's the correct way to use it now?

bessman commented 3 years ago

Isn't that happening in the init?

It is, but only once, and since both temperature and pressure data is stored in the same register (_REG_RESULT) the temperature is immediately overwritten.

what's the correct way to use it now?

Same as I2CSlave.read, i.e. first argument is a list of bytes to write into the register specified by the optional second argument (which defaults to 0x0). So instead of:

def initTemperature(self):
    self.write([self._REG_CONTROL, self._CMD_TEMP])

it should be

def initTemperature(self):
    self.write([self._CMD_TEMP], self._REG_CONTROL)
bessman commented 3 years ago

Data sheet: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf