miketeachman / micropython-rotary

MicroPython module to read a rotary encoder.
MIT License
276 stars 56 forks source link

Update rotary.py #4

Closed mc2880 closed 4 years ago

mc2880 commented 4 years ago

Propose adding "setvalue" or other similar named function, so the starting point of the rotary movement can be externally set.

I need this for a project where I may save and load the centre position of a HSV scroll.

miketeachman commented 4 years ago

Hi @mc2880. Thanks for this request! It's a great feature to add. By coincidence, I have an immediate need for this same feature.

In addition to setting the value, I need to change the min_val and max_val range of the encoder.

Perhaps a single init() method could be added to change multiple configuration parameters in one method call?

my_encoder = RotaryIRQ( <arguments> ) # instantiate encoder object

init() examples

my_encoder.init(value = 20) # change value. No change to other config parameters my_encoder.init(value = 20, min_val = 0, max_val = 59) # change value, min_val, max_val

Would this work for your use case?
I'm also asking myself if this init() approach fits commonly understood patterns in python and micropython for changing the configuration of an instantiated object.

Note: I have time to make this change next week (week of Jan 6)

miketeachman commented 4 years ago

@mc2880 I added a new set() method to the class.

Usage:
my_encoder.set(value = 20) # change value. No change to other config parameters

This should work for the use case you describe. If it works for you we should close the pull request.

miketeachman commented 4 years ago

Assuming that the new set() method satisfies the requirements described by the PR