2021-08-23 15:21:38 +00:00
|
|
|
import plasma
|
|
|
|
from plasma import plasma2040
|
|
|
|
|
2021-08-23 15:28:27 +00:00
|
|
|
# Import helpers for RGB LEDs and Buttons
|
|
|
|
from pimoroni import RGBLED, Button
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
# Import bme68x and I2C helper
|
2021-08-23 15:28:27 +00:00
|
|
|
from breakout_bme68x import BreakoutBME68X
|
2021-08-23 15:21:38 +00:00
|
|
|
from pimoroni_i2c import PimoroniI2C
|
|
|
|
|
2021-08-24 10:41:56 +00:00
|
|
|
# Uses a BME68x to monitor the ambient temperature, pressure and humidity, and show them as bars on an LED strip.
|
2021-08-23 15:21:38 +00:00
|
|
|
# Press "A" to cycle to the next mode.
|
|
|
|
# Press "B" to cycle to the previous mode.
|
|
|
|
|
|
|
|
# Set how many LEDs you have
|
|
|
|
NUM_LEDS = 30
|
|
|
|
|
|
|
|
# How many times the LEDs will be updated per second
|
|
|
|
UPDATES = 60
|
|
|
|
|
|
|
|
# The temperature range to show (in degrees celsius)
|
|
|
|
TEMPERATURE_C_MIN = 20.0
|
|
|
|
TEMPERATURE_C_MAX = 35.0
|
|
|
|
|
|
|
|
# The pressure range to show (in pascals)
|
|
|
|
PRESSURE_PA_MIN = 87000.0
|
|
|
|
PRESSURE_PA_MAX = 108500.0
|
|
|
|
|
|
|
|
# The humidity range to show (in percent)
|
|
|
|
HUMIDITY_MIN = 0.0
|
|
|
|
HUMIDITY_MAX = 100.0
|
|
|
|
|
|
|
|
|
|
|
|
# The start and end hues for the temperature range
|
|
|
|
TEMPERATURE_HUE_START = 0.667
|
|
|
|
TEMPERATURE_HUE_END = 1.0
|
|
|
|
|
|
|
|
# The start and end hues for the pressure range
|
|
|
|
PRESSURE_HUE_START = 0.333
|
|
|
|
PRESSURE_HUE_END = 0.0
|
|
|
|
|
|
|
|
# The start and end hues for the humidity range
|
|
|
|
HUMIDITY_HUE_START = 0.333
|
|
|
|
HUMIDITY_HUE_END = 0.667
|
|
|
|
|
|
|
|
|
|
|
|
# Pick *one* LED type by uncommenting the relevant line below:
|
|
|
|
|
|
|
|
# APA102 / DotStar™ LEDs
|
|
|
|
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK)
|
|
|
|
|
|
|
|
# WS2812 / NeoPixel™ LEDs
|
2021-08-23 15:28:27 +00:00
|
|
|
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
button_a = Button(plasma2040.BUTTON_A, repeat_time=0)
|
|
|
|
button_b = Button(plasma2040.BUTTON_B, repeat_time=0)
|
|
|
|
led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
|
|
|
|
|
2021-08-24 15:06:44 +00:00
|
|
|
PINS_PLASMA2040 = {"sda": plasma2040.SDA, "scl": plasma2040.SCL}
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
i2c = PimoroniI2C(**PINS_PLASMA2040)
|
|
|
|
bme = BreakoutBME68X(i2c)
|
|
|
|
|
|
|
|
|
|
|
|
ALL, TEMPERATURE, PRESSURE, HUMIDITY = range(4)
|
|
|
|
|
2021-08-23 15:28:27 +00:00
|
|
|
|
2021-08-23 15:21:38 +00:00
|
|
|
# Maps a value from one range to another
|
|
|
|
def map(x, in_min, in_max, out_min, out_max):
|
|
|
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
|
|
|
|
|
2021-08-23 15:28:27 +00:00
|
|
|
|
2021-08-23 15:21:38 +00:00
|
|
|
# Sets a section of the led strip to show a hue gradient based on the provided percent
|
|
|
|
def colour_gauge(percent, start_led, end_led, start_hue, end_hue):
|
|
|
|
if end_led > start_led:
|
|
|
|
length = end_led - start_led
|
|
|
|
light_pixels = percent * float(length)
|
|
|
|
|
|
|
|
for i in range(length):
|
|
|
|
h = map(i, 0.0, float(length - 1), start_hue, end_hue)
|
|
|
|
|
|
|
|
i2 = i + 1
|
|
|
|
if i2 <= light_pixels:
|
|
|
|
led_strip.set_hsv(i + start_led, h, 1.0, 1.0)
|
|
|
|
elif i <= light_pixels:
|
|
|
|
scale = map(light_pixels, float(i), float(i2), 0.0, 1.0)
|
|
|
|
led_strip.set_hsv(i + start_led, h, 1.0, scale)
|
|
|
|
else:
|
|
|
|
led_strip.set_hsv(i + start_led, 0.0, 0.0, 0.0)
|
|
|
|
|
|
|
|
|
|
|
|
first_third = int(NUM_LEDS / 3)
|
|
|
|
second_third = int((NUM_LEDS * 2) / 3)
|
|
|
|
|
|
|
|
mode = ALL
|
|
|
|
|
|
|
|
# Start updating the LED strip
|
|
|
|
led_strip.start()
|
2021-08-23 15:28:27 +00:00
|
|
|
|
2021-08-23 15:21:38 +00:00
|
|
|
while True:
|
|
|
|
temperature, pressure, humidity, _, _, _, _ = bme.read()
|
|
|
|
print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%".format(
|
|
|
|
temperature, pressure, humidity))
|
2021-08-23 15:28:27 +00:00
|
|
|
|
2021-08-23 15:21:38 +00:00
|
|
|
if mode == ALL:
|
|
|
|
t = map(temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, 0, first_third, TEMPERATURE_HUE_START, TEMPERATURE_HUE_END)
|
|
|
|
|
|
|
|
t = map(pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, first_third, second_third, PRESSURE_HUE_START, PRESSURE_HUE_END)
|
|
|
|
|
|
|
|
t = map(humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, second_third, NUM_LEDS, HUMIDITY_HUE_START, HUMIDITY_HUE_END)
|
|
|
|
|
|
|
|
elif mode == TEMPERATURE:
|
|
|
|
t = map(temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, 0, NUM_LEDS, TEMPERATURE_HUE_START, TEMPERATURE_HUE_END)
|
|
|
|
|
|
|
|
elif mode == PRESSURE:
|
|
|
|
t = map(pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, 0, NUM_LEDS, PRESSURE_HUE_START, PRESSURE_HUE_END)
|
|
|
|
|
|
|
|
elif mode == HUMIDITY:
|
|
|
|
t = map(humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0, 1.0)
|
|
|
|
colour_gauge(t, 0, NUM_LEDS, HUMIDITY_HUE_START, HUMIDITY_HUE_END)
|
2021-08-23 15:28:27 +00:00
|
|
|
|
2021-08-23 15:21:38 +00:00
|
|
|
a_pressed = button_a.read()
|
|
|
|
b_pressed = button_b.read()
|
|
|
|
|
|
|
|
if mode == ALL:
|
|
|
|
led.set_rgb(127, 127, 127)
|
2021-08-23 15:28:27 +00:00
|
|
|
if a_pressed:
|
|
|
|
mode = TEMPERATURE
|
|
|
|
elif b_pressed:
|
|
|
|
mode = HUMIDITY
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
elif mode == TEMPERATURE:
|
|
|
|
led.set_rgb(255, 0, 255)
|
2021-08-23 15:28:27 +00:00
|
|
|
if a_pressed:
|
|
|
|
mode = PRESSURE
|
|
|
|
elif b_pressed:
|
|
|
|
mode = ALL
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
elif mode == PRESSURE:
|
|
|
|
led.set_rgb(255, 255, 0)
|
2021-08-23 15:28:27 +00:00
|
|
|
if a_pressed:
|
|
|
|
mode = HUMIDITY
|
|
|
|
elif b_pressed:
|
|
|
|
mode = TEMPERATURE
|
2021-08-23 15:21:38 +00:00
|
|
|
|
|
|
|
elif mode == HUMIDITY:
|
|
|
|
led.set_rgb(0, 255, 255)
|
2021-08-23 15:28:27 +00:00
|
|
|
if a_pressed:
|
|
|
|
mode = ALL
|
|
|
|
elif b_pressed:
|
|
|
|
mode = PRESSURE
|