Rotary controller OLED menu updated

Last modified date

Comments: 2

Many moons ago (well, size years – approximately 71 full moons), I put a little python code up on GitHub to show a menu on a 128×32 pixel OLED screen and allow changing the highlighted option with a rotary controller (potentiometer).

I had intended to create some kind of clock radio alarm with a raspberry pi, but never swung back to that project as I just use my phone (like most people would), so that code never got touched after the initial commit.

Yesterday I decided the code needed a little love (and because the bread board with the OLED screen and rotary controller was still on my desk taunting me!)

New things

The updated code has had a little restructure and added some new things:

  • New way of creating menus

    It’s not just a simple dict any longer, but instead the menu needs to be constructed using the MenuAction or MenuParent classes.
  • Menu options can now perform actions

    Using the MenuAction class, when you use the rotary controller’s push/button action, it can now perform the action of that menu option, be it a lambda or a class function, or whatever callable type you want to use.
  • Menus can be multi-level

    Using the MenuParent allows you to group together sub-actions, and allow you to navigate to that level of menu on the rotary push action. There’s also a back option automatically added for any parent.

Demo

Here’s an example video of it in action. How to set it up (what pins to use on the raspberry pi and what the code looks like) can all be found on the project’s repo on GitHub.

Share

2 Responses

  1. Hi, your code and menu management are really nice.
    I’ve taken all your code and installed the dependencies, with no problem.
    But I have a problem, when I execute it, I get this message :

    jbe@raspberrypi-001:~/Menu$ python3 menutest.py
    Traceback (most recent call last):
    File “menutest.py”, line 4, in
    from Oled.Menu import Menu, MenuAction, MenuParent
    File “/Menu/Oled/Menu.py”, line 19, in
    @dataclass
    File “/Menu/Oled/Menu.py”, line 22, in MenuParent
    actions: list[MenuAction] = field(default_factory=list)
    TypeError: ‘type’ object is not subscriptable

    I use the latest version of Debian on a Raspberry Pi 3. And the latest version of Python 3.7.3 (default, Jun 29 2023, 18:03:57) [GCC 8.3.0] on linux.

  2. Hi @jbe; I’m really sorry for they delayed response – didn’t noticed that you had posted here!

    I believe the issue is because Python 3.7 doesn’t like the generics hinting of list[MenuAction] – I think that support came in with Python 3.9.

    What you should be able to do is import the List type with:

    from typing import List

    There’s already an import for Union, so you could just add onto that, such as:

    from typing import Union, List

    and then update the line to use the upper-cased type you just imported, such as:

    actions: List[MenuAction] = field(default_factory=list)

    You’ll probably find that you also need to do the same with the __init__ in the Menu class, such as:

    def __init__(self, options: List[Union[MenuParent, MenuAction]] = None):

    Hopefully that works for you! Any more problems, please feel free to post here or create an issue on GitHub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.