forked from UniPiTechnology/evok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_adc_coef.py
32 lines (25 loc) · 1016 Bytes
/
set_adc_coef.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
"""
This is a simple tool to allow modifying the coefficient for analog-digital conversion
used in the evok's AnalogInput class. Use if your conversion does not match.
It converts the provided float to hex float and writes it to the specified memory.
See the evok.conf for adc coef starting address.
Make sure to restart the evok service.
Example: python set_adc_coef.py -c 5.56 -a 0x00
"""
#!/usr/bin/python
import smbus
import struct
import argparse
from time import sleep
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--coef', help='Desired coeficient', required=True)
parser.add_argument('-a', '--address', help='Starting hex address of coeficient, e.g. 0xf0', required=True)
args = vars(parser.parse_args())
bus = smbus.SMBus(1)
DEVICE_ADDRESS = 0x50
addr = int(args['address'], 16)
hexstr = hex(struct.unpack('<I', struct.pack('<f', float(args['coef'])))[0])[2:]
for i in [0, 2, 4, 6]:
bus.write_byte_data(DEVICE_ADDRESS, addr, int(hexstr[i:i + 2], 16))
addr += 1
sleep(0.05)