def init_hardware(): global button, display, leds, drive, infraredSensor drive = LargeMotor(OUTPUT_B) infraredSensor = InfraredSensor(INPUT_3) infraredSensor.mode = InfraredSensor.MODE_IR_PROX leds = Leds() # ultrasonicSensor = UltrasonicSensor(INPUT_2) # ultrasonicSensor.mode = UltrasonicSensor.MODE_US_DIST_CM display = Display() button = Button() button.on_enter = btn_on_enter
def play(self): delay = 0 step = [5, 10, 15, 20, 25, 30, 35, 40, 45, 55, 60, 65, 70] button = Button() button.on_up = self.volumeUp button.on_down = self.volumeDown button.on_left = self.multiplyUp button.on_right = self.multiplyDown button.on_enter = self.togglePause button.on_backspace = self.backButton ir = InfraredSensor() ts = TouchSensor() servo = MediumMotor() while True: self.volume = self.getVolume() button.process() if self.pause == True: continue distance = int(math.fabs(ir.value())) position = int(math.fabs(servo.position)) for x in step: if distance <= x: hertz = int(x * 15) # print("Hertz - " + str(hertz)) break for x in step: if position <= x: duration = int(x * 5 * self.multiply) # print("Duration - " + str(duration)) break if ts.is_pressed: if delay == 200: delay = 0 else: if delay == 0: delay = 200 # play sound self.sound.tone([(hertz, duration, delay)])
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()
def down(state): print('Down button pressed' if state else 'Down button released') def enter(state): print('Enter button pressed' if state else 'Enter button released') def backspace(state): print('Backspace button pressed' if state else 'Backspace button released') btn.on_left = left btn.on_right = right btn.on_up = up btn.on_down = down btn.on_enter = enter btn.on_backspace = backspace # This loop checks buttons state continuously (every 0.01s). # If the new state differs from the old state then the appropriate # button event handlers are called. while True: btn.process() # Check for currently pressed buttons. sleep(0.01) # If running this script from VS Code, press the stop button to quit # if running from Brickman, long-press backspace button to quit
try: missions[selectedProgram].run() if selectedProgram < numMissions - 1: selectedProgram = selectedProgram + 1 except Exception as e: soundGenerator.beep() robot.debug("EXCEPTION") robot.debug(e) selectedProgram = selectedProgram + 1 robot.afterMission() print(missionNames[selectedProgram]) # Register the buttonListener buttonListener.on_left = left buttonListener.on_right = right buttonListener.on_enter = enter soundGenerator.beep() # Read the calibrated values and test if the Gyro is drifting robot.init() #testoPesto.run() print(missionNames[selectedProgram]) # Our Main Loop while True: # Check if any buttons are pressed, and call the corresponding event handler buttonListener.process() # Debounce; Make sure the user has time to release the button sleep(0.1)
def down(state): # down 이벤트 함수 if state: # 아래 버튼 눌림 lcd.text_pixels('Down button pressed', x=0, y=50, font='courB14') lcd.update() sleep(0.2) else: # 아래 버튼 뗌 lcd.text_pixels('Down button released', x=0, y=50, font='courB14') lcd.update() sleep(0.2) def enter(state): # enter 이벤트 함수 if state: # 엔터 버튼 눌림 lcd.text_pixels('Enter button pressed', x=0, y=50, font='courB14') lcd.update() sleep(0.2) else: # 엔터 버튼 뗌 lcd.text_pixels('Enter button released', x=0, y=50, font='courB14') lcd.update() sleep(0.2) btn.on_left = left #왼쪽 버튼 이벤트 함수를 left 함수로 등록 btn.on_right = right #오른쪽 버튼 이벤트 함수를 right 함수로 등록 btn.on_up = up #위쪽 버튼 이벤트 함수를 up 함수로 등록 btn.on_down = down #아래쪽 버튼 이벤트 함수를 down 함수로 등록 btn.on_enter = enter #엔터 버튼 이벤트 함수를 enter 함수로 등록 while True: btn.process() # 버튼 이벤트 상태를 감지 함 sleep(0.01)
global run bt = button.buttons_pressed if "left" in bt or "right" in bt or "enter" in bt: if type(plus) == type(2): if plus > 0: program += 1 else: program -= 1 elif type(plus) == type(True): run = currentProgram currentProgram = (program % len(programs)) + 1 button.on_right = lambda x: c(1) button.on_left = lambda x: c(-1) button.on_enter = lambda x: c(True) # Program running handling currentProgram = 1 pastProgram = currentProgram console.reset_console() console.text_at("Selected: " + str(currentProgram)) while True: button.process() if currentProgram != pastProgram: clearColumn(1) console.text_at("Selected: " + str(currentProgram)) if run: clearColumn(1)
tank_drive.on_for_rotations(SpeedPercent(-50), SpeedPercent(-50), 1) #red = turn * def red(): tank_drive.on_for_rotations(SpeedPercent(0), SpeedPercent(50), 1) #blue = turn * def blue(): tank_drive.on_for_rotations(SpeedPercent(50), SpeedPercent(0), 1) #definning what "ColorChecking" is def ColorChecking(): if color.color == color.COLOR_YELLOW: #if yellow yellow() elif color.color == color.COLOR_GREEN: #if green green() elif color.color == color.COLOR_RED: #if red red() elif color.color == color.COLOR_BLUE: blue() #This is where the movement happens. the function "ColorChecking" is a function to decide what to do based on color. btn.on_enter = ColorChecking() #not whenever we touch enter (or the mibble button) then it will call ColorChecking(). Not just when this line happens start = time.time() while (time.time() - start) <= 30: #this code essentially waits 30 seconds pass