MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
HIDNode.cpp
Go to the documentation of this file.
1// HIDNode.cpp
2
3#include "HIDNode.hpp"
4
6
8 : m_config(std::move(config))
9{
12 }
13}
14
16{
19 }
20
21 const auto& bytes = value.as_bytes();
22
23 switch (m_config.mode) {
25 return parse_axis(bytes);
26
28 return parse_button(bytes);
29
32 ? m_config.custom_parser(bytes)
34 }
35
37}
38
39double HIDNode::parse_axis(std::span<const uint8_t> bytes) const
40{
41 if (bytes.size() <= m_config.byte_offset) {
43 }
44
45 double raw = 0.0;
46
47 if (m_config.byte_size == 1) {
48 uint8_t b = bytes[m_config.byte_offset];
50 ? static_cast<double>(static_cast<int8_t>(b))
51 : static_cast<double>(b);
52 } else if (m_config.byte_size == 2 && bytes.size() > m_config.byte_offset + 1) {
53 uint16_t w = bytes[m_config.byte_offset] | (bytes[m_config.byte_offset + 1] << 8);
55 ? static_cast<double>(static_cast<int16_t>(w))
56 : static_cast<double>(w);
57 }
58
59 // Normalize to 0-1
61 normalized = std::clamp(normalized, 0.0, 1.0);
62
63 // Apply deadzone if configured
64 if (m_config.deadzone > 0.0) {
66 }
67
68 return normalized;
69}
70
71double HIDNode::parse_button(std::span<const uint8_t> bytes) const
72{
73 if (bytes.size() <= m_config.byte_offset) {
75 }
76
77 bool pressed = (bytes[m_config.byte_offset] & m_config.bit_mask) != 0;
78
79 if (m_config.invert) {
80 pressed = !pressed;
81 }
82
83 return pressed ? 1.0 : 0.0;
84}
85
87{
88 // Center at 0.5 for center-return axes
89 double centered = (normalized - 0.5) * 2.0; // -1 to 1
90 double abs_val = std::abs(centered);
91
92 if (abs_val < m_config.deadzone) {
93 return 0.5; // Dead center
94 }
95
96 // Scale remaining range
97 double sign = (centered >= 0) ? 1.0 : -1.0;
98 double scaled = (abs_val - m_config.deadzone) / (1.0 - m_config.deadzone);
99
100 return 0.5 + (sign * scaled * 0.5); // Back to 0-1
101}
102
103} // namespace MayaFlux::Nodes::Input
size_t b
double parse_button(std::span< const uint8_t > bytes) const
Parse button value from HID report bytes.
Definition HIDNode.cpp:71
double extract_value(const Core::InputValue &value) override
Extract a scalar value from an InputValue.
Definition HIDNode.cpp:15
double parse_axis(std::span< const uint8_t > bytes) const
Parse axis value from HID report bytes.
Definition HIDNode.cpp:39
HIDNode(HIDConfig config={})
Definition HIDNode.cpp:7
double apply_deadzone(double normalized) const
Definition HIDNode.cpp:86
@ BUTTON
Digital button (bit mask)
@ CUSTOM
User-provided parser function.
@ AXIS
Joystick/gamepad axis with normalization & deadzone.
@ NONE
No smoothing - immediate value changes (buttons)
std::vector< double > normalized(const std::vector< double > &data, double target_peak)
Normalize single-channel data (non-destructive)
Definition Yantra.cpp:593
const std::vector< uint8_t > & as_bytes() const
@ BYTES
Raw byte data (HID reports, sysex)
Generic input value container.
uint8_t bit_mask
Bit mask (for BUTTON)
Definition HIDNode.hpp:27
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
Unified configuration for all HID input types.
Definition HIDNode.hpp:21
double default_value
Initial output value.
Definition InputNode.hpp:83