Coder Social home page Coder Social logo

upymenu's Introduction

uPyMenu

uPyMenu is a micropython menu implementation for LCD displays. Coming from an Arduino experience, I was used with LiquidCrystal and LiquidMenu, but they don't offer a python implementation. So I figured I could create one myself.

Development

Current development is done based on when I have to to work on it when I want to. But feel free to fork it and add your functionality. If you find any bugs or have suggestions for features, please open up a issue (or pull-request if you create the feature yourself!).

TODO

  • Add arguments to callback actions in the menu

Usage

The example below renders a menu with a submenu and actions that have callbacks attached which you can use to run your own function. It requires one dependency, and that's python_lcd for interacting with the LCD itself.

from machine import Pin, I2C # Basics for creating an LCD interface
from esp8266_i2c_lcd import I2cLcd # Example LCD interface used
from upymenu import Menu, MenuAction, MenuNoop

def action_callback():
    print("callback action chosen")

submenu = Menu("Submenu")
submenu_action_1 = MenuAction("Submenu Action", callback=action_callback)
submenu_action_2 = MenuAction("Submenu Action 1", callback=action_callback)
submenu.add_option(submenu_action_1)
submenu.add_option(submenu_action_2)

menu_action = MenuAction("Action", callback=action_callback)
menu = Menu("Main Menu")
menu.add_option(submenu)
menu.add_option(menu_action)
menu.add_option(MenuNoop("Nothing here"))

# Example config for LCD via i2c, you will need this 
# for the menu to function, the screen size is required
# to render the menu correctly on the screen.
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
lcd = I2cLcd(i2c, 0x3F, 4, 20)

current_menu = menu.start(lcd) # Starts the menu on the LCD

menu.focus_next() # Focus on the next item in the menu 
menu.focus_prev() # Focus on the previous item in the menu 

# Choose the focused item, if it's and action execute 
# the callback, or if it is a menu, render that menu.
menu = menu.choose()

# If it's a submenu, you can use the parent() function
# to navigate back up to the tree.
menu = menu.parent() 

Testing

If you have a micropython port running locally, you can run the test suite with it, ie: micropython tests.py. This is as close I can get with my current knowledge about micropython. Any pointers and suggestions towards improvement here would be appreciated!

upymenu's People

Contributors

dependabot[bot] avatar jplattel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

upymenu's Issues

Bug with more than 5 items

Hello, I am using you library and is working great now (Thanks) but I have to make a few changes to make it work.

  1. First one was that the menu was render side wise not vertical. Maybe this is only with my display library. To fix it I swap the variables in render().
    def _render_cursor(self):
        for l in range(0, self.lines):
            self.lcd.move_to(0, l)

and


  def _render_options(self):
        # Render the options:
        for l, option in enumerate(self.viewport):
            self.lcd.move_to(1, l)  # Move to the line

2)When I have more than 8 options the view chunk retrun was off. With this code is working for me (also no need of import math anymore):

    def _current_chunk(self):
        return int((self.focus -1) / (self.lines ))  # current chunk
  1. and finally I added the variable self.current with the menu object been render so I can control the sub menus more easily:
    def __init__(self, title, render_title=False):
        self.current = self
    def parent(self):
        if self.parent_menu:
            self.active = False
            self.parent_menu.current = self.parent_menu
            return self.parent_menu.start(self.lcd)

    def _choose_menu(self, submenu):
        self.active = False
        self.current = submenu

thanks for sharing the library. Regards.

All menu items are not displayed

Good afternoon. I'm trying to implement a menu on the 2004 display. The code will be below. The problem is that when activated, the menu does not show the entire list of items on the screen, but only the last one. And also, instead of the cursor on the screen, the letters "SA". What could be the problem?
If the menu works fine, the next question is: how to fasten the execution of the interrupt function to it.
`called_back_main = False
called_back_sub = False

def action_callback_main():
global called_back_main
print ("Действие главного меню")
called_back_main = True
def action_callback_sub():
global called_back_sub
print ("Действие подменю")
called_back_sub = True

submenu = Menu("Submenu")
submenu_action_1 = MenuAction("Submenu Action", callback=action_callback_sub)
submenu_action_2 = MenuAction("Submenu Action 1", callback=action_callback_sub)
submenu.add_option(submenu_action_1)
submenu.add_option(submenu_action_2)

menu = Menu("Main menu")
menu_action_1 = MenuAction("Action_1", callback=action_callback_main)
menu_action_2 = MenuAction(" Action_2", callback=action_callback_main)
menu.add_option(submenu)
menu.add_option(menu_action_1)
menu.add_option(menu_action_2)
menu.add_option(MenuNoop(" Главное меню"))

current_menu = menu.start(lcd)`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.