设备初始化为BLE Peripheral或Central, 实现数据传输功能。注:仅带PSRAM机型(如M5Fire, M5Core2)支持。
from m5stack import *
from m5ui import *
from uiflow import *
from ble.ble_peripheral import BLEPeripheral
import time
setScreenColor(0x222222)
DATA = None
Device_Name = None
rand = None
periph = BLEPeripheral()
label0 = M5TextBox(12, 41, "DEVICE NAME:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label1 = M5TextBox(137, 41, "label1", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
title0 = M5Title(title="M5FIRE-PERIPHERAL", x=85, fgcolor=0xFFFFFF, bgcolor=0xa40000)
label2 = M5TextBox(12, 80, "SEND DATA:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label3 = M5TextBox(119, 80, "label3", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label4 = M5TextBox(12, 120, "RECV DATA:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label5 = M5TextBox(119, 120, "label5", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label6 = M5TextBox(46, 219, "SEND", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label9 = M5TextBox(200, 219, "DISCONNECT", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label7 = M5TextBox(12, 160, "STATUS:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label8 = M5TextBox(86, 160, "label8", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
import random
def on_recv(Data):
global DATA, Device_Name, rand
DATA = Data
label5.setText(str(DATA.decode()))
pass
def buttonA_wasPressed():
global DATA, Device_Name, rand
periph.write_to(str(rand))
label3.setText(str(rand))
pass
btnA.wasPressed(buttonA_wasPressed)
def buttonC_wasPressed():
global DATA, Device_Name, rand
periph.disconnect()
pass
btnC.wasPressed(buttonC_wasPressed)
Device_Name = 'M5Fire1'
periph.init(Device_Name)
periph.read_callback(on_recv)
label1.setText(str(Device_Name))
while True:
if periph.is_connected():
label8.setText('Connected')
else:
label8.setText('Disconnected')
rand = random.randint(1000000, 9999999)
wait_ms(100)
wait_ms(2)
from ble.ble_peripheral import BLEPeripheral
periph = BLEPeripheral()
periph.init(Device_Name)
periph.is_connected()
periph.disconnect()
periph.write_to(data)
def on_recv(Data):
print(str(DATA.decode()))
periph.read_callback(on_recv)
from m5stack import *
from m5ui import *
from uiflow import *
from ble.ble_central import BLECentral
import time
setScreenColor(0x222222)
data1 = None
name = None
Device_Name = None
rand = None
central = BLECentral()
label0 = M5TextBox(12, 40, "REMOTE DEV NAME:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label1 = M5TextBox(185, 40, "label1", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
title0 = M5Title(title="M5FIRE-CENTRAL", x=95, fgcolor=0xFFFFFF, bgcolor=0xa40000)
label2 = M5TextBox(12, 72, "STATUS:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label3 = M5TextBox(89, 72, "label3", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label4 = M5TextBox(12, 106, "SEND DATA:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label7 = M5TextBox(46, 213, "SCAN", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label5 = M5TextBox(120, 106, "label5", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label8 = M5TextBox(12, 140, "RECV DATA:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label6 = M5TextBox(139, 213, "SEND", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label9 = M5TextBox(120, 140, "label9", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label10 = M5TextBox(12, 174, "CON STATUS:", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label11 = M5TextBox(132, 174, "label11", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
label12 = M5TextBox(200, 213, "DISCONNECT", lcd.FONT_Ubuntu, 0xFFFFFF, rotate=0)
import random
def on_recv(Data):
global data1, name, Device_Name, rand
data1 = Data
label9.setText(str(data1.decode()))
pass
central.on_notify(on_recv)
def on_scan(x, y, dev_name):
global data1, name, Device_Name, rand
name = dev_name
if name == Device_Name:
central.connect()
label3.setText(str((str('Connect: ') + str(Device_Name))))
pass
def buttonA_wasPressed():
global data1, name, Device_Name, rand
central.scan(2000, on_scan)
pass
btnA.wasPressed(buttonA_wasPressed)
def buttonB_wasPressed():
global data1, name, Device_Name, rand
central.write_to(str(rand))
label5.setText(str(rand))
pass
btnB.wasPressed(buttonB_wasPressed)
def buttonC_wasPressed():
global data1, name, Device_Name, rand
central.disconnect()
pass
btnC.wasPressed(buttonC_wasPressed)
Device_Name = 'M5Fire1'
label1.setText(str(Device_Name))
while True:
if central.is_connected():
label11.setText('Connected')
else:
label11.setText('Disconnected')
rand = random.randint(1000000, 9999999)
wait_ms(100)
wait_ms(2)
from ble.ble_central import BLECentral
central = BLECentral()
def on_scan(x, y, dev_name):
global data1, name, Device_Name, rand
name = dev_name
if name == Device_Name:
central.connect()
central.scan(2000, on_scan)
central.disconnect()
central.is_connected()
def on_recv(Data):
data1 = Data
print(str(data1.decode()))
central.on_notify(on_recv)
central.write_to(data)