Browse Source

timeout for scan results. more useful error printing.

Thomas Buck 8 months ago
parent
commit
5351ba3f77
3 changed files with 35 additions and 3 deletions
  1. 17
    0
      python-test/lcd.py
  2. 10
    2
      python-test/state_scan.py
  3. 8
    1
      python-test/states.py

+ 17
- 0
python-test/lcd.py View File

@@ -246,3 +246,20 @@ class LCD(framebuf.FrameBuffer):
246 246
 
247 247
     def textC(self, s, x, y, c):
248 248
         self.text(s, x - int(len(s) * 8 / 2), y - 5, c)
249
+
250
+    def textLine(self, s, c, off = 0):
251
+        charsPerLine = int(self.width / 8)
252
+        lines = list(s[0+i:charsPerLine+i] for i in range(0, len(s), charsPerLine))
253
+        n = 0
254
+        for i, l in enumerate(lines):
255
+            self.text(l, 0, (i + off) * 10, c)
256
+            n += 1
257
+            if i >= (self.height / 10):
258
+                break
259
+        return n
260
+
261
+    def textBlock(self, s, c):
262
+        lines = s.split("\n")
263
+        off = 0
264
+        for l in lines:
265
+            off += self.textLine(l, c, off)

+ 10
- 2
python-test/state_scan.py View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 import uasyncio as asyncio
4 4
 from scan import ble_scan
5
+import time
5 6
 
6 7
 class StateScan:
7 8
     def __init__(self, lcd):
@@ -30,7 +31,7 @@ class StateScan:
30 31
                 name = n.name()
31 32
                 mac = n.device.addr_hex()
32 33
                 rssi = n.rssi
33
-                value = [name, mac, rssi]
34
+                value = [name, mac, rssi, time.time()]
34 35
 
35 36
                 async with self.lock:
36 37
                     found = False
@@ -39,6 +40,7 @@ class StateScan:
39 40
                             found = True
40 41
                             self.results[i][0] = name
41 42
                             self.results[i][2] = rssi
43
+                            self.results[i][3] = time.time()
42 44
                             break
43 45
 
44 46
                     if found == False:
@@ -46,7 +48,7 @@ class StateScan:
46 48
 
47 49
     def draw_list(self):
48 50
         for i, d in enumerate(self.results):
49
-            name, mac, rssi = self.results[i]
51
+            name, mac, rssi, timeout = self.results[i]
50 52
             s1 = "{}: {}".format(i + 1, name)
51 53
             s2 = "[{}] {}".format(mac, rssi)
52 54
 
@@ -89,6 +91,12 @@ class StateScan:
89 91
                 elif self.current < (len(self.results) - 1):
90 92
                     self.current += 1
91 93
 
94
+            # remove entries after timeout
95
+            self.results = [x for x in self.results if (time.time() - x[3]) < 10.0]
96
+            if self.current != None:
97
+                if self.current >= len(self.results):
98
+                    self.current = len(self.results) - 1
99
+
92 100
             self.draw_list()
93 101
 
94 102
         return -1

+ 8
- 1
python-test/states.py View File

@@ -90,7 +90,14 @@ lcd.brightness(1.0)
90 90
 try:
91 91
     state_machine(lcd)
92 92
 except Exception as e:
93
+    import io
94
+    import sys
95
+    os = io.StringIO()
96
+    sys.print_exception(e, os)
97
+    s = os.getvalue()
98
+    os.close()
99
+
93 100
     lcd.fill(lcd.black)
94
-    lcd.textC(str(e), int(lcd.width / 2), int(lcd.height / 2), lcd.white)
101
+    lcd.textBlock(s, lcd.white)
95 102
     lcd.show()
96 103
     raise e

Loading…
Cancel
Save