Created
January 12, 2014 00:12
-
-
Save anonymous/8378731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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