Skip to content

Instantly share code, notes, and snippets.

@havlan
Created February 18, 2019 10:12
Show Gist options
  • Select an option

  • Save havlan/c53b6d52d6057b1c261eddb371169004 to your computer and use it in GitHub Desktop.

Select an option

Save havlan/c53b6d52d6057b1c261eddb371169004 to your computer and use it in GitHub Desktop.
from stmpy import Machine, Driver
import ipywidgets as widgets
from IPython.display import display
class KitchenTimer:
def on_button_press(self, b):
self.stm.send('switch') # <---- here we send a signal
def __init__(self):
# load images and store them
self.on_60 = open("images/timer/on_60.jpg", "rb").read()
self.off_60 = open("images/timer/off_60.jpg", "rb").read()
self.on_45 = open("images/timer/on_45.jpg", "rb").read()
self.off_45 = open("images/timer/off_45.jpg", "rb").read()
self.on_30 = open("images/timer/on_30.jpg", "rb").read()
self.off_30 = open("images/timer/off_30.jpg", "rb").read()
self.on_15 = open("images/timer/on_15.jpg", "rb").read()
self.off_15 = open("images/timer/off_15.jpg", "rb").read()
self.plug_on = open("images/timer/plug_on.jpg", "rb").read()
self.plug_off = open("images/timer/plug_off.jpg", "rb").read()
self.led_15 = widgets.Image(value=self.off_15, format='jpg', width=50, height=50)
self.led_30 = widgets.Image(value=self.off_30, format='jpg', width=50, height=50)
self.led_45 = widgets.Image(value=self.off_45, format='jpg', width=50, height=50)
self.led_60 = widgets.Image(value=self.off_60, format='jpg', width=50, height=50)
left_box = widgets.VBox([self.led_60, self.led_45])
right_box = widgets.VBox([self.led_15, self.led_30])
box = widgets.HBox([left_box, right_box])
self.plug = widgets.Image(value=self.plug_off, format='jpg', width=100, height=100)
# display the user interface
# a button
self.button = widgets.Button(description="Button")
self.button.on_click(self.on_button_press)
display(box, self.button, self.plug)
def switch_led(self, led, on):
if led is '15' and on: self.led_15.set_trait(name='value', value=self.on_15)
if led is '15' and not on: self.led_15.set_trait(name='value', value=self.off_15)
if led is '30' and on: self.led_30.set_trait(name='value', value=self.on_30)
if led is '30' and not on: self.led_30.set_trait(name='value', value=self.off_30)
if led is '45' and on: self.led_45.set_trait(name='value', value=self.on_45)
if led is '45' and not on: self.led_45.set_trait(name='value', value=self.off_45)
if led is '60' and on: self.led_60.set_trait(name='value', value=self.on_60)
if led is '60' and not on: self.led_60.set_trait(name='value', value=self.off_60)
def switch_plug(self, on):
if on: self.plug.set_trait(name='value', value=self.plug_on)
else: self.plug.set_trait(name='value', value=self.plug_off)
kt = KitchenTimer()
t0 = {'source': 'initial',
'target': 'off',}
off = {'name': 'off',
'entry': 'switch_led("0",False); switch_plug(False); stop_timer("t")',
}
t1 = {'trigger': 'switch',
'source': 'off',
'effect': 'switch_plug(True)',
'target':'on_15'}
on_15 = {'name': 'on_15',
'entry':'start_timer("t", 4000);switch_led("15", True)'}
t2 = {'trigger':'t',
'source':'on_15',
'target':'off',
'effect':'switch_led("15", False)'}
t3 = {'trigger':'switch',
'source':'on_15',
'target':'on_30',}
on_30 = {'name':'on_30',
'entry':'start_timer("t", 4000);switch_led("30", True)'}
t4 = {'trigger':'t',
'source':'on_30',
'target':'on_15',
'effect':'switch_led("30", False)'}
t5 = {'trigger':'switch',
'source':'on_30',
'target':'on_45'
}
on_45 = {'name':'on_45',
'entry':'start_timer("t", 4000);switch_led("45", True)'}
t6 = {'trigger':'t',
'source':'on_45',
'target':'on_30',
'effect':'switch_led("45", False)'}
t7 = {'trigger':'switch',
'source':'on_45',
'target':'on_60'}
t8 = {'trigger':'t',
'source':'on_60',
'target':'on_45',
'effect':'switch_led("60", False)'}
on_60 = {'name':'on_60',
'entry':'start_timer("t", 4000);switch_led("60", True)'}
t9 = {'trigger':'switch',
'source':'on_60',
'target':'off',
'effect':'switch_led("60",False);switch_led("45",False);switch_led("30",False);switch_led("15",False);'}
machine = Machine(name='kitchentimer', transitions=[t0, t1, t2, t3, t4, t5, t6, t7, t8, t9], obj=kt, states=[off, on_15, on_30, on_45, on_60])
kt.stm = machine
driver = Driver()
driver.add_machine(machine)
driver.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment