Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR should significantly speed up the I2C transaction and greatly improve the performance of this library. The code change is trivial, but there is a lot of history behind it.
First off, this library was originally written as a simple port of existing Arduino libraries. The huge delays included in the I2C transaction came from there. Some attempts were made to reduce or remove these. Using a repeated start was also attempted. These efforts had mixed success. It could be made to work fast on one platform, but then break on another. Obviously there was some subtle timing requirement that was not being satisfied. But with no real information (no datasheet) to go on, the exact nature of this remained unknown. So the existing delays were simply left in place.
Some recent PR's (#17 and #18) have brought this issue back into the light. Various work arounds were discussed as a way to speed up the library - all assuming the huge delays were still necessary. See those PR threads for more info. Progress was made, but then at some point the idea of removing the delays was again attempted and seemed to work. As before, this worked on some platforms, but not others. This motivated the investigation to snoop actual Wiimote-to-Nunchuk traffic to finally have some idea of what the "requirements" are. The results are documented here:
#2 (comment)
Based on those findings, it seems the critical timing requirement is an O(200us) delay between the write and read transactions. On some slower boards, this can happen naturally without any explicit delay between the two lines of code. But on faster boards, the delay would be <200us and the Nunchuk would NACK on the read. Adding an explicit delay fixed this for all boards. However, the ability to actually generate a 200us delay in Python is a mixed bag. Some hosts can do that, others can not. For those that can not, the attempt results in essentially zero delay. Therefore, this PR uses a conservative default of 2000us which seems to work on all platforms. The value is available as a parameter to allow fine tuning if desired and possible.
Other misc:
_I2C_INIT_DELAY
is being left in place for now. Can it be reduced? Unknown. Can investigate for a future PR.Tested on: