main.old 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import asyncio
  9. from bleak import BleakClient,BleakScanner
  10. # import json
  11. STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
  12. class InfoWidget(Widget):
  13. # def __init__(self, **kwargs):
  14. # super(InfoWidget, self).__init__(**kwargs)
  15. async def discover_devices(self):
  16. self.size = (800,480)
  17. devices = await BleakScanner.discover()
  18. grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4)
  19. grid00.top = self.top
  20. self.add_widget(grid00)
  21. grid01 = GridLayout(cols=2,rows=1,width=grid00.width,height=grid00.height / 2)
  22. grid00.add_widget(grid01)
  23. grid01.add_widget(Label(text="Device Name:",width=grid00.width / 2,height=grid00.height / 2,font_size=50))
  24. deviceName = "HC-06"
  25. grid01.add_widget(TextInput(text=deviceName,width=grid00.width / 2,height=grid00.height / 2,font_size=40))
  26. connect_button = Button(text="Connect",width=grid00.width,height=grid00.height / 2,center_x=grid00.center_x,font_size=40)
  27. Builder.load_string('''
  28. <Button>:
  29. id: connect_button
  30. on_touch_down: root.root.remove_widget(grid00)
  31. ''')
  32. grid00.add_widget(connect_button)
  33. # for d in devices:
  34. # if d.name == deviceName:
  35. # await self.connect_and_receive(d.address)
  36. # self.add_widget(Label(text="Connected",width=self.width,height=self.height / 4))
  37. # break
  38. # grid1 = GridLayout(cols=2,rows=5,width=self.width,height=self.height)
  39. # self.add_widget(grid1)
  40. # for num,d in enumerate(devices):
  41. # button = Button(text=f"{d.name}")
  42. # button.height = grid1.height / 5
  43. # button.width = grid1.width / 2
  44. # button.on_touch_down = lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address))
  45. # grid1.add_widget(button)
  46. # if num >= 9:
  47. # break
  48. # async def connect_and_receive(self, address):
  49. # async with BleakClient(address) as client:
  50. # if await client.is_connected():
  51. # rawdata = await client.read_gatt_char(STM32_UUID)
  52. # data = rawdata.decode("utf-8")
  53. # if data.startswith("[") and data.endswith("]"):
  54. # list = json.loads(data)
  55. # info = "基波频率:" + str(list[0]) + "Hz\n" + \
  56. # "THD:" + str(list[6]) + "%\n" + \
  57. # "基波幅值:" + str(list[1]) + "V\n" + \
  58. # "二次谐波:" + str(list[2]) + "V\n" + \
  59. # "三次谐波:" + str(list[3]) + "V\n" + \
  60. # "四次谐波:" + str(list[4]) + "V\n" + \
  61. # "五次谐波:" + str(list[5]) + "V\n"
  62. # else:
  63. # info = "传输数据出错"
  64. # label = Label(text=info)
  65. # self.add_widget(label)
  66. # return
  67. # else:
  68. # print("Failed to connect to device.")
  69. # def debug(self):
  70. # # self.size = (800,480)
  71. # grid = GridLayout(cols=2,rows=4,width=self.width,height=self.height)
  72. # self.add_widget(grid)
  73. # for i in range(8):
  74. # button = Button(text=str(i))
  75. # button.width = grid.width / 2
  76. # button.height = grid.height / 4
  77. # grid.add_widget(button)
  78. class STI2021A(App):
  79. def build(self):
  80. info = InfoWidget()
  81. asyncio.run(info.discover_devices())
  82. # info.debug()
  83. return info
  84. if __name__ == '__main__':
  85. STI2021A().run()