Browse Source

fix timing issues

Thomas Buck 1 year ago
parent
commit
c75f5840b3

+ 55
- 36
python-test/poll.py View File

9
 serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
9
 serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
10
 serviceUuidVolcano4 = bluetooth.UUID("10110000-5354-4f52-5a26-4249434b454c")
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
 async def ble_conn(address):
23
 async def ble_conn(address):
13
     dev = await ble_scan(address)
24
     dev = await ble_scan(address)
14
 
25
 
16
         address = dev.device.addr_hex()
27
         address = dev.device.addr_hex()
17
         print("Connecting to '{}'...".format(address))
28
         print("Connecting to '{}'...".format(address))
18
         connection = await dev.device.connect()
29
         connection = await dev.device.connect()
30
+        await cache_services_characteristics(connection)
19
         return connection
31
         return connection
20
 
32
 
21
     return None
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
 async def get_current_temp(device):
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
     num = int.from_bytes(val, "little")
71
     num = int.from_bytes(val, "little")
29
     return num / 10.0
72
     return num / 10.0
30
 
73
 
31
 async def get_target_temp(device):
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
     num = int.from_bytes(val, "little")
76
     num = int.from_bytes(val, "little")
37
     return num / 10.0
77
     return num / 10.0
38
 
78
 
41
     while attempts > 0:
81
     while attempts > 0:
42
         val = int(temp * 10.0)
82
         val = int(temp * 10.0)
43
         d = val.to_bytes(4, "little")
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
         attempts -= 1
86
         attempts -= 1
50
 
87
 
54
     raise RuntimeError("Could not set target temperature")
91
     raise RuntimeError("Could not set target temperature")
55
 
92
 
56
 async def get_unit_is_fahrenheit(device):
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
     num = int.from_bytes(val, "little")
95
     num = int.from_bytes(val, "little")
62
     return (num & 0x200) != 0
96
     return (num & 0x200) != 0
63
 
97
 
64
 async def get_state(device):
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
     num = int.from_bytes(val, "little")
100
     num = int.from_bytes(val, "little")
70
     heater = (num & 0x0020) != 0
101
     heater = (num & 0x0020) != 0
71
     pump = (num & 0x2000) != 0
102
     pump = (num & 0x2000) != 0
74
 async def set_state(device, state):
105
 async def set_state(device, state):
75
     heater, pump = state
106
     heater, pump = state
76
     if heater == True:
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
     elif heater == False:
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
     if pump == True:
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
     elif pump == False:
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
 if __name__ == "__main__":
117
 if __name__ == "__main__":
99
     async def test_poll(device):
118
     async def test_poll(device):

+ 10
- 7
python-test/state_connect.py View File

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

+ 2
- 2
python-test/state_heat.py View File

58
                     return 6 # wait for temperature
58
                     return 6 # wait for temperature
59
             else:
59
             else:
60
                 if self.state == False:
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
                 else:
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
         self.lcd.show()
65
         self.lcd.show()
66
         return -1 # stay in this state
66
         return -1 # stay in this state

+ 2
- 2
python-test/state_pump.py View File

62
                 if now - self.start <= self.duration:
62
                 if now - self.start <= self.duration:
63
                     draw_graph(self.lcd, 0.0, now - self.start, self.duration)
63
                     draw_graph(self.lcd, 0.0, now - self.start, self.duration)
64
                 else:
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
             else:
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
             if self.done:
69
             if self.done:
70
                 if self.value[2] >= (len(workflow["steps"]) - 1):
70
                 if self.value[2] >= (len(workflow["steps"]) - 1):

+ 4
- 4
python-test/state_wait_temp.py View File

5
 
5
 
6
 def draw_graph(lcd, min, val, max):
6
 def draw_graph(lcd, min, val, max):
7
     if max == min:
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
         return
9
         return
10
 
10
 
11
     w = lcd.width - 10
11
     w = lcd.width - 10
12
     ratio = (val - min) / (max - min)
12
     ratio = (val - min) / (max - min)
13
     wfull = int(w * ratio)
13
     wfull = int(w * ratio)
14
     wempty = w - wfull
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
     lcd.text("{}".format(val), int(lcd.width / 2), 125, lcd.white)
17
     lcd.text("{}".format(val), int(lcd.width / 2), 125, lcd.white)
18
 
18
 
19
 class StateWaitTemp:
19
 class StateWaitTemp:
74
 
74
 
75
         async with self.lock:
75
         async with self.lock:
76
             if self.temp == 0.0:
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
             else:
78
             else:
79
                 draw_graph(self.lcd, self.min, self.temp, self.max)
79
                 draw_graph(self.lcd, self.min, self.temp, self.max)
80
 
80
 

+ 1
- 1
python-test/state_wait_time.py View File

12
 
12
 
13
         device, workflow, index = self.value
13
         device, workflow, index = self.value
14
         self.start = time.time()
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
     def exit(self):
17
     def exit(self):
18
         return (self.value[0], self.value[1], self.value[2])
18
         return (self.value[0], self.value[1], self.value[2])

+ 7
- 7
python-test/workflows.py View File

5
         "name": "Hardcore",
5
         "name": "Hardcore",
6
         "author": "xythobuz",
6
         "author": "xythobuz",
7
         "steps": [
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
         "name": "Vorbi",
14
         "name": "Vorbi",
15
         "author": "Rinor",
15
         "author": "Rinor",
16
         "steps": [
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
 ]

Loading…
Cancel
Save