2017年2月6日 星期一

MicroPython Basics: Blink a LED

https://learn.adafruit.com/micropython-basics-blink-a-led/blink-led

Blink LED

To blink the LED we'll connect to the MicroPython board's REPL (read-eval-print loop, like a Python 'command prompt') and run commands that control the digital GPIO connected to the LED.  This way we can interactively turn the LED on and off right from the board without uploading any code, sketches, etc.
If your board has built-in LEDs that you can control you'll want to note the pin names or numbers for the LEDs:
  • pyboard
    • The pyboard has 4 built-in LEDs that you can control.  See the pinout diagram here and its note that pins P2-P5 control the LEDs.  One important clarification though, when you reference those pins in code you want to use the 'CPU name' in the white box on the diagram and not the pin name in the yellow box.  So pin P2 is actually called B4, P3 is A15, etc.  All of the LED pin CPU names are:
      • B4 = blue LED
      • A15 = yellow LED
      • A14 = green LED
      • A13 = red LED
    • Note too on the pyboard pin names are strings like 'B4' for the blue LED.  Instead of trying to send a number like 4 to pin functions you'll instead send a string 'B4'.
  • Feather HUZZAH ESP8266 and HUZZAH ESP8266 Breakout
    • Both the HUZZAH ESP8266 boards have a red LED you can control connected to pin number 0.
If your board doesn't have built-in LEDs make sure you've wired up a LED to one of the digital GPIO pins as shown in the previous page.

Connect to Serial REPL

To get started you'll need to connect to the board's serial REPL so you can send commands to control the LED.  Check out the Serial REPL page from this guide for information on tools to connect to the board's REPL.  Make sure you can connect to the REPL and see the >>> prompt before continuing!

Control the LED

Now for some excitement, let's turn the LED on and off from MicroPython!  The first step is to run a line of code that imports a special MicroPython module called 'machine'.  Run the following command at the serial REPL:
  1. import machine
microcontrollers_Screen_Shot_2016-08-18_at_5.08.37_PM.png
After pressing enter you should see no output and the >>> prompt return.  This just means the command succeeded and the machine module is now imported.  Once a module is imported you can access classes, objects, etc. from inside it (just like if you were writing Python code to run on your computer).
If you're curious you can even run a help command to see what objects and things are available in the machine module that was imported:
  1. help(machine)
microcontrollers_Screen_Shot_2016-08-18_at_5.11.22_PM.png
We'll use the Pin class to create an object that represents a digital GPIO pin on the board.  Once you have an instance of this Pin object you can call functions on it to set it to high and low levels that turn on and off the LED.
First create the Pin instance with a command like the following (note change the pin number, 15 in this example, to the pin number or name for your LED):
  1. led = machine.Pin(15, machine.Pin.OUT)
microcontrollers_Screen_Shot_2016-08-18_at_5.27.54_PM.png
Again you shouldn't see any output after running the command, and the REPL prompt should appear again.
Notice how when the Pin object is created it takes two parameters, first the pin number or name and then the function of the pin.  A pin can be set as an output which can set its level or voltage to a high or low value (like 'on' or 'off'), or it can be set as an input which can read if it's at a high or low value.  To blink the LED we just need to use the pin as an output since we're telling the LED to turn on or off using the level/voltage from the pin.
If you have multiple LEDs connected to the board you can create different Pin objects to control them individually!  Use the same syntax as above but create a new object like led2 and set the pin number or name appropriately.
Now for some fun, let's turn the LED on by setting the Pin to a high level with this command:
  1. led.high()
microcontrollers_Screen_Shot_2016-08-18_at_5.34.25_PM.png
You should see the LED instantly turn on!  There won't be any output from this command either.
Now turn the LED off by calling the low() function on the Pin object:
  1. led.low()
microcontrollers_Screen_Shot_2016-08-18_at_5.35.39_PM.png
You should see the LED turn off after running the command.  Woo hoo you're blinking a LED with MicroPython!
If for some reason your LED doesn't turn on or off with the above commands go back and carefully check all the wiring between the board and LED.  Make sure the LED is oriented the right way with the long leg (anode) connected to the board pin and the short leg (cathode) connected through a resistor to the board ground.  Also double and triple check you're using the right pin number or name--boards usually have a lot of pins and it's easy to use the wrong one on accident.
Try toggling the LED on and off by running the high and low commands again.  In the serial REPL you can actually press the up arrow or down arrow keys to quickly go back to previously run commands.

Add a Loop

Blinking the LED yourself with commands you run manually will get a little tiring after a while.  However since MicroPython is really just like desktop Python you can use a Python loop to blink the LED a number of times or even forever.
First you'll want to import the time module so you can use its sleep function to delay for a short time (otherwise the loop would run so fast you couldn't tell the LED is blinking!).  Just like importing the machine module you can import time with:
  1. import time
Now let's use a for loop to blink the LED 10 times on and off.  Enter the following code in the REPL, and notice how the REPL changes and starts to indent code inside the loop for you:
  1. for i in range(10):
  2. led.high()
  3. time.sleep(0.5)
  4. led.low()
  5. time.sleep(0.5)
microcontrollers_Screen_Shot_2016-08-18_at_5.46.02_PM.png
After entering the last line, time.sleep(0.5), and pressing enter you'll need to end the indented block by pressing delete and then enter again.  At this point the loop will start running and the LED should blink on and off!
Notice as the LED blinks and the loop runs you can't see the >>> REPL prompt or enter any code.  This is because MicroPython can only do one thing at a time and running code in a loop like that requires all of the board's attention.  Only once the code stops running can MicroPython give you the prompt to enter more code.
Try entering the code again and changing the value of the time.sleep function calls.  This function takes in a number that controls how long in seconds the board will delay.  You can use decimal values like 0.5 to sleep for half a second.  If you increase the value the LED will stay on and off for longer periods and slow down the blinking.  Likewise if you decrease it then the LED will blink faster.
Also try changing the number in the range function call that starts the loop.  The value 10 means the loop should repeat 10 times, however you can increase or decrease this to control how many times the LED blinks.  Every time the loop runs it will run each instruction inside of it in order, so you can see how it starts by turning the LED on, then delaying a bit, turning the LED off, and delaying again.  When all the code inside the loop has been run it will go back to the top and run the code again, unless the loop has been run for as long as the range function specified.
For one final example let's use an infinite loop that will blink the LED forever.  Enter the following code:
  1. while True:
  2. led.high()
  3. time.sleep(0.5)
  4. led.low()
  5. time.sleep(0.5)
microcontrollers_Screen_Shot_2016-08-18_at_5.53.53_PM.png
Again remember you have to press delete after the last line to end the indented block.  Once you run this code the LED should start blinking on and off forever!
You might be worried to see the >>> REPL prompt never appears again and you can't enter code.  Remember this is because MicroPython can only do one thing at a time and the infinite loop means the board is busy blinking the LED.
Luckily you can tell the board to stop whatever it's doing and return back to the REPL prompt.  Press the Ctrl-c key on your keyboard and you should see the code stop with a KeyboardInterrupt error like this:
microcontrollers_Screen_Shot_2016-08-18_at_5.56.04_PM.png
This is a handy tip to remember, if you ever need to stop a program that's running on the board just connect to the REPL and press Ctrl-c.  Sometimes you might need to press Ctrl-c a few times to stop everything.
That's all there is to blinking a LED with MicroPython!  With a few simple commands you can control a LED connected to a board pin.  Then using standard Python loop and delay functions you can make MicroPython blink the LED however you desire.
One important note though, you might wonder what happens if you start the LED blinking with a while loop like above and then reset or unplug/plug back in the board.  Try it to see what happens!
You might think the board will start up and go back to running the code you entered.  However that's not how MicroPython works--any code you enter in the REPL is just run for that moment in time.  As soon as you power down the board it will 'forget' what it was doing and start over fresh.  You'll see in a later guide how to make the board run a script when it starts, and that way you can make the board run your code like blinking a LED as soon as it powers up.
If you're curious to go further be sure to explore your board's documentation to see what else the machine module and Pin class expose.  For example here's the documentation for MicroPython ESP8266 machine module.

Python开发实例:TPYBoard开发板DIY红绿灯

Python开发实例:TPYBoard开发板DIY红绿灯

https://my.oschina.net/micropython/blog/756109


    1.实验目的

    1. 学习在PC机系统中扩展简单I/O 接口的方法。
    2. 进一步学习编制数据输出程序的设计方法。
    3. 学习模拟交通灯控制的方法。

    2.所需元器件

    220欧电阻一个
    红色LED数码管一个
    面包板一块
    TPYBoard板子一块
    数据线一条
    红、绿、黄三个led灯
    杜邦线若干

    3.点亮led灯

    将三个led灯插在面包板上,led负极插入面包板的负极(横向插孔),正极插入面包板的纵向插,将222欧电阻插入面包板的负极上(横向插孔)和纵向插孔中,将led灯的正极分别与TPYBoard的引脚连接起来,因为要做红绿灯,只需三个引脚即可,本人用的为Y1、Y2、Y3三个引脚,将三个led灯的正极通过杜邦线连接到TPYboard的Y1,、Y2、Y3的引脚上,然后将电阻纵向插孔用杜邦线接到TPYboard的GND引脚,在main.py文件中将Y1、Y2、Y3引脚的电平拉高,即可看到三个灯同时亮起来。代码为:
  1. # main.py -- put your code here!
  2. import pyb
  3. led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
  4. led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
  5. led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
  6. While True:
  7.      led1.value(1)
  8.      led2.value(1)
  9.      led3.value(1)

下面为0-9数字对应针脚的高电平,即对应TPYboard的引脚拉高电平

    数字高电平针脚

    0         a,b,c,d,e,f
    1         e,f
    2         a,b,g,e,d
    3         a,b,g,c,d
    4         b,c,g,f
    5         a,f,g,c,d
    6         a,f,e,d,c,g
    7         a,b,c
    8         a,b,c,d,e,f,g

    5.模拟红绿灯

    我们按照上面的步骤做完以后,然后通电,修改main.py文件,即可让灯随着数码管的变化而变化,具体代码如下:
  1. # main.py -- put your code here!
  2. import pyb
  3. led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
  4. led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
  5. led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
  6. x1 = pyb.Pin("X1",pyb.Pin.OUT_PP)
  7. x2 = pyb.Pin("X2",pyb.Pin.OUT_PP)
  8. x3 = pyb.Pin("X3",pyb.Pin.OUT_PP)
  9. x4 = pyb.Pin("X4",pyb.Pin.OUT_PP)
  10. x5 = pyb.Pin("X5",pyb.Pin.OUT_PP)
  11. x6 = pyb.Pin("X6",pyb.Pin.OUT_PP)
  12. x8 = pyb.Pin("X8",pyb.Pin.OUT_PP)
  13. def six():
  14.    x1.value(1)
  15.    x2.value(1)
  16.    x3.value(1)
  17.    x5.value(1)
  18.    x6.value(1)
  19.    x8.value(1)
  20.    pyb.delay(1000)
  21.    x1.value(0)
  22.    x2.value(0)
  23.    x3.value(0)
  24.    x6.value(0)
  25.    x5.value(0)
  26.    x8.value(0)
  27. def nine():
  28.    x1.value(1)
  29.    x2.value(1)
  30.    x3.value(1)
  31.    x4.value(1)
  32.    x5.value(1)
  33.    x8.value(1)
  34.    pyb.delay(1000)
  35.    x1.value(0)
  36.    x2.value(0)
  37.    x3.value(0)
  38.    x4.value(0)
  39.    x5.value(0)
  40.    x8.value(0)
  41. def eight():
  42.    x1.value(1)
  43.    x2.value(1)
  44.    x3.value(1)
  45.    x4.value(1)
  46.    x5.value(1)
  47.    x6.value(1)
  48.    x8.value(1)
  49.    pyb.delay(1000)
  50.    x1.value(0)
  51.    x2.value(0)
  52.    x3.value(0)
  53.    x4.value(0)
  54.    x5.value(0)
  55.    x6.value(0)
  56.    x8.value(0)
  57. def zero():
  58.    x2.value(1)
  59.    x3.value(1)
  60.    x4.value(1)
  61.    x5.value(1)
  62.    x6.value(1)
  63.    x8.value(1)
  64.    pyb.delay(1000)
  65.    x2.value(0)
  66.    x3.value(0)
  67.    x4.value(0)
  68.    x5.value(0)
  69.    x6.value(0)
  70.    x8.value(0)
  71. def seven():
  72.    x3.value(1)
  73.    x4.value(1)
  74.    x8.value(1)
  75.    pyb.delay(1000)
  76.    x3.value(0)
  77.    x4.value(0)
  78.    x8.value(0)
  79. def five():
  80.    x1.value(1)
  81.    x2.value(1)
  82.    x3.value(1)
  83.    x5.value(1)
  84.    x8.value(1)
  85.    pyb.delay(1000)
  86.    x1.value(0)
  87.    x2.value(0)
  88.    x3.value(0)
  89.    x5.value(0)
  90.    x8.value(0)
  91. def four():
  92.    x1.value(1)
  93.    x2.value(1)
  94.    x4.value(1)
  95.    x8.value(1)
  96.    pyb.delay(1000)
  97.    x1.value(0)
  98.    x2.value(0)
  99.    x4.value(0)
  100.    x8.value(0)
  101. def three():
  102.    x1.value(1)
  103.    x3.value(1)
  104.    x4.value(1)
  105.    x5.value(1)
  106.    x8.value(1)
  107.    pyb.delay(1000)
  108.    x1.value(0)
  109.    x4.value(0)
  110.    x3.value(0)
  111.    x5.value(0)
  112.    x8.value(0)
  113. def two():
  114.    x1.value(1)
  115.    x3.value(1)
  116.    x4.value(1)
  117.    x5.value(1)
  118.    x6.value(1)
  119.    pyb.delay(1000)
  120.    x1.value(0)
  121.    x3.value(0)
  122.    x4.value(0)
  123.    x5.value(0)
  124.    x6.value(0)
  125. def one():
  126.    x2.value(1)
  127.    x6.value(1)
  128.    pyb.delay(1000)
  129.    x2.value(0)
  130.    x6.value(0)
  131. while True:
  132.    led1.value(1)
  133.    nine()
  134.    eight()
  135.    seven()
  136.    six()
  137.    five()
  138.    four()
  139.    three()
  140.    two()
  141.    one()
  142.    zero()
  143.    led1.value(0)
  144.    led2.value(1)
  145.    nine()
  146.    eight()
  147.    seven()
  148.    six()
  149.    five()
  150.    four()
  151.    three()
  152.    two()
  153.    one()
  154.    zero()
  155.    led2.value(0)
  156.    led3.value(1)
  157.    three()
  158.    two()
  159.    one()
  160.    zero()
  161.    led3.value(0)

Windows 環境 Python3 安裝

源自於http://chiamingyen.github.io/kmolsite/blog/leo-editor-jiao-xue-yi.html

Windows 環境 Python3 安裝

可攜 Python3.4 設定

建立 data 目錄, 並在 data 目錄中建立 home, tmp, apps, Python34, SciTE 等子目錄, 其中 Python34 來自 C:\Python34, SciTE 也是安裝後直接複製到 data 目錄中.
將 lanuchLeo.py 存到 data/apps 目錄中, 用來啟動 Leo Editor
1
2
3
4
5
6
""" Leo launcher script
A minimal script to launch leo.
"""

import leo.core.runLeo
leo.core.runLeo.run()

啟動與關閉可攜系統的 start.bat 與 stop.bat 設定碼如下:
start.bat 設定
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@echo off
REM 設定 V 硬碟代號與 data 目錄對應
set Disk=y
subst %Disk%: "data"
REM 設定 leo 相關對應 Home 位置
set HomePath=%Disk%:\home
set HomeDrive=%Disk%:\home
set Home=%Disk%:\home
REM 設定 Leo 所用的編輯器
set LEO_EDITOR=%Disk%:\SciTE\SciTE.exe
REM 將後續的指令執行, 以 %Disk% 為主
%Disk%:
REM 設定 PYTHONPATH
set PYTHONPATH=%Disk%:\Python34
REM Jupyter 設定
set JUPYTER_CONFIG_DIR=%Disk%:\home\.jupyter\profile_nbserver
REM 設定 Jre 路徑, 特別注意 CLASSPATH 後的分號, 一定要有
set JAVA_HOME=%Disk%:\Java\jre1.8.0_51
set CLASSPATH=.;
REM 設定 node.js 相關路徑
set NODE_PATH=%Disk%:\nodejs\node_modules\npm\node_modules;%Disk%:\nodejs\node_modules\npm
set path1=%PATH%;%Disk%:\Python34;%Disk%:\Anaconda3\Scripts;%JAVA_HOME%\bin;%Disk%:\nodejs;
set path2=%Disk%:\apps\Git\bin;%Disk%:\apps\pandoc;%Disk%:\apps\portableLatex\MiKTeX\texmf\miktex\bin;
path=%path1%;%path2%;
REM 啟動  SciTE
start /MIN %Disk%:\SciTE\SciTE.exe
REM 取 IPV4 IP address
for /f "delims=[] tokens=2" %%a in ('ping -4 %computername% -n 1 ^| findstr "["') do (set thisip=%%a)
REM echo %thisip%
REM 取 IPV6 IP address
REM for /f "delims=[] tokens=2" %%a in ('ping %computername% -n 1 ^| findstr "["') do (set thisip=%%a)
REM echo %thisip%
REM 啟動 Jupyter
cd %Disk%:\tmp
start %Disk%:\Python34\python.exe "%Disk%:\Python34\Scripts\jupyter-script.py" notebook --ip=%thisip% --certfile=%Disk%:\home\ssl_cert.pem --profile=nbserver
cd ..
REM 啟動 VMEmulator
REM start %Disk%:\apps\nand2tetris\tools\VMEmulator.bat
REM 啟動 Assembler
REM start %Disk%:\apps\nand2tetris\tools\Assembler.bat
REM 啟動 CPUEmulator
REM start %Disk%:\apps\nand2tetris\tools\CPUEmulator.bat
REM 啟動 HardwareSimulator
REM start %Disk%:\apps\nand2tetris\tools\HardwareSimulator.bat
REM 啟動 JackCompiler
REM start %Disk%:\apps\nand2tetris\tools\JackCompiler.bat
REM 啟動 TextComparer
REM start %Disk%:\apps\nand2tetris\tools\TextComparer.bat
REM 啟動 node.js
REM 啟動 node.js
REM start /MIN cmd.exe /k "%Disk%:\nodejs\nodejsvars.bat"
REM start %Disk%:\nodejs\node.exe
start /MIN cmd.exe
start /MIN cmd.exe
start /MIN cmd.exe
REM 啟動 node.js
REM start %Disk%:\IDE\nodejs\node.exe
REM C:\WINDOWS\system32\cmd.exe /k %Disk%:\IDE\nodejs\nodejsvars.bat
REM 啟動 Leo 編輯器
python.exe %Disk%:\apps\launchLeo.py
Exit

stop.bat
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@echo off
set Disk=y
REM 關閉 nginx
REM taskkill /IM nginx.exe /F
REM 關閉 SciTE
taskkill /IM SciTE.exe /F
REM 關閉 python
taskkill /IM python.exe /F
REM 清除 log 資料
path=%PATH%;
REM del /Q /F  Y:\tmp\*.*
REM copy Y:\www\cmsimplexh_20130809\cmsimple\log_clean.txt V:\www\cmsimpleSpring2013\cmsimple\log.txt
REM del /Q /F  Y:\tmp\*.*
REM 終止虛擬硬碟與目錄的對應
subst %Disk%: /D
REM 關閉 cmd 指令視窗
taskkill /IM cmd.exe /F
EXIT

當然也可以直接下載以 Python 3.3 建立的 Windows 可攜式 Leo Editor: https://github.com/chiamingyen/portable_leoeditor

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...