-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble.py
64 lines (58 loc) · 2.28 KB
/
ble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import serial
import bglib
from PySide import QtCore
class BLE(QtCore.QObject):
scan_response = QtCore.Signal(dict)
timeout = QtCore.Signal()
def __init__(self, port, baud_rate, packet_mode = False):
super(BLE, self).__init__()
self.led = False
self.port = port
self.baud_rate = baud_rate
self.packet_mode = packet_mode
self.ble = bglib.BGLib()
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)
self.ser.flushInput()
self.ser.flushOutput()
ser = self.ser
# disconnect if we are connected already
ble.send_command(ser, ble.ble_cmd_connection_disconnect(0))
ble.check_activity(ser, 1)
# stop advertising if we are advertising already
ble.send_command(ser, ble.ble_cmd_gap_set_mode(0, 0))
ble.check_activity(ser, 1)
# stop scanning if we are scanning already
ble.send_command(ser, ble.ble_cmd_gap_end_procedure())
ble.check_activity(ser, 1)
# set scan parameters
ble.send_command(ser, ble.ble_cmd_gap_set_scan_parameters(0xC8, 0xC8, 1))
ble.check_activity(ser, 1)
# start scanning now
ble.send_command(ser, ble.ble_cmd_gap_discover(1))
ble.check_activity(ser, 1)
# IO port stuff for LED; doesn't work currently
ble.ble_cmd_hardware_io_port_config_pull(0, 0, 0)
ble.ble_cmd_hardware_io_port_config_direction(0, 1)
ble.ble_cmd_hardware_io_port_config_function(0, 0)
ble.ble_cmd_hardware_io_port_write(0, 1, 0)
def timeout(self, sender, args):
# might want to try the following lines to reset, though it probably
# wouldn't work at this point if it's already timed out:
#ble.send_command(ser, ble.ble_cmd_system_reset(0))
#ble.check_activity(ser, 1)
print "BGAPI parser timed out. Make sure the BLE device is in a known/idle state."
self.timeout.emit()
def handle_scan_response(self, sender, args):
if self.led == False:
self.ble.ble_cmd_hardware_io_port_write(0, 1, 1)
self.led = True
else:
self.ble.ble_cmd_hardware_io_port_write(0, 1, 0)
self.led = False
self.scan_response.emit(args)
def check_activity(self):
return self.ble.check_activity(self.ser)