ഇവന്റുകൾ#
ആമുഖം#
ഇവന്റുകൾ നിങ്ങളെ ഫംഗ്ഷനുകൾ സമാന്തരമായി പ്രവർത്തിപ്പിക്കാൻ അനുവദിക്കുന്നു. തുടർച്ചയായി ഫംഗ്ഷനുകൾ വിളിക്കുന്നതിനുപകരം, നിങ്ങൾക്ക് ഒരു ഇവന്റിലേക്ക് ഒന്നിലധികം ഫംഗ്ഷനുകൾ രജിസ്റ്റർ ചെയ്യാനും അവയെല്ലാം ഒരേസമയം ട്രിഗർ ചെയ്യാനും കഴിയും. രജിസ്റ്റർ ചെയ്ത ഓരോ ഫംഗ്ഷനും അതിന്റേതായ ത്രെഡിൽ പ്രവർത്തിക്കുന്നു, ഇത് നിങ്ങളുടെ റോബോട്ടിനെ ഒരേസമയം ഒന്നിലധികം ജോലികൾ ചെയ്യാൻ പ്രാപ്തമാക്കുന്നു - ഡ്രൈവ് ചെയ്യുമ്പോൾ സ്ക്രീനിൽ മൂല്യങ്ങൾ പ്രദർശിപ്പിക്കുന്നത് പോലെ.
ഒരു ഇവന്റ് സൃഷ്ടിക്കുന്നതിനും ഉപയോഗിക്കുന്നതിനും, ഈ ഘട്ടങ്ങൾ ക്രമത്തിൽ പാലിക്കുക:
Eventഉപയോഗിച്ച് ഇവന്റ് സൃഷ്ടിക്കുക.Eventപ്രക്ഷേപണം ചെയ്യുമ്പോൾ, ഫംഗ്ഷൻ ഒരു ആർഗ്യുമെന്റായി നൽകിeventഒബ്ജക്റ്റ് വിളിച്ചുകൊണ്ട് ട്രിഗർ ചെയ്യുന്നതിനായി ഒരു ഫംഗ്ഷൻ രജിസ്റ്റർ ചെയ്യുക.Broadcast an
Event:broadcast— ഒരു ഇവന്റ് ഒബ്ജക്റ്റിലെ എല്ലാ രജിസ്റ്റർ ചെയ്ത ഫംഗ്ഷനുകളും ഒരേസമയം പ്രവർത്തിപ്പിക്കാൻ ട്രിഗർ ചെയ്യുന്നു.broadcast_and_wait— ഒരു ഇവന്റ് ഒബ്ജക്റ്റിലെ എല്ലാ രജിസ്റ്റർ ചെയ്ത ഫംഗ്ഷനുകളും ട്രിഗർ ചെയ്യുകയും തുടരുന്നതിന് മുമ്പ് അവ പൂർത്തിയാകുന്നതുവരെ കാത്തിരിക്കുകയും ചെയ്യുന്നു.
ഒരു ഇവന്റ് സൃഷ്ടിക്കുക#
Event പ്രത്യേക ത്രെഡുകളിൽ ഫംഗ്ഷൻ എക്സിക്യൂഷൻ കൈകാര്യം ചെയ്യുന്ന ഒരു ഇവന്റ് ഒബ്ജക്റ്റ് സൃഷ്ടിക്കുന്നു.
Usage:
Event()
പാരാമീറ്റർ |
വിവരണം |
|---|---|
ഈ കൺസ്ട്രക്റ്ററിന് പാരാമീറ്ററുകൾ ഇല്ല. |
# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
move_event(blink_screen)
move_event.broadcast()
drivetrain.drive(FORWARD)
ഒരു ഇവന്റിലേക്ക് പ്രവർത്തനങ്ങൾ രജിസ്റ്റർ ചെയ്യുക#
ഒരു ഇവന്റിലേക്ക് ഒരു ഫംഗ്ഷൻ രജിസ്റ്റർ ചെയ്യുമ്പോൾ, ഇവന്റ് പ്രക്ഷേപണം ചെയ്യുമ്പോൾ അത് ഒരു പ്രത്യേക ത്രെഡിൽ എക്സിക്യൂട്ട് ചെയ്യും.
Usage:
event(callback, args)
പാരാമീറ്റർ |
വിവരണം |
|---|---|
|
മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്. |
|
പരിപാടി പ്രക്ഷേപണം ചെയ്യുമ്പോൾ നടപ്പിലാക്കുന്നതിനായി മുമ്പ് നിർവചിക്കപ്പെട്ട ഒരു ഫംഗ്ഷൻ. |
|
ഓപ്ഷണൽ. കോൾബാക്ക് ഫംഗ്ഷനിലേക്ക് കൈമാറേണ്ട ഒരു tuple ആർഗ്യുമെന്റുകൾ. |
# Blink the screen while turning
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
def turning():
drivetrain.turn(RIGHT)
# Register multiple functions to the Event object
move_event(blink_screen)
move_event(turning)
move_event.broadcast()
# Move in a specified direction
move_event = Event()
def move_distance(movement_angle):
drivetrain.turn_for(RIGHT, movement_angle, DEGREES)
drivetrain.drive_for(FORWARD, 100, MM)
# Change the parameter to modify how the robot moves
move_event(move_distance, (180,))
move_event.broadcast()
ഒരു ഇവന്റ് പ്രക്ഷേപണം ചെയ്യുക#
broadcast#
broadcast ഒരു ഇവന്റ് ട്രിഗർ ചെയ്യുന്നു, രജിസ്റ്റർ ചെയ്ത എല്ലാ ഫംഗ്ഷനുകളും പ്രത്യേക ത്രെഡുകളിൽ ആരംഭിക്കുന്നു. ഈ രീതി തുടർന്നുള്ള ഫംഗ്ഷനുകളുടെ നിർവ്വഹണം താൽക്കാലികമായി നിർത്തുന്നില്ല.
Usage:
event.broadcast()
പാരാമീറ്റർ |
വിവരണം |
|---|---|
|
മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്. |
# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
move_event(blink_screen)
move_event.broadcast()
drivetrain.drive(FORWARD)
broadcast_and_wait#
broadcast_and_wait ഒരു ഇവന്റ് ആരംഭിക്കുന്നു, പക്ഷേ തുടർന്നുള്ള ഫംഗ്ഷനുകൾ തുടരുന്നതിന് മുമ്പ് രജിസ്റ്റർ ചെയ്ത എല്ലാ ഫംഗ്ഷനുകളും പൂർത്തിയാകുന്നതുവരെ കാത്തിരിക്കുന്നു.
Usage:
event.broadcast_and_wait()
പാരാമീറ്റർ |
വിവരണം |
|---|---|
|
മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്. |
# Move after the screen is pressed
move_event = Event()
def move_and_turn():
drivetrain.drive_for(FORWARD, 150, MM)
drivetrain.turn_for(RIGHT, 90)
move_event(move_and_turn)
while True:
if brain.buttonCheck.pressing():
move_event.broadcast_and_wait()
break
brain.screen.print("Movement done.")