Menus for DisplayOTron

Hi all,

Recently got some Phillips Hue lights so thought it’d be fun to try and use a Pi with the DisplayOTron to control them.

I’m using the phue library (https://github.com/studioimaginaire/phue) to interact with the lights and that all seems okay but I’m struggling to get a menu setup calling the relevant code from the Hue API.

Eventually I’d like to have nested menus so I could drill down from a top menu to control a single light but so far I’m struggling to get even the basics working.

So far I have something like this…

for l in lights:
menu.add_item("Switch on "+ l.name,process_light(l.name))

process_light function is -

def process_light(light):
print("You selected light " + light)

This builds up the menu correctly and displays it to the LED but I was hoping I could just get it to just print something to screen to confirm I’m passing the name of the light from the menu correctly but when I select the menu option nothing happens. Any ideas why?

I also tried the MenuOption class but I’m confused how I can pass into it the value which was selected as it doesnt look like MenuOption takes a parameter. This is the code I tried -

menu.add_item("Switch off " + l.name,poweroff())

class poweroff(MenuOption):
def init(self):
self.start = self.millis()
MenuOption.init(self)
def redraw(self, menu):
menu.write_row(0, ‘Switching off light’)
menu.clear_row(1)
menu.clear_row(2)
time.sleep(1)
menu.left()

Not sure I explained that very well but any assistance on how to use the menus for the DisplayOTron would be much appreciated (I’ve looked at previous posts but no one seems to be building a dynamic menu on the fly like I’m trying to).

Thanks!

You can have a MenuOption derived class accept a parameter, a good example is the Volume() plugin which needs a backlight instance: https://github.com/pimoroni/displayotron/blob/53e6a0419e551019f5dd690b5fdf8fe3e979488f/examples/dothat/advanced/radio.py#L45

If you look at the code of the plugin itself, you can see that it saves this backlight object to a class variable for use later: https://github.com/pimoroni/displayotron/blob/master/examples/plugins/volume.py#L20-L30

So in your case you could perhaps:

class poweroff(MenuOption):
    def __init__(self, current_led):
        self.current_led = current_led
        MenuOption.__init__(self)

    def begin(self):
        self.start_time = self.millis()

    def redraw(self, menu):
        menu.write_row(0, 'Switching off light {}'.format(self.current_led.name))
        menu.clear_row(1)
        menu.clear_row(2)
        self.current_led.TURN_OFF()
        if self.millis() - self.start_time > 1000:
            menu.left()

menu.add_item("Switch off {}".format(l.name), poweroff(l))

Although you should avoid doing anything but redrawing in redraw and instead hook into the up, down, left, right and select methods to provide menu items for turning the light ON/OFF and exiting the menu item.

Or, perhaps the plugin should take a list of LEDs and display one menu option for each.

Hi, its been a long while since this was posted but I just want to say thanks! I’ve finally been able to build an application which allows me to -

Switch on/off Phillips Hue Lights individually or all at once
Change brightness of lights individually or all at once
Change colour to preset options for Colour Lights
Switch on/off TP-Link Plugs

The code is all dynamic and pulls in names/states of the devices automatically. Happy to share the code if anyone wants to take a look but my Python knowledge is limited to say the least so its probably massively inefficient!

Got a few ideas to enhance the app in the future but my next plan is to get hold of a Pi Zero and build a front facia to press the buttons.

Cheers again.

1 Like