diff --git a/python/i2cdriver.py b/python/i2cdriver.py index f2e0314..9306fb7 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -343,19 +343,26 @@ def __repr__(self): self.scl, self.sda) - def capture_start(self, start = START, abyte = BYTE, stop = STOP): + def capture_start(self, idle=False, start = START, abyte = BYTE, stop = STOP): self.__ser_w([ord('c')]) def nstream(): - while 1: - for b in self.ser.read(256): - yield (b >> 4) & 0xf - yield b & 0xf + while True: + bb = self.ser.read(256) + if PYTHON2: + for b in bb: + yield (ord(b) >> 4) & 0xf + yield ord(b) & 0xf + else: + for b in bb: + yield (b >> 4) & 0xf + yield b & 0xf def parser(): starting = False rw = 0 for n in nstream(): if n == 0: - pass + if idle: + yield None elif n == 1: starting = True bits = [] diff --git a/python/samples/i2cgui.py b/python/samples/i2cgui.py index c4de7d4..93886a6 100644 --- a/python/samples/i2cgui.py +++ b/python/samples/i2cgui.py @@ -5,6 +5,7 @@ import sys import os import re +import csv import threading from functools import partial @@ -39,6 +40,21 @@ def ping_thr(win): wx.PostEvent(win, PingEvent()) time.sleep(1) +StopCapture = False + +def capture_thr(sd, log_csv): + global StopCapture + c = sd.capture_start(True) + with open(log_csv, 'w') as csvfile: + logcsv = csv.writer(csvfile) + for token in c(): + if token: + token.dump(logcsv, "csv") # write to CSV + if StopCapture: + break + StopCapture = False + sd.capture_stop() + class HexTextCtrl(wx.TextCtrl): def __init__(self, *args, **kwargs): super(HexTextCtrl, self).__init__(*args, **kwargs) @@ -52,6 +68,12 @@ def on_text(self, event): self.ChangeValue(value) self.SetSelection(*selection) +class MyDialog(wx.Dialog): + def __init__(self, parent, title): + super(MyDialog, self).__init__(parent, title = title, size = (250,150)) + panel = wx.Panel(self) + self.btn = wx.Button(panel, wx.ID_OK, label = "ok", size = (50,20), pos = (75,50)) + class Frame(wx.Frame): def __init__(self, preferred = None): @@ -76,6 +98,12 @@ def rpair(a, b): r.Add(b, 1, wx.RIGHT) return r + def epair(a, b): + r = wx.BoxSizer(wx.HORIZONTAL) + r.Add(a, 1, wx.LEFT) + r.Add(b, 1, wx.RIGHT) + return r + def label(s): return wx.StaticText(self, label = s) @@ -149,6 +177,10 @@ def addrbutton(s): self.ckM = wx.ToggleButton(self, label = "Monitor mode") self.ckM.Bind(wx.EVT_TOGGLEBUTTON, self.check_m) + self.capture = False + self.ckC = wx.ToggleButton(self, label = "Capture mode") + self.ckC.Bind(wx.EVT_TOGGLEBUTTON, self.check_c) + self.txVal = HexTextCtrl(self, size=wx.DefaultSize, style=0) self.rxVal = HexTextCtrl(self, size=wx.DefaultSize, style=wx.TE_READONLY) @@ -194,7 +226,7 @@ def addrbutton(s): label(""), hcenter(cb), label(""), - hcenter(self.ckM), + hcenter(epair(self.ckM, self.ckC)), hcenter(self.reset_button), label(""), hcenter(info), @@ -286,7 +318,7 @@ def connect(self, dev): self.refresh(None) def refresh(self, e): - if self.sd and not self.monitor: + if self.sd and not self.monitor and not self.capture: lowhigh = ["LOW", "HIGH"] self.sd.getstatus() self.label_serial.SetLabel(self.sd.serial) @@ -331,6 +363,32 @@ def check_m(self, e): if self.monitor: [self.hot(i, False) for i in self.heat] + def check_c(self, e): + global StopCapture + cm = e.EventObject.GetValue() + # self.sd.monitor(self.monitor) + if cm: + openFileDialog = wx.FileDialog(self, "CSV dump to file", "", "", + "CSV files (*.csv)|*.csv", + wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) + openFileDialog.ShowModal() + self.log_csv = openFileDialog.GetPath() + openFileDialog.Destroy() + + StopCapture = False + t = threading.Thread(target=capture_thr, args=(self.sd, self.log_csv)) + t.setDaemon(True) + t.start() + else: + StopCapture = True + wx.MessageBox("Capture finished. Traffic written to " + self.log_csv, "Message" ,wx.OK | wx.ICON_INFORMATION) + while StopCapture: + pass + [d.Enable(not cm) for d in self.dynamic] + if cm: + [self.hot(i, False) for i in self.heat] + self.capture = cm + def set_speed(self, e): w = e.EventObject s = int(w.GetString(w.GetCurrentSelection()))