18 from alsaaudio
import Mixer
19 from threading
import Thread, Timer
31 init_display_manager_bus_connection
34 from mycroft.util import play_wav, create_signal, connected, check_for_signal
37 from queue
import Queue
50 Reads data from Serial port. 52 Listens to all commands sent by Arduino that must be be performed on 55 E.g. Mycroft Stop Feature 56 #. Arduino sends a Stop command after a button press on a Mycroft unit 57 #. ``EnclosureReader`` captures the Stop command 58 #. Notify all Mycroft Core processes (e.g. skills) to be stopped 60 Note: A command is identified by a line break 69 self.
lang = lang
or 'en-us' 78 data = self.serial.readline()[:-2]
81 data_str = data.decode()
82 except UnicodeError
as e:
83 data_str = data.decode(
'utf-8', errors=
'replace')
84 LOG.warning(
'Invalid characters in response from ' 85 ' enclosure: {}'.format(repr(e)))
87 except Exception
as e:
88 LOG.error(
"Reading error: {0}".format(e))
98 if "mycroft.stop" not in data:
101 if "Command: system.version" in data:
104 self.bus.emit(
Message(
"enclosure.started"))
106 if "mycroft.stop" in data:
109 self.bus.emit(
Message(
"mycroft.stop"))
111 if "volume.up" in data:
112 self.bus.emit(
Message(
"mycroft.volume.increase",
113 {
'play_sound':
True}))
115 if "volume.down" in data:
116 self.bus.emit(
Message(
"mycroft.volume.decrease",
117 {
'play_sound':
True}))
119 if "system.test.begin" in data:
120 self.bus.emit(
Message(
'recognizer_loop:sleep'))
122 if "system.test.end" in data:
123 self.bus.emit(
Message(
'recognizer_loop:wake_up'))
125 if "mic.test" in data:
127 prev_vol = mixer.getvolume()[0]
129 self.bus.emit(
Message(
"speak", {
130 'utterance':
"I am testing one two three"}))
133 record(
"/tmp/test.wav", 3.0)
134 mixer.setvolume(prev_vol)
135 play_wav(
"/tmp/test.wav").communicate()
138 subprocess.call(
'speaker-test -P 10 -l 0 -s 1', shell=
True)
140 if "unit.shutdown" in data:
142 self.bus.emit(
Message(
"enclosure.eyes.color",
143 {
'r': 70, 'g': 65, 'b': 69}))
145 Message(
"enclosure.eyes.timedspin",
147 self.bus.emit(
Message(
"enclosure.mouth.reset"))
149 self.bus.emit(
Message(
"system.shutdown"))
151 if "unit.reboot" in data:
153 self.bus.emit(
Message(
"enclosure.eyes.color",
154 {
'r': 70, 'g': 65, 'b': 69}))
155 self.bus.emit(
Message(
"enclosure.eyes.spin"))
156 self.bus.emit(
Message(
"enclosure.mouth.reset"))
158 self.bus.emit(
Message(
"system.reboot"))
160 if "unit.setwifi" in data:
161 self.bus.emit(
Message(
"system.wifi.setup", {
'lang': self.
lang}))
163 if "unit.factory-reset" in data:
164 self.bus.emit(
Message(
"speak", {
167 'rm ~/.mycroft/identity/identity2.json',
169 self.bus.emit(
Message(
"system.wifi.reset"))
170 self.bus.emit(
Message(
"system.ssh.disable"))
172 self.bus.emit(
Message(
"enclosure.mouth.reset"))
173 self.bus.emit(
Message(
"enclosure.eyes.spin"))
174 self.bus.emit(
Message(
"enclosure.mouth.reset"))
176 self.bus.emit(
Message(
"system.reboot"))
178 if "unit.enable-ssh" in data:
180 self.bus.emit(
Message(
"system.ssh.enable"))
181 self.bus.emit(
Message(
"speak", {
184 if "unit.disable-ssh" in data:
186 self.bus.emit(
Message(
"system.ssh.disable"))
187 self.bus.emit(
Message(
"speak", {
190 if "unit.enable-learning" in data
or "unit.disable-learning" in data:
191 enable =
'enable' in data
192 word =
'enabled' if enable
else 'disabled' 194 LOG.info(
"Setting opt_in to: " + word)
195 new_config = {
'opt_in': enable}
196 user_config = LocalConf(USER_CONFIG)
197 user_config.merge(new_config)
200 self.bus.emit(
Message(
"speak", {
209 Writes data to Serial port. 210 #. Enqueues all commands received from Mycroft enclosures 212 #. Process them on the received order by writing on the Serial port 214 E.g. Displaying a text on Mycroft's Mouth 215 #. ``EnclosureMouth`` sends a text command 216 #. ``EnclosureWriter`` captures and enqueue the command 217 #. ``EnclosureWriter`` removes the next command from the queue 218 #. ``EnclosureWriter`` writes the command to Serial port 220 Note: A command has to end with a line break 235 cmd = self.commands.get() +
'\n' 236 self.serial.write(cmd.encode())
237 self.commands.task_done()
238 except Exception
as e:
239 LOG.error(
"Writing error: {0}".format(e))
242 self.commands.put(str(command))
250 Serves as a communication interface between Arduino and Mycroft Core. 252 ``Enclosure`` initializes and aggregates all enclosures implementation. 254 E.g. ``EnclosureEyes``, ``EnclosureMouth`` and ``EnclosureArduino`` 256 It also listens to the basic events in order to perform those core actions 259 E.g. Start and Stop talk animation 262 _last_internet_notification = 0
277 self.writer.write(
"system.version")
312 if time.time() - Enclosure._last_internet_notification < 30:
316 Enclosure._last_internet_notification = time.time()
321 self.bus.emit(
Message(
"speak", {
322 'utterance':
"This device is not connected to the Internet. " 323 "Either plug in a network cable or hold the " 324 "button on top for two seconds, then select " 325 "wifi from the menu"}))
328 self.bus.emit(
Message(
'system.wifi.setup', {
'lang': self.
lang}))
332 self.
port = self.config.get(
"port")
333 self.
rate = self.config.get(
"rate")
337 LOG.info(
"Connected to: %s rate: %s timeout: %s" %
340 LOG.error(
"Impossible to connect to serial port: "+str(self.
port))
344 self.bus.on(
'enclosure.mouth.events.activate',
346 self.bus.on(
'enclosure.mouth.events.deactivate',
348 self.bus.on(
'enclosure.reset',
353 self.bus.on(
'recognizer_loop:record_begin', self.mouth.listen)
354 self.bus.on(
'recognizer_loop:record_end', self.mouth.reset)
355 self.bus.on(
'recognizer_loop:audio_output_start', self.mouth.talk)
356 self.bus.on(
'recognizer_loop:audio_output_end', self.mouth.reset)
359 self.bus.remove(
'recognizer_loop:record_begin', self.mouth.listen)
360 self.bus.remove(
'recognizer_loop:record_end', self.mouth.reset)
361 self.bus.remove(
'recognizer_loop:audio_output_start',
363 self.bus.remove(
'recognizer_loop:audio_output_end',
369 self.writer.write(
"eyes.reset")
370 self.writer.write(
"mouth.reset")
373 self.bus.emit(
Message(
"speak", {
'utterance': text}))
386 Handler for 'mycroft.paired', unmutes the mic after the pairing is 389 self.bus.emit(
Message(
"mycroft.mic.unmute"))
393 LOG.info(
"Checking internet connection")
397 self.
speak(
"This unit is not connected to the Internet. " 398 "Either plug in a network cable or hold the " 399 "button on top for two seconds, then select " 400 "wifi from the menu")
409 self.bus.emit(
Message(
"mycroft.mic.mute"))
419 data = {
'allow_timeout':
False,
'lang': self.
lang}
420 self.bus.emit(
Message(
'system.wifi.setup', data))
def __init__(self, serial, bus, size=16)
def check_for_signal(signal_name, sec_lifetime=0)
def wait_while_speaking()
def on_stop_handled(self, event)
def record(file_path, duration, rate, channels)
def create_signal(signal_name)
def init_display_manager_bus_connection()
def __init__(self, serial, bus, lang=None)
def get(phrase, lang=None, context=None)