The function analyzeDeviceInput(targetDevice, reportID, reportDataHex)
is called whenever the data coming from a device has changed in at least one of the monitored bytes. With
let reportBuffer = buffer.Buffer.from(reportDataHex, 'hex');
the current state can be retrieved, but in order to be able to find out which bits have really changed, we'd need access to the previous report, too, like
let reportPreviousBuffer = buffer.Buffer.from(reportPreviousDataHex, 'hex');
Why? Let say I have a device with two buttons, A and B, which when pressed separately change byte 1 from 0x00 to 0x01 or 0x00 to 0x02, respectively. When both were pressed at exactly the very same time, byte 1 would change from 0x00 to 0x03, but this ideal transition does never quite happen; in practice one of the button will virtually always be pressed a tiny split second before the other one, and so byte 1 will first change from 0x00 to 0x01 and immediately thereafter to 0x03, or it will first change to 0x02 and then immediately thereafter to 0x03.
I.e. if we got the following code
if((reportBuffer.readUInt8(1) & 0x01) == 0x01) {
log('A');
bttTriggerDeviceTrigger(targetDevice, 'A');
} // <- an "else" here wouldn't really make things better but break them in a different way!
if((reportBuffer.readUInt8(1) & 0x02) == 0x02) {
log('B');
bttTriggerDeviceTrigger(targetDevice, 'B');
}
one of the buttons will virtually always be triggered twice even though it is only pressed once, i.e. we will see a trigger sequence of either A, A, B or B, A, B.
What I want to do is first find out if there was a change for a certain bit and only in this case process it further, like
let bit_A = 0x01;
let prev_A = (reportPreviousBuffer.readUInt8(1) & bit_A);
let curr_A = (reportBuffer.readUInt8(1) & bit_A);
if((curr_A ^ prev_A) != 0 && curr_A != 0) {
log('A');
bttTriggerDeviceTrigger(targetDevice, 'A');
}
// and so forth for the other buttons
I hope I managed to describe this well enough, please feel free to ask if not!