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()

 

posted on 2018-12-26 14:44  荷树栋  阅读(1316)  评论(0编辑  收藏  举报

导航