Pārlūkot izejas kodu

Merge pull request #86 from SummerGGift/optimize_example

【完善】修改 pin 相关的 mpy 示例程序
朱天龙 (Armink) 6 gadi atpakaļ
vecāks
revīzija
2559d28631

+ 1 - 1
examples/stm32l4_pandora/beeper.py

@@ -11,7 +11,7 @@
 import utime as time
 from machine import Pin
 
-PIN_BEEPER = 37
+PIN_BEEPER = 18    # PB2, get the pin number from get_pin_number.py
 
 # create beeper object from pin PIN_BEEPER, Set pin PIN_BEEPER to output mode
 beeper = Pin(("beep", PIN_BEEPER), Pin.OUT_PP)

+ 1 - 1
examples/stm32l4_pandora/blink.py

@@ -11,7 +11,7 @@
 import utime as time
 from machine import Pin
 
-PIN_LED_R = 38
+PIN_LED_R = 71    # PE7, get the pin number from get_pin_number.py
 
 # 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)

+ 40 - 0
examples/stm32l4_pandora/get_pin_num.py

@@ -0,0 +1,40 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-28     SummerGift   first version
+#
+
+def get_pin_num(pin_index):
+    """
+    Get the GPIO pin number through the GPIO index, format must be "P + <A~K> + number", such as PE7
+    """
+
+    if pin_index[0] != 'P':
+        print("ERROR : Please pass in the correct parameters P + <A~K> + number, such as PE7")
+        return
+
+    if not pin_index[1].isupper():
+        print("ERROR : Please pass in the correct parameters P + <A~K> + number, such as PE7")
+        return
+
+    return (ord(pin_index[1]) - ord('A')) * 16 + int(pin_index[2:])
+
+print("The pin number of PE7 is %d, then blink the red led."%get_pin_num("PE7")) # Get the pin number for PE7
+
+# then you can use the pin num to control the device, such as led:
+
+import utime as time
+from machine import Pin
+
+# create led object from get_pin_num("PE7") and set pin to output mode
+led = Pin(("led_red", get_pin_num("PE7")), 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)

+ 2 - 2
examples/stm32l4_pandora/key.py

@@ -10,8 +10,8 @@
 
 from machine import Pin
 
-PIN_LED_R   = 38
-PIN_KEY0    = 57
+PIN_LED_R   = 71    # PE7, get the pin number from get_pin_number.py
+PIN_KEY0    = 58    # PD10, get the pin number from get_pin_number.py
 KEY_PRESSED = 0
 
 # create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode

+ 3 - 3
examples/stm32l4_pandora/rgb_led.py

@@ -11,9 +11,9 @@
 import utime as time
 from machine import Pin
 
-PIN_LED_R = 38
-PIN_LED_G = 39
-PIN_LED_B = 40
+PIN_LED_R = 71    # PE7, get the pin number from get_pin_number.py
+PIN_LED_G = 72    # PE8
+PIN_LED_B = 73    # PE9
 
 LED_ON  = 0
 LED_OFF = 1