浏览代码

fix timing issues

Thomas Buck 8 个月前
父节点
当前提交
c75f5840b3

+ 55
- 36
python-test/poll.py 查看文件

@@ -9,6 +9,17 @@ from scan import ble_scan
9 9
 serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
10 10
 serviceUuidVolcano4 = bluetooth.UUID("10110000-5354-4f52-5a26-4249434b454c")
11 11
 
12
+service3 = None
13
+service4 = None
14
+characteristic1 = None
15
+characteristic3 = None
16
+characteristicd = None
17
+characteristicc = None
18
+characteristicf = None
19
+characteristic10 = None
20
+characteristic13 = None
21
+characteristic14 = None
22
+
12 23
 async def ble_conn(address):
13 24
     dev = await ble_scan(address)
14 25
 
@@ -16,23 +27,52 @@ async def ble_conn(address):
16 27
         address = dev.device.addr_hex()
17 28
         print("Connecting to '{}'...".format(address))
18 29
         connection = await dev.device.connect()
30
+        await cache_services_characteristics(connection)
19 31
         return connection
20 32
 
21 33
     return None
22 34
 
35
+async def cache_services_characteristics(device):
36
+    global service3, service4
37
+    global characteristic1, characteristic3
38
+    global characteristicd, characteristicc
39
+    global characteristicf, characteristic10
40
+    global characteristic13, characteristic14
41
+
42
+    service3 = await device.service(serviceUuidVolcano3)
43
+    service4 = await device.service(serviceUuidVolcano4)
44
+
45
+    uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
46
+    characteristic1 = await service4.characteristic(uuid1)
47
+
48
+    uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
49
+    characteristic3 = await service4.characteristic(uuid3)
50
+
51
+    uuidd = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
52
+    characteristicd = await service3.characteristic(uuidd)
53
+
54
+    uuidc = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
55
+    characteristicc = await service3.characteristic(uuidc)
56
+
57
+    uuidf = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
58
+    characteristicf = await service4.characteristic(uuidf)
59
+
60
+    uuid10 = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
61
+    characteristic10 = await service4.characteristic(uuid10)
62
+
63
+    uuid13 = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
64
+    characteristic13 = await service4.characteristic(uuid13)
65
+
66
+    uuid14 = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
67
+    characteristic14 = await service4.characteristic(uuid14)
68
+
23 69
 async def get_current_temp(device):
24
-    service = await device.service(serviceUuidVolcano4)
25
-    uuid = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
26
-    characteristic = await service.characteristic(uuid)
27
-    val = await characteristic.read()
70
+    val = await characteristic1.read()
28 71
     num = int.from_bytes(val, "little")
29 72
     return num / 10.0
30 73
 
31 74
 async def get_target_temp(device):
32
-    service = await device.service(serviceUuidVolcano4)
33
-    uuid = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
34
-    characteristic = await service.characteristic(uuid)
35
-    val = await characteristic.read()
75
+    val = await characteristic3.read()
36 76
     num = int.from_bytes(val, "little")
37 77
     return num / 10.0
38 78
 
@@ -41,10 +81,7 @@ async def set_target_temp(device, temp):
41 81
     while attempts > 0:
42 82
         val = int(temp * 10.0)
43 83
         d = val.to_bytes(4, "little")
44
-        service = await device.service(serviceUuidVolcano4)
45
-        uuid = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
46
-        characteristic = await service.characteristic(uuid)
47
-        await characteristic.write(d)
84
+        await characteristic3.write(d)
48 85
 
49 86
         attempts -= 1
50 87
 
@@ -54,18 +91,12 @@ async def set_target_temp(device, temp):
54 91
     raise RuntimeError("Could not set target temperature")
55 92
 
56 93
 async def get_unit_is_fahrenheit(device):
57
-    service = await device.service(serviceUuidVolcano3)
58
-    uuid = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
59
-    characteristic = await service.characteristic(uuid)
60
-    val = await characteristic.read()
94
+    val = await characteristicd.read()
61 95
     num = int.from_bytes(val, "little")
62 96
     return (num & 0x200) != 0
63 97
 
64 98
 async def get_state(device):
65
-    service = await device.service(serviceUuidVolcano3)
66
-    uuid = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
67
-    characteristic = await service.characteristic(uuid)
68
-    val = await characteristic.read()
99
+    val = await characteristicc.read()
69 100
     num = int.from_bytes(val, "little")
