Added micropython version of bme example, and backported some fixes

pull/194/head
ZodiusInfuser 2021-08-23 16:21:38 +01:00
rodzic bfcb688264
commit 88a9449e0a
4 zmienionych plików z 157 dodań i 5 usunięć

Wyświetl plik

@ -11,6 +11,11 @@
#include "rgbled.hpp"
#include "button.hpp"
/*
Press "A" to cycle to the next mode.
Press "B" to cycle to the previous mode.
*/
using namespace pimoroni;
using namespace plasma;
@ -49,15 +54,15 @@ constexpr float HUMIDITY_HUE_END = 0.667f;
// Pick *one* LED type by uncommenting the relevant line below:
// APA102-style LEDs with Data/Clock lines. AKA DotStar
APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
//APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
// WS28X-style LEDs with a single signal line. AKA NeoPixel
//WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
Button button_a(plasma2040::BUTTON_A);
Button button_b(plasma2040::BUTTON_B);
Button button_a(plasma2040::BUTTON_A, ACTIVE_LOW, 0);
Button button_b(plasma2040::BUTTON_B, ACTIVE_LOW, 0);
RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
@ -120,7 +125,7 @@ int main() {
auto result = bme68x.read_forced(&data);
(void)result;
printf("%.2f, %.2f, %.2f\n", data.temperature, data.pressure, data.humidity);
printf("%.2fc, %.2fPa, %.2f%%\n", data.temperature, data.pressure, data.humidity);
switch(mode) {
case DisplayMode::ALL:

Wyświetl plik

@ -0,0 +1,147 @@
import plasma
from plasma import plasma2040
import time
# Import helpers for RGB LEDs, Buttons, and Analog
from pimoroni import RGBLED, Button, Analog
# Import bme68x and I2C helper
from breakout_bme68x import BreakoutBME68X, STATUS_HEATER_STABLE
from pimoroni_i2c import PimoroniI2C
# 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
# led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
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)
PINS_PLASMA2040 = {"sda": 20, "scl": 21}
i2c = PimoroniI2C(**PINS_PLASMA2040)
bme = BreakoutBME68X(i2c)
ALL, TEMPERATURE, PRESSURE, HUMIDITY = range(4)
# 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
# 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()
while True:
temperature, pressure, humidity, _, _, _, _ = bme.read()
print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%".format(
temperature, pressure, humidity))
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)
a_pressed = button_a.read()
b_pressed = button_b.read()
if mode == ALL:
led.set_rgb(127, 127, 127)
if a_pressed: mode = TEMPERATURE
elif b_pressed: mode = HUMIDITY
elif mode == TEMPERATURE:
led.set_rgb(255, 0, 255)
if a_pressed: mode = PRESSURE
elif b_pressed: mode = ALL
elif mode == PRESSURE:
led.set_rgb(255, 255, 0)
if a_pressed: mode = HUMIDITY
elif b_pressed: mode = TEMPERATURE
elif mode == HUMIDITY:
led.set_rgb(0, 255, 255)
if a_pressed: mode = ALL
elif b_pressed: mode = PRESSURE