vash3d / pigpio_encoder

Python module for the KY040 rotary encoder.
GNU General Public License v3.0
14 stars 4 forks source link

Added offset to setup_rotary() #19

Closed confestim closed 2 years ago

confestim commented 2 years ago

This offset will let people trigger a callback when they reach either end of a given range. If you guys accept this, I'll maybe add a method to do this, but as of right now, I believe this could work for people that want this type of callback.

In my situation, I need the setup_rotary to be called again with different params once the counter has reached either end of a given range.

volkerjaenisch commented 2 years ago

Dear Boyan!

I really appreciate your contribution. But, sorry, I do not understand what you like to gain from the proposed code change.

This is not an NO but a welcome.

Maybe it can help if you present a code example that illustrates what you like to achieve.

Cheers,

Volker

--

========================================================= inqbus Scientific Computing Dr. Volker Jaenisch Hungerbichlweg 3 +49 (8860) 9222 7 92 86977 Burggenhttps://inqbus.de

confestim commented 2 years ago

Hi Volker,

The use case for this small change is to allow the counter to start with an offset, so you can have an event triggered when self.counter == (self.min or self.max).

Example:

from itertools import cycle 

ranges = cycle(([1,10], [10-20], [4-5))
def setup_encoder(ranges:list, scale:float=.25):
        # prints current value
    def rotary_callback(counter):
        print(round(counter, 2))

    # init rotary
    my_rotary = Rotary(
        clk_gpio=17,
        dt_gpio=27,
        sw_gpio=22
    )

    # setup rotary
    my_rotary.setup_rotary(
        min=minimum,
        max=maximum,
                start_offset=.1 # <-----
        scale=scale,
        debounce=300,
        rotary_callback=rotary_callback
    )

    #my_rotary.watch()
    return my_rotary

rotary = setup_encoder(next(ranges))
# rotary.counter now starts at 1.1 instead of 1 when pass start_offset = .1
# but rotary.min is still 1

# we can make now our own event that unfolds when min is met

def on_minimum(rotary):
   global rotary
   if rotary.counter == rotary.min:
        del rotary
        rotary = setup_encoder(next(ranges))
        # and now rotary.counter is 10.1, while rotary.min is 10
        print("oh no it's the end")

# and then we can do whatever we want
while True:
    print(rotary.counter)
    on_minimum(rotary)

# please do excuse my crappy code...

I hope I've gotten my point across.

volkerjaenisch commented 2 years ago

Dear Boyan!

Sorry but I still don't get what your point is. Why does your callback not just add the offset?

OFFSET = 0.1

def  rotary_callback(counter):
    print(round(counter  + OFFSET,2))

Please don't get me wrong I do not oppose extending the rotary at all. But as a core developer I have to keep in mind the separation of concern. So if a functionality can painlessly be realized outside of the rotary there is no need to extend the rotary. Only if a functionality depends on an internal state of the rotary that is e.g. not available in the callback there is the need to extend the API of the rotary.

Please try again to convince me.

Cheers, Volker

--

inqbus Scientific Computing    Dr.  Volker Jaenisch
Hungerbichlweg 3               +49 (8860) 9222 7 92
86977 Burggenhttps://inqbus.de

=========================================================

confestim commented 2 years ago

That's fair, I get your point. I'm closing the PR now.