70 101
     heater = (num & 0x0020) != 0
71 102
     pump = (num & 0x2000) != 0
@@ -74,26 +105,14 @@ async def get_state(device):
74 105
 async def set_state(device, state):
75 106
     heater, pump = state
76 107
     if heater == True:
77
-        service = await device.service(serviceUuidVolcano4)
78
-        uuid = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
79
-        characteristic = await service.characteristic(uuid)
80
-        await characteristic.write(int(0).to_bytes(1, "little"))
108
+        await characteristicf.write(int(0).to_bytes(1, "little"))
81 109
     elif heater == False:
82
-        service = await device.service(serviceUuidVolcano4)
83
-        uuid = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
84
-        characteristic = await service.characteristic(uuid)
85
-        await characteristic.write(int(0).to_bytes(1, "little"))
110
+        await characteristic10.write(int(0).to_bytes(1, "little"))
86 111
 
87 112
     if pump == True:
88
-        service = await device.service(serviceUuidVolcano4)
89
-        uuid = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
90
-        characteristic = await service.characteristic(uuid)
91
-        await characteristic.write(int(0).to_bytes(1, "little"))
113
+        await characteristic13.write(int(0).to_bytes(1, "little"))
92 114
     elif pump == False:
93
-        service = await device.service(serviceUuidVolcano4)
94
-        uuid = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
95
-        characteristic = await service.characteristic(uuid)
96
-        await characteristic.write(int(0).to_bytes(1, "little"))
115
+        await characteristic14.write(int(0).to_bytes(1, "little"))
97 116
 
98 117
 if __name__ == "__main__":
99 118
     async def test_poll(device):

+ 10
- 7
python-test/state_connect.py 查看文件

@@ -1,6 +1,7 @@
1 1
 #!/usr/bin/env python
2 2
 
3 3
 import uasyncio as asyncio
4
+from poll import cache_services_characteristics
4 5
 
5 6
 class StateConnect:
6 7
     def __init__(self, lcd, state):
@@ -10,6 +11,7 @@ class StateConnect:
10 11
         self.lock = asyncio.Lock()
11 12
 
12 13
     def enter(self, val = None):
14
+        self.step = False
13 15
         self.done = False
14 16
         self.client = None
15 17
         self.connector = asyncio.create_task(self.connect(val))
@@ -28,6 +30,9 @@ class StateConnect:
28 30
 
29 31
         if self.state:
30 32
             client = await d.device.connect()
33
+            async with self.lock:
34
+                self.step = True
35
+            await cache_services_characteristics(client)
31 36
         else:
32 37
             await d[0].disconnect()
33 38
             client = None
@@ -42,11 +47,6 @@ class StateConnect:
42 47
         self.lcd.text("Volcano Remote Control App", 0, 0, self.lcd.green)
43 48
         self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
44 49
 
