-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__init__.py
121 lines (98 loc) · 4.01 KB
/
__init__.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
# Copyright 2017, Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
from threading import Thread
from mycroft.messagebus.message import Message
from mycroft.skills.core import MycroftSkill
#######################################################################
# Helpers for building the animation frames
def animate(t, often, func, *args):
'''
Args:
t (int) : seconds from now to begin the frame (secs)
often (int/string): (int) how often to repeat the frame (secs)
(str) when to trigger, relative to the clock,
for synchronized repetitions
func: the function to invoke
*args: arguments to pass to func
'''
return {
"time": time.time() + t,
"often": often,
"func": func,
"args": args
}
def _get_time(often, t):
return often - t % often
#######################################################################
class Mark1DemoSkill(MycroftSkill):
def __init__(self):
super(Mark1DemoSkill, self).__init__("Mark1DemoSkill")
self.animations = []
self.playing = False
self.thread = None
def initialize(self):
# Listen for the message send by the DEMO menu item on a Mark 1
self.add_event("mycroft.mark1.demo", self.demo, False)
def run(self):
while self.playing:
for animation in self.animations:
if animation["time"] <= time.time():
# Execute animation action
animation["func"](*animation["args"])
# Adjust time for next loop
if type(animation["often"]) is int:
animation["time"] = time.time() + animation["often"]
else:
often = int(animation["often"])
t = animation["time"]
animation["time"] = time.time() + _get_time(
often, t)
time.sleep(0.1)
self.thread = None
def demo(self, message):
if not self.thread:
self.playing = True
self.enclosure.mouth_text("...starting...")
# Sync clock to make chorus as good as possible
self.bus.emit(Message("system.ntp.sync"))
time.sleep(15)
self.enclosure.mouth_reset()
# Build the list of animation actions to run
self.animations = [
# Change the eyes every 2 seconds, looping at 8 seconds
animate(0, 8, self.enclosure.eyes_look, "r"),
animate(2, 8, self.enclosure.eyes_look, "l"),
animate(4, 8, self.enclosure.eyes_look, "d"),
animate(6, 8, self.enclosure.eyes_look, "u"),
# Every 120 seconds, sing a song. This is synchronized
# based on the clock, so multiple units can sing in abs
# chorus.
#
# skill-singing handles the mycroft.sing message
animate(_get_time(120, time.time()), "120",
self.bus.emit, Message("mycroft.sing"))
]
self.thread = Thread(None, self.run)
self.thread.daemon = True
self.thread.start()
def stop(self):
if self.playing:
self.playing = False # the thread will kill itself
self.enclosure.eyes_reset()
return True
else:
return False
def create_skill():
return Mark1DemoSkill()