Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jamesbowman/i2cdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbowman committed Apr 29, 2019
2 parents e281042 + a1111ed commit 2697bf5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
19 changes: 13 additions & 6 deletions python/i2cdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
62 changes: 60 additions & 2 deletions python/samples/i2cgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import os
import re
import csv
import threading
from functools import partial

Expand Down Expand Up @@ -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)
Expand All @@ -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):

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit 2697bf5

Please sign in to comment.