Merge pull request #107 from pimoroni/patch-temperature-demo

Improved thermometer demo
This commit is contained in:
Philip Howard 2021-03-30 12:38:00 +01:00 committed by GitHub
commit 500581d679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 22 deletions

View File

@ -2,24 +2,52 @@
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner!
import machine
import utime
import utime
import gc
# Pico Display boilerplate
import picodisplay as display
width = display.get_width()
height = display.get_height()
gc.collect()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
# reads from Pico's temp sensor and converts it into a more manageable number
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
conversion_factor = 3.3 / (65535)
temp_min = 10
temp_max = 30
bar_width = 5
# Set the display backlight to 50%
display.set_backlight(0.5)
temperatures = []
colors = [(0, 0, 255), (0, 255, 0), (255, 255, 0), (255, 0, 0)]
def temperature_to_color(temp):
temp = min(temp, temp_max)
temp = max(temp, temp_min)
f_index = float(temp - temp_min) / float(temp_max - temp_min)
f_index *= len(colors) - 1
index = int(f_index)
if index == len(colors) - 1:
return colors[index]
blend_b = f_index - index
blend_a = 1.0 - blend_b
a = colors[index]
b = colors[index + 1]
return [int((a[i] * blend_a) + (b[i] * blend_b)) for i in range(3)]
while True:
# fills the screen with black
display.set_pen(0, 0, 0)
@ -27,43 +55,36 @@ while True:
# the following two lines do some maths to convert the number from the temp sensor into celsius
reading = sensor_temp.read_u16() * conversion_factor
temperature = round(27 - (reading - 0.706) / 0.001721)
temperature = 27 - (reading - 0.706) / 0.001721
temperatures.append(temperature)
# shifts the temperatures history to the left by one sample
if len(temperatures) > 48: # 240/5
temperatures = temperatures[1::]
else:
temperatures.append(temperature)
if len(temperatures) > width // bar_width:
temperatures.pop(0)
i = 0
for t in temperatures:
# chooses a pen colour based on the temperature
display.set_pen(0, 255, 0)
if t > 20:
display.set_pen(255, 0, 0)
elif t < 13:
display.set_pen(0, 0, 255)
display.set_pen(*temperature_to_color(t))
# draws the reading as a tall, thin rectangle
display.rectangle(i, height - (t * 4), 5, height)
display.rectangle(i, height - (round(t) * 4), bar_width, height)
# the next tall thin rectangle needs to be drawn 5 pixels to the right of the last one
i += 5
# the next tall thin rectangle needs to be drawn
# "bar_width" (default: 5) pixels to the right of the last one
i += bar_width
# heck lets also set the LED to match
display.set_led(0, 255, 0)
if temperature > 20:
display.set_led(255, 0, 0)
elif temperature < 13:
display.set_led(0, 0, 255)
display.set_led(*temperature_to_color(temperature))
# draws a white background for the text
display.set_pen(255, 255, 255)
display.rectangle(1, 1, 50, 25)
display.rectangle(1, 1, 100, 25)
# writes the reading as text in the white rectangle
display.set_pen(0, 0, 0)
display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 3)
display.text("{:.2f}".format(temperature) + "c", 3, 3, 0, 3)
# time to update the display
display.update()