Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
andete committed Mar 2, 2014
1 parent c20a1c0 commit 5244697
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
54 changes: 47 additions & 7 deletions ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,52 @@ class BLE(QtCore.QObject):
uuid_secundary = [0x28, 0x01] # 0x2801
uuid_client_characteristic_configuration = [0x29, 0x02] # 0x2902

def __init__(self, port, baud_rate, packet_mode = False):
def __init__(self, baud_rate, packet_mode = False):
super(BLE, self).__init__()
self.led = False
self.port = port
self.port = None
self.baud_rate = baud_rate
self.packet_mode = packet_mode
self.ble = bglib.BGLib()
self.address = None

def address_response(self, sender, args):
self.address = ':'.join(['%02X' % b for b in args['address'][::-1]])
self.auto_detected = True

def auto_detect_serial(self):
import glob
glist = glob.glob('/dev/ttyACM*') + glob.glob('/dev/ttyUSB*')
self.auto_detected = False
for port in glist:
print "trying", port
ble = None
ble = bglib.BGLib()
ble.package_mode = self.packet_mode
ble.ble_rsp_system_address_get += self.address_response
ser = serial.Serial(port, self.baud_rate, timeout=1)
ser.flushInput()
ser.flushOutput()
ble.send_command(ser, ble.ble_cmd_system_address_get())
time.sleep(0.1)
ble.check_activity(ser, 1)
if self.auto_detected:
self.port = port
self.ser = ser
self.ble = ble
print "Using serial:", port
return
else:
ser.close()
raise Exception("Serial port not found")

def start(self, port = None):
if port is None:
self.auto_detect_serial()
else:
self.ble = bglib.BGLib()
self.ble.packet_mode = self.packet_mode
self.ble.ble_rsp_system_address_get += self.address_response
ble = self.ble
ble.packet_mode = self.packet_mode
ble.on_timeout += self.timeout
ble.ble_evt_gap_scan_response += self.handle_scan_response
self.ser = serial.Serial(port=self.port, baudrate=self.baud_rate, timeout=1)
Expand Down Expand Up @@ -70,7 +107,10 @@ def __init__(self, port, baud_rate, packet_mode = False):
self.ble.ble_evt_connection_status += self.handle_connection_status
# handle service info
self.ble.ble_evt_attclient_group_found += self.handle_attclient_group_found

# get local address
ble.send_command(ser, ble.ble_cmd_system_address_get())
ble.check_activity(ser, 1)
print "local device:", self.address

def send_command(self, cmd):
return self.ble.send_command(self.ser, cmd)
Expand Down Expand Up @@ -110,7 +150,8 @@ def check_activity(self):
return self.ble.check_activity(self.ser)

def connect_direct(self, target):
print "connecting to", target
f = ':'.join(['%02X' % b for b in target[::-1]])
print "connecting to", f
address = target
addr_type = 0 # public
timeout = 30 # 3 sec
Expand All @@ -124,4 +165,3 @@ def connect_direct(self, target):
def primary_service_discovery(self, handle):
print "service discovery for %d ..." % handle
self.send_command(self.ble.ble_cmd_attclient_read_by_group_type(handle, 0x0001, 0xFFFF, list(reversed(BLE.uuid_primary))))

40 changes: 32 additions & 8 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from PySide.QtCore import Qt

from productize import parse_data
from ble import BLE, ActivityThread
import ble
from ble import BLE

class MainWin(QtGui.QMainWindow):

Expand All @@ -26,7 +27,9 @@ def __init__(self):
self.setCentralWidget(self.qtab)
self.qtab.currentChanged.connect(self.tab_changed)
self.qtab.setCurrentIndex(0)
self.ble = BLE("/dev/ttyACM0", 115200)
self.ble = ble.BLE(115200)
self.ble.start()
self.setWindowTitle("BTLE tool using device "+self.ble.address)
self.run_collection()

def add_action(self, menu, text, slot, shortcut=None, checkable=False, checked=False):
Expand Down Expand Up @@ -65,6 +68,7 @@ def _add(text, slot = None):
if slot != None: action.triggered.connect(slot)
else: action.setDisabled(True)
_add('&Connect', self.connect)
self.collect_view.doubleClicked.connect(self.connect)
self.collect_model = QtGui.QStandardItemModel()
self.collect_model.setColumnCount(4)
self.collect_model.setHorizontalHeaderLabels(['time','from', 'name', 'data'])
Expand All @@ -77,7 +81,9 @@ def _add(text, slot = None):
return self.collect_view

def make_device_widget(self, handle, mac):
return QtGui.QLabel("%d %s" % (handle, mac))
w = QtGui.QLabel("%d %s" % (handle, mac))
w.mac = mac
return w

def tab_changed(self, i):
pass
Expand All @@ -87,13 +93,16 @@ def row_changed(self, current, previous):
self.selected_device_raw = self.collect_model.item(current.row(), 1).data()

def run_collection(self):
self.activity_thread = ActivityThread(self.ble)
self.activity_thread = ble.ActivityThread(self.ble)
self.ble.scan_response.connect(self.scan_response)
self.ble.connection_status.connect(self.connection_status)
self.activity_thread.start()

def scan_response(self, args):
s = QtGui.QStandardItem
def s(x):
y = QtGui.QStandardItem(x)
y.setEditable(False)
return y
time_ = time.strftime("%H:%M:%S %d/%m/%Y", time.localtime())
ftime = s(time_)
sender = ':'.join(['%02X' % b for b in args["sender"][::-1]])
Expand All @@ -115,15 +124,30 @@ def scan_response(self, args):
self.collect_view.resizeColumnToContents(2)
self.collect_view.resizeColumnToContents(3)

def tab_exists(self, mac):
found = None
for i in range(1,self.qtab.count()):
if self.qtab.widget(i).mac == mac:
self.qtab.setCurrentIndex(i)
found = i
break
print "found", found
return found

def connection_status(self, handle, mac, status):
print "connection_status called", status
if status == BLE.CONNECTED:
self.qtab.setCurrentIndex(self.qtab.addTab(self.make_device_widget(handle, mac), mac))
idx = self.qtab.addTab(self.make_device_widget(handle, mac), mac)
self.qtab.setCurrentIndex(idx)
self.ble.primary_service_discovery(handle)

def connect(self):
if not self.selected_device is None:
self.ble.connect_direct(self.selected_device_raw)
idx = self.tab_exists(self.selected_device)
if idx is None:
if not self.selected_device is None:
self.ble.connect_direct(self.selected_device_raw)
else:
self.qtab.setCurrentIndex(idx)

def main():
QtCore.QCoreApplication.setOrganizationName("productize")
Expand Down

0 comments on commit 5244697

Please sign in to comment.