Пример #1
0
def command_loop():
    btn = Button()
    btn.on_up = up_pressed
    btn.on_down = down_pressed
    while not btn.enter:
        btn.process()
        sleep(0.01)
Пример #2
0
    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)])
Пример #3
0

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
Пример #4
0
import time
from collections import deque

# When the up button is pressed, end the script.
end = False

buttons = Button()


def end_script(state):
    global end
    if state is True:
        end = True


buttons.on_up = end_script

# Some behavioural constants
STEP_LENGTH = (1, 3)  # Move in a new direction every 1-3 seconds
MOTOR_SPEEDS = (-100, 100)  # Motor values are anything between -100 and 100
PRINT_TIME = 5  # Print sensor values every 5 seconds


def random_between(a, b):
    # Returns a random float between a and b:
    return a + random.random() * (b - a)


# Initialise all sensors.
lm1 = LargeMotor(address="outB")
lm2 = LargeMotor(address="outC")
Пример #5
0
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)
Пример #6
0
	# touch any part of the body
	if( cnt >= 3):
		indx=-3
		while indx > 0-cnt:
			tmpX=Sx[indx]
			tmpY=Sy[indx]
			if( tmpX==x and tmpY==y):
				return True
			indx -= 3

	return False

# subscribe the button events
btn.on_left = left
btn.on_right = right
btn.on_up = up
btn.on_down = down
createBuger()
display.clear()

# draw hanburger
display.draw.ellipse((hx,hy,hx+step,hy+step))

while True:
	# draw score on the screen
	display.draw.text((0,2), 'Score {0}'.format(length), fill='black')

	# process the button event
	btn.process()

	# start the game while any direction is given