이벤트#

소개#

이벤트를 사용하면 함수를 비동기적으로 시작할 수 있습니다. 이벤트가 브로드캐스트되면 이벤트의 콜백 함수가 별도의 태스크(스레드)에서 시작되므로 메인 코드는 동시에 계속 실행될 수 있습니다.

중요: 이벤트 객체 event 하나의 콜백 함수만 저장됩니다. 동일한 이벤트 객체에 다른 함수를 등록하면 이전 콜백 함수가 교체됩니다.

하나의 트리거로 여러 가지 동작을 실행하고 싶다면 다음 방법 중 하나를 사용하세요.

  • 하나의 콜백에서 여러 헬퍼 함수를 ​​호출하거나

  • 여러 이벤트 객체를 사용하고 각각을 브로드캐스트하십시오.

  • 작업을 독립적으로 실행하려면 별도의 스레드를 사용하세요.

이벤트를 생성하고 사용하려면 다음 단계를 순서대로 따르십시오.

  1. event로 이벤트를 생성합니다.

  2. 이벤트 이름을 사용하여 이벤트 콜백 설정 (예: myEvent(myCallback);).

  3. Broadcast an event:

    • broadcast — 이벤트 객체에 등록된 모든 함수를 병렬로 실행하도록 트리거합니다.

    • broadcastAndWait — 이벤트 객체에 등록된 모든 함수를 트리거하고 계속하기 전에 함수가 완료될 때까지 기다립니다.

클래스 생성자#

1 — 콜백 함수 없이 event 객체를 생성합니다.

event();

2 — 콜백 함수를 포함하는 event 객체를 생성합니다.

event( void (* callback)(void) );

3 — 콜백 함수를 사용하여 다른 이벤트로부터 event 객체를 생성합니다.

event( 
  event v, 
  void (* callback)(void) );

4 — 인수를 받는 콜백 함수를 가진 event 객체를 생성합니다.

event( void (* callback)(void *), void *arg );

5 — 콜백 함수를 인수로 받아 다른 이벤트로부터 event 객체를 생성합니다.

event( event v, void (* callback)(void *), void *arg );

클래스 소멸자#

event 객체를 파괴하고 관련 리소스를 해제합니다.

~event();

매개변수#

매개변수

유형

설명

callback

void (*)(void)

이벤트가 브로드캐스트될 때 실행되도록 사전에 정의된 함수입니다.

v

event

복사할 기존 이벤트 객체입니다.

callback

void (*)(void *)

void 포인터 인수를 받는 함수로, 이벤트가 브로드캐스팅될 때 실행됩니다.

arg

void *

콜백 함수에 전달될 데이터에 대한 포인터입니다.

메모#

  • 이벤트 객체 event 하나의 콜백 함수가 저장됩니다. 동일한 이벤트 객체에 다른 함수를 등록하면 이전 콜백 함수가 교체됩니다.

  • 하나의 트리거로 여러 가지 동작을 실행하고 싶다면 다음 방법 중 하나를 사용하세요.

    • 하나의 콜백에서 여러 헬퍼 함수를 ​​호출하거나

    • 여러 이벤트 객체를 사용하고 각각을 브로드캐스트하십시오.

    • 작업을 독립적으로 실행하려면 별도의 스레드를 사용하십시오.

예#

콜백 함수를 정의하십시오( int main() 외부).

// Create a callback function
void onMyEvent() {
  Brain.Screen.print("Callback function executed!");
}

int main() 내부에 콜백을 등록합니다.

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Create an event called myEvent
  // Set onMyEvent as the callback function
  event myEvent(onMyEvent);
}

회원 기능#

event 클래스에는 다음과 같은 멤버 함수가 포함됩니다.

  • operator — 이벤트에 대한 콜백 함수를 설정합니다.

  • broadcast — 이벤트의 콜백 함수가 병렬로 실행되도록 트리거합니다.

  • broadcastAndWait — 이벤트의 콜백 함수를 트리거하고 완료될 때까지 기다립니다.

