Skip to content

Commit 442a4ec

Browse files
authored
Merge pull request #299 from tachikoma0023/patch-1
Fix Fightstick.ino state comparison logic
2 parents 1a3d3ea + 8500df4 commit 442a4ec

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

examples/Fightstick/Fightstick.ino

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,19 @@ void loop()
124124
bleGamepad.setHats(hatValues[0], hatValues[1], hatValues[2], hatValues[3]);
125125

126126
// Update previous states to current states and send report
127+
// Uses memcmp and memcpy to efficiently analyze/copy bytes in chunks
127128
// readable, but with compiler warning:
128129
// if (currentButtonStates != previousButtonStates || currentHatStates != previousHatStates)
129-
if ((memcmp((const void *)currentButtonStates, (const void *)previousButtonStates, sizeof(currentButtonStates)) != 0) && (memcmp((const void *)currentHatStates, (const void *)previousHatStates, sizeof(currentHatStates)) != 0))
130+
// previousButtonStates = currentButtonStates;
131+
// previousHatStates = currentHatStates;
132+
if ((memcmp((const void *)currentButtonStates, (const void *)previousButtonStates, sizeof(currentButtonStates)) != 0) || (memcmp((const void *)currentHatStates, (const void *)previousHatStates, sizeof(currentHatStates)) != 0))
130133
{
131-
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
132-
{
133-
previousButtonStates[currentIndex] = currentButtonStates[currentIndex];
134-
}
135-
136-
for (byte currentIndex = 0; currentIndex < numOfHats * 4; currentIndex++)
137-
{
138-
previousHatStates[currentIndex] = currentHatStates[currentIndex];
139-
}
140-
134+
memcpy((void *)previousButtonStates, (void *)currentButtonStates, sizeof(currentButtonStates));
135+
memcpy((void *)previousHatStates, (void *)currentHatStates, sizeof(currentHatStates));
141136
bleGamepad.sendReport(); // Send a report if any of the button states or hat directions have changed
142137
}
143138

144139
delay(10); // Reduce for less latency
145140
}
146141
}
142+

0 commit comments

Comments
 (0)