Enviro MicroPython examples (#406)

Co-authored-by: Gee Bartlett <geeus81@gmail.com>
Co-authored-by: helgibbons <50950368+helgibbons@users.noreply.github.com>
pull/412/head
Gee Bartlett 2022-06-28 10:11:03 +01:00 zatwierdzone przez GitHub
rodzic e73119484b
commit 2653cc7df5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
6 zmienionych plików z 461 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,62 @@
"""BME688 / BME680 demo
This demo will work for both the BME680 and BME688.
"""
import time
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
from pimoroni import RGBLED
from breakout_bme68x import BreakoutBME68X, STATUS_HEATER_STABLE
from pimoroni_i2c import PimoroniI2C
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
bmp = BreakoutBME68X(i2c, address=0x77)
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
led = RGBLED(6, 7, 8, invert=True) # seting pins for the RGB led
led.set_rgb(0, 0, 0)
# setup background
BG = display.create_pen(0, 0, 0)
TEXT = display.create_pen(255, 255, 255)
TEMP = display.create_pen(255, 0, 0)
PRESS = display.create_pen(255, 255, 0)
HUMID = display.create_pen(0, 255, 0)
display.set_pen(BG)
display.clear()
def draw_graph(temp_value, press_value, humid_value):
scaled_temp = int(temp_value + 40) # Range from -40 to +60 = 1-100
scaled_press = int((press_value - 87000) / ((108400 - 87000) / 100)) # Range 108400 - 87000 = 1 -100
scaled_humid = int(humid_value) # Range 1 - 100
display.set_pen(BG)
display.clear()
display.set_pen(TEMP)
display.rectangle(0, 60, scaled_temp, 60)
display.text("TEMP: {:0.2f}c".format(temp_value), scaled_temp + 5, 80, scale=2)
display.set_pen(PRESS)
display.text("PRESS: {:0.2f}Pa".format(press_value), scaled_press + 5, 140, scale=2)
display.rectangle(0, 120, scaled_press, 60)
display.set_pen(HUMID)
display.text("HUMID: {:0.2f}%".format(humid_value), scaled_humid + 5, 200, scale=2)
display.rectangle(0, 180, scaled_humid, 60)
display.set_pen(TEXT)
display.text("BME688 enviro Sensor", 5, 10, scale=2)
display.update()
while True:
temperature, pressure, humidity, gas, status, _, _ = bmp.read()
draw_graph(temperature, pressure, humidity)
heater = "Stable" if status & STATUS_HEATER_STABLE else "Unstable"
print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%, {:0.2f} Ohms, Heater: {}".format(
temperature, pressure, humidity, gas, heater))
time.sleep(1.0)

Wyświetl plik

@ -0,0 +1,69 @@
# This example shows you a simple, non-interrupt way of reading Pico Enviro's buttons with a loop that checks to see if buttons are pressed.
import time
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
display.set_font("bitmap8")
button_a = Button(12, invert=True)
button_b = Button(13, invert=True)
button_x = Button(14, invert=True)
button_y = Button(15, invert=True)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
TEAL = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
RED = display.create_pen(255, 0, 0)
GREEN = display.create_pen(0, 255, 0)
BLUE = display.create_pen(0, 0, 255)
WIDTH, HEIGHT = display.get_bounds()
while True:
if button_a.is_pressed: # if a button press is detected then...
display.set_pen(BLACK) # set pen to black
display.clear() # clear display to the pen colour
display.set_pen(WHITE) # change the pen colour
display.text("Button A pressed", 10, 10, WIDTH - 10, 3) # display some text on the screen
display.update() # update the display
time.sleep(1) # pause for a sec
elif button_b.is_pressed:
display.set_pen(BLACK)
display.clear()
display.set_pen(TEAL)
display.text("Button B pressed", 10, 10, WIDTH - 10, 3)
display.update()
time.sleep(1)
elif button_x.is_pressed:
display.set_pen(BLACK)
display.clear()
display.set_pen(MAGENTA)
display.text("Button X pressed", 10, 10, WIDTH - 10, 3)
display.update()
time.sleep(1)
elif button_y.is_pressed:
display.set_pen(BLACK)
display.clear()
display.set_pen(YELLOW)
display.text("Button Y pressed", 10, 10, WIDTH - 10, 3)
display.update()
time.sleep(1)
else:
display.set_pen(BLACK)
display.clear()
display.set_pen(RED)
display.text("Press any button!", 10, 10, WIDTH, 3)
display.update()
time.sleep(0.1) # this number is how frequently the pico checks for button presses

