]> sigrok.org Git - sigrok-meter.git/blob - sigrok-meter
Use python 2.
[sigrok-meter.git] / sigrok-meter
1 #!/usr/bin/env python
2
3 ##
4 ## This file is part of the sigrok-meter project.
5 ##
6 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 ##
22
23 from multiprocessing import Process, Queue
24 from gi.repository import Gtk, GObject
25 from sigrok.core import *
26 from sigrok.core import lowlevel as ll
27
28 def init_and_run(queue):
29     def datafeed_in(device, packet):
30         if packet.type is PacketType.ANALOG:
31             data = packet.payload.data
32             unit, unit_str = packet.payload.unit, ""
33             if unit is Unit.VOLT:
34                 unit_str = " V"
35             elif unit is Unit.OHM:
36                 unit_str = " Ohm"
37             elif unit is Unit.AMPERE:
38                 unit_str = " A"
39             mqflags, mqflags_str = packet.payload.mqflags, ""
40             if QuantityFlag.AC in mqflags:
41                 mqflags_str = " AC"
42             elif QuantityFlag.DC in mqflags:
43                 mqflags_str = " DC"
44             for i in range(packet.payload.num_samples):
45                 dev = "%s %s" % (device.vendor, device.model)
46                 val = "%f%s%s" % (data[i], unit_str, mqflags_str)
47                 queue.put((dev, val))
48
49     # log = Log()
50     # log.level = LogLevel.SPEW
51     context = Context()
52     drivers_to_use = ['tecpel-dmm-8061-ser']
53     drivers = [context.drivers[d] for d in drivers_to_use]
54     devices = [d.scan(conn="/dev/ttyUSB0")[0] for d in drivers]
55     # devices = [d.scan()[0] for d in drivers]
56     # for dev in devices:
57     #     dev.limit_samples = 1000
58     session = Session(context)
59     for dev in devices:
60         session.open_device(dev)
61         session.add_device(dev)
62     session.add_callback(datafeed_in)
63     session.start()
64     session.run()
65     session.stop()
66
67 class SigrokMeter:
68     def __init__(self):
69         self.builder = Gtk.Builder()
70         self.builder.add_from_file("sigrok-meter.glade")
71         self.builder.connect_signals(self)
72         self.value_label = self.builder.get_object("value_label")
73         self.value_label2 = self.builder.get_object("value_label2")
74         self.win = self.builder.get_object("mainwindow")
75         self.win.show_all()
76         self.queue = Queue()
77         GObject.timeout_add(100, self.update_label_if_needed)
78
79     def update_label_if_needed(self):
80         try:
81             t = self.queue.get_nowait()
82             l = self.value_label if t[0] != "Victor" else self.value_label2
83             l.set_text("%s: %s" % (t[0], t[1]))
84         except:
85             pass
86         GObject.timeout_add(100, self.update_label_if_needed)
87
88     def on_quit(self, *args):
89         Gtk.main_quit(*args)
90
91     def on_about(self, action):
92         about = self.builder.get_object("aboutdialog")
93         sr_pkg = ll.sr_package_version_string_get()
94         sr_lib = ll.sr_lib_version_string_get()
95         s = "Using libsigrok %s (lib version %s)." % (sr_pkg, sr_lib)
96         about.set_comments(s)
97         about.run()
98         about.hide()
99
100 if __name__ == '__main__':
101     s = SigrokMeter()
102     process = Process(target=init_and_run, args=(s.queue,))
103     process.start()
104     Gtk.main()
105     process.terminate()
106