Skip to content

Commit cc577bb

Browse files
committed
Fix for MicroPython 1.20.0 (Not returning from interrupt callback when transferring the experiment blocked the BLE module.)
1 parent ccbb4a3 commit cc577bb

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

phyphoxBLE/phyphoxBLE.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import bluetooth
22
import struct
33
import phyphoxBLE.experiment
4-
import io
54
import time
5+
import _thread
66
from io import StringIO
77
from io import BytesIO
88
from phyphoxBLE.ble_advertising import advertising_payload
@@ -89,7 +89,7 @@ def _irq(self, event, data):
8989
control_data = self._ble.gatts_read(self._handle_experiment_control)
9090
if self.debug: print(control_data)
9191
if control_data == b'\x01':
92-
self.when_subscription_received()
92+
_thread.start_new_thread(self.when_subscription_received, (conn_handle,))
9393
if self.debug: print("Sending experiment")
9494

9595
"""
@@ -178,7 +178,7 @@ def crc32_update(self, table, initial, buf, e_len):
178178
return c ^ 0xFFFFFFFF
179179

180180

181-
def when_subscription_received(self):
181+
def when_subscription_received(self, conn_handle):
182182
if self.debug: print("subscription received")
183183

184184
self._stop_advertise()
@@ -193,23 +193,20 @@ def when_subscription_received(self):
193193
header = "phyphox".encode() + struct.pack('>I',arrayLength) + struct.pack('>I',checksum) + b'\x00' + b'\x00' + b'\x00' + b'\x00' + b'\x00'
194194
time.sleep_ms(30)
195195

196-
for conn_handle in self._connections:
197-
self._ble.gatts_notify(conn_handle, self._handle_experiment, header)
198-
time.sleep_ms(30)
196+
self._ble.gatts_notify(conn_handle, self._handle_experiment, header)
197+
time.sleep_ms(30)
199198

200199
for i in range(int(self._exp_len/20)):
201200
exp.seek(i*20)
202201
byteSlice = exp.read(20)
203-
for conn_handle in self._connections:
204-
self._ble.gatts_notify(conn_handle, self._handle_experiment, byteSlice)
205-
time.sleep_ms(30)
202+
self._ble.gatts_notify(conn_handle, self._handle_experiment, byteSlice)
203+
time.sleep_ms(30)
206204
if(self._exp_len%20 != 0):
207205
rest = self._exp_len%20
208206
exp.seek(self._exp_len-rest)
209207
byteSlice = exp.read(rest)
210-
for conn_handle in self._connections:
211-
self._ble.gatts_notify(conn_handle, self._handle_experiment, byteSlice)
212-
time.sleep_ms(10)
208+
self._ble.gatts_notify(conn_handle, self._handle_experiment, byteSlice)
209+
time.sleep_ms(10)
213210
self._subscribed = True
214211
self._advertise()
215212

@@ -222,7 +219,7 @@ def addExperiment(self, exp):
222219
exp.getLastBytes(buf)
223220
buf.seek(0)
224221
str_data = buf.read().encode('utf8')
225-
self._p_exp = io.BytesIO(str_data)
222+
self._p_exp = BytesIO(str_data)
226223
self._p_exp.seek(0)
227224
self._p_exp.read()
228225
lastPos = self._p_exp.tell()
@@ -269,5 +266,3 @@ def start(self, device_name="phyphox", exp_pointer=None, exp_len=None):
269266

270267

271268

272-
273-

0 commit comments

Comments
 (0)