Skip to content

DiFluid R2: negative TDS decoded as ~655 % (concentration field is signed int16) #980

Open

Description

@DanBighamWattShop

Problem

The native DiFluid R2 driver currently decodes the concentration/TDS field as unsigned. In difluid_r2_protocol.dart, _parseTds uses _readUint16(data, 1) / 100.0.

DiFluid's protocol document (protocolR2.md) shows the concentration field as a signed 16-bit value ×100. Its own Average example contains FF F9, which represents -0.07.

Negative readings around zero are valid. With the current native decoder, FF F9 becomes 655.29 % and is exposed that way through the Sensors API.

Expected

Decode the concentration/TDS field as signed int16, so FF F9 becomes -0.07.

Do not change refractive-index signedness as part of this issue. The signed concentration behavior is supported by the protocol example; there is not equivalent justification for treating the RI field as signed.

Scope

Keep this as a minimal correctness fix for the existing native fallback:

  • decode concentration/TDS as signed int16;
  • add a protocol regression test using a negative concentration packet;
  • preserve existing positive TDS and RI decoding behavior.

No broader DiFluid R2 feature work should be added to the native implementation here.

Driver-runtime direction

Longer-term DiFluid R2 development is moving to the JS BLE Sensor driver runtime. #977 tracks that work, including native Average support.

The native R2 implementation remains useful as a fallback for now, so it should still decode valid readings correctly while that migration is in progress. Once the JS driver has feature parity, automated coverage and real-hardware validation, we can separately retire the native implementation.

Acceptance criteria

  • FF F9 decodes to -0.07.
  • Existing positive concentration decoding remains unchanged.
  • Existing refractive-index decoding remains unchanged.
  • A regression test covers the negative concentration case.

Hardware confirmation is welcome but is not required to establish this parser bug because the protocol example itself exercises the signed representation.

Activity

  1. tadelv commented on Oct 12, 2026

    @tadelv
    Member

    Maintainer direction: the minimal native fix is accepted. Please keep the change to signed int16 concentration/TDS decoding plus its regression test; do not change RI signedness in this PR. Broader R2 work is being moved to the JS BLE Sensor driver tracked in #977.

  2. added this to the 0.9.1 milestone on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-agentFully specified, ready for an AFK agent

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions