1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- from kivy.app import App
- from kivy.uix.widget import Widget
- from kivy.uix.button import Button
- from kivy.uix.gridlayout import GridLayout
- from kivy.uix.label import Label
- from kivy.uix.textinput import TextInput
- from kivy.lang import Builder
- import asyncio
- from bleak import BleakClient,BleakScanner
- # import json
- STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
- class InfoWidget(Widget):
- # def __init__(self, **kwargs):
- # super(InfoWidget, self).__init__(**kwargs)
-
- async def discover_devices(self):
- self.size = (800,480)
- 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)
- # 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(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):
- # # self.size = (800,480)
- # grid = GridLayout(cols=2,rows=4,width=self.width,height=self.height)
- # self.add_widget(grid)
- # for i in range(8):
- # button = Button(text=str(i))
- # button.width = grid.width / 2
- # button.height = grid.height / 4
- # grid.add_widget(button)
- class STI2021A(App):
- def build(self):
- info = InfoWidget()
- asyncio.run(info.discover_devices())
- # info.debug()
- return info
- if __name__ == '__main__':
- STI2021A().run()
|