Skip to content

Instantly share code, notes, and snippets.

@dougharris
Created May 13, 2018 18:39
Show Gist options
  • Save dougharris/9393a0b0972620d684529722479523c8 to your computer and use it in GitHub Desktop.
Save dougharris/9393a0b0972620d684529722479523c8 to your computer and use it in GitHub Desktop.
CircuitPython code to treat Adafruit Gemma.M0 as a simple combination lock
# Simple combination lock style code for typing a password.
# (code adapted from the demo code installed on the Gemma.M0 in the
# swag bags at PyCon 2018)
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from digitalio import DigitalInOut, Direction
from touchio import TouchIn
import adafruit_dotstar as dotstar
import board
import time
#
# Configuration stuff here.
#
# touch_pattern - the sequence of inputs to click, e.g. [ 1, 1, 2 ] means
# press A1 twice followed by A2
# password_filename - the name of the file holding your password. n.b. Keeping
# your login password unencrypted in a mountable usb device
# is a bad idea
#
# sequence of touches to match before typing password
touch_pattern = [0, 1, 2, 0]
password_filename = '.password'
# One pixel connected internally!
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Capacitive touch on A2
touch0 = TouchIn(board.A0)
# Capacitive touch on A2
touch1 = TouchIn(board.A1)
# Capacitive touch on A2
touch2 = TouchIn(board.A2)
# Used if we do HID output, see below
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)
######################### HELPERS ##############################
RED = [255, 0, 0]
GREEN = [0, 255, 0]
BLUE = [0, 0, 255]
BLACK = [0, 0, 0]
DEEP_PURPLE = [153, 0, 255]
def show_status(color):
dot[0] = color
time.sleep(0.5)
dot[0] = [0, 0, 0]
def touch_from_int(input_id):
return eval("touch" + str(input_id))
def check_touch(input_id):
# check this input
this_input = touch_from_int(input_id)
if not this_input.value:
return False
# if it's touched, make sure the others aren't
ids = [0, 1, 2]
ids.remove(input_id)
other_inputs = [touch_from_int(i) for i in ids]
if any([input.value for input in other_inputs]):
return False
return True
######################### MAIN LOOP ##############################
entered = []
show_status(BLACK)
while True:
# Start over if A0 and A2 are touched together
if touch0.value and touch2.value:
show_status(BLUE)
entered = []
continue
# Check each of the inputs individually
for input in [0, 1, 2]:
if check_touch(input):
entered.append(input)
print(entered)
if len(entered) == len(touch_pattern):
print("correct length")
if entered == touch_pattern:
print("Pattern matched. Entering password")
with open(password_filename) as f:
password = f.read()
layout.write(password + '\n')
show_status(GREEN)
else:
print("Pattern not matched")
show_status(RED)
entered = []
else:
show_status(DEEP_PURPLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment