main.py 4.0 KB

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