main1.old 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from kivy.app import App
  2. from kivy.uix.widget import Widget
  3. from kivy.uix.button import Button
  4. from kivy.uix.gridlayout import GridLayout
  5. from kivy.uix.label import Label
  6. from kivy.uix.textinput import TextInput
  7. from kivy.lang import Builder
  8. from kivy.clock import Clock
  9. from kivy.properties import BooleanProperty
  10. import asyncio
  11. from bleak import BleakClient,BleakScanner
  12. import time
  13. # import json
  14. STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
  15. class InfoWidget(Widget):
  16. def __init__(self, **kwargs):
  17. super(InfoWidget, self).__init__(**kwargs)
  18. self.last_flag = False
  19. self.flag = False
  20. self.scanning = Label(text="Scanning...",font_size=100,top=self.top)
  21. self.devices = None
  22. def update(self, dt):
  23. if self.flag and (not self.last_flag):
  24. self.connect()
  25. self.last_flag = self.flag
  26. def start(self):
  27. self.size = (1600,800)
  28. self.add_widget(self.scanning)
  29. def connect(self):
  30. self.grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4,top=self.top)
  31. self.remove_widget(self.scanning)
  32. self.grid00.top = self.top
  33. self.add_widget(self.grid00)
  34. grid01 = GridLayout(cols=2,rows=1,width=self.grid00.width,height=self.grid00.height / 2)
  35. self.grid00.add_widget(grid01)
  36. grid01.add_widget(Label(text="Device Name:",width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=50))
  37. deviceName = "HC-06"
  38. grid01.add_widget(TextInput(text=deviceName,width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=40))
  39. connect_button = Button(text="Connect",width=self.grid00.width,height=self.grid00.height / 2,center_x=self.grid00.center_x,font_size=40)
  40. self.grid00.add_widget(connect_button)
  41. # for d in devices:
  42. # if d.name == deviceName:
  43. # await self.connect_and_receive(d.address)
  44. # self.add_widget(Label(text="Connected",width=self.width,height=self.height / 4))
  45. # break
  46. # grid1 = GridLayout(cols=2,rows=5,width=self.width,height=self.height)
  47. # self.add_widget(grid1)
  48. # for num,d in enumerate(devices):
  49. # button = Button(text=f"{d.name}")
  50. # button.height = grid1.height / 5
  51. # button.width = grid1.width / 2
  52. # button.on_touch_down = lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address))
  53. # grid1.add_widget(button)
  54. # if num >= 9:
  55. # break
  56. # async def connect_and_receive(self, address):
  57. # async with BleakClient(address) as client:
  58. # if await client.is_connected():
  59. # rawdata = await client.read_gatt_char(STM32_UUID)
  60. # data = rawdata.decode("utf-8")
  61. # if data.startswith("[") and data.endswith("]"):
  62. # list = json.loads(data)
  63. # info = "基波频率:" + str(list[0]) + "Hz\n" + \
  64. # "THD:" + str(list[6]) + "%\n" + \
  65. # "基波幅值:" + str(list[1]) + "V\n" + \
  66. # "二次谐波:" + str(list[2]) + "V\n" + \
  67. # "三次谐波:" + str(list[3]) + "V\n" + \
  68. # "四次谐波:" + str(list[4]) + "V\n" + \
  69. # "五次谐波:" + str(list[5]) + "V\n"
  70. # else:
  71. # info = "传输数据出错"
  72. # label = Label(text=info)
  73. # self.add_widget(label)
  74. # return
  75. # else:
  76. # print("Failed to connect to device.")
  77. # def debug(self):
  78. # # self.size = (800,480)
  79. # grid = GridLayout(cols=2,rows=4,width=self.width,height=self.height)
  80. # self.add_widget(grid)
  81. # for i in range(8):
  82. # button = Button(text=str(i))
  83. # button.width = grid.width / 2
  84. # button.height = grid.height / 4
  85. # grid.add_widget(button)
  86. async def discover_devices(self):
  87. self.devices = await BleakScanner.discover()
  88. self.flag = True
  89. def start_discovery(self, dt):
  90. asyncio.create_task(self.discover_devices())
  91. class STI2021A(App):
  92. def build(self):
  93. # 在这里设置asyncio事件循环
  94. loop = asyncio.get_event_loop()
  95. if not loop.is_running():
  96. asyncio.set_event_loop(loop)
  97. info = InfoWidget()
  98. info.start()
  99. Clock.schedule_once(info.start_discovery, 1)
  100. Clock.schedule_interval(info.update, 1)
  101. return info
  102. def run(self):
  103. # 获取当前的事件循环
  104. loop = asyncio.get_event_loop()
  105. # 运行应用程序,直到完成
  106. loop.run_until_complete(self.async_run())
  107. loop.close()
  108. if __name__ == '__main__':
  109. STI2021A().run()