MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
HIDNode.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "InputNode.hpp"
4
6
7/**
8 * @enum HIDParseMode
9 * @brief How to interpret HID report bytes
10 */
11enum class HIDParseMode {
12 AXIS, ///< Joystick/gamepad axis with normalization & deadzone
13 BUTTON, ///< Digital button (bit mask)
14 CUSTOM ///< User-provided parser function
15};
16
17/**
18 * @struct HIDConfig
19 * @brief Unified configuration for all HID input types
20 */
23
24 // Byte parsing
25 size_t byte_offset { 0 };
26 size_t byte_size { 1 }; ///< 1 or 2 bytes (for AXIS)
27 uint8_t bit_mask { 0xFF }; ///< Bit mask (for BUTTON)
28
29 // Axis normalization
30 bool is_signed { false };
31 double min_raw { 0.0 };
32 double max_raw { 255.0 };
33 double deadzone { 0.05 };
34
35 // Button inversion
36 bool invert { false };
37
38 // Custom parser
39 std::function<double(std::span<const uint8_t>)> custom_parser;
40
41 // Factory methods for clean construction
42 static HIDConfig axis(size_t offset, size_t bytes = 1, bool signed_val = false)
43 {
44 return HIDConfig {
46 .byte_offset = offset,
47 .byte_size = bytes,
48 .is_signed = signed_val
49 };
50 }
51
52 static HIDConfig button(size_t offset, uint8_t mask = 0xFF, bool invert_val = false)
53 {
54 return HIDConfig {
56 .byte_offset = offset,
57 .bit_mask = mask,
58 .invert = invert_val
59 };
60 }
61
62 HIDConfig with_deadzone(double dz) const
63 {
64 HIDConfig copy = *this;
65 copy.deadzone = dz;
66 return copy;
67 }
68
69 HIDConfig with_range(double min_val, double max_val) const
70 {
71 HIDConfig copy = *this;
72 copy.min_raw = min_val;
73 copy.max_raw = max_val;
74 return copy;
75 }
76
77 template <typename F>
78 static HIDConfig custom(F&& parser)
79 {
80 return HIDConfig {
82 .custom_parser = std::forward<F>(parser)
83 };
84 }
85};
86/**
87 * @class HIDAxisNode
88 * @brief InputNode for joystick/gamepad axes with deadzone
89 *
90 * ### Examples
91 * @code
92 * // Clean, expressive usage
93 * auto stick_x = std::make_shared<HIDNode>(HIDConfig::axis(0, 2, true)
94 * .with_deadzone(0.1)
95 * .with_range(-32768, 32767));
96 *
97 * auto trigger = std::make_shared<HIDNode>(HIDConfig::axis(4, 1)
98 * .with_range(0, 255));
99 *
100 * auto button_a = std::make_shared<HIDNode>(HIDConfig::button(6, 0x01));
101 *
102 * auto custom = std::make_shared<HIDNode>(HIDConfig::custom(
103 * [](auto bytes) { return complex_parsing_logic(bytes); }
104 * ));
105 * @endcode
106 */
107class MAYAFLUX_API HIDNode : public InputNode {
108public:
109 explicit HIDNode(HIDConfig config = {});
110
111 void save_state() override { }
112
113 void restore_state() override { }
114
115protected:
116 double extract_value(const Core::InputValue& value) override;
117
118private:
120
121 /**
122 * @brief Parse axis value from HID report bytes
123 * @param bytes HID report bytes
124 * @return Normalized axis value (0.0 to 1.0)
125 */
126 [[nodiscard]] double parse_axis(std::span<const uint8_t> bytes) const;
127
128 /**
129 * @brief Parse button value from HID report bytes
130 * @param bytes HID report bytes
131 * @return Button state (0.0 or 1.0)
132 */
133 [[nodiscard]] double parse_button(std::span<const uint8_t> bytes) const;
134
135 [[nodiscard]] double apply_deadzone(double normalized) const;
136};
137
138} // namespace MayaFlux::Nodes::Input
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition HIDNode.hpp:111
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition HIDNode.hpp:113
Abstract base class for nodes that receive external input.
HIDParseMode
How to interpret HID report bytes.
Definition HIDNode.hpp:11
@ BUTTON
Digital button (bit mask)
@ CUSTOM
User-provided parser function.
@ AXIS
Joystick/gamepad axis with normalization & deadzone.
Generic input value container.
HIDConfig with_deadzone(double dz) const
Definition HIDNode.hpp:62
HIDConfig with_range(double min_val, double max_val) const
Definition HIDNode.hpp:69
uint8_t bit_mask
Bit mask (for BUTTON)
Definition HIDNode.hpp:27
static HIDConfig button(size_t offset, uint8_t mask=0xFF, bool invert_val=false)
Definition HIDNode.hpp:52
size_t byte_size
1 or 2 bytes (for AXIS)
Definition HIDNode.hpp:26
std::function< double(std::span< const uint8_t >)> custom_parser
Definition HIDNode.hpp:39
static HIDConfig axis(size_t offset, size_t bytes=1, bool signed_val=false)
Definition HIDNode.hpp:42
static HIDConfig custom(F &&parser)
Definition HIDNode.hpp:78
Unified configuration for all HID input types.
Definition HIDNode.hpp:21
Configuration for InputNode behavior.
Definition InputNode.hpp:79