import os
import sys
import time
import logging
from ev3dev2.sound import Sound
from ev3dev2.led import Leds
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, LargeMotor
from agt import AlexaGadget
# set logger to display on both EV3 Brick and console
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)
class MindstormsGadget(AlexaGadget):
def __init__(self):
super().__init__()
self.leds = Leds()
self.sound = Sound()
self.thumb_motor = LargeMotor(OUTPUT_A)
self.index_motor = LargeMotor(OUTPUT_B)
self.middle_motor = LargeMotor(OUTPUT_C)
self.ring_motor = LargeMotor(OUTPUT_D)
self.bpm = 0
self.trigger_bpm = "off"
def on_connected(self, device_addr):
self.leds.set_color("LEFT", "GREEN")
self.leds.set_color("RIGHT", "GREEN")
logger.info("{} connected to Echo device".format(self.friendly_name))
def on_disconnected(self, device_addr):
self.leds.set_color("LEFT", "BLACK")
self.leds.set_color("RIGHT", "BLACK")
logger.info("{} disconnected from Echo device".format(self.friendly_name))
def on_alexa_gadget_statelistener_stateupdate(self, directive):
color_list = ['BLACK', 'AMBER', 'YELLOW', 'GREEN']
for state in directive.payload.states:
if state.name == 'open hand':
if state.value == 'active':
self.thumb_motor.run_timed(speed_sp=25, time_sp=1000)
self.index_motor.run_timed(speed_sp=25, time_sp=1000)
self.middle_motor.run_timed(speed_sp=25, time_sp=1000)
self.ring_motor.run_timed(speed_sp=25, time_sp=1000)
if state.name == 'close hand':
if state.value == 'active':
self.thumb_motor.run_timed(speed_sp=-25, time_sp=1000)
self.index_motor.run_timed(speed_sp=-25, time_sp=1000)
self.middle_motor.run_timed(speed_sp=-25, time_sp=1000)
self.ring_motor.run_timed(speed_sp=-25, time_sp=1000)
if __name__ == '__main__':
MindstormsGadget().main()
# Set LCD font and turn off blinking LEDs
os.system('setfont Lat7-Terminus12x6')
gadget.leds.set_color("LEFT", "BLACK")
gadget.leds.set_color("RIGHT", "BLACK")
# Startup sequence
gadget.sound.play_song((('C4', 'e'), ('D4', 'e'), ('E5', 'q')))
gadget.leds.set_color("LEFT", "GREEN")
gadget.leds.set_color("RIGHT", "GREEN")
# Gadget main entry point
gadget.main()
# Shutdown sequence
gadget.sound.play_song((('E5', 'e'), ('C4', 'e')))
gadget.leds.set_color("LEFT", "BLACK")
gadget.leds.set_color("RIGHT", "BLACK")
Comments