-
Notifications
You must be signed in to change notification settings - Fork 86
Description
Sending a DX
command (host->sensor) to a streaming sensor stops data acquisition. However, this doesn't happen instantaneously. Once the DX
command is sent out, the host will still receive a few Data Block
receipts before seeing the DX
receipt.
Currently the sweep_device_stop_scanning
uses the following technique:
- Send
DX
command (host -> sensor) - Wait a while (5000 us), for any remaining
Data Block
receipts and theDX
receipt to arrive (sensor -> host). - Flush the serial port.
- Send another
DX
command (host -> sensor) - Read the
DX
receipt (sensor -> host) that returns as a response to step 4
This was really a temporary workaround and should be replaced by an actual check for the DX
receipt among the incoming Data Block
receipts. The initial reaction might be to assume we cannot reliably check for a DX
receipt among incoming Data Block
receipts because the bytes of a Data Block
are variable. The fear is that they could theoretically take the appearance of a DX
receipt. However, we can still reliably discern the two because:
- While it is true that
Data Block
bytes 1-6 can take any form, byte 0 (sync/error byte) is limited to very specific error codes and is therefore controllable. We can guarantee that byte 0 will never look like the first byte of aDX
receipt. - The sensor guarantees the transmission of complete
Data Block
receipts before sending aDX
receipt. That is to say that aDX
receipt will never come mid-Data Block
. - The sensor guarantees that no
Data Block
receipts are transmitted after theDX
receipt is transmitted.
Obviously we can't protect against data corruption with 100% reliability, but we can certainly protect against valid Data Block
receipts being misinterpreted as a DX
receipt.
In sweep_device_stop_scanning
, we should do something along the lines of:
- Send a
DX
command (host -> sensor) - Look at the incoming bytes, and discern if they belong to a
Data Block
receipt or aDX
receipt. - In the case of a
Data Block
receipt, handle it... (as we would during scanning, or simply trash it). In the case of aDX
receipt, we proceed as if we picked up from step 5 above.