ഇവന്റുകൾ#

ആമുഖം#

ഇവന്റ് ഒബ്‌ജക്റ്റുകൾ ഉപയോഗിച്ച് സമാന്തരമായി ഫംഗ്‌ഷനുകൾ പ്രവർത്തിപ്പിക്കാൻ ഇവന്റുകൾ നിങ്ങളെ അനുവദിക്കുന്നു. ഫംഗ്‌ഷനുകളെയോ ത്രെഡുകളെയോ ഒന്നിനുപുറകെ ഒന്നായി വിളിക്കുന്നതിനുപകരം, ഫംഗ്‌ഷനുകൾ രജിസ്റ്റർ ചെയ്യാനും അവയെല്ലാം ഒരേസമയം ട്രിഗർ ചെയ്യാനും ഇവന്റുകൾ നിങ്ങളെ അനുവദിക്കുന്നു. രജിസ്റ്റർ ചെയ്ത ഓരോ ഫംഗ്‌ഷനും അതിന്റേതായ ത്രെഡിൽ പ്രവർത്തിക്കുന്നു, അതിനാൽ നിങ്ങളുടെ റോബോട്ടിന് ഒരേ സമയം ഒന്നിലധികം കാര്യങ്ങൾ ചെയ്യാൻ കഴിയും - TouchLED-കൾ മിന്നുന്നതും ഡ്രൈവ് ചെയ്യുന്നതും പോലെ.

ലഭ്യമായ രീതികളുടെയും കൺസ്ട്രക്റ്ററുകളുടെയും ഒരു ലിസ്റ്റ് താഴെ കൊടുക്കുന്നു:

  • Event – ഒരു പുതിയ ഇവന്റ് ഒബ്‌ജക്റ്റ് സൃഷ്‌ടിക്കുന്നു.

  • broadcast – ഒരു ഇവന്റ് ഒബ്‌ജക്റ്റിലെ എല്ലാ രജിസ്റ്റർ ചെയ്ത ഫംഗ്‌ഷനുകളും സമാന്തരമായി പ്രവർത്തിപ്പിക്കാൻ ട്രിഗർ ചെയ്യുന്നു.

  • broadcast_and_wait – ഒരു ഇവന്റ് ഒബ്‌ജക്റ്റിലെ എല്ലാ രജിസ്റ്റർ ചെയ്ത ഫംഗ്ഷനുകളും ട്രിഗർ ചെയ്യുകയും തുടരുന്നതിന് മുമ്പ് അവ പൂർത്തിയാകുന്നതുവരെ കാത്തിരിക്കുകയും ചെയ്യുന്നു.

  • event – ഓപ്ഷണലായി ആർഗ്യുമെന്റുകൾ ഉപയോഗിച്ച് ഇവന്റ് ഒബ്ജക്റ്റിലേക്ക് ഒരു ഫംഗ്ഷൻ രജിസ്റ്റർ ചെയ്യുന്നു.

Create an Event Object#

Event പ്രത്യേക ത്രെഡുകളിൽ ഫംഗ്ഷൻ എക്സിക്യൂഷൻ കൈകാര്യം ചെയ്യുന്ന ഒരു ഇവന്റ് ഒബ്ജക്റ്റ് സൃഷ്ടിക്കുന്നു.

Usage:
Event()

പാരാമീറ്റർ

വിവരണം

ഈ കൺസ്ട്രക്റ്ററിന് പാരാമീറ്ററുകൾ ഇല്ല.

def dash_lines():
    # Move the pen up and down
    while True:
        pen.move(DOWN)
        wait(0.5, SECONDS)
        pen.move(UP)
        wait(0.5, SECONDS)

def main():
    # Set the event
    move_event = Event()
    move_event(dash_lines)
    # Draw the dash line while driving forward
    move_event.broadcast()
    drivetrain.drive_for(FORWARD, 1000, MM)

# VR threads — Do not delete
vr_thread(main)

Broadcast#

broadcast ഒരു ഇവന്റ് ട്രിഗർ ചെയ്യുന്നു, രജിസ്റ്റർ ചെയ്ത എല്ലാ ഫംഗ്ഷനുകളും പ്രത്യേക ത്രെഡുകളിൽ ആരംഭിക്കുന്നു. ഈ രീതി തുടർന്നുള്ള ഫംഗ്ഷനുകളുടെ നിർവ്വഹണം താൽക്കാലികമായി നിർത്തുന്നില്ല.

Usage:
my_event.broadcast()

പാരാമീറ്റർ

വിവരണം

event

മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്.

def dash_lines():
    # Move the pen up and down
    while True:
        pen.move(DOWN)
        wait(0.5, SECONDS)
        pen.move(UP)
        wait(0.5, SECONDS)

def main():
    # Set the event
    move_event = Event()
    move_event(dash_lines)
    # Draw the dash line while driving forward
    move_event.broadcast()
    drivetrain.drive_for(FORWARD, 1000, MM)

# VR threads — Do not delete
vr_thread(main)

Broadcast and wait#

broadcast_and_wait ഒരു ഇവന്റ് ആരംഭിക്കുന്നു, പക്ഷേ തുടർന്നുള്ള ഫംഗ്ഷനുകൾ തുടരുന്നതിന് മുമ്പ് രജിസ്റ്റർ ചെയ്ത എല്ലാ ഫംഗ്ഷനുകളും പൂർത്തിയാകുന്നതുവരെ കാത്തിരിക്കുന്നു.

Usage:
my_event.broadcast_and_wait()

പാരാമീറ്റർ

വിവരണം

event

മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്.

def move_and_turn():
    # Drive forward and turn
    drivetrain.drive_for(FORWARD, 150, MM)
    drivetrain.turn_for(RIGHT, 90, DEGREES)

def main():
    # Wait for the event to finish after 3 seconds
    move_event = Event()
    move_event(move_and_turn)
    while True:
        if brain.timer_time(SECONDS) > 3:
            move_event.broadcast_and_wait()
            break
        wait(5, MSEC)
    brain.print("Movement done.")

# VR threads — Do not delete
vr_thread(main)

Register Functions to an Event#

ഒരു ഇവന്റിലേക്ക് ഒരു ഫംഗ്ഷൻ രജിസ്റ്റർ ചെയ്യുമ്പോൾ, ഇവന്റ് പ്രക്ഷേപണം ചെയ്യുമ്പോൾ അത് ഒരു പ്രത്യേക ത്രെഡിൽ എക്സിക്യൂട്ട് ചെയ്യും.

Usage:
event(callback, args)

പാരാമീറ്റർ

വിവരണം

event

മുമ്പ് സൃഷ്ടിച്ച ഇവന്റ് ഒബ്ജക്റ്റിന്റെ പേര്.

callback

പരിപാടി പ്രക്ഷേപണം ചെയ്യുമ്പോൾ നടപ്പിലാക്കുന്നതിനായി മുമ്പ് നിർവചിക്കപ്പെട്ട ഒരു ഫംഗ്ഷൻ.

def dash_lines():
    # Move the pen up and down
    while True:
        pen.move(DOWN)
        wait(0.5, SECONDS)
        pen.move(UP)
        wait(0.5, SECONDS)

def main():
    # Set and call the event and start driving
    move_event = Event()
    move_event(dash_lines)
    move_event.broadcast()
    drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)