Microbit Python 課程介紹 --MicroPython Buttons

2017-05-22 08:29

Microbit Python 課程介紹 --MicroPython Buttons

Microbit Shop

我們有使用這裝置寫程式作了一些事,這是輸出(output)。我們還需要這裝置能去反應,我們稱之輸入(inputs).

這其實很容易,輸出是這裝置給這世界,輸入則是給這裝置。對 micro:bit 而言最明顯的輸入是兩個按鍵,分別是 A 跟 B. 我們來看一下MicroPython 可以怎麼對按鍵作反應。

這十分簡單:                                      

from microbit import *

 

sleep(10000)

display.scroll(str(button_a.get_presses()))

一開始會先讓裝置休息ten thousand milliseconds (i.e. 10 seconds) ,然後讓讓螢幕捲動你按A按鍵button A次數的數字,就這樣。

這好像是個沒什麼用處的程式碼,但介紹了許多有趣的慨念:

1.      sleep 函式(function)是讓你的Micro:bit 休息(sleep) 多少milliseconds,如果,你想要暫停你的程式就可以使用它。但它是函式(function),不是附在物件(object.)上的方法 method, 兩者有相像之處,也有不同之處。 

2.     這裡有個物件叫 button_a ,它可允許你使用 get_presses 方法(method)得到按按鍵的次數。

因為 get_presses 得到的是數值,但 display.scroll 只能顯示字元(characters), 我們需要去轉換數值到字元,所以我們用 str 函式,它是“string” 的縮寫。

第三行程式有些像洋蔥, display.scroll  包了 str ,而str  又包了 button_a.get_presses. Python 是允許這種像俄羅斯娃娃的巢狀寫法,讓逕行到下一行前,就將”內心的運算” (the inner-most answer) 作完 。.

讓我們假設你按鍵按了10次,讓我們看一下Python 如何完整的執行第三行程式:

display.scroll(str(button_a.get_presses()))

 

首先先得到 get_presses按按鍵的次數 10 ,然後轉換為字串:

display.scroll(str(10))

 

最後Python 就知道要將”10” 在螢幕作捲動:

display.scroll("10")

看起來很多工作, 但MicroPython 讓它執行效率非常好。

Event Loops(事件迴圈)

我們在寫程式式常常需要等待事件發生,就像我們是不是可以迴圈式的等待按按鍵的事件發生,然後,反映並執行要做哪些事情

在 Python 使用迴圈的關鍵字是 while ,它會去確認有什麼東西是 True,然後去執行迴圈包覆地程式碼( block of code called the body of the loop),如果,是False ,則會執行迴圈之後的程式。.

-----------  這段主要是作文字上描述如何用生活上的 to do list 慨念,去想如何寫迴圈內主體,跟後面的範例程式碼較無關,就先保留原文,不翻譯

Python makes it easy to define blocks of code. Say I have a to-do list written on a piece of paper. It probably looks something like this:

Shopping

Fix broken gutter

Mow the lawn

If I wanted to break down my to-do list a bit further, I might write something like this:

Shopping:

    Eggs

    Bacon

    Tomatoes

Fix broken gutter:

    Borrow ladder from next door

    Find hammer and nails

    Return ladder

Mow the lawn:

    Check lawn around pond for frogs

    Check mower fuel level

It’s obvious that the main tasks are broken down into sub-tasks that are indented underneath the main task to which they are related. So Eggs, Bacon and Tomatoes are obviously related to Shopping. By indenting things we make it easy to see, at a glance, how the tasks relate to each other.

-------------------------------------------------------------------------------------------------------

 

 

這稱之為巢狀nesting. 我們用這樣的形式去撰寫回圈內主體程式碼就像這段:

from microbit import *

 

while running_time() < 10000:

    display.show(Image.ASLEEP)

 

display.show(Image.SURPRISED)

這running_time 函式主要是傳回重裝置開始後多少 ms的數值( the number of milliseconds )

這while running_time() < 10000: 段是確認執行時間(the running time ) 少於10000 milliseconds (等於 10 seconds). 它便顯示 Image.ASLEEP

當大於10000 milliseconds 它便顯示 Image.SURPRISED,為什麼?  因為 while 的判斷是是為 False (running_time 是不再10 < 10000). 所以執行完迴圈的主體程式碼(the while loop’s block of code) ,然後,.執行之後的程式碼。這樣就好像你睡了10 sec,之後醒來臉上訝異的樣子! 試試吧 !

 

Handling an Event

如果你想要 MicroPython 在無限迴圈

while True:

# Do stuff

 

中去確認案件是否被按的事件 is_pressed.

因為 while 是確認判斷式是否為 True ,但 True 的表示式 就永遠為 True ,所以你就得到無限迴圈!

讓我們來作個超級簡單的電子寵物,它在你按button A.前都是悲傷的,按button B 就死掉!  (如何改一下這劇本? )

from microbit import *

 

while True:

    if button_a.is_pressed():

        display.show(Image.HAPPY)

    elif button_b.is_pressed():

        break

    else:

        display.show(Image.SAD)

 

display.clear()

 

我們使用if, elif (short for “else if”) and else  狀況判斷式(conditionals )去處理,不同按按鍵的事件及程式碼。這樣很容易理解吧!

一般的狀況式的表示就像:  

if something is True:

    # do one thing

elif some other thing is True:

    # do another thing

else:

    # do yet another thing.

這就跟一般的英文口語說法非常相似!

 is_pressed method 這方法只會產生兩個結果 True or False,如果按了button 回傳 True,反之,回傳 False。當按了按鍵A就出現笑臉,當按了按鍵B 則 break跳出迴圈,清除螢幕clear。都沒按按鍵則出現悲傷的臉!

可以不要讓這個劇本這麼悲傷嗎? 是不是可以增加兩個按鍵可以同時按的事件,Python 有 and, or 和 not 邏輯運算(logical operators) 去增加不同的表示式(multiple truth statements) 。

Microbit 台灣 商店購買

Microbit 中文 課程 : Python , Javascript, 物聯網

              中國

—————

返回