api.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 from .display_manager import DisplayManager
16 from mycroft.messagebus.message import Message
17 
18 
19 '''
20 API for the functions that affect the Mark I device.
21 NOTE: current state management is poorly implemented,
22 will be changed in the future.
23 '''
24 
25 
27  """
28  This API is intended to be used to interface with the hardware
29  that is running Mycroft. It exposes all possible commands which
30  can be sent to a Mycroft enclosure implementation.
31 
32  Different enclosure implementations may implement this differently
33  and/or may ignore certain API calls completely. For example,
34  the eyes_color() API might be ignore on a Mycroft that uses simple
35  LEDs which only turn on/off, or not at all on an implementation
36  where there is no face at all.
37  """
38 
39  def __init__(self, bus, name=""):
40  self.bus = bus
41  self.name = name
42  self.display_manager = DisplayManager(self.name)
43 
44  def register(self, skill_name=""):
45  """Registers a skill as active. Used for speak() and speak_dialog()
46  to 'patch' a previous implementation. Somewhat hacky.
47  """
48  if self.name != "":
49  self.display_manager.set_active(self.name)
50  else:
51  self.display_manager.set_active(skill_name)
52 
53  def reset(self):
54  """The enclosure should restore itself to a started state.
55  Typically this would be represented by the eyes being 'open'
56  and the mouth reset to its default (smile or blank).
57  """
58  self.bus.emit(Message("enclosure.reset"))
59 
60  def system_reset(self):
61  """The enclosure hardware should reset any CPUs, etc."""
62  self.bus.emit(Message("enclosure.system.reset"))
63 
64  def system_mute(self):
65  """Mute (turn off) the system speaker."""
66  self.bus.emit(Message("enclosure.system.mute"))
67 
68  def system_unmute(self):
69  """Unmute (turn on) the system speaker."""
70  self.bus.emit(Message("enclosure.system.unmute"))
71 
72  def system_blink(self, times):
73  """The 'eyes' should blink the given number of times.
74  Args:
75  times (int): number of times to blink
76  """
77  self.bus.emit(Message("enclosure.system.blink", {'times': times}))
78 
79  def eyes_on(self):
80  """Illuminate or show the eyes."""
81  self.bus.emit(Message("enclosure.eyes.on"))
82 
83  def eyes_off(self):
84  """Turn off or hide the eyes."""
85  self.bus.emit(Message("enclosure.eyes.off"))
86 
87  def eyes_blink(self, side):
88  """Make the eyes blink
89  Args:
90  side (str): 'r', 'l', or 'b' for 'right', 'left' or 'both'
91  """
92  self.bus.emit(Message("enclosure.eyes.blink", {'side': side}))
93 
94  def eyes_narrow(self):
95  """Make the eyes look narrow, like a squint"""
96  self.bus.emit(Message("enclosure.eyes.narrow"))
97 
98  def eyes_look(self, side):
99  """Make the eyes look to the given side
100  Args:
101  side (str): 'r' for right
102  'l' for left
103  'u' for up
104  'd' for down
105  'c' for crossed
106  """
107  self.bus.emit(Message("enclosure.eyes.look", {'side': side}))
108 
109  def eyes_color(self, r=255, g=255, b=255):
110  """Change the eye color to the given RGB color
111  Args:
112  r (int): 0-255, red value
113  g (int): 0-255, green value
114  b (int): 0-255, blue value
115  """
116  self.bus.emit(Message("enclosure.eyes.color",
117  {'r': r, 'g': g, 'b': b}))
118 
119  def eyes_setpixel(self, idx, r=255, g=255, b=255):
120  """Set individual pixels of the Mark 1 neopixel eyes
121  Args:
122  idx (int): 0-11 for the right eye, 12-23 for the left
123  r (int): The red value to apply
124  g (int): The green value to apply
125  b (int): The blue value to apply
126  """
127  if idx < 0 or idx > 23:
128  raise ValueError('idx ({}) must be between 0-23'.format(str(idx)))
129  self.bus.emit(Message("enclosure.eyes.setpixel",
130  {'idx': idx, 'r': r, 'g': g, 'b': b}))
131 
132  def eyes_fill(self, percentage):
133  """Use the eyes as a type of progress meter
134  Args:
135  amount (int): 0-49 fills the right eye, 50-100 also covers left
136  """
137  if percentage < 0 or percentage > 100:
138  raise ValueError('percentage ({}) must be between 0-100'.
139  format(str(percentage)))
140  self.bus.emit(Message("enclosure.eyes.fill",
141  {'percentage': percentage}))
142 
143  def eyes_brightness(self, level=30):
144  """Set the brightness of the eyes in the display.
145  Args:
146  level (int): 1-30, bigger numbers being brighter
147  """
148  self.bus.emit(Message("enclosure.eyes.level", {'level': level}))
149 
150  def eyes_reset(self):
151  """Restore the eyes to their default (ready) state."""
152  self.bus.emit(Message("enclosure.eyes.reset"))
153 
154  def eyes_spin(self):
155  """Make the eyes 'roll'
156  """
157  self.bus.emit(Message("enclosure.eyes.spin"))
158 
159  def eyes_timed_spin(self, length):
160  """Make the eyes 'roll' for the given time.
161  Args:
162  length (int): duration in milliseconds of roll, None = forever
163  """
164  self.bus.emit(Message("enclosure.eyes.timedspin",
165  {'length': length}))
166 
167  def eyes_volume(self, volume):
168  """Indicate the volume using the eyes
169  Args:
170  volume (int): 0 to 11
171  """
172  if volume < 0 or volume > 11:
173  raise ValueError('volume ({}) must be between 0-11'.
174  format(str(volume)))
175  self.bus.emit(Message("enclosure.eyes.volume", {'volume': volume}))
176 
177  def mouth_reset(self):
178  """Restore the mouth display to normal (blank)"""
179  self.bus.emit(Message("enclosure.mouth.reset"))
180  self.display_manager.set_active(self.name)
181 
182  def mouth_talk(self):
183  """Show a generic 'talking' animation for non-synched speech"""
184  self.bus.emit(Message("enclosure.mouth.talk"))
185  self.display_manager.set_active(self.name)
186 
187  def mouth_think(self):
188  """Show a 'thinking' image or animation"""
189  self.bus.emit(Message("enclosure.mouth.think"))
190  self.display_manager.set_active(self.name)
191 
192  def mouth_listen(self):
193  """Show a 'thinking' image or animation"""
194  self.bus.emit(Message("enclosure.mouth.listen"))
195  self.display_manager.set_active(self.name)
196 
197  def mouth_smile(self):
198  """Show a 'smile' image or animation"""
199  self.bus.emit(Message("enclosure.mouth.smile"))
200  self.display_manager.set_active(self.name)
201 
202  def mouth_viseme(self, start, viseme_pairs):
203  """ Send mouth visemes as a list in a single message.
204 
205  Arguments:
206  start (int): Timestamp for start of speech
207  viseme_pairs: Pairs of viseme id and cumulative end times
208  (code, end time)
209 
210  codes:
211  0 = shape for sounds like 'y' or 'aa'
212  1 = shape for sounds like 'aw'
213  2 = shape for sounds like 'uh' or 'r'
214  3 = shape for sounds like 'th' or 'sh'
215  4 = neutral shape for no sound
216  5 = shape for sounds like 'f' or 'v'
217  6 = shape for sounds like 'oy' or 'ao'
218  """
219  self.bus.emit(Message("enclosure.mouth.viseme_list",
220  {"start": start, "visemes": viseme_pairs}))
221 
222  def mouth_text(self, text=""):
223  """Display text (scrolling as needed)
224  Args:
225  text (str): text string to display
226  """
227  self.display_manager.set_active(self.name)
228  self.bus.emit(Message("enclosure.mouth.text", {'text': text}))
229 
230  def mouth_display(self, img_code="", x=0, y=0, refresh=True):
231  """Display images on faceplate. Currently supports images up to 16x8,
232  or half the face. You can use the 'x' parameter to cover the other
233  half of the faceplate.
234  Args:
235  img_code (str): text string that encodes a black and white image
236  x (int): x offset for image
237  y (int): y offset for image
238  refresh (bool): specify whether to clear the faceplate before
239  displaying the new image or not.
240  Useful if you'd like to display multiple images
241  on the faceplate at once.
242  """
243  self.display_manager.set_active(self.name)
244  self.bus.emit(Message('enclosure.mouth.display',
245  {'img_code': img_code,
246  'xOffset': x,
247  'yOffset': y,
248  'clearPrev': refresh}))
249 
250  def mouth_display_png(self, image_absolute_path,
251  invert=False, x=0, y=0, refresh=True):
252  """ Send an image to the enclosure.
253 
254  Args:
255  image_absolute_path (string): The absolute path of the image
256  invert (bool): inverts the image being drawn.
257  x (int): x offset for image
258  y (int): y offset for image
259  refresh (bool): specify whether to clear the faceplate before
260  displaying the new image or not.
261  Useful if you'd like to display muliple images
262  on the faceplate at once.
263  """
264  self.display_manager.set_active(self.name)
265  self.bus.emit(Message("enclosure.mouth.display_image",
266  {'img_path': image_absolute_path,
267  'xOffset': x,
268  'yOffset': y,
269  'invert': invert,
270  'clearPrev': refresh}))
271 
272  def weather_display(self, img_code, temp):
273  """Show a the temperature and a weather icon
274 
275  Args:
276  img_code (char): one of the following icon codes
277  0 = sunny
278  1 = partly cloudy
279  2 = cloudy
280  3 = light rain
281  4 = raining
282  5 = stormy
283  6 = snowing
284  7 = wind/mist
285  temp (int): the temperature (either C or F, not indicated)
286  """
287  self.display_manager.set_active(self.name)
288  self.bus.emit(Message("enclosure.weather.display",
289  {'img_code': img_code, 'temp': temp}))
290 
291  def activate_mouth_events(self):
292  """Enable movement of the mouth with speech"""
293  self.bus.emit(Message('enclosure.mouth.events.activate'))
294 
295  def deactivate_mouth_events(self):
296  """Disable movement of the mouth with speech"""
297  self.bus.emit(Message('enclosure.mouth.events.deactivate'))
def eyes_blink(self, side)
Definition: api.py:87
def register(self, skill_name="")
Definition: api.py:44
def system_blink(self, times)
Definition: api.py:72
def __init__(self, bus, name="")
Definition: api.py:39


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40