-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
92 lines (74 loc) · 2.27 KB
/
utils.py
File metadata and controls
92 lines (74 loc) · 2.27 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
This file contains utility methods that are not specific to any one class, such as a stopwatch, and translate
Additionally, this is subsuming mode.py, restart tools will be migrated here.
"""
def unpack_val_dict(d):
if not isinstance(d, dict):
return d
for key in d.keys():
# print(key)
d[key] = unpack_val_dict(d[key]["value"])
return d
config = {}
with open("config.json", 'r') as cfg:
import json
config = json.load(cfg)
import microcontroller as mc
from binascii import hexlify
uidstr = str(hexlify(mc.cpu.uid))[2:-1]
import os
if(config['global_info']["HW_UID"]["value"] != uidstr)or("0x"+uidstr not in os.listdir()):
mc.reset()
del(mc)
del(hexlify)
del(os)
unpack_val_dict(config['global_info'])
unpack_val_dict(config['connections'][0])
config.update(config.pop('global_info'))
config.update(config.pop('connections')[0])
# print(config)
import board
import digitalio
pins = {
"led_green" : digitalio.DigitalInOut( board.LED_GREEN ),
"led_blue" : digitalio.DigitalInOut( board.LED_BLUE),
"led_red" : digitalio.DigitalInOut( board.LED_RED),
}
for pin in pins.values():
pin.direction = digitalio.Direction.OUTPUT
pin.value = True
import asyncio
import microcontroller as mc
from ulab import numpy as np
def get_mag(arr):
try:
return np.linalg.norm(arr)
except:
sum = 0.0
for i in arr:
sum += i*i
return np.sqrt(sum)
def comp_writable():
mc.nvm[0] = True
mc.reset()
def self_writable():
mc.nvm[0] = False
mc.reset()
async def stopwatch(n : float, ev : asyncio.Event = None):
if(n > 0):
await asyncio.sleep(n)
if(ev is not None):
ev.set()
# print("Stopwatch, setting event")
def translate(x_min, x_max, y_min, y_max, input):
if input < x_min:
# print(f"Input ({input}) was less than x_min({x_min})")
return y_min
if input > x_max:
# print(f"Input ({input}) was greater than x_max({x_max})")
return y_max
x_span = x_max - x_min
y_span = y_max - y_min
scaled = (y_span / x_span) * (input - x_min)
shifted = scaled + y_min
return shifted