analgauge.py

Thu, 16 Feb 2017 16:24:25 +0100

author
Malte Di Donato <mdd@neo-soft.org>
date
Thu, 16 Feb 2017 16:24:25 +0100
changeset 4
99a5439f349f
parent 1
31032bc7b0e6
child 6
b84252fe69c6
permissions
-rw-r--r--

calibration tests added

1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
1 import usb.core
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
2 from psutil import cpu_percent
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
3 from time import sleep
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
4 from random import random
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
5 from sys import exit
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
6
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
7 #VALUE_MIN = 0x00 # TODO
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
8 VALUE_MAX = 0x7B
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
9
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
10 VENDOR_ID = 0x16c0
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
11 PRODUCT_ID = 0x05df
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
12
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
13 class AnalGauge:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
14 def __init__(self):
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
15 self.dev = usb.core.find(
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
16 idVendor = VENDOR_ID,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
17 idProduct = PRODUCT_ID
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
18 )
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
19 if not self.dev:
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
20 print "AnalGauge Device not found!"
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
21 exit(1)
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
22
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
23 def update(self, d):
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
24 try:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
25 data = [int(d)]
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
26 result = self.dev.ctrl_transfer(
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
27 0x21, 0x9,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
28 wValue = 0x200, wIndex = 0x00,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
29 data_or_wLength = data
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
30 )
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
31 except usb.core.USBError, e:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
32 self.dev.detach_kernel_driver(0)
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
33 print(e)
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
34
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
35 if __name__ == "__main__":
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
36 g = AnalGauge()
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
37
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
38 #g.update(VALUE_MAX)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
39 #exit()
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
40
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
41 #while True:
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
42 # g.update(random() * VALUE_MAX)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
43 # sleep(0.5)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
44
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
45 while True:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
46 pct = cpu_percent(interval=1)
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
47 g.update((VALUE_MAX * pct)/100)

mercurial