Skip to content

Conversation

@lizlooney
Copy link
Collaborator

In common_storage.ts, save the events declared in the module.

In mrc_event_handler.ts:
Refactored hardcoded fields names into constants FIELD_SENDER and FIELD_EVENT_NAME.
Added otherBlockId to EventHandlerExtraState and mrcOtherBlockId to EventHandlerMixin.
Added onLoad method that updates event handler blocks if the event definition has been changed.
Added functions addRobotEventHandlerBlocks and createRobotEventHandlerBlock to create blocks for the toolbox.

In editor.ts, added method getEventsFromRobot.

In hardware_category.ts, added function getRobotEventsBlocks and call it in getHardwareCategory when the current module is an opmode.

In mrc_event_handler.ts:
  Refactored hardcoded fields names into constants FIELD_SENDER and FIELD_EVENT_NAME.
  Added otherBlockId to EventHandlerExtraState and mrcOtherBlockId to EventHandlerMixin.
  Added onLoad method that updates event handler blocks if the event definition has been changed.
  Added functions addRobotEventHandlerBlocks and createRobotEventHandlerBlock to create blocks for the toolbox.

In editor.ts, added method getEventsFromRobot.

In hardware_category.ts, added function getRobotEventsBlocks and call it in getHardwareCategory when the current module is an opmode.
@lizlooney lizlooney requested a review from alan412 July 19, 2025 05:03
  Added fields hasHardware and hasEventHandler. Set these in mrcWorkspaceToCode before calling super.workspaceToCode.
  Renamed method defineClassVariables to generateInitStatements. Renamed local variable variableDefinitions to initStatements.
  Removed setHasHardware and addHardwarePort.

In mrc_components.ts, added methods getNewPort and getHardwarePorts. getHardwarePorts collects the hardware ports and is called by extended_python_generator before we start generating python for blocks. (Previous code called addHardwarePort when the mrc_component blocks was generating python, but that is too late.)

In mrc_event_handler.ts, added function getHasEventHandler. This is used to determine whether the generated __init__ method needs to call register_events.

In generator_context.ts, removed hasHardware, clear(), setHasHardware(), and getHasHardware().

In mrc_call_python_function.ts, use forEach loops where possible. Use BLOCK_NAME instead of 'mrc_call_python_function'.

Moved code that iterates through mrc_mechanism_component_holder blocks from editor.ts
to mrc_mechanism_component_holder.ts functions getComponents and getEvents.
In mrc_mechanism_component_holder.ts added function getHardwarePorts to collect ports needed for define_hardware.

Moved code that iterates through mrc_class_method_def blocks from editor.ts
to mrc_class_method_def.ts functions getMethodsForWithin, getMethodsForOutside,
and getMethodNamesAlreadyOverriddenInWorkspace.

In robot_base.py, added register_event and unregister_event methods.

In methods_category.ts, don't show blocks for robot methods register_event and unregister_event.
@lizlooney
Copy link
Collaborator Author

I added another commit here to fix the python code for registering event handlers.

It also fixes a problem where (previously) the hardware ports were collected as the python code was being generated block-by-block, but that didn't work because we need all the ports to generate the call to define_hardware when we generate the code for the init method.

@lizlooney lizlooney marked this pull request as draft July 20, 2025 19:53
  Rename register_event and unregister_event to register_event_handler and unregister_event_handler.
  Allow multiple handlers to be registered for an event.
  Add fire_event function.

Changed python code for firing an event to call fire_event.

When determining whether a workspace has hardware or events, check whether blocks are enabled.
@lizlooney lizlooney marked this pull request as ready for review July 20, 2025 20:56
Copy link
Collaborator

@alan412 alan412 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going ahead and merging this in as it is a lot of great work. I am adding to our discussion document whether we want to allow multiple event handlers in the same module for the same event.

@alan412 alan412 merged commit 1bdefa1 into wpilibsuite:main Jul 21, 2025
1 check passed
@lizlooney lizlooney deleted the pr_event_handlers branch July 21, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants