码迷,mamicode.com
首页 > 编程语言 > 详细

python Console menu

时间:2018-12-26 15:54:43      阅读:439      评论:0      收藏:0      [点我收藏+]

标签:dex   ted   should   tor   dem   nis   def   int   end   

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

标签:dex   ted   should   tor   dem   nis   def   int   end   

原文地址:https://www.cnblogs.com/zjbfvfv/p/10179072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!