#!/usr/bin/python3 ## ## This file is part of the sigrok-meter project. ## ## Copyright (C) 2013 Uwe Hermann ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## from multiprocessing import Process, Queue from gi.repository import Gtk, GObject from sigrok.core import Context, Driver, Device, Session, Packet, Log from sigrok.core import lowlevel as ll def init_and_run(queue): def datafeed_in(device, packet): if packet.type == Packet.ANALOG: data = packet.payload.data unit, unit_str = packet.payload.unit, "" if unit == ll.SR_UNIT_VOLT: unit_str = " V" elif unit == ll.SR_UNIT_OHM: unit_str = " Ohm" elif unit == ll.SR_UNIT_AMPERE: unit_str = " A" mqflags, mqflags_str = packet.payload.unit, "" if mqflags & ll.SR_MQFLAG_AC: mqflags_str = " AC" elif mqflags & ll.SR_MQFLAG_DC: mqflags_str = " DC" for i in range(packet.payload.num_samples): queue.put("%f%s%s" % (data[i], unit_str, mqflags_str)) # log = Log() # log.level = Log.SPEW context = Context() driver = context.drivers['voltcraft-vc820'] device = driver.scan()[0] device.limit_samples = 1000 session = Session(context) session.add_device(device) session.add_callback(datafeed_in) session.start() session.run() session.stop() class SigrokMeter: def __init__(self): self.builder = Gtk.Builder() self.builder.add_from_file("sigrok-meter.glade") self.builder.connect_signals(self) self.value_label = self.builder.get_object("value_label") self.win = self.builder.get_object("mainwindow") self.win.show_all() self.queue = Queue() GObject.timeout_add(100, self.update_label_if_needed) def update_label_if_needed(self): try: t = self.queue.get_nowait() self.value_label.set_text(t) except: pass GObject.timeout_add(100, self.update_label_if_needed) def on_quit(self, *args): Gtk.main_quit(*args) def on_about(self, action): about = self.builder.get_object("aboutdialog") sr_pkg = ll.sr_package_version_string_get() sr_lib = ll.sr_lib_version_string_get() s = "Using libsigrok %s (lib version %s)." % (sr_pkg, sr_lib) about.set_comments(s) about.run() about.hide() if __name__ == '__main__': s = SigrokMeter() process = Process(target=init_and_run, args=(s.queue,)) process.start() Gtk.main() process.terminate()