-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
59 lines (49 loc) · 2.12 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
from mycroft import MycroftSkill, intent_file_handler
from mycroft.messagebus.message import Message
import time
import RPi.GPIO as GPIO
BUTTON = 17
class WakeButtonRpi(MycroftSkill):
def __init__(self):
self.ignore_short_after_long = False
MycroftSkill.__init__(self)
def initialize(self):
try:
BUTTON = self.settings.get('gpio_port', 17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(BUTTON, GPIO.RISING, bouncetime = 500)
except GPIO.error:
self.log.warning("Can't initialize GPIO - skill will not load")
self.speak_dialog("error.initialize")
finally:
self.schedule_repeating_event(self.handle_button,
None, 0.1, 'WakeButtonRpi')
self.add_event('recognizer_loop:record_begin',
self.handle_listener_started)
self.add_event('recognizer_loop:record_end',
self.handle_listener_ended)
def handle_button(self, message):
longpress_threshold = 2
if GPIO.event_detected(BUTTON):
self.log.info("GPIO.event_detected")
pressed_time = time.time()
while GPIO.input(BUTTON):
time.sleep(0.2)
pressed_time = time.time() - pressed_time
if pressed_time < longpress_threshold:
if self.ignore_short_after_long == True:
self.log.info("ignoring short after long")
self.ignore_short_after_long = False
else:
self.bus.emit(Message("mycroft.mic.listen"))
else:
self.bus.emit(Message("mycroft.stop"))
self.ignore_short_after_long = True
def handle_listener_started(self, message):
# code to excecute when active listening begins...
self.log.info("WakeButtonRpi listener started")
def handle_listener_ended(self, message):
self.log.info("WakeButtonRpi listener ended")
def create_skill():
return WakeButtonRpi()