Wyświetl plik

@ -0,0 +1,95 @@
import time
import random
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
from pimoroni import RGBLED
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
led = RGBLED(6, 7, 8)
led.set_rgb(255, 255, 255)
WIDTH, HEIGHT = display.get_bounds()
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
class Ball:
def __init__(self, x, y, r, dx, dy, pen):
self.x = x
self.y = y
self.r = r
self.dx = dx
self.dy = dy
self.pen = pen
# initialise shapes
balls = []
for i in range(0, 100):
r = random.randint(0, 10) + 3
balls.append(
Ball(
random.randint(r, r + (WIDTH - 2 * r)),
random.randint(r, r + (HEIGHT - 2 * r)),
r,
(14 - r) / 2,
(14 - r) / 2,
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
)
)
hue = 0
while True:
hue += 1
r, g, b = [int(255 * c) for c in hsv_to_rgb(hue / 360.0, 1.0, 1.0)] # rainbow magic
led.set_rgb(r, g, b) # pretty colours Led
BG = display.create_pen(r, g, b)
display.set_pen(BG)
display.clear()
for ball in balls:
ball.x += ball.dx
ball.y += ball.dy
xmax = WIDTH - ball.r
xmin = ball.r
ymax = HEIGHT - ball.r
ymin = ball.r
if ball.x < xmin or ball.x > xmax:
ball.dx *= -1
if ball.y < ymin or ball.y > ymax:
ball.dy *= -1
display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))
display.update()
time.sleep(0.01)

Wyświetl plik

@ -0,0 +1,58 @@
import time
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
from pimoroni import RGBLED
from pimoroni_i2c import PimoroniI2C
from breakout_ltr559 import BreakoutLTR559
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
led = RGBLED(6, 7, 8, invert=True) # seting pins for the RGB led
led.set_rgb(0, 0, 0)
# setup background
BG = display.create_pen(0, 0, 0)
TEXT = display.create_pen(255, 255, 255)
PROX = display.create_pen(255, 0, 0)
LUX = display.create_pen(255, 255, 0)
display.set_pen(BG)
display.clear()
def draw_graph(lux_value, prox_value):
scaled_lux = int(200 / 1600 * lux_value)
scaled_prox = int(200 / 1600 * prox_value)
display.set_pen(BG)
display.clear()
display.set_pen(LUX)
display.rectangle(0, 240 - scaled_lux, 120, scaled_lux)
display.text("PROX: {0}".format(prox_value), 125, 120, scale=2)
display.set_pen(PROX)
display.text("LUX: {0}".format(lux_value), 5, 120, scale=2)
display.rectangle(120, 240 - scaled_prox, 120, scaled_prox)
display.set_pen(TEXT)
display.text("Light+Prox Sensor", 5, 10, scale=2)
display.text("PROX: {0}".format(prox_value), 125, 120, scale=2)
display.update()
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
ltr = BreakoutLTR559(i2c)
part_id = ltr.part_id()
print("Found LTR559. Part ID: 0x", '{:02x}'.format(part_id), sep="")
# Draw blank graph
draw_graph(0, 0)
while True:
reading = ltr.get_reading()
if reading is not None:
print("Lux:", reading[BreakoutLTR559.LUX], "Prox:", reading[BreakoutLTR559.PROXIMITY])
draw_graph(reading[BreakoutLTR559.LUX], reading[BreakoutLTR559.PROXIMITY])
time.sleep(0.1)

Wyświetl plik

