-
Notifications
You must be signed in to change notification settings - Fork 5
/
provision.py
executable file
·67 lines (54 loc) · 1.83 KB
/
provision.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
#!/usr/bin/env python3
"""
Provision a new Minnow device with a unique incrementing serial number and records the device and serial number in a file
Writes the EEPROM './pyftdi-minnow.ini' file with the device serial number
"""
import os
import sys
import datetime
import logging
from pyftdi.ftdi import Ftdi
from pyftdi.bin import ftconf
logger = logging.getLogger('provision')
logging.basicConfig(level=logging.INFO)
def select_device(devices):
"""
Returns first device or allows user input to select index to use
"""
if not devices or len(devices) == 0:
raise RuntimeError("No devices found")
print(f"Found devices:")
for i, d in enumerate(devices):
print(f"[{i}]: {d}")
if len(devices) > 1:
# let user select device to use
return devices[int(input("Enter device index to use: "))]
else:
return devices[0]
def get_first_device():
"""Return the first FTDI device found"""
devices = Ftdi.list_devices_urls()
return select_device(devices)
def get_device_num(fn: str = 'devices.txt'):
# create file if not exists
if not os.path.isfile(fn):
logger.warning(f"Creating {fn}")
open(fn, 'w').close()
with open(fn, 'r') as f:
lines = f.readlines()
return len(lines)
VERSION = 0x1
DEVICE = get_first_device()[0]
DT = datetime.datetime.now()
YEAR = DT.year % 100
FILE = f'./devices_{YEAR:02d}{VERSION:01x}.txt'
NUM = get_device_num(FILE)
PRODUCT = 'Minnow USBC DAM 230X'
MANUFACTURER = 'JBR Engineering'
SERIAL = f'{YEAR:02d}{VERSION:01x}{NUM:04d}'
logger.info(f"Configuring device: {DEVICE}, serial: {SERIAL}")
with open(FILE, 'a') as f:
f.write(f'{DT.isoformat()},{DEVICE},{SERIAL}\n')
# args for ftconf
sys.argv.extend(['-d', DEVICE, '-p', PRODUCT, '-m', MANUFACTURER, '-s', SERIAL, '-c', 'cbus_func_3=GPIO', '-u'])
ftconf.main()