-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory_test.py
executable file
·70 lines (55 loc) · 1.9 KB
/
factory_test.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
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
from serial import Serial
from enum import Enum
from time import sleep
import struct
# top 3 bits are command, bottom 5 are argument
ARGUMENT_MASK = 0x1F
COMMAND_MASK = 0xE0
COMMAND_POS = 5
class Command(Enum):
READ_POT = 0
READ_CV = 1
READ_GATE = 2
GENERATE_TEST_SIGNALS = 3
CALIBRATE = 4
READ_NORMALIZATION = 5
FORCE_DAC_CODE = 6
WRITE_CALIBRATION_DATA_NIBBLE = 7 # done in nibbles (4 bit chunks)
class TestingRequest:
def __init__(self, command: Command, argument: int):
assert 0 <= argument < 32
self.command = command
self.argument = argument
# 1 byte output
def packed(self) -> bytes:
output = self.command.value << COMMAND_POS
output |= self.argument & ARGUMENT_MASK
return output.to_bytes(1)
class CalibrationData:
def __init__(self, scale: float, offset: float):
self.scale = scale
self.offset = offset
# 4 byte output
def packed(self) -> bytes:
# two uint16_t (unsigned shorts) for a 4 byte (uint32_t) message
# scale is high short
# offset is low short
return struct.pack(">HH", int(self.scale), int(self.offset))
class DebugPort:
def __init__(self, port: str):
self.port = Serial(port, timeout=5)
def send(self, command: Command, argument: int):
self.port.write(TestingRequest(command, argument).packed())
def read(self) -> int:
result = self.port.read()
if (len(result) > 0):
return result[0]
else:
raise IOError("No answer from device.")
def write_calibration_data(self, scale: float, offset: float):
for byte in CalibrationData(scale, offset).packed():
# top nibble
self.send(Command.WRITE_CALIBRATION_DATA_NIBBLE, byte >> 4)
sleep(0.1)
# bottom nibble
self.send(Command.WRITE_CALIBRATION_DATA_NIBBLE, byte & 0xF)