micro:bit (python) 的无线电模块
module radio for micro:bit (python)
这是我在 python
中的 microbit 代码
所以我想知道如何将 target_x 和 target_y 从微位 1 发送到第二个微位?
微位 1 :
radio.on()
target_x = random.randint(0,4)
target_y = random.randint(0,4)
if button_a.was.pressed():
radio.send()
微比特 2:
radio.on()
order = radio.receive()
microbit.display.set_pixel(target_x,target_y,7)
所以我想知道如何将 target_x 和 target_y 从微位 1 发送到第二个微位?
感谢您的回答
我使用两个微位测试了下面的代码。我在接收方添加了一个 'except, try' 子句以防消息损坏。应该实施更多的错误检查以建立可靠的无线接口,但这回答了问题。
radio_send_randints.py
''' transmit random x and y on button push '''
import random
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
if button_a.was_pressed():
target_x = random.randint(0,4)
target_y = random.randint(4)
message = "{},{}".format(target_x, target_y)
radio.send(message)
sleep(100)
radio_receive_randints.py
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
incoming = radio.receive()
if incoming:
try:
target_x, target_y = incoming.split(',')
except:
continue
display.set_pixel(int(target_x), int(target_y), 7)
这是我在 python
中的 microbit 代码所以我想知道如何将 target_x 和 target_y 从微位 1 发送到第二个微位?
微位 1 :
radio.on()
target_x = random.randint(0,4)
target_y = random.randint(0,4)
if button_a.was.pressed():
radio.send()
微比特 2:
radio.on()
order = radio.receive()
microbit.display.set_pixel(target_x,target_y,7)
所以我想知道如何将 target_x 和 target_y 从微位 1 发送到第二个微位?
感谢您的回答
我使用两个微位测试了下面的代码。我在接收方添加了一个 'except, try' 子句以防消息损坏。应该实施更多的错误检查以建立可靠的无线接口,但这回答了问题。
radio_send_randints.py
''' transmit random x and y on button push '''
import random
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
if button_a.was_pressed():
target_x = random.randint(0,4)
target_y = random.randint(4)
message = "{},{}".format(target_x, target_y)
radio.send(message)
sleep(100)
radio_receive_randints.py
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
incoming = radio.receive()
if incoming:
try:
target_x, target_y = incoming.split(',')
except:
continue
display.set_pixel(int(target_x), int(target_y), 7)