-
Notifications
You must be signed in to change notification settings - Fork 36
/
camera.py
152 lines (137 loc) · 5.49 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
The MIT License (MIT)
Copyright (c) 2015-2018 Howard Chan
https://github.com/howard-chan/HSM
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from hsm import HSM
from hsm import HSM_Event
class evt(HSM_Event):
'''Class that implements the Camera events and inherits from HSM_Event class
Since this is python, we can just use strings instead of enumeration which
makes this demo easier to demonstrate the event handling
'''
#PWR, RELEASE, MODE, LOWBATT = range(HSM_Event.FIRST, HSM_Event.FIRST+4)
PWR="PWR"
RELEASE="RELEASE"
MODE="MODE"
LOWBATT="LOWBATT"
class Camera(HSM):
'''Class that implements the Camera HSM and inherits from the HSM class
Creating a HSM requires the following steps:
1) Initialize the base HSM class
2) Define the HSM states hierarchy
3) Set the starting state
4) Define the state handlers
a) State handler must return "None" if the event IS handled
b) State handler must return "event" if the event IS NOT handled
c) State handler may handle the ENTRY event for state setup
d) State handler may handle the EXIT event for state teardown/cleanup
e) State handler may handle the INIT for self transition to child state
f) Self transition to child state MUST NOT be handled by ENTRY or EXIT event
g) Events ENTRY, EXIT and INIT do no need to return None for brevity
'''
def __init__(self, name):
# Step 1: Initialize the HSM
super(Camera, self).__init__(name)
# Step 2: Create the HSM States
self.stateOff = self.CreateState("Off", self.StateOffHndlr)
self.stateOn = self.CreateState("On", self.StateOnHndlr)
self.stateOnShoot = self.CreateState("On.Shoot", self.StateOnShootHndlr, self.stateOn)
self.stateOnDisp = self.CreateState("On.Disp", self.StateOnDispHndlr, self.stateOn)
self.stateOnDispPlay = self.CreateState("On.Disp.Play", self.StateOnDispPlayHndlr, self.stateOnDisp)
self.stateOnDispMenu = self.CreateState("On.Disp.Menu", self.StateOnDispMenuHndlr, self.stateOnDisp)
# Step 3: Set the starting state
self.SetInitState(self.stateOff)
# Step 4[Optional] Enable HSM debug
self.hsmDebug = True
def StateOffHndlr(self, event):
if event == evt.ENTRY:
print "\tEnter Low Power Mode"
elif event == evt.EXIT:
print "\tExit Low Power Mode"
elif event == evt.PWR:
self.Tran(self.stateOn)
return None
return event
def StateOnHndlr(self, event):
if event == evt.ENTRY:
print "\tOpen Lens"
elif event == evt.EXIT:
print "\tClose Lens"
elif event == evt.INIT:
self.Tran(self.stateOnShoot)
elif event == evt.PWR:
self.Tran(self.stateOff)
return None
elif event == evt.LOWBATT:
print "\tBeep low battery warning"
return None
return event
def StateOnShootHndlr(self, event):
if event == evt.ENTRY:
print "\tEnable Sensor"
elif event == evt.EXIT:
print "\tDisable Sensor"
elif event == evt.RELEASE:
print "\tCLICK!, save photo"
return None
elif event == evt.MODE:
self.Tran(self.stateOnDispPlay)
return None
return event
def StateOnDispHndlr(self, event):
if event == evt.ENTRY:
print "\tTurn on LCD"
elif event == evt.EXIT:
print "\tTurn off LCD"
return event
def StateOnDispPlayHndlr(self, event):
if event == evt.ENTRY:
print "\tDisplay Pictures"
elif event == evt.MODE:
self.Tran(self.stateOnDispMenu)
return None
return event
def StateOnDispMenuHndlr(self, event):
if event == evt.ENTRY:
print "\tDisplay Menu"
elif event == evt.MODE:
self.Tran(self.stateOnShoot)
return None
return event
def main():
# Instantiate Camera
basic = Camera("Canon")
# Turn on the Power
basic.Run(evt.PWR)
# Take a picture
basic.Run(evt.RELEASE)
# Take another picture
basic.Run(evt.RELEASE)
# Playback the photo
basic.Run(evt.MODE)
# Oops, pushed the release button by accident
basic.Run(evt.RELEASE)
# Go to menu settings
basic.Run(evt.MODE)
# Uh oh, low battery
basic.Run(evt.LOWBATT)
# Time to turn it off
basic.Run(evt.PWR)
if __name__ == "__main__":
main()