|
| 1 | +import time |
| 2 | +import math |
| 3 | +import board |
| 4 | +from digitalio import DigitalInOut, Direction, Pull |
| 5 | +import pulseio |
| 6 | +from adafruit_lsm6ds import LSM6DS33, AccelRange, AccelHPF, Rate |
| 7 | +from adafruit_display_text import label |
| 8 | +import displayio |
| 9 | +import terminalio |
| 10 | + |
| 11 | +button_a = DigitalInOut(board.BUTTON_A) |
| 12 | +button_a.direction = Direction.INPUT |
| 13 | +button_a.pull = Pull.UP |
| 14 | + |
| 15 | +splash = displayio.Group(max_size=3) |
| 16 | + |
| 17 | +# draw the bad egg! |
| 18 | +begg_file = open("broken_egg.bmp", "rb") |
| 19 | +begg_bmp = displayio.OnDiskBitmap(begg_file) |
| 20 | +begg_sprite = displayio.TileGrid(begg_bmp, |
| 21 | + pixel_shader=displayio.ColorConverter()) |
| 22 | +splash.append(begg_sprite) |
| 23 | + |
| 24 | +# draw the good egg on top |
| 25 | +gegg_file = open("good_egg.bmp", "rb") |
| 26 | +gegg_bmp = displayio.OnDiskBitmap(gegg_file) |
| 27 | +gegg_sprite = displayio.TileGrid(gegg_bmp, |
| 28 | + pixel_shader=displayio.ColorConverter()) |
| 29 | +splash.append(gegg_sprite) |
| 30 | + |
| 31 | +# Draw a label |
| 32 | +text_group = displayio.Group(max_size=1, scale=2, x=10, y=220) |
| 33 | +text = "Current & Max Acceleration" |
| 34 | +text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF) |
| 35 | +text_group.append(text_area) # Subgroup for text scaling |
| 36 | +splash.append(text_group) |
| 37 | + |
| 38 | +# display everything so far |
| 39 | +board.DISPLAY.show(splash) |
| 40 | + |
| 41 | +# connect to the accelerometer |
| 42 | +sensor = LSM6DS33(board.I2C()) |
| 43 | +# highest range for impacts! |
| 44 | +sensor.accelerometer_range = AccelRange.RANGE_16G |
| 45 | +# we'll read at about 1KHz |
| 46 | +sensor.accelerometer_rate = Rate.RATE_1_66K_HZ |
| 47 | +# Instead of raw accelerometer data, we'll read the *change* in acceleration (shock) |
| 48 | +sensor.high_pass_filter = AccelHPF.SLOPE |
| 49 | +sensor.high_pass_filter_enabled = True |
| 50 | + |
| 51 | +# and make a lil buzzer |
| 52 | +buzzer = pulseio.PWMOut(board.SPEAKER, variable_frequency=True) |
| 53 | +buzzer.frequency = 1000 |
| 54 | + |
| 55 | +max_slope = 0 |
| 56 | +egg_ok = True |
| 57 | +while True: |
| 58 | + # This isn't the acceleration but the SLOPE (change!) in acceleration |
| 59 | + x, y, z = sensor.acceleration |
| 60 | + # take the vector length by squaring, adding, taking root |
| 61 | + slope_g = math.sqrt(x*x + y*y + z*z) / 9.8 # take vector, convert to g |
| 62 | + # we'll track the max delta g |
| 63 | + |
| 64 | + if max_slope < slope_g: |
| 65 | + max_slope = slope_g |
| 66 | + print(slope_g) |
| 67 | + text_area.text = "Max Slope %0.1fg" % (max_slope) |
| 68 | + if max_slope >= 9 and egg_ok: |
| 69 | + gegg_sprite.x = 300 |
| 70 | + time.sleep(0.1) |
| 71 | + egg_ok = False |
| 72 | + buzzer.duty_cycle = 2**15 |
| 73 | + time.sleep(2) |
| 74 | + buzzer.duty_cycle = 0 |
| 75 | + continue |
| 76 | + |
| 77 | + |
| 78 | + if button_a.value is False and egg_ok is False: |
| 79 | + print("Reset") |
| 80 | + time.sleep(0.1) # debounce |
| 81 | + max_slope = 0 |
| 82 | + gegg_sprite.x = 0 |
| 83 | + egg_ok = True |
0 commit comments