Browse Source

much faster ble connection

Thomas Buck 4 months ago
parent
commit
6344033842
2 changed files with 82 additions and 37 deletions
  1. 77
    34
      python-test/poll.py
  2. 5
    3
      python-test/state_connect.py

+ 77
- 34
python-test/poll.py View File

@@ -27,53 +27,96 @@ async def cache_services_characteristics(device, cb = None):
27 27
     global characteristicf, characteristic10
28 28
     global characteristic13, characteristic14
29 29
 
30
-    service3 = await device.service(serviceUuidVolcano3)
30
+    service3 = None
31
+    service4 = None
32
+    characteristic1 = None
33
+    characteristic3 = None
34
+    characteristicd = None
35
+    characteristicc = None
36
+    characteristicf = None
37
+    characteristic10 = None
38
+    characteristic13 = None
39
+    characteristic14 = None
40
+
41
+    val = 0
42
+
43
+    service3 = await device.service(serviceUuidVolcano3, 100)
31 44
     if cb != None:
32
-        await cb(0.1)
45
+        val += 1
46
+        await cb(val)
33 47
 
34
-    service4 = await device.service(serviceUuidVolcano4)
35
-    if cb != None:
36
-        await cb(0.2)
37
-
38
-    uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
39
-    characteristic1 = await service4.characteristic(uuid1)
40
-    if cb != None:
41
-        await cb(0.3)
48
+    uuidc = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
49
+    uuidd = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
50
+    async for c in service3.characteristics(None, 100):
51
+        if c.uuid == uuidc:
52
+            characteristicc = c
53
+            if cb != None:
54
+                val += 1
55
+                #await cb(val)
56
+
57
+        if c.uuid == uuidd:
58
+            characteristicd = c
59
+            if cb != None:
60
+                val += 1
61
+                #await cb(val)
42 62
 
43
-    uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
44
-    characteristic3 = await service4.characteristic(uuid3)
45 63
     if cb != None:
46
-        await cb(0.4)
64
+        await cb(val)
47 65
 
48
-    uuidd = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
49
-    characteristicd = await service3.characteristic(uuidd)
50
-    if cb != None:
51
-        await cb(0.5)
66
+    # -------------------------------------------------------------------------
52 67
 
53
-    uuidc = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
54
-    characteristicc = await service3.characteristic(uuidc)
68
+    service4 = await device.service(serviceUuidVolcano4, 100)
55 69
     if cb != None:
56
-        await cb(0.6)
70
+        val += 1
71
+        await cb(val)
57 72
 
73
+    uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
74
+    uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
58 75
     uuidf = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
59
-    characteristicf = await service4.characteristic(uuidf)
60
-    if cb != None:
61
-        await cb(0.7)
62
-
63 76
     uuid10 = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
64
-    characteristic10 = await service4.characteristic(uuid10)
65
-    if cb != None:
66
-        await cb(0.8)
67
-
68 77
     uuid13 = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
69
-    characteristic13 = await service4.characteristic(uuid13)
70
-    if cb != None:
71
-        await cb(0.9)
72
-
73 78
     uuid14 = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
74
-    characteristic14 = await service4.characteristic(uuid14)
79
+    async for c in service4.characteristics(None, 100):
80
+        if c.uuid == uuid1:
81
+            characteristic1 = c
82
+            if cb != None:
83
+                val += 1
84
+                #await cb(val)
85
+
86
+        if c.uuid == uuid3:
87
+            characteristic3 = c
88
+            if cb != None:
89
+                val += 1
90
+                #await cb(val)
91
+
92
+        if c.uuid == uuidf:
93
+            characteristicf = c
94
+            if cb != None:
95
+                val += 1
96
+                #await cb(val)
97
+
98
+        if c.uuid == uuid10:
99
+            characteristic10 = c
100
+            if cb != None:
101
+                val += 1
102
+                #await cb(val)
103
+
104
+        if c.uuid == uuid13:
105
+            characteristic13 = c
106
+            if cb != None:
107
+                val += 1
108
+                #await cb(val)
109
+
110
+        if c.uuid == uuid14:
111
+            characteristic14 = c
112
+            if cb != None:
113
+                val += 1
114
+                #await cb(val)
115
+
75 116
     if cb != None:
76
-        await cb(1.0)
117
+        await cb(val)
118
+
119
+    return (val >= 10)
77 120
 
78 121
 async def get_current_temp(device):
79 122
     val = await characteristic1.read()

+ 5
- 3
python-test/state_connect.py View File

@@ -14,7 +14,7 @@ class StateConnect:
14 14
 
15 15
     def enter(self, val = None):
16 16
         self.step = False
17
-        self.iteration = 0.0
17
+        self.iteration = 0
18 18
         self.done = False
19 19
         self.client = None
20 20
         self.connector = asyncio.create_task(self.connect(val))
@@ -43,7 +43,9 @@ class StateConnect:
43 43
             async with self.lock:
44 44
                 self.step = True
45 45
 
46
-            await cache_services_characteristics(client, self.progress)
46
+            success = await cache_services_characteristics(client, self.progress)
47
+            if not success:
48
+                raise RuntimeError("Error fetching characteristics")
47 49
         else:
48 50
             await d[0].disconnect()
49 51
             client = None
@@ -76,7 +78,7 @@ class StateConnect:
76 78
                     if self.step == False:
77 79
                         self.lcd.textC("Connecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
78 80
                     else:
79
-                        draw_graph(self.lcd, 0, int(self.iteration * 10), 10)
81
+                        draw_graph(self.lcd, 0, self.iteration, 10)
80 82
                         self.lcd.textC("Fetching parameters...", int(self.lcd.width / 2), int(self.lcd.height / 2) - 10, self.lcd.white, self.lcd.black)
81 83
 
82 84
         return -1

Loading…
Cancel
Save