Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Hacksaar
Sphinx
Commits
006a3aa5
Commit
006a3aa5
authored
Jul 09, 2014
by
Constantin
Browse files
ledactor is actually used now
parent
373dfe2c
Changes
5
Hide whitespace changes
Inline
Side-by-side
actor.py
View file @
006a3aa5
...
...
@@ -6,10 +6,6 @@ class Actor:
CMD_BUZZ
=
0
CMD_UNLOCK
=
1
CMD_LOCK
=
2
CMD_GREEN_ON
=
3
CMD_GREEN_OFF
=
4
CMD_RED_ON
=
5
CMD_RED_OFF
=
6
class
CMD
():
def
__init__
(
self
,
name
,
pin
,
tid
,
todo
,
verbose
=
True
):
...
...
@@ -46,13 +42,9 @@ class Actor:
=> different commands with same tid will never be executed simultaneously
'''
CMDs
=
{
CMD_UNLOCK
:
CMD
(
"unlock"
,
pin
=
12
,
tid
=
0
,
todo
=
[(
True
,
0.3
),
(
False
,
0.1
)]),
CMD_LOCK
:
CMD
(
"lock"
,
pin
=
16
,
tid
=
0
,
todo
=
[(
True
,
0.3
),
(
False
,
0.1
)]),
CMD_BUZZ
:
CMD
(
"buzz"
,
pin
=
22
,
tid
=
1
,
todo
=
[(
True
,
2.5
),
(
False
,
0.1
)]),
CMD_GREEN_ON
:
CMD
(
"green on"
,
pin
=
23
,
tid
=
2
,
verbose
=
False
,
todo
=
[(
True
,
0
)]),
CMD_GREEN_OFF
:
CMD
(
"green off"
,
pin
=
23
,
tid
=
2
,
verbose
=
False
,
todo
=
[(
False
,
0
)]),
CMD_RED_ON
:
CMD
(
"red on"
,
pin
=
26
,
tid
=
2
,
verbose
=
False
,
todo
=
[(
True
,
0
)]),
CMD_RED_OFF
:
CMD
(
"red off"
,
pin
=
26
,
tid
=
2
,
verbose
=
False
,
todo
=
[(
False
,
0
)]),
CMD_UNLOCK
:
CMD
(
"unlock"
,
pin
=
12
,
tid
=
0
,
todo
=
[(
True
,
0.3
),
(
False
,
0.1
)]),
CMD_LOCK
:
CMD
(
"lock"
,
pin
=
16
,
tid
=
0
,
todo
=
[(
True
,
0.3
),
(
False
,
0.1
)]),
CMD_BUZZ
:
CMD
(
"buzz"
,
pin
=
22
,
tid
=
1
,
todo
=
[(
True
,
2.5
),
(
False
,
0.1
)]),
}
def
__init__
(
self
):
...
...
ledactor.py
View file @
006a3aa5
from
libtuer
import
logger
#
import RPi.GPIO as GPIO
import
RPi.GPIO
as
GPIO
import
time
import
threading
import
signal
...
...
@@ -24,8 +24,7 @@ class Blinker(threading.Thread):
# list is copied to avoid concurrent modification exceptions
for
(
state
,
delay
)
in
list
(
self
.
_pattern
):
GPIO
.
output
(
self
.
_pin
,
state
)
if
(
delay
>
0
):
self
.
_notifyer
.
wait
(
delay
)
self
.
_notifyer
.
wait
(
delay
)
if
self
.
_notifyer
.
is_set
()
or
self
.
_terminate
:
break
# LEDs off in the end
...
...
@@ -34,11 +33,9 @@ class Blinker(threading.Thread):
def
_getTid
(
self
):
return
self
.
_thread
.
ident
def
_sendSig
(
self
,
sig
):
self
.
_terminate
=
True
def
stop
(
self
):
self
.
_terminate
=
True
self
.
_notifyer
.
set
()
self
.
join
()
def
setPattern
(
self
,
pattern
):
...
...
@@ -56,15 +53,27 @@ class LedActor:
def
__init__
(
self
):
# launch threads, all running the "_execute" method
self
.
_threads
=
{}
for
(
name
,
pin
)
in
ledPins
.
items
():
for
(
name
,
pin
)
in
self
.
ledPins
.
items
():
logger
.
debug
(
"Setting pin %d to output as
\"
%s
\"
LED, initial state: False"
%
(
pin
,
name
))
GPIO
.
setup
(
pin
,
GPIO
.
OUT
)
GPIO
.
output
(
pin
,
False
)
self
.
_threads
[
name
]
=
Blinker
(
name
,
pin
)
self
.
_threads
[
name
].
start
()
def
setPattern
(
self
,
ledName
,
pattern
):
self
.
_threads
[
ledName
].
setPattern
(
pattern
)
def
setState
(
self
,
ledName
,
state
):
# timeout of None waits infinitely
assert
state
in
(
True
,
False
)
self
.
setPattern
(
ledName
,
[(
state
,
None
)])
def
setOn
(
self
,
ledName
):
self
.
setState
(
ledName
,
True
)
def
setOff
(
self
,
ledName
):
self
.
setState
(
ledName
,
False
)
def
stop
(
self
):
for
blinker
in
self
.
_threads
.
values
():
blinker
.
stop
()
\ No newline at end of file
pins.py
View file @
006a3aa5
...
...
@@ -18,8 +18,8 @@ class PinWatcher():
self
.
_newstatelen
=
0
# only valid if newstate != None
def
read
(
self
):
curstate
=
GPIO
.
input
(
self
.
pin
)
assert
curstate
in
(
0
,
1
)
curstate
=
True
if
GPIO
.
input
(
self
.
pin
)
else
False
assert
curstate
in
(
True
,
False
)
if
curstate
!=
self
.
state
:
# the state is about to change
if
curstate
==
self
.
_newstate
:
...
...
statemachine.py
View file @
006a3aa5
...
...
@@ -118,6 +118,8 @@ class StateMachine():
return
self
.
state_machine
.
old_pins
def
actor
(
self
):
return
self
.
state_machine
.
actor
def
ledactor
(
self
):
return
self
.
state_machine
.
ledactor
def
api
(
self
):
return
self
.
state_machine
.
api
def
handle_event
(
self
,
ev
,
arg
):
# don't override
...
...
@@ -142,17 +144,15 @@ class StateMachine():
class
AbstractNonStartState
(
State
):
def
handle_pins_event
(
self
):
if
self
.
pins
().
door_locked
!=
(
not
self
.
pins
().
space_active
):
self
.
actor
().
act
(
Actor
.
CMD_RED_ON
)
else
:
self
.
actor
().
act
(
Actor
.
CMD_RED_OFF
)
# if door locked is inconsistent with space switch, turn red led on
self
.
ledactor
().
setState
(
"red"
,
self
.
pins
().
door_locked
!=
(
not
self
.
pins
().
space_active
))
return
super
().
handle_pins_event
()
class
AbstractLockedState
(
AbstractNonStartState
):
'''A state with invariant "The space is locked", switching to StateAboutToOpen when the space becomes unlocked'''
def
__init__
(
self
,
sm
,
nervlist
=
None
):
super
().
__init__
(
sm
,
nervlist
)
self
.
actor
().
act
(
Actor
.
CMD_GREEN_OFF
)
self
.
led
actor
().
setOff
(
"green"
)
def
handle_pins_event
(
self
):
if
not
self
.
pins
().
door_locked
:
logger
.
info
(
"Door unlocked, space is about to open"
)
...
...
@@ -163,7 +163,7 @@ class StateMachine():
'''A state with invariant "The space is unlocked", switching to StateZu when the space becomes locked'''
def
__init__
(
self
,
sm
,
nervlist
=
None
):
super
().
__init__
(
sm
,
nervlist
)
self
.
actor
().
act
(
Actor
.
CMD_GREEN_ON
)
self
.
led
actor
().
setOn
(
"green"
)
def
handle_pins_event
(
self
):
if
self
.
pins
().
door_locked
:
logger
.
info
(
"Door locked, closing space"
)
...
...
@@ -194,16 +194,17 @@ class StateMachine():
class
StateFallback
(
State
):
def
__init__
(
self
,
sm
,
nervlist
=
None
):
super
().
__init__
(
sm
,
nervlist
)
self
.
_red_state
=
False
if
self
.
old_pins
().
space_active
is
not
None
:
self
.
ledactor
().
setState
(
"green"
,
self
.
old_pins
().
space_active
)
else
:
self
.
ledactor
().
setOff
(
"green"
)
self
.
ledactor
().
setPattern
(
"red"
,[(
True
,
0.1
),(
False
,
1.9
)])
self
.
api
().
set_state
(
False
)
def
handle_pins_event
(
self
):
pins
=
self
.
pins
()
old_pins
=
self
.
old_pins
()
# set green LED according to space switch
if
pins
.
space_active
:
self
.
actor
().
act
(
Actor
.
CMD_GREEN_ON
)
else
:
self
.
actor
().
act
(
Actor
.
CMD_GREEN_OFF
)
self
.
ledactor
().
setState
(
"green"
,
pins
.
space_active
)
# primitive leaving procedure if space switch turned off
if
not
pins
.
space_active
and
old_pins
.
space_active
:
logger
.
info
(
"StateFallback: Space was disabled, locking the door in {0} seconds"
.
format
(
FALLBACK_LEAVE_DELAY_LOCK
))
...
...
@@ -219,15 +220,6 @@ class StateMachine():
logger
.
info
(
"StateFallback: bell rings, let's buzz"
)
self
.
actor
().
act
(
Actor
.
CMD_BUZZ
)
# not calling superclass because we want to stay in fallback mode
def
handle_wakeup_event
(
self
):
# blink red LED
if
self
.
_red_state
:
self
.
actor
().
act
(
Actor
.
CMD_RED_OFF
)
self
.
_red_state
=
False
else
:
self
.
actor
().
act
(
Actor
.
CMD_RED_ON
)
self
.
_red_state
=
True
return
super
().
handle_wakeup_event
()
def
handle_cmd_unlock_event
(
self
,
arg
):
if
arg
is
not
None
:
arg
(
"200 okay: Trying to unlock the door. The System is in fallback mode, success information is not available."
)
...
...
@@ -383,8 +375,9 @@ class StateMachine():
return
StateMachine
.
StateAuf
(
self
.
state_machine
)
return
super
().
handle_pins_event
()
def
__init__
(
self
,
actor
,
waker
,
api
,
fallback
=
False
):
def
__init__
(
self
,
actor
,
ledactor
,
waker
,
api
,
fallback
=
False
):
self
.
actor
=
actor
self
.
ledactor
=
ledactor
self
.
api
=
api
self
.
callback
=
ThreadFunction
(
self
.
_callback
,
name
=
"StateMachine"
)
self
.
current_state
=
StateMachine
.
StateStart
(
self
,
fallback
=
fallback
)
...
...
tuerd
View file @
006a3aa5
#!/usr/bin/python3
import
RPi.GPIO
as
GPIO
import
statemachine
,
actor
,
pins
,
tysock
,
waker
,
spaceapi
import
statemachine
,
actor
,
ledactor
,
pins
,
tysock
,
waker
,
spaceapi
from
libtuer
import
logger
import
argparse
...
...
@@ -30,9 +30,10 @@ GPIO.setmode(GPIO.BOARD)
# bring 'em all up
the_actor
=
actor
.
Actor
()
the_led_actor
=
ledactor
.
LedActor
()
the_waker
=
waker
.
Waker
()
the_api
=
spaceapi
.
SpaceApi
(
the_waker
)
the_machine
=
statemachine
.
StateMachine
(
the_actor
,
the_waker
,
the_api
,
args
.
fallback
)
the_machine
=
statemachine
.
StateMachine
(
the_actor
,
the_led_actor
,
the_waker
,
the_api
,
args
.
fallback
)
the_socket
=
tysock
.
TySocket
(
the_machine
)
the_pins
=
pins
.
PinsWatcher
(
the_machine
)
...
...
@@ -51,6 +52,7 @@ the_pins.stop() # as does this
the_machine
.
stop
()
the_api
.
stop
()
the_actor
.
stop
()
the_led_actor
.
stop
()
# shutdown GPIO stuff
GPIO
.
cleanup
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment