analgauge.py

Fri, 17 Feb 2017 13:26:39 +0100

author
mbayer
date
Fri, 17 Feb 2017 13:26:39 +0100
changeset 6
b84252fe69c6
parent 4
99a5439f349f
permissions
-rw-r--r--

changes to run client on windows platform too via libusb dev

6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
1 """
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
2 Simple CPU load Display
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
3
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
4 On Linux simply run with
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
5 python analgauge.py &
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
7 On Windows:
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
8 Make sure to install libusb-win32-devel-filter-1.2.6.0.exe
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
9 from here: https://sourceforge.net/projects/libusb-win32/files/libusb-win32-releases/1.2.6.0/
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
10 Then assign a filter to the connected device matching VENDOR_ID and PRODUCT_ID
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
11
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
12 pythonw.exe analgauge.py
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
13 """
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
14 import usb.core
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
15 from psutil import cpu_percent
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
16 from time import sleep
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
17 from random import random
6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
18 from sys import exit, platform
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
19
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
20 #VALUE_MIN = 0x00 # TODO
6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
21 VALUE_MAX = 0x78
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 VENDOR_ID = 0x16c0
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
24 PRODUCT_ID = 0x05df
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
25
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
26 class AnalGauge:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
27 def __init__(self):
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
28 self.dev = usb.core.find(
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
29 idVendor = VENDOR_ID,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
30 idProduct = PRODUCT_ID
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
31 )
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
32 if not self.dev:
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
33 print "AnalGauge Device not found!"
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
34 exit(1)
6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
35 self.dev.set_configuration()
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
36
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
37 def update(self, d):
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
38 try:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
39 data = [int(d)]
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
40 result = self.dev.ctrl_transfer(
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
41 0x21, 0x9,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
42 wValue = 0x200, wIndex = 0x00,
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
43 data_or_wLength = data
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
44 )
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
45 except usb.core.USBError, e:
6
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
46 if platform == "linux":
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
47 self.dev.detach_kernel_driver(0)
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
48 else:
b84252fe69c6 changes to run client on windows platform too via libusb dev
mbayer
parents: 4
diff changeset
49 exit(1)
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
50 print(e)
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
51
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
52 if __name__ == "__main__":
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
53 g = AnalGauge()
4
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
54
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
55 #g.update(VALUE_MAX)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
56 #exit()
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
57
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
58 #while True:
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
59 # g.update(random() * VALUE_MAX)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
60 # sleep(0.5)
99a5439f349f calibration tests added
Malte Di Donato <mdd@neo-soft.org>
parents: 1
diff changeset
61
1
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
62 while True:
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
63 pct = cpu_percent(interval=1)
31032bc7b0e6 added firmware and python client
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
64 g.update((VALUE_MAX * pct)/100)

mercurial