event 멤버 함수를 호출하기 전에 아래와 같이 event 인스턴스를 생성해야 합니다.

/* This constructor is required when using VS Code.
Event configuration can be created with or without
a callback function as shown in the examples above. */

// Create an event without a callback
event myEvent;

// Or create an event with a callback
event myEvent(onMyEvent);

operator#

함수 호출 연산자를 사용하여 이벤트에 대한 콜백 함수를 설정합니다.

Available Functions
void operator()( void (* callback)() );

Parameters

매개변수

유형

설명

callback

void (*)(void)

이벤트가 브로드캐스트될 때 자동으로 호출되는 미리 정의된 콜백 함수입니다. 함수는 필수 콜백 시그니처와 일치해야 합니다. 자세한 내용은 콜백 함수를 참조하세요.

Return Values

이 함수는 값을 반환하지 않습니다.

Notes
  • 콜백을 다시 설정하면 이전 콜백이 덮어쓰여집니다.

  • 콜백 함수는 매개변수가 없어야 하며 반환값이 void여야 합니다.

Examples

콜백 함수를 정의하십시오( int main() 외부).

// Create a callback function
void drawSquare() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawRectangle(40, 40, 120, 120);
}

void drawCircle() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawCircle(100, 100, 60);
}

int main() 내부에 콜백을 등록합니다.

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Create the event drawEvent
  event drawEvent;

  Brain.Screen.print("Drawing a square...");

  // Set the event to drawSquare
  drawEvent(drawSquare);

  wait(1, seconds);

  // Trigger the event
  drawEvent.broadcast();

  wait(1, seconds);

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Drawing a circle...");

  // Replace drawSquare with drawCircle
  drawEvent(drawCircle);

  wait(1, seconds);

  // Trigger the event a second time
  drawEvent.broadcast();
}

broadcast#

broadcast 이벤트 콜백을 시작하고 즉시 계속 진행합니다.

Member Functions
void broadcast();

Parameters

이 함수는 매개변수를 받지 않습니다.

Return Values

이 함수는 값을 반환하지 않습니다.

Examples

콜백 함수를 정의하십시오( int main() 외부).

// Create a callback function
void onMyEvent() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Event fired!");
}

int main() 내부에 콜백을 등록합니다.

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  Brain.Screen.print("Broadcasting in 2 seconds...");

  // Create an event called myEvent
  // Set onMyEvent as the callback function
  event myEvent(onMyEvent);

  wait(2, seconds);

  // Trigger the event
  myEvent.broadcast();
}

broadcastAndWait#

콜백 함수를 시작하고 함수가 완료될 때까지 (또는 타임아웃될 때까지) 기다립니다.

Available Functions
void broadcastAndWait( int32_t timeout = 60000 );

Parameters

매개변수

유형

설명

timeout

int32_t

코드가 다시 실행되기 전에 대기할 시간(밀리초)입니다(기본값은 60000).

Return Values

이 함수는 값을 반환하지 않습니다.

Notes
  • 이 기능은 종료되는 콜백 함수에서만 사용하십시오(무한 루프에는 사용하지 마십시오).

  • 이 함수는 콜백이 완료되거나 타임아웃될 때까지 실행을 차단하는 대기 함수입니다.

Examples

콜백 함수를 정의하십시오( int main() 외부).

// Create a callback function
void startDriving() {
  Drivetrain.drive(forward);
  wait(2, seconds);
}

int main() 내부에 콜백을 등록합니다.

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  Brain.Screen.print("Starting drive...");

  event driveEvent(startDriving);
  // Give the event time to be set
  wait(0.25, seconds);

  // Run the event and wait until it finishes
  driveEvent.broadcastAndWait();

  // Stop driving after 2 seconds
  Drivetrain.stop();
  Brain.Screen.clearScreen();
  Brain.Screen.print("Drive complete!");
}