-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
44 lines (36 loc) · 1.13 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from machine import Pin, I2C
import utime
import errno
def setup_I2C_bus():
i2c = I2C(0,
scl=Pin(1, Pin.PULL_UP),
sda=Pin(0, Pin.PULL_UP),
freq=100000)
utime.sleep_ms(100)
print(i2c.scan())
return i2c
# To be used to repeatedly try I2C operations that can fail with an OSError [errno 5] IO error#
#def try_until_runs(timeout):
def try_until_runs(func):
def wrapper_try_until_runs(*args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except OSError as oserr:
print(oserr)
continue
except Exception as e:
raise e
return wrapper_try_until_runs
def set_timeout(seconds):
def timeout_tryer(func):
def timeout_tryer_wrapper(*args, **kwargs):
start_time = utime.mktime(utime.gmtime())
while (utime.mktime(utime.gmtime()) - start_time) < seconds:
return func(*args, **kwargs)
raise TimeoutError
return timeout_tryer_wrapper
return timeout_tryer
# return try_until_runs_dec
SUCCESS = 0
FAILURE = 1