@@ -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