pimoroni / sgp30-python

Python library for the SGP30 air quality sensor
https://shop.pimoroni.com/products/sgp30-air-quality-sensor-breakout
MIT License
37 stars 18 forks source link

set_humindity does not accept int #9

Closed Rayn0r closed 8 months ago

Rayn0r commented 3 years ago

Calling set_humidity will fail when an int object is passed to the command function. It works great for set_baseline with two arguments, but does not if you pass a single one.

My first attempt for a function to calculation the absolute humindity in g/m³ looks like this(it works in python 2.7, 3.4 and 3.9.0 here):

def calcabsolutehumidtyvalue(temp,hum):
    exponent = (17.67*temp)/(temp+243.5)
    firstres = math.exp(exponent)
    result = (6.112*firstres*hum*2.1674)/(273.15+temp)
    integerpart = math.floor(result)
    decimalpart = result - integerpart
    twofiftyfithpart = round(255 * decimalpart)
    shiftint = int(integerpart) << 8
    finalvalue = shiftint + int(twofiftyfithpart)
    valuetowrite = "0x{:04x}".format(finalvalue)
    val=int(valuetowrite, 16)
    return val

The relevant part of my test_script looks like this:

from sgp30 import SGP30
sgp30 = SGP30()
result =  sgp30.command('set_humidity', calcabsolutehumidtyvalue(23.4,37.6))

Upon calling it, I get:

File "/usr/local/lib/python3.4/dist-packages/sgp30/init.py", line 58, in command parameters = list(parameters) TypeError: 'int' object is not iterable

Calling it with a list object, makes it work:

result =  sgp30.command('set_humidity', [calcabsolutehumidtyvalue(23.4,37.6)])

How can it be fixed, so that one can pass an int to set_humidity?

André

Gadgetoid commented 3 years ago

Just use a list- since that argument expects a list of "parameters" and a list of one is fine. There's no sense in changing the command method, since it's not really for end users.

The approach here - I guess - would be to add a set_humidity function to the underlying class that hides this little detail.