الفعاليات#
مقدمة#
تتيح لك الأحداث تشغيل الوظائف بالتوازي باستخدام كائنات الأحداث. فبدلاً من استدعاء الوظائف أو الخيوط واحدة تلو الأخرى، تسمح لك الأحداث بتسجيل الوظائف ثم تشغيلها جميعًا في وقت واحد. تعمل كل وظيفة مسجلة في خيط خاص بها، مما يُمكّن الروبوت من القيام بمهام متعددة في الوقت نفسه، مثل وميض مصابيح اللمس والقيادة.
فيما يلي قائمة بالأساليب والمنشئات المتاحة:
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()
المعلمة |
وصف |
|---|---|
|
اسم كائن الحدث الذي تم إنشاؤه مسبقًا. |
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()
المعلمة |
وصف |
|---|---|
|
اسم كائن الحدث الذي تم إنشاؤه مسبقًا. |
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)
المعلمة |
وصف |
|---|---|
|
اسم كائن الحدث الذي تم إنشاؤه مسبقًا. |
|
دالة مُعرَّفة مسبقاً لتنفيذها عند بث الحدث. |
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)