Verified Commit 003aa2a0 authored by Rahix's avatar Rahix
Browse files

refactor(preload/bhi): Make app more pythonic



This patch makes the BHI160 app more pythonic and fixes a few
bugs/issues:

- An exception (including KeyboardInterrupt) will trigger the sensor to
  be closed again.  Otherwise you'd get strange behavior trying to
  reopen then afterwards.
- Use simple_menu.button_events() instead of home-cooked solution.  This
  should hopefully make the code more future-proof.
- Fix overlapping text caused by the font-rendering changes.

Signed-off-by: Rahix's avatarRahix <rahix@rahix.de>
parent d974cd2f
Pipeline #4001 passed with stages
in 44 seconds
import bhi160 import bhi160
import display
import utime
import buttons import buttons
import color
disp = display.open() import contextlib
sensor = 0 import display
import itertools
sensors = [ import simple_menu
{"sensor": bhi160.BHI160Orientation(), "name": "Orientation"},
{"sensor": bhi160.BHI160Accelerometer(), "name": "Accelerometer"}, STATUS_COLORS = [
{"sensor": bhi160.BHI160Gyroscope(), "name": "Gyroscope"}, color.RED,
{"sensor": bhi160.BHI160Magnetometer(), "name": "Magnetometer"}, # Orange
color.RED * 0.5 + color.YELLOW * 0.5,
color.YELLOW,
color.GREEN,
] ]
while True: with contextlib.ExitStack() as cx:
# Read and print sample disp = cx.enter_context(display.open())
samples = sensors[sensor]["sensor"].read()
if len(samples) > 0:
disp.clear()
sample = samples[0]
color = [255, 0, 0] args = {"sample_rate": 10, "sample_buffer_len": 20}
if sample.status == 1:
color = [255, 128, 0]
elif sample.status == 2:
color = [255, 255, 0]
elif sample.status == 3:
color = [0, 200, 0]
disp.print(sensors[sensor]["name"], posy=0) sensor_iter = itertools.cycle(
disp.print("X: %f" % sample.x, posy=20, fg=color) [
disp.print("Y: %f" % sample.y, posy=40, fg=color) (cx.enter_context(bhi160.BHI160Orientation(**args)), "Orientation"),
disp.print("Z: %f" % sample.z, posy=60, fg=color) (cx.enter_context(bhi160.BHI160Accelerometer(**args)), "Accel"),
(cx.enter_context(bhi160.BHI160Gyroscope(**args)), "Gyro"),
(cx.enter_context(bhi160.BHI160Magnetometer(**args)), "Magnetic"),
]
)
sensor, sensor_name = next(sensor_iter)
disp.update() for ev in simple_menu.button_events(timeout=0.1):
# Pressing the bottom right button cycles through sensors
if ev == buttons.BOTTOM_RIGHT:
sensor, sensor_name = next(sensor_iter)
# Read button samples = sensor.read()
v = buttons.read(buttons.BOTTOM_RIGHT) if not samples:
if v == 0: continue
button_pressed = False
if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: sample = samples[-1]
button_pressed = True col = STATUS_COLORS[sample.status]
sensor = (sensor + 1) % len(sensors)
utime.sleep(0.1) disp.clear()
disp.print("{:^11s}".format(sensor_name), posy=0, posx=3)
disp.print("X:{: 9.4f}".format(sample.x), posy=20, fg=col)
disp.print("Y:{: 9.4f}".format(sample.y), posy=40, fg=col)
disp.print("Z:{: 9.4f}".format(sample.z), posy=60, fg=col)
disp.update()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment