Просмотр исходного кода

Merge pull request #78 from SummerGGift/add_more_example

【添加】iot board 开发板更多示例程序
朱天龙 (Armink) 6 лет назад
Родитель
Сommit
010f4e8f28

+ 2 - 3
examples/basic/random.py

@@ -11,8 +11,7 @@
 import random
 
 for j in range(0, 2):
-    random.seed(13)         #指定随机数种子
-    for i in range(0, 10):  #生成0到10范围内的随机序列
+    random.seed(13)  # 指定随机数种子
+    for i in range(0, 10):  # 生成0到10范围内的随机序列
         print(random.randint(1, 10))
     print("end")
-

+ 3 - 2
examples/basic/rtthread.py

@@ -10,6 +10,7 @@
 
 import rtthread
 
-print(rtthread.is_preempt_thread())       # determine if code is running in a preemptible thread
-print(rtthread.current_tid() )            # current thread id
+# determine if code is running in a preemptible thread
+print(rtthread.is_preempt_thread())
+print(rtthread.current_tid())            # current thread id
 rtthread.stacks_analyze()                 # show thread information

+ 5 - 4
examples/basic/ucollections.py

@@ -8,17 +8,18 @@
 # 2019-06-13     SummerGift   first version
 #
 
+from ucollections import OrderedDict
 from ucollections import namedtuple
 
+print("namedtuple example:")
 MyTuple = namedtuple("MyTuple", ("id", "name"))
 t1 = MyTuple(1, "foo")
 t2 = MyTuple(2, "bar")
 print(t1.name)
+print(t2.name)
 assert t2.name == t2[1]
-ucollections.OrderedDict(...)
-
-from ucollections import OrderedDict
 
+print("\nOrderedDict example:")
 # To make benefit of ordered keys, OrderedDict should be initialized
 # from sequence of (key, value) pairs.
 d = OrderedDict([("z", 1), ("a", 2)])
@@ -26,4 +27,4 @@ d = OrderedDict([("z", 1), ("a", 2)])
 d["w"] = 5
 d["b"] = 3
 for k, v in d.items():
-    print(k, v)
+    print("%s %s"%(k, v))

+ 27 - 5
examples/basic/uos.py

@@ -10,10 +10,32 @@
 
 import uos
 
+print("获得当前所在目录:")
+print(uos.getcwd())
+
+print("创建文件夹 :rtthread")
 uos.mkdir("rtthread")
-uos.getcwd()
+
+print("列出当前目录下的文件列表:")
+print(uos.listdir())
+
+print("移动当前目录到 rtthread 文件夹下:")
 uos.chdir("rtthread")
-uos.getcwd()
-uos.listdir()
-uos.rmdir("11")
-uos.listdir()
+
+print("获得当前所在目录:")
+print(uos.getcwd())
+
+print("切换到上一级目录:")
+uos.chdir("..")
+
+print("获得当前所在目录:")
+print(uos.getcwd())
+
+print("列出当前目录下的文件列表:")
+print(uos.listdir())
+
+print("删除 rtthread 文件夹:")
+uos.rmdir("rtthread")
+
+print("列出当前目录下的文件列表:")
+print(uos.listdir())

+ 2 - 1
examples/basic/utime.py

@@ -13,8 +13,9 @@ import utime
 utime.sleep(1)                                    # sleep for 1 second
 utime.sleep_ms(500)                               # sleep for 500 milliseconds
 utime.sleep_us(10)                                # sleep for 10 microseconds
+
 start = utime.ticks_ms()                          # get value of millisecond counter
-delta = utime.ticks_diff(utime.ticks_ms(), start) # compute time difference
+delta = utime.ticks_diff(utime.ticks_ms(), start)  # compute time difference
 print(utime.ticks_add(utime.ticks_ms(), -100))
 print(utime.ticks_add(0, -1))
 

+ 1 - 1
examples/network/network_wlan_sta.py

@@ -12,5 +12,5 @@ import network
 
 wlan = network.WLAN(network.STA_IF)
 wlan.scan()
-wlan.connect("rtthread","02188888888")
+wlan.connect("rtthread", "02188888888")
 wlan.isconnected()

+ 4 - 4
examples/network/tcp_client.py

@@ -10,7 +10,7 @@
 
 import usocket
 
-s = usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)
-s.connect(("192.168.10.110",6000))  
-s.send("micropython")               
-s.close()
+client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
+client.connect(("192.168.10.110", 6000))
+client.send("rt-thread micropython!")
+client.close()

+ 12 - 7
examples/network/tcp_server.py

@@ -10,10 +10,15 @@
 
 import usocket
 
-s = usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)  # Create STREAM TCP socket
-s.bind(('192.168.12.32', 6001))   
-s.listen(5)
-s.setblocking(True)
-sock,addr=s.accept()              
-sock.recv(10)                    
-s.close()
+# Create STREAM TCP socket
+server = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
+server.bind(('192.168.12.203', 6001))
+server.listen(5)
+server.setblocking(True)
+
+while True:
+    # 等待客户端连接
+    clientsocket, addr = server.accept()
+    print("connect address: %s" % str(addr))
+    clientsocket.send('welcome to rt-thread micropython!')
+    clientsocket.close()

+ 26 - 0
examples/w601_iot_board/beeper.py

@@ -0,0 +1,26 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import utime as time
+from machine import Pin
+
+PIN_BEEPER = 37
+
+# create beeper object from pin PIN_BEEPER, Set pin PIN_BEEPER to output mode
+beeper = Pin(("beep", PIN_BEEPER), Pin.OUT_PP)
+
+beeper.value(1)            # trun the buzzer on
+time.sleep(0.5)
+beeper.value(0)            # trun the buzzer off
+time.sleep(0.5)
+beeper.value(1)
+time.sleep(0.5)
+beeper.value(0)
+time.sleep(0.5)

+ 23 - 0
examples/w601_iot_board/blink.py

@@ -0,0 +1,23 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import utime as time
+from machine import Pin
+
+PIN_LED_R = 38
+
+# create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode
+led = Pin(("led_red", PIN_LED_R), Pin.OUT_PP)
+
+while True:
+    led.value(0)  # Set led turn on
+    time.sleep(0.5)
+    led.value(1)  # Set led turn off
+    time.sleep(0.5)

+ 25 - 0
examples/w601_iot_board/key.py

@@ -0,0 +1,25 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import Pin
+
+PIN_LED_R   = 38
+PIN_KEY0    = 57
+KEY_PRESSED = 0
+
+# create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode
+led = Pin(("led_red", PIN_LED_R), Pin.OUT_PP)
+key_0 = Pin(("key_0", PIN_KEY0), Pin.IN, Pin.PULL_UP)
+
+while True:
+    if key_0.value() == KEY_PRESSED:
+        led.value(0)  # Set led turn on
+    else:
+        led.value(1)

+ 44 - 0
examples/w601_iot_board/rgb_led.py

@@ -0,0 +1,44 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import utime as time
+from machine import Pin
+
+PIN_LED_R = 38
+PIN_LED_G = 39
+PIN_LED_B = 40
+
+LED_ON  = 0
+LED_OFF = 1
+
+led_r = Pin(("LED RED", PIN_LED_R), Pin.OUT_PP)
+led_g = Pin(("LED GREEN", PIN_LED_G), Pin.OUT_PP)
+led_b = Pin(("LED BLUE", PIN_LED_B), Pin.OUT_PP)
+
+blink_tab = [(LED_ON, LED_ON, LED_ON),
+             (LED_OFF, LED_ON, LED_ON),
+             (LED_ON, LED_OFF, LED_ON),
+             (LED_ON, LED_ON, LED_OFF),
+             (LED_OFF, LED_OFF, LED_ON),
+             (LED_ON, LED_OFF, LED_OFF),
+             (LED_OFF, LED_ON, LED_OFF),
+             (LED_ON, LED_OFF, LED_OFF)]
+
+count = 0
+
+while True:
+    group_num = count % len(blink_tab)
+
+    led_r.value(blink_tab[group_num][0])           # set led status
+    led_g.value(blink_tab[group_num][1])
+    led_b.value(blink_tab[group_num][2])
+
+    count += 1
+    time.sleep(0.5)