123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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
- from kivy.clock import Clock
- from kivy.properties import BooleanProperty
- import asyncio
- from bleak import BleakClient,BleakScanner
- import time
- # import json
- STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
- class InfoWidget(Widget):
- def __init__(self, **kwargs):
- super(InfoWidget, self).__init__(**kwargs)
- self.last_flag = False
- self.flag = False
- self.scanning = Label(text="Scanning...",font_size=100,top=self.top)
- self.devices = None
-
- def update(self, dt):
- if self.flag and (not self.last_flag):
- self.connect()
- self.last_flag = self.flag
-
- def start(self):
- self.size = (1600,800)
- self.add_widget(self.scanning)
- def connect(self):
- self.grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4,top=self.top)
- self.remove_widget(self.scanning)
- self.grid00.top = self.top
- self.add_widget(self.grid00)
- grid01 = GridLayout(cols=2,rows=1,width=self.grid00.width,height=self.grid00.height / 2)
- self.grid00.add_widget(grid01)
- grid01.add_widget(Label(text="Device Name:",width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=50))
- deviceName = "HC-06"
- grid01.add_widget(TextInput(text=deviceName,width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=40))
- connect_button = Button(text="Connect",width=self.grid00.width,height=self.grid00.height / 2,center_x=self.grid00.center_x,font_size=40)
- self.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)
- async def discover_devices(self):
- self.devices = await BleakScanner.discover()
- self.flag = True
- def start_discovery(self, dt):
- asyncio.create_task(self.discover_devices())
- class STI2021A(App):
- def build(self):
- # 在这里设置asyncio事件循环
- loop = asyncio.get_event_loop()
- if not loop.is_running():
- asyncio.set_event_loop(loop)
-
- info = InfoWidget()
- info.start()
- Clock.schedule_once(info.start_discovery, 1)
- Clock.schedule_interval(info.update, 1)
- return info
- def run(self):
- # 获取当前的事件循环
- loop = asyncio.get_event_loop()
- # 运行应用程序,直到完成
- loop.run_until_complete(self.async_run())
- loop.close()
- if __name__ == '__main__':
- STI2021A().run()
|