def colourAttachment_values(): button = Button() stop = False os.system('setfont Lat15-TerminusBold14') # os.system('setfont Lat15-TerminusBold32x16') # Try this larger font print('insert black', file=stderr) print('insert black') button.wait_for_pressed(['enter']) black = colourAttachment.raw print('insert green', file=stderr) print('insert green') Delay_seconds(lambda: stop, 2) green = colourAttachment.raw print('insert red', file=stderr) print('insert red') Delay_seconds(lambda: stop, 2) red = colourAttachment.raw print('insert yellow', file=stderr) print('insert yellow') Delay_seconds(lambda: stop, 2) yellow = colourAttachment.raw print('insert white', file=stderr) print('insert white') Delay_seconds(lambda: stop, 2) white = colourAttachment.raw attachment_values = [black, green, red, yellow, white] print(black[0], file=stderr) return attachment_values
def button_interrupt(): if local: button = None else: button = Button() while running: if local: while running: i, o, e = select([sys.stdin], [], [], 0.1) if i and sys.stdin.readline().strip() == 'b': break else: while running: button.wait_for_pressed(['backspace'], 1000) if button.backspace: break send_signal(SIGINT, -1, False) end = time() + 3 while len(processes) > 0: update_processes() if time() > end: send_signal(SIGKILL, -1, False)
import threading import time from time import sleep import sys console = Console(font='Lat15-TerminusBold20x10') #Console.set_font(font='Lat15-TerminusBold24x12', reset_console=True) #打印到LCD print('start test:') #按键 button = Button() print('please press enter') print(button.buttons_pressed, file=sys.stderr) button.wait_for_pressed('enter', timeout_ms=10000) print(button.buttons_pressed, file=sys.stderr) def key_enter(new_state): print(new_state) print('press enter', file=sys.stderr) #定义按键的响应函数 button.on_enter = key_enter while True: if button.right: print("press right", file=sys.stderr) button.process()
from time import sleep import os from ev3dev2.button import Button os.system('setfont Lat7-Terminus12x6') btn = Button() with open("status") as f: status = f.read().strip() descriptions = { "off": "SmartSafe will not automatically start and will have to be manually started from SSH or Brickman.", "display": "SmartSafe will start automatically with 20 secounds of bootup and will break Brickman. To exit while running use SSH or say \"alexa open lego mindstorms\" then \"control 0 auth [your password]\"", "nodisplay": "SmartSafe will start automatically and will run in the background without displaying to the screen." } print("The current operation mode is", status) print("[OK]") sleep(1) btn.wait_for_pressed("enter") print("To change operation mode run _off.sh, _display.sh, or _nodisplay.sh") print("[OK]") sleep(1) btn.wait_for_pressed("enter") print("In this mode", descriptions[status]) print("[OK]") sleep(1) btn.wait_for_pressed("enter")
class MindstormsGadget(AlexaGadget): def __init__(self): super().__init__(gadget_config_path='./auth.ini') # order queue self.queue = Queue() self.button = Button() self.leds = Leds() self.sound = Sound() self.console = Console() self.console.set_font("Lat15-TerminusBold16.psf.gz", True) self.dispense_motor = LargeMotor(OUTPUT_A) self.pump_motor = LargeMotor(OUTPUT_B) self.touch_sensor = TouchSensor(INPUT_1) # Start threads threading.Thread(target=self._handle_queue, daemon=True).start() threading.Thread(target=self._test, daemon=True).start() def on_connected(self, device_addr): self.leds.animate_rainbow(duration=3, block=False) self.sound.play_song((('C4', 'e3'), ('C5', 'e3'))) def on_disconnected(self, device_addr): self.leds.animate_police_lights('RED', 'ORANGE', duration=3, block=False) self.leds.set_color("LEFT", "BLACK") self.leds.set_color("RIGHT", "BLACK") self.sound.play_song((('C5', 'e3'), ('C4', 'e3'))) def _test(self): while 1: self.button.wait_for_pressed('up') order = { 'name': 'Test', 'tea': 'Jasmine', 'sugar': 100, 'ice': 100 } self.queue.put(order) sleep(1) def _handle_queue(self): while 1: if self.queue.empty(): continue order = self.queue.get() self._make(name=order['name'], tea=order['tea'], sugar=order['sugar'], ice=order['ice']) def _send_event(self, name, payload): self.send_custom_event('Custom.Mindstorms.Gadget', name, payload) def _affirm_receive(self): self.leds.animate_flash('GREEN', sleeptime=0.25, duration=0.5, block=False) self.sound.play_song((('C3', 'e3'), ('C3', 'e3'))) def on_custom_mindstorms_gadget_control(self, directive): try: payload = json.loads(directive.payload.decode("utf-8")) print("Control payload: {}".format(payload), file=sys.stderr) control_type = payload["type"] # regular voice commands if control_type == "automatic": self._affirm_receive() order = { "name": payload["name"] or "Anonymous", "tea": payload["tea"] or "Jasmine Milk Tea", "sugar": payload["sugar"] or 100, "ice": payload["ice"] or 100, } self.queue.put(order) # series of voice commands elif control_type == "manual": # Expected params: [command] control_command = payload["command"] if control_command == "dispense": self._affirm_receive() if payload['num']: self._dispense(payload['num']) else: self._dispense() elif control_command == "pour": self._affirm_receive() if payload['num']: self._pour(payload['num']) else: self._pour() except KeyError: print("Missing expected parameters: {}".format(directive), file=sys.stderr) def _make(self, name=None, tea="Jasmine Milk Tea", sugar=100, ice=100): if not self.touch_sensor.is_pressed: # cup is not in place self._send_event('CUP', None) self.touch_sensor.wait_for_pressed() sleep(3) # cup enter delay # mid_col = console.columns // 2 # mid_row = console.rows // 2 # mid_col = 1 # mid_row = 1 # alignment = "L" process = self.sound.play_file('mega.wav', 100, Sound.PLAY_NO_WAIT_FOR_COMPLETE) # dispense boba self._dispense() # dispense liquid self._pour(tea=tea) # self.console.text_at( # s, column=mid_col, row=mid_row, alignment=alignment, reset_console=True # ) # notify alexa that drink is finished payload = { "name": name, "tea": tea, "sugar": sugar, "ice": ice, } self._send_event("DONE", payload) process.kill() # kill song self.sound.play_song((('C4', 'q'), ('C4', 'q'), ('C4', 'q')), delay=0.1) self.touch_sensor.wait_for_released() # dispense liquid def _pour(self, time_in_s=10, tea="Jasmine Milk Tea"): # send event to alexa payload = {"time_in_s": time_in_s, "tea": tea} self._send_event("POUR", payload) self.pump_motor.run_forever(speed_sp=1000) sleep(time_in_s) self.pump_motor.stop() # dispense boba def _dispense(self, cycles=10): # send event to alexa payload = {"cycles": cycles} self._send_event("DISPENSE", payload) # ensure the dispenser resets to the correct position everytime if cycles % 2: cycles += 1 # agitate the boba to make it fall for i in range(cycles): deg = 45 if i % 2 else -45 self.dispense_motor.on_for_degrees(SpeedPercent(75), deg) sleep(0.5)
#!/usr/bin/env python3 from ev3dev2.motor import OUTPUT_B, OUTPUT_C, MoveSteering from ev3dev2.sensor.lego import TouchSensor, UltrasonicSensor from ev3dev2.button import Button from time import sleep from ev3dev2.sound import Sound from threading import Thread from ev3dev2.display import Display us = UltrasonicSensor() us.mode = 'US-DIST-CM' ts = TouchSensor() lcd = Display() sound = Sound() sound.beep() btn = Button() while True: Distance = us.value() if ts.is_pressed == True and Distance < 400 and btn.wait_for_pressed( 'left'): lcd.text_pixels('Motor Start& Idle', x=0, y=60, font='courB14') lcd.update() sound.beep() sleep(0.2) sleep(1) else: sleep(0.01)
colourLeft = ColorSensor(INPUT_3) colourRight = ColorSensor(INPUT_2) gyro = GyroSensor(INPUT_1) largeMotor_Left = LargeMotor(OUTPUT_B) largeMotor_Right = LargeMotor(OUTPUT_C) mediumMotor = MediumMotor(OUTPUT_D) steering_drive = MoveSteering(OUTPUT_B, OUTPUT_C) tank_block = MoveTank(OUTPUT_B, OUTPUT_C) #_________________________________________________________________________________________________________________________________ # this is a program that we do not use that we where testing inbtween nregionals and nationals while True: x = 0 r = 0 g = 0 b = 0 while True: button.wait_for_pressed(['enter']) rgb = colourAttachment.raw print(rgb, file=stderr) x = x + 1 r = r + rgb[0] g = g + rgb[1] b = b + rgb[2] if x == 5: r = int(r / 5) g = int(g / 5) b = int(b / 5) print('average was: {}, {}, {}'.format(r, g, b), file=stderr) print('new area', file=stderr) break
'left': 3, 'right': 2, 'enter': 4, 'backspace': 6 } button = Button() leds = Leds() display = Display() leds.all_off() display.clear() t = time() while time() - t < 1: display.text_grid( f"Button pressed: {button.any()}, Button number: {button.get_pressed_button_number}" ) print(button.any(), button.get_pressed_button_number) # works with numbers if button.wait_for_pressed(6): print("6 pressed") display.text_grid("6 pressed") leds.set_color('RIGHT', (0, 0, 1)) # and names if button.wait_for_pressed('up'): print('up pressed') display.text_grid("up pressed")