]> sigrok.org Git - sigrok-meter.git/blob - sigrok-meter
fcf5eb4f10c2202c778c44f3ff06817f780fee11
[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
27 def init_and_run(queue):
28     def datafeed_in(device, packet):
29         if packet.type == PacketType.ANALOG:
30             data = packet.payload.data
31             unit, unit_str = packet.payload.unit, ""
32             if unit == Unit.VOLT:
33                 unit_str = " V"
34             elif unit == Unit.OHM:
35                 unit_str = " Ohm"
36             elif unit == Unit.AMPERE:
37                 unit_str = " A"
38             mqflags, mqflags_str = packet.payload.mq_flags, ""
39             if QuantityFlag.AC in mqflags:
40                 mqflags_str = " AC"
41             elif QuantityFlag.DC in mqflags:
42                 mqflags_str = " DC"
43             for i in range(packet.payload.num_samples):
44                 dev = "%s %s" % (device.vendor, device.model)
45                 val = "%f%s%s" % (data[0][i], unit_str, mqflags_str)
46                 queue.put((dev, val))
47
48     context = Context_create()
49     context.log_level = LogLevel.SPEW
50     drivers_to_use = ['tecpel-dmm-8061-ser']
51     drivers = [context.drivers[d] for d in drivers_to_use]
52     devices = [d.scan(conn="/dev/ttyUSB0")[0] for d in drivers]
53     # devices = [d.scan()[0] for d in drivers]
54     # for dev in devices:
55     #     dev.limit_samples = 1000
56     session = context.create_session()
57     for dev in devices:
58         session.add_device(dev)
59         dev.open()
60     session.add_datafeed_callback(datafeed_in)
61     session.start()
62     session.run()
63     session.stop()
64
65 class SigrokMeter:
66     def __init__(self):
67         self.builder = Gtk.Builder()
68         self.builder.add_from_file("sigrok-meter.glade")
69         self.builder.connect_signals(self)
70         self.value_label = self.builder.get_object("value_label")
71         self.value_label2 = self.builder.get_object("value_label2")
72         self.win = self.builder.get_object("mainwindow")
73         self.win.show_all()
74         self.queue = Queue()
75         GObject.timeout_add(100, self.update_label_if_needed)
76
77     def update_label_if_needed(self):
78         try:
79             t = self.queue.get_nowait()
80             l = self.value_label if t[0] != "Victor" else self.value_label2
81             l.set_text("%s: %s" % (t[0], t[1]))
82         except:
83             pass
84         GObject.timeout_add(100, self.update_label_if_needed)
85
86     def on_quit(self, *args):
87         Gtk.main_quit(*args)
88
89     def on_about(self, action):
90         about = self.builder.get_object("aboutdialog")
91         context = Context_create()
92         sr_pkg = context.package_version
93         sr_lib = context.lib_version
94         s = "Using libsigrok %s (lib version %s)." % (sr_pkg, sr_lib)
95         about.set_comments(s)
96         about.run()
97         about.hide()
98
99 if __name__ == '__main__':
100     s = SigrokMeter()
101     process = Process(target=init_and_run, args=(s.queue,))
102     process.start()
103     Gtk.main()
104     process.terminate()
105