/// <summary>
/// Check whether the bit at the specified index is set (1) in the given word.
/// </summary>
/// <param name="word">The 16-bit word to inspect.</param>
/// <param name="bitIndex">Bit index to check (0 = least significant bit).</param>
/// <returns>True if the bit is set; otherwise false.</returns>
static bool IsBitOn(ushort word, int bitIndex)
{
return ((word >> bitIndex) & 1) == 1;
}
/// <summary>
/// Determine whether a specific bit transitioned from 0 to 1 between two snapshots.
/// </summary>
/// <param name="prev">Previous word snapshot.</param>
/// <param name="curr">Current word snapshot.</param>
/// <param name="bitIndex">Bit index to check for the rising edge (0 = least significant bit).</param>
/// <returns>True if the bit was 0 in <paramref name="prev"/> and is 1 in <paramref name="curr"/>; otherwise false.</returns>
static bool IsNewlyOn(ushort prev, ushort curr, int bitIndex)
{
ushort newlyOn = (ushort)(curr & ~prev);
return IsBitOn(newlyOn, bitIndex);
}
//example
/// <summary>
/// Snapshot of the previous calibration status word. Null indicates no previous snapshot is available.
/// </summary>
private ushort? _prevCalibStatus = null;
/// <summary>
/// Handle changes in a calibration status word by comparing the current value with the previous snapshot.
/// Logs events for bits that changed from 0 to 1. After processing, updates the previous snapshot.
/// Bit meanings (checked in this method):
/// 0 = calibReady, 1 = calibRunning, 2 = calibSuccess, 3 = calibError,
/// 4 = calibRequest, 5 = calibZeroAdjust, 6 = calibSpanAdjust, 7 = calibDataSaving.
/// </summary>
/// <param name="curr">Current calibration status word.</param>
private void HandleCalibrationStatus(ushort curr)
{
if (_prevCalibStatus == null)
{
// First time, you can't compare, so just keep it
_prevCalibStatus = curr;
return;
}
ushort prev = _prevCalibStatus.Value;
ushort newlyOn = (ushort)(curr & ~prev);
// Are there any new flags
if (newlyOn != 0)
{
if (IsBitOn(newlyOn, 0)) Log("calibReady became ON");
if (IsBitOn(newlyOn, 1)) Log("calibRunning became ON");
if (IsBitOn(newlyOn, 2)) Log("calibSuccess became ON");
if (IsBitOn(newlyOn, 3)) Log("calibError became ON");
if (IsBitOn(newlyOn, 4)) Log("calibRequest became ON");
if (IsBitOn(newlyOn, 5)) Log("calibZeroAdjust became ON");
if (IsBitOn(newlyOn, 6)) Log("calibSpanAdjust became ON");
if (IsBitOn(newlyOn, 7)) Log("calibDataSaving became ON");
}
_prevCalibStatus = curr;
}
/// <summary>
/// Log which calibration-related flags are currently set in the provided word.
/// Uses the same bit-to-meaning mapping as HandleCalibrationStatus.
/// </summary>
/// <param name="curr">Current calibration status word to inspect.</param>
private void LogCurrentFlags(ushort curr)
{
if (IsBitOn(curr, 0)) Log("ON: calibReady");
if (IsBitOn(curr, 1)) Log("ON: calibRunning");
if (IsBitOn(curr, 2)) Log("ON: calibSuccess");
if (IsBitOn(curr, 3)) Log("ON: calibError");
if (IsBitOn(curr, 4)) Log("ON: calibRequest");
if (IsBitOn(curr, 5)) Log("ON: calibZeroAdjust");
if (IsBitOn(curr, 6)) Log("ON: calibSpanAdjust");
if (IsBitOn(curr, 7)) Log("ON: calibDataSaving");
}