from machine import Pin,Timer, I2C
import time
import bluetooth
# 定义全局变量
BLE_MESSAGE = ""
Vbat_Pin = 29
I2C_SDA = 11
I2C_SDL = 12
SEND_M = False
class ESP32S3_BLE():
def init(self, name):
self.timer1 =Timer(0)
self.name =name
# 初始化蓝牙
self.ble =bluetooth.BLE()
# 开启蓝牙
self.ble.active(True)
# 设置蓝牙名称
self.ble.config(gap_name=name)
# 配置蓝牙回调函数
self.ble.irq(self.ble_irq)
# 注册蓝牙
self.register()
self.advertiser()
def ble_irq(self, event, data):
global BLE_MESSAGE
# IRQCENTRAL_CONNECT 蓝牙终端链接了此设备
print(event)
if event == 1:
self.connected()
# IRQCENTRAL_DISCONNECT 蓝牙终端断开此设备
elif event == 2:
self.advertiser()
self.disconnected()
# IRQGATTS_WRITE 蓝牙终端向 ESP32-S3 发送数据,接收数据处理
elif event == 3:
buffer = self.ble.gatts_read(self.rx)
BLE_MESSAGE = buffer.decode('UTF-8').strip()
def connected(self):
global SEND_M
print("connnect")
SEND_M = True
def disconnected(self):
global SEND_M
print("disconnected")
SEND_M = False
def send(self, data):
self.ble.gatts_notify(1, self.tx, data + '\n')
def register(self):
service_uuid = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
reader_uuid = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
sender_uuid = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
services = (
(
bluetooth.UUID(service_uuid),
(
(bluetooth.UUID(sender_uuid), bluetooth.FLAG_NOTIFY),
(bluetooth.UUID(reader_uuid), bluetooth.FLAG_WRITE),
)
),
)
((self.tx, self.rx,), ) = self.ble.gatts_register_services(services)
def advertiser(self):
name = bytes(self.name, 'UTF-8')
adv_data = bytearray('\x02\x01\x02', 'UTF-8') + bytearray((len(name) + 1, 0x09), 'UTF-8') + name
self.ble.gap_advertise(100, adv_data)
class QMI8658(object):
def init(self,address=0X6B):
self._address = address
self._bus = I2C(scl=Pin(I2C_SDL), sda=Pin(I2C_SDA), freq=1_000)
bRet=self.WhoAmI()
if bRet :
self.Read_Revision()
else :
return NULL
self.Config_apply()
def readbyte(self,cmd):
rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)
return rec[0]
def readblock(self, reg, length=1):
rec=self._bus.readfrom_mem(int(self._address),int(reg),length)
return rec
def readu16(self,cmd):
LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)
MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)
return (MSB[0] << 8) + LSB[0]
def writebyte(self,cmd,val):
self._bus.writeto_mem(int(self._address),int(cmd),bytes([int(val)]))
def WhoAmI(self):
bRet=False
if (0x05) == self._read_byte(0x00):
bRet = True
return bRet
def Read_Revision(self):
return self._read_byte(0x01)
def Config_apply(self):
# REG CTRL1
self._write_byte(0x02,0x60)
# REG CTRL2 : QMI8658AccRange_8g and QMI8658AccOdr_1000Hz
self._write_byte(0x03,0x23)
# REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz
self._write_byte(0x04,0x53)
# REG CTRL4 : No
self._write_byte(0x05,0x00)
# REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter
self._write_byte(0x06,0x11)
# REG CTRL6 : Disables Motion on Demand.
self._write_byte(0x07,0x00)
# REG CTRL7 : Enable Gyroscope And Accelerometer
self._write_byte(0x08,0x03)
def Read_Raw_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_timestamp = self._read_block(0x30,3)
raw_acc_xyz=self._read_block(0x35,6)
raw_gyro_xyz=self._read_block(0x3b,6)
raw_xyz=self._read_block(0x35,12)
timestamp = (raw_timestamp[2]<<16)|(raw_timestamp[1]<<8)|(raw_timestamp[0])
for i in range(6):
# xyz[i]=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz[i*2])
# xyz[i+3]=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])
xyz[i] = (raw_xyz[(i*2)+1]<<8)|(raw_xyz[i*2])
if xyz[i] >= 32767:
xyz[i] = xyz[i]-65535
return xyz
def Read_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_xyz=self.Read_Raw_XYZ()
#QMI8658AccRange_8g
acc_lsb_div=(1<<12)
#QMI8658GyrRange_512dps
gyro_lsb_div = 64
for i in range(3):
xyz[i]=raw_xyz[i]/acc_lsb_div#(acc_lsb_div/1000.0)
xyz[i+3]=raw_xyz[i+3]*1.0/gyro_lsb_div
return xyz
def LOAD_MSG():
qmi8658 = QMI8658()
ble = ESP32S3_BLE("FINGERTIPS BLE")
while(True):
#read QMI8658
xyz = qmi8658.Read_XYZ()
x = "{:+.2f} {:+.2f} {:+.2f} {:+.2f} {:+.2f} {:+.2f}".format(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5])
print(x)
if SEND_M == True :
ble.send(x)
if name == "__main__":
# DOF_READ()
# ble = ESP32S3_BLE("FINGERTIPS BLE")
#LOAD_MSG()
# 创建蓝牙对象且设置蓝牙名称
while True:
# 打开 LED
if BLE_MESSAGE == 'LED ON':
print('LED is ON.')
ble.send('LED is ON.')
BLE_MESSAGE = ""
# 关闭 LED
elif BLE_MESSAGE == 'LED OFF':
print('LED is OFF.')
ble.send('LED is OFF.')
BLE_MESSAGE = ""
time.sleep_ms(1000)
print(SEND_M)
if SEND_M == True :
print("1111")
ble.send("111")