python Console menu

Posted 荷树栋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Console menu相关的知识,希望对你有一定的参考价值。

I just finished a demo which is to provide an easy way to control hardware resources of A sample. The communication between PC and A sample is UART. The PC software is written by Python.

Python can implement features very soon and has a rich libraries. I used two libraries, one is console-menu, and another is pyserial.

pip install console-menu

https://pypi.org/project/console-menu/

pip install pyserial

https://pypi.org/project/pyserial/

 

When startup, a prompt input will display:

 

Input the COM number what you want. And then the operation window will display.

 

You can select the menu for future operation, e.g. Alarm,

 

 

I attached the source code here for your reference.

from consolemenu import ConsoleMenu
from consolemenu.items import FunctionItem, SubmenuItem, CommandItem
from consolemenu import Screen
import time
import Serial


class GPIOOperator(object):
    def __init__(self, UART, menu, cmd, text):
        self._uart = UART
        self._cmd = cmd
        self._state = 0
        submenu = ConsoleMenu(text, show_exit_option=True)
        function_item = FunctionItem("ON", self.OnTrunOnClick, should_exit=False)
        submenu.append_item(function_item)
        function_item = FunctionItem("OFF", self.OnOnTurnOffClick, should_exit = False)
        submenu.append_item(function_item) 
        submenu_item = SubmenuItem(text, submenu, menu=menu, should_exit = True)
        menu.append_item(submenu_item)

    def _GenData(self):
        data = (self._cmd << 4) | (self._stat << 1)
        parity = 0
        for index in range(0, 8):
            if (data & 1 << index):
                parity = parity + 1
        
        if (parity % 2 == 0):
            data = data | 1;
        return data;

    def OnTrunOnClick(self):
        self._stat = 1
        data = self._GenData()
 
        if (self._uart.isOpen()):
            self._uart.write(data)
        
    def OnOnTurnOffClick(self):
        self._stat = 0
        data = self._GenData()
 
        if (self._uart.isOpen()):
            self._uart.write(data)
        


class GPIOOperators(object):
    def __init__(self):
        self._menu = ConsoleMenu("FPC 220 firmware == PC Test Software")
        self._uart = Serial.UART()
        self._CMD_ALARM = 0
        self._STR_ALARM = "Alarm"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_ALARM, self._STR_ALARM)

        self._CMD_ALARM_LED = 1
        self._STR_ALARM_LED = "Alarm LED"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_ALARM_LED, self._STR_ALARM_LED)

        self._CMD_PATTERN = 2
        self._STR_PATTERN = "Pattern"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_PATTERN, self._STR_PATTERN)

        self._CMD_TROUBLE = 3
        self._STR_TROUBLE = "Trouble"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_TROUBLE, self._STR_TROUBLE)

        self._CMD_GREEN_LED = 4
        self._STR_GREEN_LED = "Green LED"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_GREEN_LED, self._STR_GREEN_LED)

        self._CMD_CLEANME = 5
        self._STR_CLEANME = "Clean me"
        self._alarm = GPIOOperator(self._uart, self._menu, self._CMD_CLEANME, self._STR_CLEANME)
        
        FunctionItem("Input COM", self.OnComInput(), should_exit=False)

    def OnComInput(self):
        ret = Screen().input("Set the COM number:");
        self._uartnum = ret

    def run(self):
        Screen().clear()
        while(1):
            \'\'\'if (self._uart.isOpen() == False):
                self._uart.open(self._uartnum)
                if (self._uart.isOpen() == False):
                    print("Cannot Open the UART!")
                    time.sleep(1)
            else:
                self._menu.show()
            \'\'\'
            self._menu.show()
            if self._menu.is_selected_item_exit() == True:
                break
        
        self._uart.close()
    

if __name__ == "__main__":
    GPIOOperations = GPIOOperators()
    GPIOOperations.run()

 

以上是关于python Console menu的主要内容,如果未能解决你的问题,请参考以下文章

从对话框片段返回后,工具栏视图引用变为null

C#程序员经常用到的10个实用代码片段

面向面试编程代码片段之GC

前端开发常用js代码片段

php Wordpress functions.php片段使用Yoast的插件将Google Analytics跟踪添加到wp_nav_menu

VS中添加自定义代码片段——偷懒小技巧