|
| 1 | +import time |
| 2 | +import board |
| 3 | +import alarm |
| 4 | +import displayio |
| 5 | +import adafruit_lis3dh |
| 6 | + |
| 7 | +# get the display |
| 8 | +epd = board.DISPLAY |
| 9 | + |
| 10 | +# set up accelerometer |
| 11 | +lis = adafruit_lis3dh.LIS3DH_I2C(board.I2C(), address=0x19) |
| 12 | + |
| 13 | +# See: ST Design Tip DT0008 - Simple screen rotation using |
| 14 | +# the accelerometer built-in 4D detection interrupt |
| 15 | +# pylint: disable=protected-access |
| 16 | +lis._write_register_byte(0x20, 0x3F) # low power mode with ODR = 25Hz |
| 17 | +lis._write_register_byte(0x22, 0x40) # AOI1 interrupt generation is routed to INT1 pin |
| 18 | +lis._write_register_byte(0x23, 0x80) # FS = ±2g low power mode with BDU bit enabled |
| 19 | +lis._write_register_byte( |
| 20 | + 0x24, 0x0C |
| 21 | +) # Interrupt signal on INT1 pin is latched with D4D_INT1 bit enabled |
| 22 | +lis._write_register_byte( |
| 23 | + 0x32, 0x20 |
| 24 | +) # Threshold = 32LSBs * 15.625mg/LSB = 500mg. (~30 deg of tilt) |
| 25 | +lis._write_register_byte(0x33, 0x01) # Duration = 1LSBs * (1/25Hz) = 0.04s |
| 26 | + |
| 27 | +# read to clear |
| 28 | +_ = lis._read_register_byte(0x31) |
| 29 | + |
| 30 | +# get current accel values |
| 31 | +_, y, _ = lis.acceleration |
| 32 | + |
| 33 | +# update based on orientation |
| 34 | +if y > 0: |
| 35 | + # upside up |
| 36 | + bmp_file = "clean.bmp" |
| 37 | + rotation = 270 |
| 38 | + irq_config = 0b01000100 |
| 39 | +else: |
| 40 | + # upside down |
| 41 | + bmp_file = "dirty.bmp" |
| 42 | + rotation = 90 |
| 43 | + irq_config = 0b01001000 |
| 44 | + |
| 45 | +# show bitmap |
| 46 | +epd.rotation = rotation |
| 47 | +with open(bmp_file, "rb") as fp: |
| 48 | + bitmap = displayio.OnDiskBitmap(fp) |
| 49 | + tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter()) |
| 50 | + group = displayio.Group(max_size=1) |
| 51 | + group.append(tile_grid) |
| 52 | + epd.show(group) |
| 53 | + time.sleep(epd.time_to_refresh + 0.01) |
| 54 | + epd.refresh() |
| 55 | + while epd.busy: |
| 56 | + pass |
| 57 | + |
| 58 | +# config accelo irq |
| 59 | +lis._write_register_byte(0x30, irq_config) |
| 60 | + |
| 61 | +# go to sleep |
| 62 | +pin_alarm = alarm.pin.PinAlarm(pin=board.ACCELEROMETER_INTERRUPT, value=True) |
| 63 | +alarm.exit_and_deep_sleep_until_alarms(pin_alarm) |
0 commit comments