Skip to content

Commit

Permalink
Examples: Slight code tidyup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Feb 9, 2024
1 parent beb2f5b commit aada6a4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions examples/compensated-temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)


# Gets the CPU temperature in degrees C
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
process = Popen(["vcgencmd", "measure_temp"], stdout=PIPE)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
return float(output[output.index("=") + 1 : output.rindex("'")])


factor = 1.2 # Smaller numbers adjust temp down, vice versa
smooth_size = 10 # Dampens jitter due to rapid CPU temp changes
Expand All @@ -41,6 +43,6 @@ def get_cpu_temperature():
raw_temp = bmp280.get_temperature()
comp_temp = raw_temp - ((smoothed_cpu_temp - raw_temp) / factor)

print("Compensated temperature: {:05.2f} *C".format(comp_temp))
print(f"Compensated temperature: {comp_temp:05.2f} *C")

time.sleep(1.0)
4 changes: 2 additions & 2 deletions examples/dump-calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
bmp280.setup()

for key in dir(bmp280.calibration):
if key.startswith('dig_'):
if key.startswith("dig_"):
value = getattr(bmp280.calibration, key)
print('{} = {}'.format(key, value))
print(f"{key} = {value}")
4 changes: 2 additions & 2 deletions examples/relative-altitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
baseline_values = []
baseline_size = 100

print("Collecting baseline values for {:d} seconds. Do not move the sensor!\n".format(baseline_size))
print(f"Collecting baseline values for {baseline_size:d} seconds. Do not move the sensor!\n")

for i in range(baseline_size):
pressure = bmp280.get_pressure()
Expand All @@ -30,5 +30,5 @@

while True:
altitude = bmp280.get_altitude(qnh=baseline)
print('Relative altitude: {:05.2f} metres'.format(altitude))
print(f"Relative altitude: {altitude:05.2f} metres")
time.sleep(1)
2 changes: 1 addition & 1 deletion examples/temperature-and-pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
while True:
temperature = bmp280.get_temperature()
pressure = bmp280.get_pressure()
print('{:05.2f}*C {:05.2f}hPa'.format(temperature, pressure))
print(f"{temperature:05.2f}*C {pressure:05.2f}hPa")
time.sleep(1)
2 changes: 1 addition & 1 deletion examples/temperature-forced-mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

while True:
temperature = bmp280.get_temperature()
print('{:05.2f}*C'.format(temperature))
print("{temperature:05.2f}*C")
time.sleep(1)

0 comments on commit aada6a4

Please sign in to comment.