Skip to content

Commit

Permalink
[wpilib] Implement Sendable for HID classes (wpilibsuite#6782)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacey-sooty authored Jul 18, 2024
1 parent 34b8b7a commit 7663211
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 14 deletions.
19 changes: 17 additions & 2 deletions wpilibc/src/generate/main/native/cpp/hid.cpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "frc/{{ ConsoleName }}Controller.h"

#include <hal/FRCUsageReporting.h>
#include <wpi/sendable/SendableBuilder.h>

#include "frc/event/BooleanEvent.h"

Expand Down Expand Up @@ -77,7 +78,7 @@ bool {{ ConsoleName }}Controller::GetLeftBumperReleased() {
bool {{ ConsoleName }}Controller::GetRightBumperReleased() {
return GetRawButtonReleased(Button::kRightBumper);
}
{% elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
{%- elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
bool {{ ConsoleName }}Controller::GetTouchpad() const {
return GetRawButton(Button::kTouchpad);
}
Expand All @@ -89,4 +90,18 @@ bool {{ ConsoleName }}Controller::GetTouchpadPressed() {
bool {{ ConsoleName }}Controller::GetTouchpadReleased() {
return GetRawButtonReleased(Button::kTouchpad);
}
{% endif %}
{%- endif %}

void {{ ConsoleName }}Controller::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "{{ ConsoleName }}");
{%- for trigger in triggers %}
builder.AddDoubleProperty("{{ capitalize_first(trigger.name) }}", [this] { return Get{{ capitalize_first(trigger.name) }}Axis(); }, nullptr);
{%- endfor -%}
{% for stick in sticks %}
builder.AddDoubleProperty("{{ stick.NameParts|map("capitalize")|join }}", [this] { return Get{{ stick.NameParts|map("capitalize")|join }}(); }, nullptr);
{%- endfor -%}
{% for button in buttons %}
builder.AddBooleanProperty("{{ capitalize_first(button.name) }}", [this] { return Get{{ capitalize_first(button.name) }}Button(); }, nullptr);
{%- endfor %}
}
9 changes: 8 additions & 1 deletion wpilibc/src/generate/main/native/include/frc/hid.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{%- endmacro %}
#pragma once

#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>

#include "frc/GenericHID.h"

namespace frc {
Expand All @@ -24,7 +27,9 @@ namespace frc {
* correct mapping, and only through the official NI DS. Sim is not guaranteed
* to have the same mapping, as well as any 3rd party controllers.
*/
class {{ ConsoleName }}Controller : public GenericHID {
class {{ ConsoleName }}Controller : public GenericHID,
public wpi::Sendable,
public wpi::SendableHelper<{{ ConsoleName }}Controller> {
public:
/**
* Construct an instance of a controller.
Expand Down Expand Up @@ -203,6 +208,8 @@ class {{ ConsoleName }}Controller : public GenericHID {
static constexpr int k{{ capitalize_first(trigger.name) }} = {{ trigger.value }};
{%- endfor %}
};

void InitSendable(wpi::SendableBuilder& builder) override;
};

} // namespace frc
26 changes: 26 additions & 0 deletions wpilibc/src/generated/main/native/cpp/PS4Controller.cpp

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

26 changes: 26 additions & 0 deletions wpilibc/src/generated/main/native/cpp/PS5Controller.cpp

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

25 changes: 25 additions & 0 deletions wpilibc/src/generated/main/native/cpp/StadiaController.cpp

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

22 changes: 22 additions & 0 deletions wpilibc/src/generated/main/native/cpp/XboxController.cpp

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.

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

23 changes: 20 additions & 3 deletions wpilibj/src/generate/hid.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package edu.wpi.first.wpilibj;

{{ "// " if SkipReporting }}import edu.wpi.first.hal.FRCNetComm.tResourceType;
{{ "// " if SkipReporting }}import edu.wpi.first.hal.HAL;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.wpilibj.event.BooleanEvent;
import edu.wpi.first.wpilibj.event.EventLoop;

Expand All @@ -24,7 +26,7 @@ import edu.wpi.first.wpilibj.event.EventLoop;
* only through the official NI DS. Sim is not guaranteed to have the same mapping, as well as any
* 3rd party controllers.
*/
public class {{ ConsoleName }}Controller extends GenericHID {
public class {{ ConsoleName }}Controller extends GenericHID implements Sendable {
/** Represents a digital button on a {{ ConsoleName }}Controller. */
public enum Button {
{%- for button in buttons %}
Expand Down Expand Up @@ -252,7 +254,7 @@ public class {{ ConsoleName }}Controller extends GenericHID {
public boolean getRightBumperReleased() {
return getRawButtonReleased(Button.kRightBumper.value);
}
{% elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
{%- elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
/**
* Read the value of the touchpad on the controller.
*
Expand Down Expand Up @@ -285,5 +287,20 @@ public class {{ ConsoleName }}Controller extends GenericHID {
public boolean getTouchpadReleased() {
return getRawButtonReleased(Button.kTouchpad.value);
}
{% endif -%}
{%- endif %}

@Override
public void initSendable(SendableBuilder builder) {
builder.setSmartDashboardType("HID");
builder.publishConstString("ControllerType", "{{ ConsoleName }}");
{%- for trigger in triggers %}
builder.addDoubleProperty("{{ capitalize_first(trigger.name) }}", this::get{{ capitalize_first(trigger.name) }}Axis, null);
{%- endfor -%}
{% for stick in sticks %}
builder.addDoubleProperty("{{ stick.NameParts|map("capitalize")|join }}", this::get{{ stick.NameParts|map("capitalize")|join }}, null);
{%- endfor -%}
{% for button in buttons %}
builder.addBooleanProperty("{{ capitalize_first(button.name) }}", this::get{{ capitalize_first(button.name) }}Button, null);
{%- endfor %}
}
}
Loading

0 comments on commit 7663211

Please sign in to comment.