Skip to content

Commit

Permalink
Use Jinja to generate C++ HID classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Feb 3, 2024
1 parent 5589545 commit d955b0b
Show file tree
Hide file tree
Showing 47 changed files with 2,389 additions and 1,091 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pregenerate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Run wpimath
run: ./wpimath/generate_numbers.py && ./wpimath/generate_quickbuf.py protoc protoc-gen-quickbuf-1.3.3-linux-x86_64.exe
- name: Run HIDs
run: ./wpilibj/generate_hids.py && ./wpilibNewCommands/generate_hids.py
run: ./wpilibj/generate_hids.py && ./wpilibc/generate_hids.py && ./wpilibNewCommands/generate_hids.py
- name: Add untracked files to index so they count as changes
run: git add -A
- name: Check output
Expand Down
1 change: 1 addition & 0 deletions .styleguide
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ generatedFileExclude {
simulation/gz_msgs/src/include/simulation/gz_msgs/msgs\.h$
fieldImages/src/main/native/resources/
apriltag/src/test/resources/
wpilibc/src/generated/
}

repoRootNameOverride {
Expand Down
7 changes: 6 additions & 1 deletion wpilibNewCommands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ if(WITH_JAVA_SOURCE)
set_property(TARGET wpilibNewCommands_src_jar PROPERTY FOLDER "java")
endif()

file(GLOB_RECURSE wpilibNewCommands_native_src src/main/native/cpp/*.cpp)
file(
GLOB_RECURSE wpilibNewCommands_native_src
src/main/native/cpp/*.cpp
src/generated/main/native/cpp/*.cpp
)
add_library(wpilibNewCommands ${wpilibNewCommands_native_src})
set_target_properties(wpilibNewCommands PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET wpilibNewCommands PROPERTY FOLDER "libraries")
Expand All @@ -69,6 +73,7 @@ target_include_directories(
wpilibNewCommands
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/generated/main/native/include>
$<INSTALL_INTERFACE:${include_dest}/wpilibNewCommands>
)

Expand Down
19 changes: 18 additions & 1 deletion wpilibNewCommands/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,24 @@ nativeUtils.exportsConfigs {
}

model {
components {}
components {
"${nativeName}Base" {
it.sources.cpp {
source {
srcDirs 'src/generated/main/native/cpp'
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/generated/main/native/include'
}
}
}
"${nativeName}" {
it.sources.cpp.exportedHeaders {
srcDirs 'src/generated/main/native/include'
}
}
}
binaries {
all {
if (!it.buildable || !(it instanceof NativeBinarySpec)) {
Expand Down
34 changes: 34 additions & 0 deletions wpilibNewCommands/generate_hids.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,39 @@ def main():
output = template.render(controller)
Output(rootPath, controllerName, output)

# C++ headers
env = Environment(
loader=FileSystemLoader(
f"{dirname}/src/generate/main/native/include/frc2/command/button"
),
autoescape=False,
keep_trailing_newline=True,
)
rootPath = f"{dirname}/src/generated/main/native/include/frc2/command/button"
template = env.get_template("commandhid.h.jinja")
for controller in controllers:
controllerName = os.path.basename(
f"Command{controller['ConsoleName']}Controller.h"
)
output = template.render(controller)
Output(rootPath, controllerName, output)

# C++ files
env = Environment(
loader=FileSystemLoader(
f"{dirname}/src/generate/main/native/cpp/frc2/command/button"
),
autoescape=False,
)
rootPath = f"{dirname}/src/generated/main/native/cpp/frc2/command/button"
template = env.get_template("commandhid.cpp.jinja")
for controller in controllers:
controllerName = os.path.basename(
f"Command{controller['ConsoleName']}Controller.cpp"
)
output = template.render(controller)
Output(rootPath, controllerName, output)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

// THIS FILE WAS AUTO-GENERATED BY ./wpilibNewCommands/generate_hids.py. DO NOT MODIFY

#include "frc2/command/button/Command{{ ConsoleName }}Controller.h"

using namespace frc2;

Trigger Command{{ ConsoleName }}Controller::Button(int button, frc::EventLoop* loop) const {
return GenericHID::Button(button, loop).CastTo<Trigger>();
}
{% for button in buttons %}
Trigger Command{{ ConsoleName }}Controller::{{ button.NameParts|map("capitalize")|join }}(frc::EventLoop* loop) const {
return {{ ConsoleName }}Controller::{{ button.NameParts|map("capitalize")|join }}(loop).CastTo<Trigger>();
}
{% endfor -%}
{% for trigger in axes if trigger.type == "trigger" -%}
{% if trigger.UseThresholdMethods %}
Trigger Command{{ ConsoleName }}Controller::{{ trigger.HumanName }}(double threshold,
frc::EventLoop* loop) const {
return {{ ConsoleName }}Controller::{{ trigger.HumanName }}(threshold, loop).CastTo<Trigger>();
}
{% endif -%}
{% endfor -%}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

// THIS FILE WAS AUTO-GENERATED BY ./wpilibNewCommands/generate_hids.py. DO NOT MODIFY

#pragma once
#include <frc/{{ ConsoleName }}Controller.h>

#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/Trigger.h"

namespace frc2 {
/**
* A version of {@link {{ ConsoleName }}Controller} with {@link Trigger} factories for
* command-based.
*
* @see {{ ConsoleName }}Controller
*/
class Command{{ ConsoleName }}Controller : public frc::{{ ConsoleName }}Controller {
public:
using {{ ConsoleName }}Controller::{{ ConsoleName }}Controller;

/**
* Constructs an event instance around this button's digital signal.
*
* @param button the button index
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the button's digital signal attached
* to the given loop.
*/
Trigger Button(int button,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
{% for button in buttons %}
/**
* Constructs an event instance around the {{ button.DocName|default(button.NameParts|map("lower")|join(" ")) }}'s digital signal.
*
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the {{ button.DocName|default(button.NameParts|map("lower")|join(" ")) }}'s digital signal
* attached to the given loop.
*/
Trigger {{ button.NameParts|map("capitalize")|join }}(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
{% endfor -%}
{% for trigger in axes if trigger.type == "trigger" -%}
{% if trigger.UseThresholdMethods %}
/**
* Constructs a Trigger instance around the axis value of the {{ trigger.DocName }}.
* The returned Trigger will be true when the axis value is greater than
* {@code threshold}.
*
* @param threshold the minimum axis value for the returned Trigger to be
* true. This value should be in the range [0, 1] where 0 is the unpressed
* state of the axis. Defaults to 0.5.
* @param loop the event loop instance to attach the Trigger to. Defaults to
* the CommandScheduler's default loop.
* @return a Trigger instance that is true when the {{ trigger.DocName }}'s axis
* exceeds the provided threshold, attached to the given loop
*/
Trigger {{ trigger.HumanName }}(double threshold = 0.5,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
{% endif -%}
{% endfor -%}
};
} // namespace frc2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d955b0b

Please sign in to comment.