最新消息:

乐高 EV3 高级编程 – 第五课:控制 EV3 按键

乐高 少儿编程 4236浏览 0评论

译者按:有一些比赛吧,我举个简单例子,在比赛开始前不告诉你 机器人 要往前走多少 cm,比赛开始后才告诉你,那个时候已经不容许改程序了,怎么办呢?好好看明白这一课吧!

 

EV3: Lesson 5 – Control the EV3 buttons

EV3:第 5 课 – 控制 EV3 主机按键

5.1 Buttons on the EV3

5.1 EV3 主机上的按键

There are 6 buttons on the EV3 brick, namely:

EV3 主机上共有 6 个按键,它们是:

Back 退出(左上角)

Up 上

Down 下

Left 左

Right 右

Enter 确认(中间)

In this lesson, we’ll create a menu, and learn how to detect if those buttons are pressed, to navigate through the menu.

在这一课,我们在 EV3 程序里创建一个菜单,并学习如何使用这些按键去浏览并选择菜单。

Create the following files:

创建以下的这几个档案:

from ev3dev.ev3 import *

def funDefineLcdBtn():
    global lcd, btn
    lcd = Screen()
    btn = Button()

zephan.top/ev3pythonles

上面是 “g.py”

 

from time import sleep, time
import g

def funShowMenu():
    intSelect = 1
    intNoOfLine = 6

    # Menu Contents in List listPN
    listPN = ['Line 1', 'Line 2', 'Line 3', 'Line 4', 'Line 5', 'Quit']

    bolSelect = False
    bolPressed = False

    # Continue to loop until the user make a selection
    while not bolSelect:
        g.lcd.clear()

        intX = 5
        intY = 5
        
        # Display title
        g.lcd.draw.text((intX, intY), 'Select and press Enter')

        # Display a line after the title, to separate the title and the content
        g.lcd.draw.line((0, intY + 12, 177, intY + 12), fill = None, width = 0)

        intY = intY + 5

        # Display all lines
        i = 1
        while i <= intNoOfLine:
            intY = intY + 15
            if i == intSelect:
                # If the cursor is on this line, reverse the color of this line
                g.lcd.draw.rectangle((0, intY, 177, intY + 10), fill = 'black')
                g.lcd.draw.text((intX, intY), listPN[i - 1], fill = 'white')
            else:
                # If the cursor is not on this line, display the text on this line 
                g.lcd.draw.text((intX, intY), listPN[i - 1])
            i = i + 1

        # Show the screen
        g.lcd.update()

        if bolPressed:
            sleep(0.5)
            bolPressed = False

        # if bolWait == True, continue waiting for key press event
        bolWait = True
        while bolWait:
            if g.btn.up:
                # if Up is pressed
                bolPressed = True
                bolWait = False
                # Cursor go up 1 line
                intSelect = intSelect - 1
                if intSelect == 0:
                    # if Cursor at line 0, set Cursor to last line
                    intSelect = intNoOfLine
            elif g.btn.down:
                # if Down is pressed
                bolPressed = True
                bolWait = False
                # Cursor go down 1 line
                intSelect = intSelect + 1
                if intSelect > intNoOfLine:
                    intSelect = 1
            elif g.btn.left:
                # if Left is pressed, do nothing
                pass
            elif g.btn.right:
                # if Right is pressed, do nothing
                pass
            elif g.btn.enter:
                # if Enter is pressed, exit loop
                bolWait = False
                bolSelect = True
            else:
                # Sleep for 0.05 seconds to detect another key press
                sleep(0.05)
    
    
    # From here User has made its selection
    g.lcd.clear()
    g.lcd.draw.text((0, 5), 'You select: ' + listPN[intSelect-1])
    g.lcd.draw.text((0, 20), 'This program will quit')
    g.lcd.draw.text((0, 35), 'in 10 seconds')
    g.lcd.update()

    sleep(10)

zephan.top/ev3pythonles

上面是 “fun.py ”

#!/usr/bin/env python3
import traceback
import g
import fun

# Initialize the LCD and Buttons
try:
    g.funDefineLcdBtn()

    fun.funShowMenu()
except:
    # If there is any error, it will be stored in the log file in the same directory
    logtime = str(time())
    f=open("log" + logtime + ".txt",'a')  
    traceback.print_exc(file=f)  
    f.flush()  
    f.close()

zephan.top/ev3pythonles

上面是主程序 “lesson5_01.py ”

 

You may start to discover that the “Main Program” is getting shorter and shorter! Yes, this is how programming should look like, make the “CORE LOGIC” as simple as possible!

你会开始发现,主程序变了越来越短!是的!这就是真正的程序的样子哦!我们应该让程序的主逻辑越简单越好!

Read through the programs, pay attention to the comments and it’s easy to understand how it works, if you don’t understand, run the program first and come back again to look at the source code.

详细的阅读一下程序,看看程序的备注,应该比较容易可以理解,如果还是搞不明白,先运行程序再回来看源代码。

Upload the above 3 programs and run lesson5_01.py in the EV3 brick:

上传上面的 3 个程序,并且运行主程序 lesson5_01.py,你会看见:

 

乐高 EV3 高级编程 - 第五课:控制 EV3 按键

 

Figure 5.1

You can navigator through the menu by using the “Up” and “Down” buttons on the EV3 brick, and make a selection by using the middle “Enter” button:

你可以使用【上】、【下】键去浏览菜单,然后按【确认键】选择:

 

乐高 EV3 高级编程 - 第五课:控制 EV3 按键

 

Figure 5.2

In our next lesson, we’ll learn how to write EV3 Python Programs in Object Oriented Approach.

在下一课,我们会学习如何使用面向对象的编程方法,去编写 EV3 Python 程序。

始发于知乎专栏:ken

您必须 登录 才能发表评论!