Skip to content

Instantly share code, notes, and snippets.

@dinukasal
Created November 1, 2017 15:28
Show Gist options
  • Save dinukasal/a4220a87f3415b74e9cda2e5f0125a89 to your computer and use it in GitHub Desktop.
Save dinukasal/a4220a87f3415b74e9cda2e5f0125a89 to your computer and use it in GitHub Desktop.
Plot logcat from android device in ubuntu
import pyqtgraph as pg
import time
plt = pg.plot()
def update(data):
plt.plot(data, clear=True)
class Thread(pg.QtCore.QThread):
newData = pg.QtCore.Signal(object)
def run(self):
while True:
data = pg.np.random.normal(size=100)
# do NOT plot data from here!
self.newData.emit(data)
time.sleep(0.05)
thread = Thread()
thread.newData.connect(update)
thread.start()
pg.QtGui.QApplication.exec_()
import sys, os
import numpy as np
from matplotlib import pyplot as plt
from collections import deque
from subprocess import call, Popen, PIPE, STDOUT
MAXLEN = 50
MAX_AMPLITUDE = 1300
# class that holds rotation data for N samples
class RotationData:
# constr
def __init__(self, maxLen):
#self.x = deque([0.0]*MAXLEN)
self.values = deque()
self.maxLen = MAXLEN
# ring buffer
def addToBuf(self, buf, val):
if len(buf) < self.maxLen:
buf.append(val)
else:
buf.popleft()
buf.append(val)
# add data
def add(self, data):
#self.addToBuf(self.x, xValue)
self.addToBuf(self.values, data)
#clear the data
def clear(self):
#self.x = deque([0.0]*MAXLEN)
self.values.clear()
# plot class
class RotationPlot:
# constr
def __init__(self, rotationData):
# set plot to animated
plt.ion()
# Create a figure of size 8x6 inches, 80 dots per inch
plt.figure(figsize=(8, 6), dpi=100)
# Create a new subplot from a grid of 1x1
plt.subplot(1,1,1)
#self.xline, = plt.plot(rotationData.x)
self.yline, = plt.plot(rotationData.values)
plt.xlim(0, MAXLEN)
plt.ylim(-MAX_AMPLITUDE, MAX_AMPLITUDE)
# update plot
def update(self, rotationData, x):
#self.xline.set_ydata(RotationData.x)
self.yline.set_xdata(np.arange(x))
self.yline.set_ydata(rotationData.values)
plt.draw()
print "Starting \n"
try:
retcodeLaunchApp = call(["adb", "logcat"])
if retcodeLaunchApp < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
else:
print "Balance app correctly launched \n"
p = Popen('android-sdk-macosx/platform-tools/adb logcat -v raw', stdout = PIPE,
stderr = STDOUT, shell = True)
# plot parameters
rotationData = RotationData(MAXLEN)
rotationPlot = RotationPlot(rotationData)
x = 0
while True:
try:
line = p.stdout.readline()
if not line: break
if line.startswith('ROT:'):
rot = line.partition('ROT:')
if x >= MAXLEN:
rotationData.clear()
x = 0
rotationData.add(rot[2])
x = x+1
rotationPlot.update(rotationData, x)
except KeyboardInterrupt:
print 'exiting'
break
except OSError as e:
print >>sys.stderr, "Execution failed:", e
sys.exit(2)
sys.exit(0)
@dinukasal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment