Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
LED control menu
#1
hi all,
im trying to creat a menu on a display-o-tronHAT from pimoroni
and would like to create a menu to control a led on and off to start with,
im still very new to python and the raspberry pi but im determind to get this working,
so far i have:
#!/usr/bin/env python

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep 

# Add the root examples dir so Python can find the plugins
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast

print("""
This advanced example uses the menu framework.
It gives you a basic menu setup with plugins. You should be able to view system info and adjust settings!

Press CTRL+C to exit.
""")

class Lightcontrol(MenuOption):
    """Control; Lights"""
    
def __init__(self):
    self.selected_option = 0
    self.options = [
        'On',
        'Off',
        ]
    self.actions = [
        self.handle_On,
        self.handle_Off,
]
    MenuOption.__init__(self)

def handle_On(self):
    print('Eeek! Doing monkey stuff!')
    time.sleep(2)
    print('Done monkey stuff!')

def handle_Off(self):
    print('Oook! Doing donkey stuff!')
    time.sleep(2)
    print('Done donkey stuff!')
def select_option(self):
    self.actions[self.selected_option]()

def next_option(self):
    self.selected_option = (self.selected_option + 1) % len(self.options)

def prev_option(self):
    self.selected_option = (self.selected_option - 1) % len(self.options)

def up(self):
    self.prev_option()

def down(self):
    self.next_option()



    def right(self):
        self.select_option()

    def get_current_option(self):
        return self.options[self.selected_option]

    def get_next_option(self):
        return self.options[(self.selected_option + 1) % len(self.options)]

    def get_prev_option(self):
        return self.options[(self.selected_option - 1) % len(self.options)]

    def redraw(self, menu):
        menu.write_option(
            row=0,
            margin=1,
            icon='',
            text=self.get_prev_option()
        )

        menu.write_option(
            row=1,
            margin=1,
            icon='>',  # Let's use a > to denote the currently selected option
            text=self.get_current_option()
        )

        menu.write_option(
            row=2,
            margin=1,
            icon='',
            text=self.get_next_option()
        )
    
    
"""
Using a set of nested lists you can describe
the menu you want to display on dot3k.

Instances of classes derived from MenuOption can
be used as menu items to show information or change settings.

See GraphTemp, GraphCPU, Contrast and Backlight for examples.
"""


menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lightcontrol(MenuOption),
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_timeout=30,
    input_handler=Text())

"""
You can use anything to control dot3k.menu,
but you'll probably want to use dot3k.touch
"""
nav.bind_defaults(menu)

while 1:
    menu.redraw()
    time.sleep(0.05)
but im getting an error:
Traceback (most recent call last):
  File "/home/pi/Scripts/temp.py", line 121, in <module>
    'Control': Lightcontrol(MenuOption),
TypeError: __init__() takes 1 positional argument but 2 were given

the end project is a raspberry pi controlled relay to turn lights on and off in aquarium,
but also want to add options for local sunrise and set to control the lights duration,
eventually want to add more but that is all i need for now
Reply
#2
Try to change Lightcontrol(MenuOption) to Lightcontrol()

menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lightcontrol(), # here
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_timeout=30,
    input_handler=Text())
You don't have to pass the parent class when you initialise the child.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
ah i see thank you, that worked but when i go into "Aquarium>Lighting" i can then see >control but cant select it or see the options on or off
Reply
#4
I am not familiar with the modules you are using.
However, see the indentation of the Lightcontrol's class code block.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
i have tried changing the indent but then i get errors saying self is not defined so had to revert back
Reply
#6
What have you tried? Lines 31 to 64 are not properly indented.
The definition of the Lightcontrol class starts on line 27 and ends on line 28
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
This doesnt give me any errors but still can't get past the control menu or see on and off
import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep 

# Add the root examples dir so Python can find the plugins
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast

print("""
This advanced example uses the menu framework.
It gives you a basic menu setup with plugins. You should be able to view system info and adjust settings!

Press CTRL+C to exit.
""")

class Lightcontrol():
    """Control; Lights"""
    
def __init__(self):
    self.selected_option = 0
    self.options = [
    'On',
    'Off',
    ]
    self.actions = [
        self.handle_On,
        self.handle_Off,
]
    MenuOption.__init__(self)

def handle_On(self):
    print('Eeek! Doing monkey stuff!')
    time.sleep(2)
    print('Done monkey stuff!')

def handle_Off(self):
    print('Oook! Doing donkey stuff!')
    time.sleep(2)
    print('Done donkey stuff!')
def select_option(self):
    self.actions[self.selected_option]()

def next_option(self):
    self.selected_option = (self.selected_option + 1) % len(self.options)

def prev_option(self):
    self.selected_option = (self.selected_option - 1) % len(self.options)

def up(self):
    self.prev_option()

def down(self):
    self.next_option()



def right(self):
    self.select_option()

def get_current_option(self):
    return self.options[self.selected_option]

def get_next_option(self):
    return self.options[(self.selected_option + 1) % len(self.options)]

def get_prev_option(self):
    return self.options[(self.selected_option - 1) % len(self.options)]

def redraw(self, menu):
    menu.write_option(
        row=0,
        margin=1,
        icon='',
        text=self.get_prev_option()
        )

    menu.write_option(
        row=1,
        margin=1,
        icon='>',  # Let's use a > to denote the currently selected option
        text=self.get_current_option()
        )

    menu.write_option(
        row=2,
        margin=1,
        icon='',
        text=self.get_next_option()
        )
    
    
"""
Using a set of nested lists you can describe
the menu you want to display on dot3k.

Instances of classes derived from MenuOption can
be used as menu items to show information or change settings.

See GraphTemp, GraphCPU, Contrast and Backlight for examples.
"""


menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lightcontrol(),
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_timeout=30,
    input_handler=Text())

"""
You can use anything to control dot3k.menu,
but you'll probably want to use dot3k.touch
"""
nav.bind_defaults(menu)

while 1:
    menu.redraw()
    time.sleep(0.05)
Reply
#8
Why did you remove class MenuOption from class Lightcontrol(MenuOption): on line 25?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Becuase i thought you said to do so below, however ive now added MenuOption back to the class but leaving it out of the nested menu so it says Lightcontrol()
this also works up to the control submenu,
i still cannot see a on or off option
(May-31-2018, 06:59 AM)wavic Wrote: Try to change Lightcontrol(MenuOption) to Lightcontrol()

menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lightcontrol(), # here
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_timeout=30,
    input_handler=Text())
You don't have to pass the parent class when you initialise the child.
this is how my code looks now
import sys
import time
 
import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep 
 
# Add the root examples dir so Python can find the plugins
sys.path.append('/home/pi/Pimoroni/displayotron/examples')
 
from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
 
print("""
This advanced example uses the menu framework.
It gives you a basic menu setup with plugins. You should be able to view system info and adjust settings!
 
Press CTRL+C to exit.
""")
 
class Lightcontrol(MenuOption):
    """Control; Lights"""
     
def __init__(self):
    self.selected_option = 0
    self.options = [
    'On',
    'Off',
    ]
    self.actions = [
        self.handle_On,
        self.handle_Off,
]
    MenuOption.__init__(self)
 
def handle_On(self):
    print('Eeek! Doing monkey stuff!')
    time.sleep(2)
    print('Done monkey stuff!')
 
def handle_Off(self):
    print('Oook! Doing donkey stuff!')
    time.sleep(2)
    print('Done donkey stuff!')
def select_option(self):
    self.actions[self.selected_option]()
 
def next_option(self):
    self.selected_option = (self.selected_option + 1) % len(self.options)
 
def prev_option(self):
    self.selected_option = (self.selected_option - 1) % len(self.options)
 
def up(self):
    self.prev_option()
 
def down(self):
    self.next_option()
 
 
 
def right(self):
    self.select_option()
 
def get_current_option(self):
    return self.options[self.selected_option]
 
def get_next_option(self):
    return self.options[(self.selected_option + 1) % len(self.options)]
 
def get_prev_option(self):
    return self.options[(self.selected_option - 1) % len(self.options)]
 
def redraw(self, menu):
    menu.write_option(
        row=0,
        margin=1,
        icon='',
        text=self.get_prev_option()
        )
 
    menu.write_option(
        row=1,
        margin=1,
        icon='>',  # Let's use a > to denote the currently selected option
        text=self.get_current_option()
        )
 
    menu.write_option(
        row=2,
        margin=1,
        icon='',
        text=self.get_next_option()
        )
     
     
"""
Using a set of nested lists you can describe
the menu you want to display on dot3k.
 
Instances of classes derived from MenuOption can
be used as menu items to show information or change settings.
 
See GraphTemp, GraphCPU, Contrast and Backlight for examples.
"""
 
 
menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lightcontrol(),
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_timeout=30,
    input_handler=Text())
 
"""
You can use anything to control dot3k.menu,
but you'll probably want to use dot3k.touch
"""
nav.bind_defaults(menu)
 
while 1:
    menu.redraw()
    time.sleep(0.05)
Reply
#10
It's still not indented properly.
Look at the class definition. All methods are at the same indentation level as the class statement itself.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remote control of the Visual Studio 2013 debug menu using Python MariusTo 0 2,478 Jan-17-2018, 04:58 PM
Last Post: MariusTo

Forum Jump:

User Panel Messages

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