Skip to content

Add MagTag Dishwasher Code #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added MagTag_Dishwasher_Status/clean.bmp
Binary file not shown.
Binary file added MagTag_Dishwasher_Status/dirty.bmp
Binary file not shown.
33 changes: 33 additions & 0 deletions MagTag_Dishwasher_Status/wake_on_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import time
import board
import alarm
import displayio

# get the display
epd = board.DISPLAY
epd.rotation = 270

# set up pin alarms
buttons = (board.BUTTON_A, board.BUTTON_B) # pick any two
pin_alarms = [alarm.pin.PinAlarm(pin=pin, value=False, pull=True) for pin in buttons]

# toggle saved state
alarm.sleep_memory[0] = not alarm.sleep_memory[0]

# set bitmap
bmp_file = "clean.bmp" if alarm.sleep_memory[0] else "dirty.bmp"

# show bitmap
with open(bmp_file, "rb") as fp:
bitmap = displayio.OnDiskBitmap(fp)
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
group = displayio.Group(max_size=1)
group.append(tile_grid)
epd.show(group)
time.sleep(epd.time_to_refresh + 0.01)
epd.refresh()
while epd.busy:
pass

# go to sleep
alarm.exit_and_deep_sleep_until_alarms(*pin_alarms)
63 changes: 63 additions & 0 deletions MagTag_Dishwasher_Status/wake_on_flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import time
import board
import alarm
import displayio
import adafruit_lis3dh

# get the display
epd = board.DISPLAY

# set up accelerometer
lis = adafruit_lis3dh.LIS3DH_I2C(board.I2C(), address=0x19)

# See: ST Design Tip DT0008 - Simple screen rotation using
# the accelerometer built-in 4D detection interrupt
# pylint: disable=protected-access
lis._write_register_byte(0x20, 0x3F) # low power mode with ODR = 25Hz
lis._write_register_byte(0x22, 0x40) # AOI1 interrupt generation is routed to INT1 pin
lis._write_register_byte(0x23, 0x80) # FS = ±2g low power mode with BDU bit enabled
lis._write_register_byte(
0x24, 0x0C
) # Interrupt signal on INT1 pin is latched with D4D_INT1 bit enabled
lis._write_register_byte(
0x32, 0x20
) # Threshold = 32LSBs * 15.625mg/LSB = 500mg. (~30 deg of tilt)
lis._write_register_byte(0x33, 0x01) # Duration = 1LSBs * (1/25Hz) = 0.04s

# read to clear
_ = lis._read_register_byte(0x31)

# get current accel values
_, y, _ = lis.acceleration

# update based on orientation
if y > 0:
# upside up
bmp_file = "clean.bmp"
rotation = 270
irq_config = 0b01000100
else:
# upside down
bmp_file = "dirty.bmp"
rotation = 90
irq_config = 0b01001000

# show bitmap
epd.rotation = rotation
with open(bmp_file, "rb") as fp:
bitmap = displayio.OnDiskBitmap(fp)
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
group = displayio.Group(max_size=1)
group.append(tile_grid)
epd.show(group)
time.sleep(epd.time_to_refresh + 0.01)
epd.refresh()
while epd.busy:
pass

# config accelo irq
lis._write_register_byte(0x30, irq_config)

# go to sleep
pin_alarm = alarm.pin.PinAlarm(pin=board.ACCELEROMETER_INTERRUPT, value=True)
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)