Repository navigation
feat(device): give PWM frequency and duty commandable defaults, clamp duty on set - #322
Conversation
… duty on set Core tracked PWM frequency and duty as bookkeeping that started at 0, which is not a commandable value, forcing every consumer (daqifi-desktop, and Daqifi.Mcp's SetPwmOutputAsync in this repo) to special-case "0 means unset, don't send it". - DaqifiStreamingDevice.PwmFrequencyHz now defaults to a new DefaultPwmFrequencyHz (1000 Hz) instead of 0. - IDigitalChannel.PwmDutyCyclePercent now defaults to 50 and clamps to 1-100 on assignment (Math.Clamp), so direct bookkeeping writes can no longer land on a non-commandable value. - SetPwmFrequency/SetPwmDutyCycle (the actual hardware-command methods) keep their existing throw-on-invalid-range behavior unchanged, including the duty=0 firmware-trap rejection — the issue asks for defaults/clamping on the bookkeeping properties themselves, not a change to command validation. - Daqifi.Mcp.DaqifiAgent.SetPwmOutputAsync drops its own "PwmFrequencyHz==0 means unset" guard now that the property is never 0, and always sends a frequency (caller-supplied or the current commandable bookkeeping value). Closes #306 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
PR Summary by QodoPWM: commandable defaults for frequency/duty; clamp duty bookkeeping on set
AI Description
Diagram
High-Level Assessment
Files changed (7)
|
Code Review by Qodo
Context used 1.
|
…utAsync Qodo flagged that SetPwmOutputAsync now unconditionally calls SetPwmFrequency after PR #322's fix for the "0 means unset" trap, adding an avoidable SCPI round-trip on duty-only updates or PWM re-enables. Track the frequency last actually sent per connected device id and only resend when it changes (or on first PWM use this session, or after a reconnect where the real on-device frequency is unknown again). Bench-verified against a real Nyquist device on COM3: first call sends, duty-only second call skips, explicit-frequency third call sends again. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Re: finding #2, "SetPwmFrequency throws not clamps" (optional/low relevance) — leaving this as-is intentionally, not fixing.
Issue #306 names Happy to revisit if the intent was actually to change the command methods too, but as written I don't think clamping a live hardware command is the right behavior here. |
Summary
DaqifiStreamingDevice.PwmFrequencyHznow defaults to a newDefaultPwmFrequencyHz(1000 Hz) instead of 0IDigitalChannel.PwmDutyCyclePercentnow defaults to 50 and clamps to 1-100 on assignment (Math.Clamp)Daqifi.Mcp.DaqifiAgent.SetPwmOutputAsyncdrops its own "PwmFrequencyHz == 0means unset" guard, since the property is never 0 anymore, and always sends a frequency (caller-supplied or the current commandable bookkeeping value)Why
Core tracked PWM frequency and duty as bookkeeping that started at 0, which is not a commandable value.
PwmFrequencyHzwas documented as "0 when none has been set this session" — but 0 is not a commandable frequency, and Core already definesMinPwmFrequencyHz/MaxPwmFrequencyHzwithout enforcing them as a default. Every consumer (daqifi-desktop, and this repo's ownDaqifi.Mcpagent) had to paper over the gap with its own "0 means unset, don't send it" special-casing.Scope note:
SetPwmFrequency/SetPwmDutyCycle— the methods that actually command the hardware — keep their existing throw-on-invalid-range behavior unchanged, including the duty=0 firmware-trap rejection (duty 0 is stored by firmware but never applied, so it's deliberately rejected rather than silently reinterpreted). The issue asks for commandable defaults + clamping on the bookkeeping properties themselves (PwmFrequencyHz,PwmDutyCyclePercent), not a change to command validation, and duty=0's rejection is a business rule distinct from ordinary range clamping.Closes #306
Test plan
DaqifiStreamingDeviceChannelManagementTestscovering the new default values (PwmFrequencyHz,PwmDutyCyclePercent) and duty clamping on assignment (-5→1,0→1,150→100, etc.)dotnet testsuite:Daqifi.Core.Tests(1484/1484, net9.0 + net10.0) andDaqifi.Mcp.Tests(23/23) all passingDaqifi.Core.csprojviaProjectReference, connected to a Nyquist device on COM3, and confirmed:PwmFrequencyHzreads 1000 Hz before anySetPwmFrequencycallPwmDutyCyclePercentreads 50 before any set, and clamps direct assignments (500→100, -20→1)SetPwmFrequencycall first🤖 Generated with Claude Code