Browse Source

【完善】优化 micropython 示例代码实现

SummerGift 6 years ago
parent
commit
019ec58ef0

+ 2 - 3
examples/basic/random.py

@@ -11,8 +11,7 @@
 import random
 import random
 
 
 for j in range(0, 2):
 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(random.randint(1, 10))
     print("end")
     print("end")
-

+ 3 - 2
examples/basic/rtthread.py

@@ -10,6 +10,7 @@
 
 
 import rtthread
 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
 rtthread.stacks_analyze()                 # show thread information

+ 1 - 1
examples/basic/uos.py

@@ -38,4 +38,4 @@ print("删除 rtthread 文件夹:")
 uos.rmdir("rtthread")
 uos.rmdir("rtthread")
 
 
 print("列出当前目录下的文件列表:")
 print("列出当前目录下的文件列表:")
-print(uos.listdir())
+print(uos.listdir())

+ 2 - 1
examples/basic/utime.py

@@ -13,8 +13,9 @@ import utime
 utime.sleep(1)                                    # sleep for 1 second
 utime.sleep(1)                                    # sleep for 1 second
 utime.sleep_ms(500)                               # sleep for 500 milliseconds
 utime.sleep_ms(500)                               # sleep for 500 milliseconds
 utime.sleep_us(10)                                # sleep for 10 microseconds
 utime.sleep_us(10)                                # sleep for 10 microseconds
+
 start = utime.ticks_ms()                          # get value of millisecond counter
 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(utime.ticks_ms(), -100))
 print(utime.ticks_add(0, -1))
 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 = network.WLAN(network.STA_IF)
 wlan.scan()
 wlan.scan()
-wlan.connect("rtthread","02188888888")
+wlan.connect("rtthread", "02188888888")
 wlan.isconnected()
 wlan.isconnected()

+ 4 - 4
examples/network/tcp_client.py

@@ -10,7 +10,7 @@
 
 
 import usocket
 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
 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()

+ 4 - 3
examples/w601_iot_board/beeper.py

@@ -13,11 +13,12 @@ from machine import Pin
 
 
 PIN_BEEPER = 37
 PIN_BEEPER = 37
 
 
-beeper = Pin(("beep", PIN_BEEPER), Pin.OUT_PP)        # create beeper object from pin PIN_BEEPER, Set pin PIN_BEEPER to output mode
+# 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 
+beeper.value(1)            # trun the buzzer on
 time.sleep(0.5)
 time.sleep(0.5)
-beeper.value(0)            # trun the buzzer off 
+beeper.value(0)            # trun the buzzer off
 time.sleep(0.5)
 time.sleep(0.5)
 beeper.value(1)
 beeper.value(1)
 time.sleep(0.5)
 time.sleep(0.5)

+ 4 - 3
examples/w601_iot_board/blink.py

@@ -13,10 +13,11 @@ from machine import Pin
 
 
 PIN_LED_R = 38
 PIN_LED_R = 38
 
 
-led = Pin(("led_red", PIN_LED_R), Pin.OUT_PP)        #create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode
+# 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:
 while True:
-    led.value(0)            #Set led turn on
+    led.value(0)  # Set led turn on
     time.sleep(0.5)
     time.sleep(0.5)
-    led.value(1)            #Set led turn off
+    led.value(1)  # Set led turn off
     time.sleep(0.5)
     time.sleep(0.5)

+ 12 - 12
examples/w601_iot_board/rgb_led.py

@@ -18,18 +18,18 @@ PIN_LED_B = 40
 LED_ON  = 0
 LED_ON  = 0
 LED_OFF = 1
 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)]
+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
 count = 0