-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow re-using ADC readings instead of re-reading all the time #358
Allow re-using ADC readings instead of re-reading all the time #358
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeating my comments from discord here:
Personally I don't see the need for the readers to cache a value, and would prefer that scripts that need it cache it themselves, backed up by the fact that only one script out of 30 has asked for it. That said, the fact that the daisy's API caches by default means that I'm probably wrong.
[required] I'd like to see how this is used by a script, and how this api change improves that script. That is, I'd like to document the motivating use case in PRs that change the firmware api. Could you update a script to use these features? (I believe you said it was Pam's that would benefit.)
As I've described on Discord and in the MR summary, the principle benefit is that this would allow you to read e.g. the value of e.g. suppose
In this example the adc is sampled 3 times in succession, once for each By allowing the re-use of the last-read sample you eliminate this desynchronization, and make the code faster by not making unnecessary ADC reads.
Yes, there are ways this could be accomplished with a wrapper class, or by using local variables and additional functions. But if the argument being made is that the firmware should be user-friendly, I'd argue that both of those solutions are less user-friendly than simply exposing the ability to bypass the ADC reads when that behaviour is desirable. EDIT
where CV1-4 are attenuated S&H copies of both analogue inputs. Using the existing implementation, this 4 line bit of code would result in 8 ADC reads, instead of the 4 that are actually required to achieve the desired output. Looking towards eventual multi-threading, being able to control which thread is actually accessing the ADC hardware may become more important as well. On one thread, having a block that calls |
This is the part that is my main issue, I guess. Why is it the API's responsibility to cache this value? If the client wants to use the value for two different calculations why not use a local variable? Also the examples you provide feel like straw-men arguments. I'm not sure you would really write this code. (Perhaps including the changes to Pam's would help me understand in this respect.) In any code where I saw a client making a non trivial API call, expecting the same value returned, I would ask them to cache it in a local variable. I wouldn't ask the API to change to accommodate the need to use the value on several lines of code. For an example that uses a different API, consider this Pams code. In the method Again, the fact that the API of a 'similar' firmware (daisy) provides this functionality, makes me think that I'm probably wrong here, but I just don't understand the motivation. |
6118381
to
30da8f1
Compare
reuse_last_reading
parameter to analogue readers
Given the feedback I've removed the changes to the core firmware and instead added a wrapper class that accomplishes the same thing, but without impacting anything else. |
"""A knob whose value remains fixed until .update(...) is called | ||
|
||
This allows multiple uses of .percent(), .choice(...), etc... without forcing a re-read of | ||
the ADC value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional] I think it'd be helpful to add an example of how to create an instance of a BufferedKnob in the docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Add the reuse_last_reading parameter to analogue reader classes to allow re-using the previous reading rather than always taking a new sample.Adds a new experimental
BufferedReader
class that allows re-using the last ADC reading instead of always re-sampling every time.percent()
,.choice(...)
, etc... gets called.