main.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from kivy.app import App
  2. from kivy.uix.widget import Widget
  3. from kivy.uix.button import Button
  4. from kivy.properties import (
  5. StringProperty,NumericProperty,
  6. )
  7. from kivy.clock import Clock
  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. info = StringProperty("")
  14. def __init__(self, **kwargs):
  15. super(InfoWidget, self).__init__(**kwargs)
  16. # asyncio.run(self.discover_devices())
  17. # async def discover_devices(self):
  18. # devices = await BleakScanner.discover()
  19. # for d in devices:
  20. # button = Button(text=f"{d.address}: {d.name}")
  21. # button.bind(on_press=lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address)))
  22. # self.add_widget(button)
  23. # async def connect_and_receive(self, address):
  24. # async with BleakClient(address) as client:
  25. # if await client.is_connected():
  26. # rawdata = await client.read_gatt_char()
  27. # data = rawdata.decode("utf-8")
  28. # if data.startswith("[") and data.endswith("]"):
  29. # list = json.loads(data)
  30. # self.info = "基波频率:" + str(list[0]) + "Hz\n" + \
  31. # "THD:" + str(list[6]) + "%\n" + \
  32. # "基波幅值:" + str(list[1]) + "V\n" + \
  33. # "二次谐波:" + str(list[2]) + "V\n" + \
  34. # "三次谐波:" + str(list[3]) + "V\n" + \
  35. # "四次谐波:" + str(list[4]) + "V\n" + \
  36. # "五次谐波:" + str(list[5]) + "V\n"
  37. # else:
  38. # self.info = "传输数据出错"
  39. # return
  40. # else:
  41. # print("Failed to connect to device.")
  42. def debug(self):
  43. for i in range(6):
  44. button = Button(text=str(i) + "!!!!!!")
  45. self.add_widget(button)
  46. class STI2021A(App):
  47. def build(self):
  48. info = InfoWidget()
  49. info.debug()
  50. return info
  51. if __name__ == '__main__':
  52. STI2021A().run()