Python Forum
Problems with Interrupts/ callback functions in Python for an Alarm Clock project
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with Interrupts/ callback functions in Python for an Alarm Clock project
#1
I recently started programming with Python and now I want to build an appliance with the raspberry pi which is basically an alarm clock. I have a pi, a small OLED display and a rotary encoder which can be turned left/ right or pushed.
Here's the library for the rotary encoder: https://github.com/joan2937/pigpio/blob/...encoder.py
Since there was no support for pushing the rotary encoder, I added the following code to the library:

def _push(self, gpio, level, tick):
    if not self.pushed:
        self.callback("push")
        self.pushed = 1  # debounce 
which works fine.
But when it comes to building my actual appliance and integrating all the things (rotary encoder, oled display, ...) somehow I screw up. Every method in my code should handle one "page" of my appliance, e.g. the methods would be home_menu, alarm_menu, settings_menu, ... I have the following problem: I can't get the navigation through the menus right. After a push from the rotary encoder is detected, I want to display the content of the specific menu element at which the push was detected
Here's an excerpt from my code:

def main_menu_callback(self, cursor, lines = ('Weckzeit: --:--', 'Einstellungen', '01.01.1970')):
        y = 0
        # draw text elements of the main menu
        for line in lines:
            self.draw.text((0,y), line, font = self.sans15, fill=255)
            y += self.textsize_addition(line)[1] + 10
        self.draw.line([0, 43, 128, 43], fill=255)

        # check if rotary encoder was pushed. If so, go to the
        # selected menu point. If it wasn't pushed but turned,
        # display the cursor on the right position.

        if isinstance(cursor, str):

            if self.pos == 0:
                # push detected. move on to the content of the first menu item, which is handled through another method (????)
            else:
                # push detected. move on to the content of the second menu item, which is handled through another method (????)

        elif cursor == -1:
            # rotary encoder was turned left
            self.pos = 0
            self.draw.polygon([(120, 9), (128, 1), (128, 17)], outline=255, fill=255)
            self.draw.polygon([(120, 33), (128, 25), (128, 41)], outline=0, fill=0)
        else:
            # rotary encoder was turned right
            self.pos = 1
            self.draw.polygon([(120, 33), (128, 25), (128, 41)], outline=255, fill=255)
            self.draw.polygon([(120, 9), (128, 1), (128, 17)], outline=0, fill=0)
        self.disp.image(self.image)
        self.disp.display()
To run the code, I called the decoder method of the rotary encoder library and added the above written method (main_menu_callback) to the call, so that main_menu_callback can be used for the callback and get's executed every time the rotary encoder is turned or pushed:


decoder = rotary_encoder.decoder(pi, rotary_CLK, rotary_DT, rotary_SWITCH, main_menu_callback)
print("Test")
One of my many open questions is: How can I make my program to wait until the decoder was canceled (with
decoder.cancel()
)?

Currently it does not wait and the
print("Test")
get's executed right at the start of the program.
Reply
#2
The example uses time.sleep() to wait before cancelling it.
   pi = pigpio.pi()
   decoder = rotary_encoder.decoder(pi, 7, 8, callback)
   time.sleep(300)
   decoder.cancel()
   pi.stop()
It sounds like you don't actually want to call decoder.cancel(), though... it sounds like you want it to just run forever, while the callbacks get handled. So maybe...
decoder = rotary_encoder.decoder(#etc)
while True:
    # handle events

    # so the pi doesn't melt :)
    time.sleep(500)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alarm system with state, class, enum. Frankduc 0 1,240 May-04-2022, 01:26 PM
Last Post: Frankduc
  simulation of alarm system Frankduc 6 1,597 Apr-21-2022, 03:45 PM
Last Post: Frankduc
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,574 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Module 'time' has no attribute 'clock' Sophie 4 3,033 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Clock\time calculation script Drone4four 3 1,442 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  Handling interrupts in OOP Lou 2 1,710 Sep-02-2021, 02:17 PM
Last Post: Larz60+
  time.clock not functioning Oldman45 3 2,667 Apr-07-2021, 08:51 AM
Last Post: Oldman45
  How to update component props every time when a Dash callback returns? sguzunov 0 2,475 Jul-27-2020, 07:11 AM
Last Post: sguzunov
  update imhow in callback from pyaudio markB 0 2,315 May-28-2020, 06:01 PM
Last Post: markB
  Appending a list in a class from a callback function snizbatch 5 3,663 Sep-01-2019, 06:27 AM
Last Post: snizbatch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020