Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage 2 of Porting Python 2 Code to Python 3 #40

Merged
merged 5 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions rflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import print_function
from __future__ import absolute_import

from builtins import str
from builtins import range
from .chipcon_nic import *
import rflib.bits as rfbits

Expand All @@ -14,7 +16,7 @@
class RfCat(FHSSNIC):
def RFdump(self, msg="Receiving", maxnum=100, timeoutms=1000):
try:
for x in xrange(maxnum):
for x in range(maxnum):
y, t = self.RFrecv(timeoutms)
print("(%5.3f) %s: %s" % (t, msg, y.encode('hex')))
except ChipconUsbTimeoutException:
Expand All @@ -31,7 +33,7 @@ def scan(self, basefreq=902e6, inc=250e3, count=104, delaysec=2, drate=38400, lo
while not keystop():
try:
print("(press Enter to quit)")
for freq in xrange(int(basefreq), int(basefreq+(inc*count)), int(inc)):
for freq in range(int(basefreq), int(basefreq+(inc*count)), int(inc)):
print("Scanning for frequency %d..." % freq)
self.setFreq(freq)
self.RFdump(timeoutms=delaysec*1000)
Expand Down
49 changes: 27 additions & 22 deletions rflib/bits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from __future__ import print_function
from __future__ import division

from builtins import hex
from builtins import range
from builtins import bytes
from past.utils import old_div
import struct

fmtsLSB = [None, "B", "<H", "<I", "<I", "<Q", "<Q", "<Q", "<Q"]
fmtsMSB = [None, "B", ">H", ">I", ">I", ">Q", ">Q", ">Q", ">Q"]
sizes = [ 0, 1, 2, 4, 4, 8, 8, 8, 8]
masks = [ (1<<(8*i))-1 for i in xrange(9) ]
masks = [ (1<<(8*i))-1 for i in range(9) ]

def wtfo(string):
outstr = []
Expand Down Expand Up @@ -38,7 +43,7 @@ def strBitReverse(string):
# convert back from MSB number to string
out = []
for x in range(len(string)):
out.append(chr(rnum&0xff))
out.append(bytes([rnum&0xff]))
rnum >>= 8
out.reverse()
print(''.join(out).encode('hex'))
Expand Down Expand Up @@ -76,7 +81,7 @@ def bitReverse(num, bitcnt):
def shiftString(string, bits):
carry = 0
news = []
for x in xrange(len(string)-1):
for x in range(len(string)-1):
newc = ((ord(string[x]) << bits) + (ord(string[x+1]) >> (8-bits))) & 0xff
news.append("%c"%newc)
newc = (ord(string[-1])<<bits) & 0xff
Expand Down Expand Up @@ -125,7 +130,7 @@ def whitenData(data, seed=0xffff, getNextByte=getNextByte_feedbackRegister7bitsM

carry = 0
news = []
for x in xrange(len(data)-1):
for x in range(len(data)-1):
newc = ((ord(data[x]) ^ getNextByte() ) & 0xff)
news.append("%c"%newc)
return "".join(news)
Expand Down Expand Up @@ -182,7 +187,7 @@ def findSyncWord(byts, sensitivity=4, minpreamble=2):
#print "bits: %x" % (bits1)

bitcount = min( 2 * sensitivity, 17 )
for frontbits in xrange( bitcount ): # with so many bit-inverted systems, let's not assume we know anything about the bit-arrangement. \x55\x55 could be a perfectly reasonable preamble.
for frontbits in range( bitcount ): # with so many bit-inverted systems, let's not assume we know anything about the bit-arrangement. \x55\x55 could be a perfectly reasonable preamble.
poss = (bits1 >> frontbits) & 0xffff
if not poss in possDwords:
possDwords.append(poss)
Expand Down Expand Up @@ -229,15 +234,15 @@ def findSyncWordDoubled(byts):


frontbits = 0
for frontbits in xrange(16, 40, 2): #FIXME: if this doesn't work, try 16, then 18+frontbits
for frontbits in range(16, 40, 2): #FIXME: if this doesn't work, try 16, then 18+frontbits
dwb1 = (bits1 >> (frontbits)) & 3
dwb2 = (bits2 >> (frontbits)) & 3
print("\tfrontbits: %d \t\t dwb1: %s dwb2: %s" % (frontbits, bin(bits1 >> (frontbits)), bin(bits2 >> (frontbits))))
if dwb2 != dwb1:
break

# frontbits now represents our unknowns... let's go from the other side now
for tailbits in xrange(16, -1, -2):
for tailbits in range(16, -1, -2):
dwb1 = (bits1 >> (tailbits)) & 3
dwb2 = (bits2 >> (tailbits)) & 3
print("\ttailbits: %d\t\t dwb1: %s dwb2: %s" % (tailbits, bin(bits1 >> (tailbits)), bin(bits2 >> (tailbits))))
Expand Down Expand Up @@ -270,7 +275,7 @@ def visBits(data):


def getBit(data, bit):
idx = bit / 8
idx = old_div(bit, 8)
bidx = bit % 8
char = data[idx]
return (ord(char)>>(7-bidx)) & 1
Expand Down Expand Up @@ -357,7 +362,7 @@ def bitSectString(string, startbit, endbit):
s = ''
bit = startbit

Bidx = bit / 8
Bidx = old_div(bit, 8)
bidx = (bit % 8)

while bit < endbit:
Expand All @@ -383,9 +388,9 @@ def bitSectString(string, startbit, endbit):
mask = ~ ( (1<<diff) - 1 )
byte &= mask

s += chr(byte)
s += bytes([byte])

ent = (min(entropy)+1.0) / (max(entropy)+1)
ent = old_div((min(entropy)+1.0), (max(entropy)+1))
#print "entropy: %f" % ent
return (s, ent)

Expand Down Expand Up @@ -451,9 +456,9 @@ def reprBitArray(bitAry, width=194):
# top line
#FIXME: UGGGGLY and kinda broken.
fraction = 1.0 * arylen/width
expand = [bitAry[int(x*fraction)] for x in xrange(width)]
expand = [bitAry[int(x*fraction)] for x in range(width)]

for bindex in xrange(width):
for bindex in range(width):
bits = 0
if bindex>0:
bits += (expand[bindex-1]) << (2)
Expand All @@ -476,7 +481,7 @@ def invertBits(data):
off = 0

if ldata&1:
output.append( chr( ord( data[0] ) ^ 0xff) )
output.append( bytes([ ord( data[0] ) ^ 0xff]) )
off = 1

if ldata&2:
Expand All @@ -488,7 +493,7 @@ def invertBits(data):
# output.append( struct.pack( "<I", struct.unpack( "<I", data[idx:idx+4] )[0] & 0xffff) )

#method2
count = ldata / 4
count = old_div(ldata, 4)
#print ldata, count
numlist = struct.unpack( "<%dI" % count, data[off:] )
modlist = [ struct.pack("<L", (x^0xffffffff) ) for x in numlist ]
Expand Down Expand Up @@ -541,12 +546,12 @@ def diff_manchester_decode(data, align=False):

last = (bit0 << 1) | bit1
if (bidx & 1):
out.append(chr(obyte))
out.append(bytes([obyte]))
obyte = 0

if not (bidx & 1):
obyte << 4 # pad 0's on end
out.append(chr(obyte))
out.append(bytes([obyte]))
return ''.join(out)


Expand All @@ -568,12 +573,12 @@ def biphase_mark_coding_encode(data):
last = bit
if bidx & 1:
print("%d - write" % bidx)
out.append(chr(obyte))
out.append(bytes([obyte]))
else:
print("%d - skip" % bidx)
if not (bidx & 1):
print("%d - write" % bidx)
out.append(chr(obyte))
out.append(bytes([obyte]))

return ''.join(out)

Expand All @@ -597,12 +602,12 @@ def manchester_decode(data, hilo=1):

last = bit
if (bidx & 1):
out.append(chr(obyte))
out.append(bytes([obyte]))
obyte = 0

if not (bidx & 1):
obyte << 4 # pad 0's on end
out.append(chr(obyte))
out.append(bytes([obyte]))
return ''.join(out)

def manchester_encode(data, hilo=1):
Expand Down Expand Up @@ -658,7 +663,7 @@ def findManchester(data, minbytes=10):
else:
# we're done, or not started
if lastCount >= minbits:
lenbytes = (lastCount / 8)
lenbytes = (old_div(lastCount, 8))
lenbits = lastCount % 8
startbyte = bidx - lenbytes
if lenbits > btidx:
Expand Down
2 changes: 1 addition & 1 deletion rflib/cc111Xhparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def parseLines(lines):
defs.update(parseLines(file('../includes/cc1111.h')))
defs.update(parseLines(file('/usr/share/sdcc/include/mcs51/cc1110.h')))

skeys = defs.keys()
skeys = list(defs.keys())
skeys.sort()
out = ["%-30s = %s"%(key,defs[key]) for key in skeys]

Expand Down
50 changes: 30 additions & 20 deletions rflib/ccspecan.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@
# Boston, MA 02110-1301, USA.

from __future__ import print_function
from __future__ import division

from future import standard_library
standard_library.install_aliases()
from builtins import bytes
from builtins import range
from past.utils import old_div
import sys
import time
import numpy
import threading
import rflib
import cPickle as pickle
# import cPickle in Python 2 instead of pickle in Python 3
if sys.version_info < (3,):
import cPickle as pickle
else:
import pickle as pickle

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import Qt, QPointF, QLineF
Expand Down Expand Up @@ -59,11 +69,11 @@ def run(self):
# this is where we pull in the data from the device
#frame_source = self._device.specan(self._low_frequency, self._high_frequency)

num_chans = int((self._high_frequency - self._low_frequency) / self._freq_step)
num_chans = int(old_div((self._high_frequency - self._low_frequency), self._freq_step))

if type(self._data) == list:
for rssi_values, timestamp in self._data:
rssi_values = [ ((ord(x)^0x80)/2)-88 for x in rssi_values[4:] ]
rssi_values = [ (old_div((ord(x)^0x80),2))-88 for x in rssi_values[4:] ]
# since we are not accessing the dongle, we need some sort of delay
time.sleep(self._delay)
frequency_axis = numpy.linspace(self._low_frequency, self._high_frequency, num=len(rssi_values), endpoint=True)
Expand All @@ -75,7 +85,7 @@ def run(self):
while not self._stop:
try:
rssi_values, timestamp = self._data.recv(APP_SPECAN, SPECAN_QUEUE, 10000)
rssi_values = [ ((ord(x)^0x80)/2)-88 for x in rssi_values ]
rssi_values = [ (old_div((ord(x)^0x80),2))-88 for x in rssi_values ]
frequency_axis = numpy.linspace(self._low_frequency, self._high_frequency, num=len(rssi_values), endpoint=True)

self._new_frame_callback(numpy.copy(frequency_axis), numpy.copy(rssi_values))
Expand Down Expand Up @@ -139,7 +149,7 @@ def _new_persisted_frames(self, frequency_bins):
self._persisted_frames_next_index = 0

def minimumSizeHint(self):
x_points = round((self._high_frequency - self._low_frequency) / self._frequency_step)
x_points = round(old_div((self._high_frequency - self._low_frequency), self._frequency_step))
y_points = round(self._high_dbm - self._low_dbm)
return QtCore.QSize(x_points * 4, y_points * 1)

Expand Down Expand Up @@ -171,7 +181,7 @@ def _draw_graph(self):
path_now = QtGui.QPainterPath()
path_max = QtGui.QPainterPath()

bins = range(len(frequency_axis))
bins = list(range(len(frequency_axis)))
x_axis = self._hz_to_x(frequency_axis)
y_now = self._dbm_to_y(rssi_values)
y_max = self._dbm_to_y(numpy.amax(self._persisted_frames, axis=0))
Expand Down Expand Up @@ -200,32 +210,32 @@ def _draw_graph(self):
pen.setBrush(Qt.red)
pen.setStyle(Qt.DotLine)
painter.setPen(pen)
painter.drawText(QPointF(x_axis[max_max] + 4, 30), '%.06f' % (self._x_to_hz(x_axis[max_max]) / 1e6))
painter.drawText(QPointF(x_axis[max_max] + 4, 30), '%.06f' % (old_div(self._x_to_hz(x_axis[max_max]), 1e6)))
painter.drawText(QPointF(30, y_max[max_max] - 4), '%d' % (self._y_to_dbm(y_max[max_max])))
painter.drawLine(QPointF(x_axis[max_max], 0), QPointF(x_axis[max_max], self.height()))
painter.drawLine(QPointF(0, y_max[max_max]), QPointF(self.width(), y_max[max_max]))
if self._mouse_x:
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 58), '(%.06f)' % ((self._x_to_hz(x_axis[max_max]) / 1e6) - (self._mouse_x / 1e6)))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 58), '(%.06f)' % ((old_div(self._x_to_hz(x_axis[max_max]), 1e6)) - (old_div(self._mouse_x, 1e6))))
pen.setBrush(Qt.yellow)
painter.setPen(pen)
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 44), '%.06f' % (self._mouse_x / 1e6))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 44), '%.06f' % (old_div(self._mouse_x, 1e6)))
painter.drawText(QPointF(54, self._dbm_to_y(self._mouse_y) - 4), '%d' % (self._mouse_y))
painter.drawLine(QPointF(self._hz_to_x(self._mouse_x), 0), QPointF(self._hz_to_x(self._mouse_x), self.height()))
painter.drawLine(QPointF(0, self._dbm_to_y(self._mouse_y)), QPointF(self.width(), self._dbm_to_y(self._mouse_y)))
if self._mouse_x2:
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 118), '(%.06f)' % ((self._mouse_x / 1e6) - (self._mouse_x2 / 1e6)))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 118), '(%.06f)' % ((old_div(self._mouse_x, 1e6)) - (old_div(self._mouse_x2, 1e6))))
if self._mouse_x2:
pen.setBrush(Qt.red)
painter.setPen(pen)
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 102), '(%.06f)' % ((self._x_to_hz(x_axis[max_max]) / 1e6) - (self._mouse_x2 / 1e6)))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 102), '(%.06f)' % ((old_div(self._x_to_hz(x_axis[max_max]), 1e6)) - (old_div(self._mouse_x2, 1e6))))
pen.setBrush(Qt.magenta)
painter.setPen(pen)
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 88), '%.06f' % (self._mouse_x2 / 1e6))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x2) + 4, 88), '%.06f' % (old_div(self._mouse_x2, 1e6)))
painter.drawText(QPointF(78, self._dbm_to_y(self._mouse_y2) - 4), '%d' % (self._mouse_y2))
painter.drawLine(QPointF(self._hz_to_x(self._mouse_x2), 0), QPointF(self._hz_to_x(self._mouse_x2), self.height()))
painter.drawLine(QPointF(0, self._dbm_to_y(self._mouse_y2)), QPointF(self.width(), self._dbm_to_y(self._mouse_y2)))
if self._mouse_x:
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 74), '(%.06f)' % ((self._mouse_x2 / 1e6) - (self._mouse_x / 1e6)))
painter.drawText(QPointF(self._hz_to_x(self._mouse_x) + 4, 74), '(%.06f)' % ((old_div(self._mouse_x2, 1e6)) - (old_div(self._mouse_x, 1e6))))
finally:
painter.end()

