Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2014 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8378731 to your computer and use it in GitHub Desktop.
Save anonymous/8378731 to your computer and use it in GitHub Desktop.
class DeterministicFiniteAutomaton:
def __init__(self, initial_state, transition_table):
self.state_old = None
self.state = initial_state
self.transitions = transition_table
def read(self, letter):
newstate = self.transitions[(self.state, letter)]
self.oldstate = self.state
self.state = newstate[0]
if self.oldstate != self.state and callable(newstate[1]):
newstate[1]()
def read_list(self, letters):
map(self.read, letters)
from __future__ import print_function
import RPi.GPIO as GPIO
from automata import DeterministicFiniteAutomaton as DFA
GPIO.setmode(GPIO.BCM)
led = 4
button_no = 8 # (n)ormally (o)pen
GPIO.setup(led, GPIO.OUT)
GPIO.setup(button_no, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def turnon(): GPIO.output(led, GPIO.HIGH)
def turnoff(): GPIO.output(led, GPIO.LOW)
transitions = {
('on', 0): ('on', None),
('on', 1): ('on-off', None),
('on-off', 1): ('on-off', None),
('on-off', 0): ('off', turnoff),
('off', 0): ('off', None),
('off', 1): ('off-on', None),
('off-on', 1): ('off-on', None),
('off-on', 0): ('on', turnon)
}
onoff = DFA('off', transitions)
import time
try:
GPIO.output(led, GPIO.LOW)
while True:
onoff.read(GPIO.input(button_no))
time.sleep(.001)
except KeyboardInterrupt:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment