|
@@ -2,11 +2,9 @@ from kivy.app import App
|
|
from kivy.uix.widget import Widget
|
|
from kivy.uix.widget import Widget
|
|
from kivy.uix.button import Button
|
|
from kivy.uix.button import Button
|
|
from kivy.uix.gridlayout import GridLayout
|
|
from kivy.uix.gridlayout import GridLayout
|
|
-from kivy.properties import (
|
|
|
|
- StringProperty,NumericProperty,
|
|
|
|
-)
|
|
|
|
-from kivy.clock import Clock
|
|
|
|
-
|
|
|
|
|
|
+from kivy.uix.label import Label
|
|
|
|
+from kivy.uix.textinput import TextInput
|
|
|
|
+from kivy.lang import Builder
|
|
import asyncio
|
|
import asyncio
|
|
from bleak import BleakClient,BleakScanner
|
|
from bleak import BleakClient,BleakScanner
|
|
|
|
|
|
@@ -15,39 +13,66 @@ import json
|
|
STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
|
|
STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
|
|
|
|
|
|
class InfoWidget(Widget):
|
|
class InfoWidget(Widget):
|
|
- info = StringProperty("")
|
|
|
|
-
|
|
|
|
def __init__(self, **kwargs):
|
|
def __init__(self, **kwargs):
|
|
super(InfoWidget, self).__init__(**kwargs)
|
|
super(InfoWidget, self).__init__(**kwargs)
|
|
|
|
+ self.size = (800,480)
|
|
|
|
+ asyncio.run(self.discover_devices())
|
|
|
|
|
|
- # asyncio.run(self.discover_devices())
|
|
|
|
|
|
+ async def discover_devices(self):
|
|
|
|
+ devices = await BleakScanner.discover()
|
|
|
|
+ grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4)
|
|
|
|
+ grid00.top = self.top
|
|
|
|
+ self.add_widget(grid00)
|
|
|
|
+ grid01 = GridLayout(cols=2,rows=1,width=grid00.width,height=grid00.height / 2)
|
|
|
|
+ grid00.add_widget(grid01)
|
|
|
|
+ grid01.add_widget(Label(text="Device Name:",width=grid00.width / 2,height=grid00.height / 2,font_size=50))
|
|
|
|
+ deviceName = "HC-06"
|
|
|
|
+ grid01.add_widget(TextInput(text=deviceName,width=grid00.width / 2,height=grid00.height / 2,font_size=40))
|
|
|
|
+ connect_button = Button(text="Connect",width=grid00.width,height=grid00.height / 2,center_x=grid00.center_x,font_size=40)
|
|
|
|
+ Builder.load_string('''
|
|
|
|
+<Button>:
|
|
|
|
+ id: connect_button
|
|
|
|
+ on_touch_down: root.root.remove_widget(grid00)
|
|
|
|
+ ''')
|
|
|
|
+ grid00.add_widget(connect_button)
|
|
|
|
|
|
- # async def discover_devices(self):
|
|
|
|
- # devices = await BleakScanner.discover()
|
|
|
|
- # for d in devices:
|
|
|
|
- # button = Button(text=f"{d.address}: {d.name}")
|
|
|
|
- # button.bind(on_press=lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address)))
|
|
|
|
- # self.add_widget(button)
|
|
|
|
|
|
+ for d in devices:
|
|
|
|
+ if d.name == deviceName:
|
|
|
|
+ await self.connect_and_receive(d.address)
|
|
|
|
+ self.add_widget(Label(text="Connected",width=self.width,height=self.height / 4))
|
|
|
|
+ break
|
|
|
|
+ # grid1 = GridLayout(cols=2,rows=5,width=self.width,height=self.height)
|
|
|
|
+ # self.add_widget(grid1)
|
|
|
|
+ # for num,d in enumerate(devices):
|
|
|
|
+ # button = Button(text=f"{d.name}")
|
|
|
|
+ # button.height = grid1.height / 5
|
|
|
|
+ # button.width = grid1.width / 2
|
|
|
|
+ # button.on_touch_down = lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address))
|
|
|
|
+ # grid1.add_widget(button)
|
|
|
|
+ # if num >= 9:
|
|
|
|
+ # break
|
|
|
|
|
|
- # async def connect_and_receive(self, address):
|
|
|
|
- # async with BleakClient(address) as client:
|
|
|
|
- # if await client.is_connected():
|
|
|
|
- # rawdata = await client.read_gatt_char()
|
|
|
|
- # data = rawdata.decode("utf-8")
|
|
|
|
- # if data.startswith("[") and data.endswith("]"):
|
|
|
|
- # list = json.loads(data)
|
|
|
|
- # self.info = "基波频率:" + str(list[0]) + "Hz\n" + \
|
|
|
|
- # "THD:" + str(list[6]) + "%\n" + \
|
|
|
|
- # "基波幅值:" + str(list[1]) + "V\n" + \
|
|
|
|
- # "二次谐波:" + str(list[2]) + "V\n" + \
|
|
|
|
- # "三次谐波:" + str(list[3]) + "V\n" + \
|
|
|
|
- # "四次谐波:" + str(list[4]) + "V\n" + \
|
|
|
|
- # "五次谐波:" + str(list[5]) + "V\n"
|
|
|
|
- # else:
|
|
|
|
- # self.info = "传输数据出错"
|
|
|
|
- # return
|
|
|
|
- # else:
|
|
|
|
- # print("Failed to connect to device.")
|
|
|
|
|
|
+ async def connect_and_receive(self, address):
|
|
|
|
+ async with BleakClient(address) as client:
|
|
|
|
+ if await client.is_connected():
|
|
|
|
+ rawdata = await client.read_gatt_char(STM32_UUID)
|
|
|
|
+ data = rawdata.decode("utf-8")
|
|
|
|
+ if data.startswith("[") and data.endswith("]"):
|
|
|
|
+ list = json.loads(data)
|
|
|
|
+ info = "基波频率:" + str(list[0]) + "Hz\n" + \
|
|
|
|
+ "THD:" + str(list[6]) + "%\n" + \
|
|
|
|
+ "基波幅值:" + str(list[1]) + "V\n" + \
|
|
|
|
+ "二次谐波:" + str(list[2]) + "V\n" + \
|
|
|
|
+ "三次谐波:" + str(list[3]) + "V\n" + \
|
|
|
|
+ "四次谐波:" + str(list[4]) + "V\n" + \
|
|
|
|
+ "五次谐波:" + str(list[5]) + "V\n"
|
|
|
|
+ else:
|
|
|
|
+ info = "传输数据出错"
|
|
|
|
+ label = Label(text=info)
|
|
|
|
+ self.add_widget(label)
|
|
|
|
+ return
|
|
|
|
+ else:
|
|
|
|
+ print("Failed to connect to device.")
|
|
def debug(self):
|
|
def debug(self):
|
|
# self.size = (800,480)
|
|
# self.size = (800,480)
|
|
grid = GridLayout(cols=2,rows=4,width=self.width,height=self.height)
|
|
grid = GridLayout(cols=2,rows=4,width=self.width,height=self.height)
|
|
@@ -61,7 +86,7 @@ class InfoWidget(Widget):
|
|
class STI2021A(App):
|
|
class STI2021A(App):
|
|
def build(self):
|
|
def build(self):
|
|
info = InfoWidget()
|
|
info = InfoWidget()
|
|
- info.debug()
|
|
|
|
|
|
+ # info.debug()
|
|
return info
|
|
return info
|
|
|
|
|
|
|
|
|