45
-        if self.state:
46
-            self.lcd.text("Connecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
47
-        else:
48
-            self.lcd.text("Disconnecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
49
-
50 50
         keys = self.lcd.buttons()
51 51
 
52 52
         if keys.once("y"):
@@ -64,9 +64,12 @@ class StateConnect:
64 64
                     return 0 # scan
65 65
             else:
66 66
                 if self.state == False:
67
-                    self.lcd.text("Disconnecting...", 0, 100, self.lcd.white)
67
+                    self.lcd.text("Disconnecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
68 68
                 else:
69
-                    self.lcd.text("Connecting...", 0, 100, self.lcd.white)
69
+                    if self.step == False:
70
+                        self.lcd.text("Connecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
71
+                    else:
72
+                        self.lcd.text("Fetching parameters...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
70 73
 
71 74
         self.lcd.show()
72 75
         return -1 # stay in this state

+ 2
- 2
python-test/state_heat.py 查看文件

@@ -58,9 +58,9 @@ class StateHeat:
58 58
                     return 6 # wait for temperature
59 59
             else:
60 60
                 if self.state == False:
61
-                    self.lcd.text("Turning heater off...", 0, 100, self.lcd.white)
61
+                    self.lcd.text("Turning heater off...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
62 62
                 else:
63
-                    self.lcd.text("Turning heater on...", 0, 100, self.lcd.white)
63
+                    self.lcd.text("Turning heater on...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
64 64
 
65 65
         self.lcd.show()
66 66
         return -1 # stay in this state

+ 2
- 2
python-test/state_pump.py 查看文件

@@ -62,9 +62,9 @@ class StatePump:
62 62
                 if now - self.start <= self.duration:
63 63
                     draw_graph(self.lcd, 0.0, now - self.start, self.duration)
64 64
                 else:
65
-                    self.lcd.text("Turning off pump...", 0, 100, self.lcd.white)
65
+                    self.lcd.text("Turning off pump...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
66 66
             else:
67
-                self.lcd.text("Turning on pump...", 0, 100, self.lcd.white)
67
+                self.lcd.text("Turning on pump...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
68 68
 
69 69
             if self.done:
70 70
                 if self.value[2] >= (len(workflow["steps"]) - 1):

+ 4
- 4
python-test/state_wait_temp.py 查看文件

@@ -5,15 +5,15 @@ from poll import set_target_temp, get_current_temp
5 5
 
6 6
 def draw_graph(lcd, min, val, max):
7 7
     if max == min:
8
-        lcd.text("{} -> {} -> {}".format(min, val, max), 0, 100, lcd.white)
8
+        lcd.text("{} -> {} -> {}".format(min, val, max), 0, int(lcd.height / 2) - 5, lcd.white)
9 9
         return
10 10
 
11 11
     w = lcd.width - 10
12 12
     ratio = (val - min) / (max - min)
13 13
     wfull = int(w * ratio)
14 14
     wempty = w - wfull
15
-    lcd.rect(4, 100, wfull + 1, 50, lcd.green, True)
16
-    lcd.rect(4 + wfull, 100, wempty + 2, 50, lcd.green, False)
15
+    lcd.rect(4, int(lcd.height / 2) - 5, wfull + 1, 50, lcd.green, True)
16
+    lcd.rect(4 + wfull, int(lcd.height / 2) - 5, wempty + 2, 50, lcd.green, False)
17 17
     lcd.text("{}".format(val), int(lcd.width / 2), 125, lcd.white)
18 18
 
19 19
 class StateWaitTemp:
@@ -74,7 +74,7 @@ class StateWaitTemp:
74 74
 
75 75
         async with self.lock:
76 76
             if self.temp == 0.0:
77
-                self.lcd.text("Setting temperature...", 0, 100, self.lcd.white)
77
+                self.lcd.text("Setting temperature...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
78 78
             else:
79 79
                 draw_graph(self.lcd, self.min, self.temp, self.max)
80 80
 

+ 1
- 1
python-test/state_wait_time.py 查看文件

@@ -12,7 +12,7 @@ class StateWaitTime:
12 12
 
13 13
         device, workflow, index = self.value
14 14
         self.start = time.time()
15
-        self.end = self.start + workflow["steps"][index][1]
15
+        self.end = self.start + int(workflow["steps"][index][1])
16 16
 
17 17
     def exit(self):
18 18
         return (self.value[0], self.value[1], self.value[2])

+ 7
- 7
python-test/workflows.py 查看文件

@@ -5,19 +5,19 @@ workflows = [
5 5
         "name": "Hardcore",
6 6
         "author": "xythobuz",
7 7
         "steps": [
8
-            (190.0, 15.0, 5.0 - 4),
9
-            (205.0, 10.0, 20.0 - 4),
10
-            (220.0, 10.0, 20.0 - 4),
8
+            (190.0, 15.0, 5.0),
9
+            (205.0, 10.0, 20.0),
10
+            (220.0, 10.0, 20.0),
11 11
         ],
12 12
     },
13 13
     {
14 14
         "name": "Vorbi",
15 15
         "author": "Rinor",
16 16
         "steps": [
17
-            (176.0, 10.0, 6.0 - 4),
18
-            (187.0, 5.0, 10.0 -3),
19
-            (204.0, 3.0, 10.0 - 3),
20
-            (217.0, 5.0, 10.0 - 3),
17
+            (176.0, 10.0, 6.0),
18
+            (187.0, 5.0, 10.0),
19
+            (204.0, 3.0, 10.0),
20
+            (217.0, 5.0, 10.0),
21 21
         ],
22 22
     },
23 23
 ]

正在加载...
取消
保存