@ -0,0 +1,85 @@
'''
Audio Sensor Example
'''
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
from pimoroni import RGBLED
from machine import Pin, ADC
import time
print("""mic.py - displays a waveform on the attached screen
""")
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
# Setup RGB Led
led = RGBLED(6, 7, 8, invert=True)
led.set_rgb(0, 0, 0)
# Setup background
BG = display.create_pen(0, 0, 0)
TEXT = display.create_pen(255, 255, 255)
MIC = display.create_pen(255, 0, 0)
display.set_pen(BG)
display.clear()
# Setup analogue Channel for Mic
MIC_PIN = 26
mic = ADC(Pin(26))
# array for storing
results = []
# Settings for bandwith and side
BANDWIDTH = 2000
SAMPLE_N = 240
# Drawing routines
def draw_background():
display.set_pen(BG)
display.clear()
display.set_pen(TEXT)
display.text("Sound Sensor", 5, 10, scale=3)
def draw_txt_overlay(sensor_data):
display.set_pen(MIC)
display.text("Peak: {0}".format(sensor_data), 5, 60, scale=3)
def draw_wave(results_array):
result_index = 0
for result in results_array:
display.set_pen(MIC)
display.pixel(result_index, int(120 + result))
result_index += 1
def read_mic():
return mic.read_u16()
def take_sample(frequency, length=240):
buffer = []
for index in range(length):
buffer.append(read_mic())
time.sleep(1 / frequency)
return buffer
while True:
results = take_sample(BANDWIDTH, SAMPLE_N)
# Rescale for display
for result_index in range(len(results)):
results[result_index] = (results[result_index] - 33100) / 30
# Display Upates
draw_background()
draw_wave(results)
draw_txt_overlay(max(results))
display.update()
time.sleep(0.2)

Wyświetl plik

@ -0,0 +1,92 @@
'''
Particle Sensor Example
This example requires seperate MicroPython drivers for the PMS5003 particulate sensor.
(You can find it at https://github.com/pimoroni/pms5003-micropython )
or install from PyPi by searching for 'pms5003-micropython' in Thonny's 'Tools > Manage Packages'
'''
from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS
from pimoroni import RGBLED
from pms5003 import PMS5003
import machine
import time
print("""particle.py - Continuously print all data values.
and draw a pretty histogram on display
""")
# Configure the PMS5003 for Enviro+
pms5003 = PMS5003(
uart=machine.UART(1, tx=machine.Pin(8), rx=machine.Pin(9), baudrate=9600),
pin_enable=machine.Pin(3),
pin_reset=machine.Pin(2),
mode="active"
)
display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS)
display.set_backlight(1.0)
# Setup RGB Led
led = RGBLED(6, 7, 8, invert=True)
led.set_rgb(0, 0, 0)
# Setup background
BG = display.create_pen(0, 0, 0)
TEXT = display.create_pen(255, 255, 255)
PM10 = display.create_pen(255, 0, 0)
PM25 = display.create_pen(255, 255, 0)
PM100 = display.create_pen(0, 255, 0)
PM125 = display.create_pen(255, 255, 0)
PM1000 = display.create_pen(255, 255, 0)
display.set_pen(BG)
display.clear()
# array for storing
results = []
# Drawing routines
def draw_background():
display.set_pen(BG)
display.clear()
display.set_pen(TEXT)
display.text("PMS5003 Sensor", 5, 10, scale=3)
def draw_txt_overlay(sensor_data):
display.set_pen(PM10)
display.text("PM1.0: {0}".format(sensor_data.pm_ug_per_m3(1.0)), 5, 60, scale=3)
display.set_pen(PM25)
display.text("PM2.5: {0}".format(sensor_data.pm_ug_per_m3(2.5)), 5, 80, scale=3)
display.set_pen(PM100)
display.text("PM10: {0}".format(sensor_data.pm_ug_per_m3(10)), 5, 100, scale=3)
def draw_hist(results_array):
result_index = 0
for result in results_array:
display.set_pen(PM100)
display.rectangle(2 * result_index, 240 - result.pm_ug_per_m3(10), 2, 240)
display.set_pen(PM25)
display.rectangle(2 * result_index, 240 - result.pm_ug_per_m3(2.5), 2, 240)
display.set_pen(PM10)
display.rectangle(2 * result_index, 240 - result.pm_ug_per_m3(1.0), 2, 240)
result_index += 1
while True:
draw_background()
data = pms5003.read()
print(data)
results.append(data)
if (len(results) > 120): # Scroll the result list by removing the first value
results.pop(0)
draw_hist(results)
draw_txt_overlay(data)
display.update()
time.sleep(0.5)