本文整理汇总了Python中fysom.Fysom.toggle方法的典型用法代码示例。如果您正苦于以下问题:Python Fysom.toggle方法的具体用法?Python Fysom.toggle怎么用?Python Fysom.toggle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fysom.Fysom
的用法示例。
在下文中一共展示了Fysom.toggle方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tr
# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import toggle [as 别名]
class Lights:
'''A state machine to control lighting
# Ascii art diagram
*
| /lights=False
|
v tr(switch)
+--------+ /lights=True +--------+
| |-------------->| |
| Lights | | Lights |
| off | | on |
| |<--------------| |
+--------+ tr(switch) +--------+
/lights=False
# API for interacting with state machine class
class Lights(AsciiSm):
switch
# Desired output from parser
states = ['Lights off', 'Lights on']
transitions = [{'default': True, 'dst': 'Lights off',
'action': 'lights=False'},
{'src': 'Lights off', 'dst': 'Lights on',
'event': 'tr(switch)', 'action': 'lights=True'},
{'src': 'Lights on', 'dst': 'Lights off',
'event': 'tr(switch)', 'action': 'lights=False'}]
'''
def __init__(self):
self.switch = False
self.sm = Fysom({
'initial': 'Lights off',
'events': [
{'name': 'toggle', 'src': 'lights_off', 'dst': 'lights_on'},
{'name': 'toggle', 'src': 'lights_on', 'dst': 'lights_off'}],
'callbacks': {
'on_Lights_on': self.lights_on,
'on_Lights_off': self.lights_off,
}
})
@property
def switch(self):
return self.switch
@switch.setter
def switch(self, value):
'''State machine input'''
print('Lights set to %d' % value)
if not self.switch and value:
self.sm.toggle()
elif self.switch and not value:
self.sm.toggle()
self.switch = value
def lights_on(self):
self.lights(True)
def lights_off(self):
self.lights(False)
def lights(self, value):
'''State machine output'''
print('Lights set to %d' % value)