Expand Down Expand Up @@ -262,7 +272,7 @@ def _draw_reticle(self):
for dbm, point in dbm_labels:
painter.drawText(point, '%+.0f' % dbm)
for frequency, point in frequency_labels:
painter.drawText(point, '%.02f' % (frequency / 1e6))
painter.drawText(point, '%.02f' % (old_div(frequency, 1e6)))

finally:
painter.end()
Expand Down Expand Up @@ -293,25 +303,25 @@ def paintEvent(self, event):
def _hz_to_x(self, frequency_hz):
delta = frequency_hz - self._low_frequency
range = self._high_frequency - self._low_frequency
normalized = delta / range
normalized = old_div(delta, range)
#print "freq: %s \nlow: %s \nhigh: %s \ndelta: %s \nrange: %s \nnormalized: %s" % (frequency_hz, self._low_frequency, self._high_frequency, delta, range, normalized)
return normalized * self.width()

def _x_to_hz(self, x):
range = self._high_frequency - self._low_frequency
tmp = x / self.width()
tmp = old_div(x, self.width())
delta = tmp * range
return delta + self._low_frequency

def _dbm_to_y(self, dbm):
delta = self._high_dbm - dbm
range = self._high_dbm - self._low_dbm
normalized = delta / range
normalized = old_div(delta, range)
return normalized * self.height()

def _y_to_dbm(self, y):
range = self._high_dbm - self._low_dbm
tmp = y / self.height()
tmp = old_div(y, self.height())
delta = tmp * range
return self._high_dbm - delta

Expand Down Expand Up @@ -345,7 +355,7 @@ def _open_data(self, data):
data._debug = 1
freq = int(self._low_freq)
spc = int(self._spacing)
numChans = int((self._high_freq-self._low_freq) / self._spacing)
numChans = int(old_div((self._high_freq-self._low_freq), self._spacing))
data._doSpecAn(freq, spc, numChans)
else:
data = pickle.load(file(data,'rb'))
Expand Down Expand Up @@ -405,7 +415,7 @@ def keyPressEvent(self, event):

# anything else is alphanumeric
try:
key= chr(event.key()).upper()
key= bytes([event.key()]).upper()
event.accept()
except:
print('Unknown key pressed: 0x%x' % event.key())
Expand Down
Loading